diff --git a/AirFighter/AirFighter/Drawnings/DirectionType.cs b/AirFighter/AirFighter/Drawnings/DirectionType.cs
index 5839437..9dee6bd 100644
--- a/AirFighter/AirFighter/Drawnings/DirectionType.cs
+++ b/AirFighter/AirFighter/Drawnings/DirectionType.cs
@@ -6,6 +6,11 @@
public enum DirectionType
{
+ ///
+ /// Неизвестное направление
+ ///
+ Unknow = -1,
+
///
/// Вверх
///
diff --git a/AirFighter/AirFighter/Drawnings/DrawningFighter.cs b/AirFighter/AirFighter/Drawnings/DrawningFighter.cs
index 612c1f7..2edcbee 100644
--- a/AirFighter/AirFighter/Drawnings/DrawningFighter.cs
+++ b/AirFighter/AirFighter/Drawnings/DrawningFighter.cs
@@ -42,6 +42,26 @@ public class DrawningFighter
///
private readonly int _drawningFighterHeight = 70;
+ ///
+ /// Координата X объекта
+ ///
+ public int? GetPosX => _startPosX;
+
+ ///
+ /// Координата Y объекта
+ ///
+ public int? GetPosY => _startPosY;
+
+ ///
+ /// Ширина объекта
+ ///
+ public int GetWidth => _drawningFighterWidth;
+
+ ///
+ /// Высота объекта
+ ///
+ public int GetHeight => _drawningFighterHeight;
+
///
/// Пустой конструктор
///
diff --git a/AirFighter/AirFighter/FormAirFighter.Designer.cs b/AirFighter/AirFighter/FormAirFighter.Designer.cs
index 3189eb7..4ed91c0 100644
--- a/AirFighter/AirFighter/FormAirFighter.Designer.cs
+++ b/AirFighter/AirFighter/FormAirFighter.Designer.cs
@@ -28,6 +28,8 @@
buttonLeft = new Button();
buttonUp = new Button();
buttonCreateFighter = new Button();
+ comboBoxStrategy = new ComboBox();
+ buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAirFighter).BeginInit();
SuspendLayout();
//
@@ -110,11 +112,33 @@
buttonCreateFighter.UseVisualStyleBackColor = true;
buttonCreateFighter.Click += buttonCreateAir_Click;
//
+ // comboBoxStrategy
+ //
+ comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
+ comboBoxStrategy.FormattingEnabled = true;
+ comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
+ comboBoxStrategy.Location = new Point(637, 12);
+ comboBoxStrategy.Name = "comboBoxStrategy";
+ comboBoxStrategy.Size = new Size(151, 28);
+ comboBoxStrategy.TabIndex = 7;
+ //
+ // buttonStrategyStep
+ //
+ buttonStrategyStep.Location = new Point(694, 57);
+ buttonStrategyStep.Name = "buttonStrategyStep";
+ buttonStrategyStep.Size = new Size(94, 29);
+ buttonStrategyStep.TabIndex = 8;
+ buttonStrategyStep.Text = "Шаг";
+ buttonStrategyStep.UseVisualStyleBackColor = true;
+ buttonStrategyStep.Click += ButtonStrategyStep_Click;
+ //
// FormAirFighter
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
+ Controls.Add(buttonStrategyStep);
+ Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateFighter);
Controls.Add(buttonUp);
Controls.Add(buttonLeft);
@@ -137,5 +161,7 @@
private Button buttonLeft;
private Button buttonUp;
private Button buttonCreateFighter;
+ private ComboBox comboBoxStrategy;
+ private Button buttonStrategyStep;
}
}
\ No newline at end of file
diff --git a/AirFighter/AirFighter/FormAirFighter.cs b/AirFighter/AirFighter/FormAirFighter.cs
index cdfe174..4567e7b 100644
--- a/AirFighter/AirFighter/FormAirFighter.cs
+++ b/AirFighter/AirFighter/FormAirFighter.cs
@@ -1,4 +1,5 @@
using ProjectAirFighter.Drawnings;
+using ProjectAirFighter.MovementStrategy;
namespace ProjectAirFighter;
@@ -6,9 +7,12 @@ public partial class FormAirFighter : Form
{
private DrawningFighter? _drawningFighter;
+ private AbstractStrategy? _strategy;
+
public FormAirFighter()
{
InitializeComponent();
+ _strategy = null;
}
private void Draw()
@@ -45,6 +49,8 @@ public partial class FormAirFighter : Form
_drawningFighter.SetPictureSize(pictureBoxAirFighter.Width, pictureBoxAirFighter.Height);
_drawningFighter.SetPosition(random.Next(10, 100), random.Next(10, 100));
+ _strategy = null;
+ comboBoxStrategy.Enabled = true;
Draw();
}
@@ -82,4 +88,42 @@ public partial class FormAirFighter : Form
Draw();
}
}
+
+ private void ButtonStrategyStep_Click(object sender, EventArgs e)
+ {
+ if (_drawningFighter == null)
+ {
+ return;
+ }
+
+ if (comboBoxStrategy.Enabled)
+ {
+ _strategy = comboBoxStrategy.SelectedIndex switch
+ {
+ 0 => new MoveToCenter(),
+ 1 => new MoveToBorder(),
+ _ => null,
+ };
+ if (_strategy == null)
+ {
+ return;
+ }
+ _strategy.SetData(new MoveableAir(_drawningFighter), pictureBoxAirFighter.Width, pictureBoxAirFighter.Height);
+ }
+
+ if (_strategy == null)
+ {
+ return;
+ }
+
+ comboBoxStrategy.Enabled = false;
+ _strategy.MakeStep();
+ Draw();
+
+ if (_strategy.GetStatus() == StrategyStatus.Finish)
+ {
+ comboBoxStrategy.Enabled = true;
+ _strategy = null;
+ }
+ }
}
\ No newline at end of file
diff --git a/AirFighter/AirFighter/MovementStrategy/AbstractStrategy.cs b/AirFighter/AirFighter/MovementStrategy/AbstractStrategy.cs
new file mode 100644
index 0000000..e1faaf5
--- /dev/null
+++ b/AirFighter/AirFighter/MovementStrategy/AbstractStrategy.cs
@@ -0,0 +1,139 @@
+namespace ProjectAirFighter.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/AirFighter/AirFighter/MovementStrategy/IMoveableObjectcs.cs b/AirFighter/AirFighter/MovementStrategy/IMoveableObjectcs.cs
new file mode 100644
index 0000000..73f3974
--- /dev/null
+++ b/AirFighter/AirFighter/MovementStrategy/IMoveableObjectcs.cs
@@ -0,0 +1,24 @@
+namespace ProjectAirFighter.MovementStrategy;
+
+///
+/// Интерфейс для работы с перемещаемым объектом
+///
+public interface IMoveableObject
+{
+ ///
+ /// Получение координаты объекта
+ ///
+ ObjectParameters? GetObjectPosition { get; }
+
+ ///
+ /// Шаг объекта
+ ///
+ int GetStep { get; }
+
+ ///
+ /// Попытка переместить объект в указанном направлении
+ ///
+ /// Направление
+ /// true - объект перемещен, false - перемещение невозможно
+ bool TryMoveObject(MovementDirection direction);
+}
diff --git a/AirFighter/AirFighter/MovementStrategy/MoveToBorder.cs b/AirFighter/AirFighter/MovementStrategy/MoveToBorder.cs
new file mode 100644
index 0000000..6fd964a
--- /dev/null
+++ b/AirFighter/AirFighter/MovementStrategy/MoveToBorder.cs
@@ -0,0 +1,51 @@
+namespace ProjectAirFighter.MovementStrategy;
+
+public class MoveToBorder : AbstractStrategy
+{
+ protected override bool IsTargetDestinaion()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null)
+ {
+ return false;
+ }
+
+ return objParams.RightBorder <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth &&
+ objParams.DownBorder <= FieldHeight && objParams.DownBorder + GetStep() >= FieldHeight;
+ }
+
+ protected override void MoveToTarget()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null)
+ {
+ return;
+ }
+
+ int diffX = objParams.RightBorder - FieldWidth;
+ if (Math.Abs(diffX) > GetStep())
+ {
+ if (diffX > 0)
+ {
+ MoveLeft();
+ }
+ else
+ {
+ MoveRight();
+ }
+ }
+
+ int diffY = objParams.DownBorder - FieldHeight;
+ if (Math.Abs(diffY) > GetStep())
+ {
+ if (diffY > 0)
+ {
+ MoveUp();
+ }
+ else
+ {
+ MoveDown();
+ }
+ }
+ }
+}
diff --git a/AirFighter/AirFighter/MovementStrategy/MoveToCenter.cs b/AirFighter/AirFighter/MovementStrategy/MoveToCenter.cs
new file mode 100644
index 0000000..b7dd88e
--- /dev/null
+++ b/AirFighter/AirFighter/MovementStrategy/MoveToCenter.cs
@@ -0,0 +1,54 @@
+namespace ProjectAirFighter.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/AirFighter/AirFighter/MovementStrategy/MoveableAir.cs b/AirFighter/AirFighter/MovementStrategy/MoveableAir.cs
new file mode 100644
index 0000000..3ade525
--- /dev/null
+++ b/AirFighter/AirFighter/MovementStrategy/MoveableAir.cs
@@ -0,0 +1,64 @@
+using ProjectAirFighter.Drawnings;
+
+namespace ProjectAirFighter.MovementStrategy;
+
+///
+/// Класс-реализация IMoveableObject с использованием DrawningCar
+///
+public class MoveableAir : IMoveableObject
+{
+ ///
+ /// Поле-объект класса DrawningCar или его наследника
+ ///
+ private readonly DrawningFighter? _air = null;
+
+ ///
+ /// Конструктор
+ ///
+ /// Объект класса DrawningCar
+ public MoveableAir(DrawningFighter air)
+ {
+ _air = air;
+ }
+
+ public ObjectParameters? GetObjectPosition
+ {
+ get
+ {
+ if (_air == null || _air.EntityFighter == null || !_air.GetPosX.HasValue || !_air.GetPosY.HasValue)
+ {
+ return null;
+ }
+ return new ObjectParameters(_air.GetPosX.Value, _air.GetPosY.Value, _air.GetWidth, _air.GetHeight);
+ }
+ }
+
+ public int GetStep => (int)(_air?.EntityFighter?.Step ?? 0);
+
+ public bool TryMoveObject(MovementDirection direction)
+ {
+ if (_air == null || _air.EntityFighter == null)
+ {
+ return false;
+ }
+
+ return _air.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/AirFighter/AirFighter/MovementStrategy/MovementDirection.cs b/AirFighter/AirFighter/MovementStrategy/MovementDirection.cs
new file mode 100644
index 0000000..89b3393
--- /dev/null
+++ b/AirFighter/AirFighter/MovementStrategy/MovementDirection.cs
@@ -0,0 +1,24 @@
+namespace ProjectAirFighter.MovementStrategy;
+
+public enum MovementDirection
+{
+ ///
+ /// Вверх
+ ///
+ Up = 1,
+
+ ///
+ /// Вниз
+ ///
+ Down = 2,
+
+ ///
+ /// Влево
+ ///
+ Left = 3,
+
+ ///
+ /// Вправо
+ ///
+ Right = 4
+}
diff --git a/AirFighter/AirFighter/MovementStrategy/ObjectParameters.cs b/AirFighter/AirFighter/MovementStrategy/ObjectParameters.cs
new file mode 100644
index 0000000..aa8e28e
--- /dev/null
+++ b/AirFighter/AirFighter/MovementStrategy/ObjectParameters.cs
@@ -0,0 +1,72 @@
+namespace ProjectAirFighter.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;
+ }
+}
diff --git a/AirFighter/AirFighter/MovementStrategy/StrategyStatus.cs b/AirFighter/AirFighter/MovementStrategy/StrategyStatus.cs
new file mode 100644
index 0000000..62108ed
--- /dev/null
+++ b/AirFighter/AirFighter/MovementStrategy/StrategyStatus.cs
@@ -0,0 +1,22 @@
+namespace ProjectAirFighter.MovementStrategy;
+
+///
+/// Статус выполнения операции перемещения
+///
+public enum StrategyStatus
+{
+ ///
+ /// Все готово к началу
+ ///
+ NotInit,
+
+ ///
+ /// Выполняется
+ ///
+ InProgress,
+
+ ///
+ /// Завершено
+ ///
+ Finish
+}