diff --git a/ProjectAirbus/ProjectAirbus/Drawning/DirectionType.cs b/ProjectAirbus/ProjectAirbus/Drawning/DirectionType.cs
index 8d00506..0ff723b 100644
--- a/ProjectAirbus/ProjectAirbus/Drawning/DirectionType.cs
+++ b/ProjectAirbus/ProjectAirbus/Drawning/DirectionType.cs
@@ -5,6 +5,11 @@
///
public enum DirectionType
{
+ ///
+ /// Неизвестное направление
+ ///
+ Unknow = -1,
+
///
/// Вверх
///
diff --git a/ProjectAirbus/ProjectAirbus/Drawning/DrawningBus.cs b/ProjectAirbus/ProjectAirbus/Drawning/DrawningBus.cs
index ce13cd1..8b2d590 100644
--- a/ProjectAirbus/ProjectAirbus/Drawning/DrawningBus.cs
+++ b/ProjectAirbus/ProjectAirbus/Drawning/DrawningBus.cs
@@ -37,7 +37,7 @@ public class DrawningBus
///
/// Ширина прорисовки аэробуса
///
- private readonly int _drawningBusWidth = 133;
+ private readonly int _drawningBusWidth = 135;
///
/// Высота прорисовки аэробуса
@@ -243,7 +243,7 @@ public class DrawningBus
g.DrawLine(pen, _startPosX.Value, _startPosY.Value, _startPosX.Value + 20, _startPosY.Value + 20);
//заднее доп крыло
- g.FillEllipse(darkBrush, _startPosX.Value - 7, _startPosY.Value + 15, 20, 5);
+ g.FillEllipse(darkBrush, _startPosX.Value, _startPosY.Value + 15, 20, 5);
//нос самолёта
g.DrawLine(pen, _startPosX.Value + 100, _startPosY.Value + 20, _startPosX.Value + 130, _startPosY.Value + 30);
diff --git a/ProjectAirbus/ProjectAirbus/FormAirbus.Designer.cs b/ProjectAirbus/ProjectAirbus/FormAirbus.Designer.cs
index 5a9faec..d59f17c 100644
--- a/ProjectAirbus/ProjectAirbus/FormAirbus.Designer.cs
+++ b/ProjectAirbus/ProjectAirbus/FormAirbus.Designer.cs
@@ -29,12 +29,14 @@
private void InitializeComponent()
{
buttonCreateAirbus = new Button();
- ButtonLeft = new Button();
+ buttonLeft = new Button();
buttonDown = new Button();
buttonRight = new Button();
buttonUp = new Button();
pictureBoxAirbus = new PictureBox();
buttonCreateBus = new Button();
+ comboBoxStrategy = new ComboBox();
+ buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAirbus).BeginInit();
SuspendLayout();
//
@@ -49,17 +51,17 @@
buttonCreateAirbus.UseVisualStyleBackColor = true;
buttonCreateAirbus.Click += ButtonCreateAirbus_Click;
//
- // ButtonLeft
+ // buttonLeft
//
- ButtonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
- ButtonLeft.BackgroundImage = Properties.Resources.free_icon_left_arrow_10696518;
- ButtonLeft.BackgroundImageLayout = ImageLayout.Stretch;
- ButtonLeft.Location = new Point(927, 491);
- ButtonLeft.Name = "ButtonLeft";
- ButtonLeft.Size = new Size(35, 35);
- ButtonLeft.TabIndex = 2;
- ButtonLeft.UseVisualStyleBackColor = true;
- ButtonLeft.Click += ButtonMove_Click;
+ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
+ buttonLeft.BackgroundImage = Properties.Resources.free_icon_left_arrow_10696518;
+ buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
+ buttonLeft.Location = new Point(927, 491);
+ buttonLeft.Name = "buttonLeft";
+ buttonLeft.Size = new Size(35, 35);
+ buttonLeft.TabIndex = 2;
+ buttonLeft.UseVisualStyleBackColor = true;
+ buttonLeft.Click += ButtonMove_Click;
//
// buttonDown
//
@@ -113,20 +115,42 @@
buttonCreateBus.Name = "buttonCreateBus";
buttonCreateBus.Size = new Size(193, 23);
buttonCreateBus.TabIndex = 7;
- buttonCreateBus.Text = "Создать автобус";
+ buttonCreateBus.Text = "Создать базовый аэробус";
buttonCreateBus.UseVisualStyleBackColor = true;
buttonCreateBus.Click += ButtonCreateBus_Click;
//
+ // comboBoxStrategy
+ //
+ comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
+ comboBoxStrategy.FormattingEnabled = true;
+ comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
+ comboBoxStrategy.Location = new Point(927, 12);
+ comboBoxStrategy.Name = "comboBoxStrategy";
+ comboBoxStrategy.Size = new Size(117, 23);
+ comboBoxStrategy.TabIndex = 8;
+ //
+ // buttonStrategyStep
+ //
+ buttonStrategyStep.Location = new Point(969, 41);
+ buttonStrategyStep.Name = "buttonStrategyStep";
+ buttonStrategyStep.Size = new Size(75, 23);
+ buttonStrategyStep.TabIndex = 9;
+ buttonStrategyStep.Text = "Шаг";
+ buttonStrategyStep.UseVisualStyleBackColor = true;
+ buttonStrategyStep.Click += buttonStrategyStep_Click;
+ //
// FormAirbus
//
AutoScaleMode = AutoScaleMode.None;
ClientSize = new Size(1059, 533);
+ Controls.Add(buttonStrategyStep);
+ Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateBus);
Controls.Add(buttonCreateAirbus);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
- Controls.Add(ButtonLeft);
+ Controls.Add(buttonLeft);
Controls.Add(pictureBoxAirbus);
Name = "FormAirbus";
Text = "Аэробус";
@@ -136,11 +160,13 @@
#endregion
private Button buttonCreateAirbus;
- private Button ButtonLeft;
+ private Button buttonLeft;
private Button buttonDown;
private Button buttonRight;
private Button buttonUp;
private PictureBox pictureBoxAirbus;
private Button buttonCreateBus;
+ private ComboBox comboBoxStrategy;
+ private Button buttonStrategyStep;
}
}
\ No newline at end of file
diff --git a/ProjectAirbus/ProjectAirbus/FormAirbus.cs b/ProjectAirbus/ProjectAirbus/FormAirbus.cs
index 235e87e..8f6fc1b 100644
--- a/ProjectAirbus/ProjectAirbus/FormAirbus.cs
+++ b/ProjectAirbus/ProjectAirbus/FormAirbus.cs
@@ -1,5 +1,7 @@
using ProjectAirbus.Drawning;
+using ProjectAirbus.MovementStrategy;
+using ProjectAirbus.MovementStrategyж;
namespace ProjectAirbus
{
@@ -13,12 +15,17 @@ namespace ProjectAirbus
///
private DrawningBus? _drawningBus;
+ ///
+ /// Стратегия перемещения
+ ///
+ private AbstractStrategy? _strategy;
///
/// Конструктор формы
///
public FormAirbus()
{
InitializeComponent();
+ _strategy = null;
}
///
@@ -62,6 +69,8 @@ namespace ProjectAirbus
}
_drawningBus.SetPictureSize(pictureBoxAirbus.Width, pictureBoxAirbus.Height);
_drawningBus.SetPosition(random.Next(10, 100), random.Next(10, 100));
+ _strategy = null;
+ comboBoxStrategy.Enabled = true;
Draw();
}
@@ -117,5 +126,46 @@ namespace ProjectAirbus
}
}
+ ///
+ ///
+ ///
+ ///
+ ///
+ private void buttonStrategyStep_Click(object sender, EventArgs e)
+ {
+ if (_drawningBus == null)
+ {
+ return;
+ }
+
+ if (comboBoxStrategy.Enabled)
+ {
+ _strategy = comboBoxStrategy.SelectedIndex switch
+ {
+ 0 => new MoveToCenter(),
+ 1 => new MoveToBorder(),
+ _ => null,
+ };
+ if (_strategy == null)
+ {
+ return;
+ }
+ _strategy.SetData(new MoveableBus(_drawningBus), pictureBoxAirbus.Width, pictureBoxAirbus.Height);
+ }
+
+ if (_strategy == null)
+ {
+ return;
+ }
+ comboBoxStrategy.Enabled = false;
+ _strategy.MakeStep();
+ Draw();
+
+ if (_strategy.GetStatus() == StrategyStatus.Finish)
+ {
+ comboBoxStrategy.Enabled = true;
+ _strategy = null;
+ }
+ }
}
}
diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/AbstractStrategy.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/AbstractStrategy.cs
new file mode 100644
index 0000000..a2f20ed
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/AbstractStrategy.cs
@@ -0,0 +1,143 @@
+using ProjectAirbus.MovementStrategyж;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.MovementStrategy;
+
+public abstract class AbstractStrategy
+{
+ ///
+ /// Перемещаемый объект
+ ///
+ private IMoveableObject? _moveableObject;
+
+ ///
+ /// Статус перемещения
+ ///
+ private StrategyStatus _state = StrategyStatus.NotInit;
+
+ ///
+ /// Ширина поля
+ ///
+ protected int FieldWidth { get; private set; }
+
+ ///
+ /// Высота поля
+ ///
+ protected int FieldHeight { get; private set; }
+
+ ///
+ /// Статус перемещения
+ ///
+ public StrategyStatus GetStatus() { return _state; }
+
+ ///
+ /// Установка данных
+ ///
+ /// Перемещаемый объект
+ /// Ширина поля
+ /// Высота поля
+ public void SetData(IMoveableObject moveableObject, int width, int height)
+ {
+ if (moveableObject == null)
+ {
+ _state = StrategyStatus.NotInit;
+ return;
+ }
+
+ _state = StrategyStatus.InProgress;
+ _moveableObject = moveableObject;
+ FieldWidth = width;
+ FieldHeight = height;
+ }
+
+ ///
+ /// Шаг перемещения
+ ///
+ public void MakeStep()
+ {
+ if (_state != StrategyStatus.InProgress)
+ {
+ return;
+ }
+
+ if (IsTargetDestinaion())
+ {
+ _state = StrategyStatus.Finish;
+ return;
+ }
+
+ MoveToTarget();
+ }
+
+ ///
+ /// Перемещение влево
+ ///
+ /// Результат перемещения (true - удалось переместиться, false - неудача)
+ protected bool MoveLeft() => MoveTo(MovementDirection.Left);
+
+ ///
+ /// Перемещение вправо
+ ///
+ /// Результат перемещения (true - удалось переместиться, false - неудача)
+ protected bool MoveRight() => MoveTo(MovementDirection.Right);
+
+ ///
+ /// Перемещение вверх
+ ///
+ /// Результат перемещения (true - удалось переместиться, false - неудача)
+ protected bool MoveUp() => MoveTo(MovementDirection.Up);
+
+ ///
+ /// Перемещение вниз
+ ///
+ /// Результат перемещения (true - удалось переместиться, false - неудача)
+ protected bool MoveDown() => MoveTo(MovementDirection.Down);
+
+ ///
+ /// Параметры объекта
+ ///
+ protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
+
+ ///
+ /// Шаг объекта
+ ///
+ ///
+ protected int? GetStep()
+ {
+ if (_state != StrategyStatus.InProgress)
+ {
+ return null;
+ }
+ return _moveableObject?.GetStep;
+ }
+
+ ///
+ /// Перемещение к цели
+ ///
+ protected abstract void MoveToTarget();
+
+ ///
+ /// Достигнута ли цель
+ ///
+ ///
+ protected abstract bool IsTargetDestinaion();
+
+ ///
+ /// Попытка перемещения в требуемом направлении
+ ///
+ /// Направление
+ /// Результат попытки (true - удалось переместиться, false - неудача)
+ private bool MoveTo(MovementDirection movementDirection)
+ {
+ if (_state != StrategyStatus.InProgress)
+ {
+ return false;
+ }
+
+ return _moveableObject?.TryMoveObject(movementDirection) ?? false;
+ }
+}
diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/IMoveableObject.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/IMoveableObject.cs
new file mode 100644
index 0000000..d768dc2
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/IMoveableObject.cs
@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.MovementStrategy;
+
+public interface IMoveableObject
+{
+ ///
+ /// Получение координаты объекта
+ ///
+ ObjectParameters? GetObjectPosition { get; }
+
+ ///
+ /// Шаг объекта
+ ///
+ int GetStep { get; }
+
+ ///
+ /// Попытка переместить объект в указанном направлении
+ ///
+ /// Направление
+ /// true - объект перемещен, false - перемещение невозможно
+ bool TryMoveObject(MovementDirection direction);
+}
diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToBorder.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToBorder.cs
new file mode 100644
index 0000000..4494328
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToBorder.cs
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.MovementStrategy;
+
+public class MoveToBorder : AbstractStrategy
+{
+ protected override bool IsTargetDestinaion()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null)
+ {
+ return false;
+ }
+ return objParams.LeftBorder - GetStep() <= 0 || objParams.RightBorder + GetStep() >= FieldWidth ||
+ objParams.TopBorder - GetStep() <= 0 || objParams.ObjectMiddleVertical + GetStep() >= FieldHeight;
+ }
+
+ protected override void MoveToTarget()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null)
+ {
+ return;
+ }
+ int x = objParams.RightBorder;
+ if (x + GetStep() < FieldWidth) MoveRight();
+ int y = objParams.DownBorder;
+ if (y + GetStep() < FieldHeight) MoveDown();
+ }
+}
diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToCenter.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToCenter.cs
new file mode 100644
index 0000000..9bb2746
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveToCenter.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.MovementStrategy;
+
+public class MoveToCenter : AbstractStrategy
+{
+ protected override bool IsTargetDestinaion()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null)
+ {
+ return false;
+ }
+ return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
+ objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
+
+ }
+
+ protected override void MoveToTarget()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null)
+ {
+ return;
+ }
+ int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
+ if (Math.Abs(diffX) > GetStep())
+ {
+ if (diffX > 0)
+ {
+ MoveLeft();
+ }
+ else
+ {
+ MoveRight();
+ }
+ }
+
+ int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
+ if (Math.Abs(diffY) > GetStep())
+ {
+ if (diffY > 0)
+ {
+ MoveUp();
+ }
+ else
+ {
+ MoveDown();
+ }
+ }
+ }
+}
diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveableBus.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveableBus.cs
new file mode 100644
index 0000000..4dc45fa
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/MoveableBus.cs
@@ -0,0 +1,69 @@
+using ProjectAirbus.Drawning;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.MovementStrategy;
+
+///
+/// Класс-реализация IMoveableObject с использованием DrawningBus
+///
+public class MoveableBus : IMoveableObject
+{
+ ///
+ /// Поле-объект класса DrawningAirplan или его наследника
+ ///
+ private readonly DrawningBus? _bus = null;
+
+ ///
+ /// Конструктор
+ ///
+ /// Объект класса DrawningAirplan
+ public MoveableBus(DrawningBus bus)
+ {
+ _bus = bus;
+ }
+
+ public ObjectParameters? GetObjectPosition
+ {
+ get
+ {
+ if (_bus == null || _bus.EntityBus == null || !_bus.GetPosX.HasValue || !_bus.GetPosY.HasValue)
+ {
+ return null;
+ }
+ return new ObjectParameters(_bus.GetPosX.Value, _bus.GetPosY.Value, _bus.GetWidth, _bus.GetHeight);
+ }
+ }
+
+ public int GetStep => (int)(_bus?.EntityBus?.Step ?? 0);
+
+ public bool TryMoveObject(MovementDirection direction)
+ {
+ if (_bus == null || _bus.EntityBus == null)
+ {
+ return false;
+ }
+
+ return _bus.MoveTransport(GetDirectionType(direction));
+ }
+
+ ///
+ /// Конвертация из MovementDirection в DirectionType
+ ///
+ /// MovementDirection
+ /// DirectionType
+ private static DirectionType GetDirectionType(MovementDirection direction)
+ {
+ return direction switch
+ {
+ MovementDirection.Left => DirectionType.Left,
+ MovementDirection.Right => DirectionType.Right,
+ MovementDirection.Up => DirectionType.Up,
+ MovementDirection.Down => DirectionType.Down,
+ _ => DirectionType.Unknow,
+ };
+ }
+}
diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/MovementDirection.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/MovementDirection.cs
new file mode 100644
index 0000000..92ebd28
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/MovementDirection.cs
@@ -0,0 +1,30 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.MovementStrategy;
+
+///
+/// Направление перемещения
+///
+public enum MovementDirection
+{
+ ///
+ /// Вверх
+ ///
+ Up = 1,
+ ///
+ /// Вниз
+ ///
+ Down = 2,
+ ///
+ /// Влево
+ ///
+ Left = 3,
+ ///
+ /// Вправо
+ ///
+ Right = 4
+}
diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/ObjectParameters.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/ObjectParameters.cs
new file mode 100644
index 0000000..6e9434f
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/ObjectParameters.cs
@@ -0,0 +1,77 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.MovementStrategy;
+
+public class ObjectParameters
+{
+
+ ///
+ /// Координата X
+ ///
+ private readonly int _x;
+
+ ///
+ /// Координата Y
+ ///
+ private readonly int _y;
+
+ ///
+ /// Ширина объекта
+ ///
+ private readonly int _width;
+
+ ///
+ /// Высота объекта
+ ///
+ private readonly int _height;
+
+ ///
+ /// Левая граница
+ ///
+ public int LeftBorder => _x;
+
+ ///
+ /// Верхняя граница
+ ///
+ public int TopBorder => _y;
+
+ ///
+ /// Правая граница
+ ///
+ public int RightBorder => _x + _width;
+
+ ///
+ /// Нижняя граница
+ ///
+ public int DownBorder => _y + _height;
+
+ ///
+ /// Середина объекта
+ ///
+ public int ObjectMiddleHorizontal => _x + _width / 2;
+
+ ///
+ /// Середина объекта
+ ///
+ public int ObjectMiddleVertical => _y + _height / 2;
+
+ ///
+ /// Конструктор
+ ///
+ /// Координата X
+ /// Координата Y
+ /// Ширина объекта
+ /// Высота объекта
+ public ObjectParameters(int x, int y, int width, int height)
+ {
+ _x = x;
+ _y = y;
+ _width = width;
+ _height = height;
+ }
+}
+
\ No newline at end of file
diff --git a/ProjectAirbus/ProjectAirbus/MovementStrategy/StrategyStatus.cs b/ProjectAirbus/ProjectAirbus/MovementStrategy/StrategyStatus.cs
new file mode 100644
index 0000000..e653d37
--- /dev/null
+++ b/ProjectAirbus/ProjectAirbus/MovementStrategy/StrategyStatus.cs
@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirbus.MovementStrategyж
+{
+ public enum StrategyStatus
+ {
+ ///
+ /// Все готово к началу
+ ///
+ NotInit,
+
+ ///
+ /// Выполняется
+ ///
+ InProgress,
+
+ ///
+ /// Завершено
+ ///
+ Finish
+ }
+}