diff --git a/ProjectAirFighter/ProjectAirFighter/Drawning/DirectionType.cs b/ProjectAirFighter/ProjectAirFighter/Drawning/DirectionType.cs
index 6b9ffd1..22c4210 100644
--- a/ProjectAirFighter/ProjectAirFighter/Drawning/DirectionType.cs
+++ b/ProjectAirFighter/ProjectAirFighter/Drawning/DirectionType.cs
@@ -4,6 +4,10 @@
///
public enum DirectionType
{
+ ///
+ /// Неизвестное напрвление
+ ///
+ Unknow = -1,
///
/// Вверх
///
diff --git a/ProjectAirFighter/ProjectAirFighter/Drawning/DrawningWarPlane.cs b/ProjectAirFighter/ProjectAirFighter/Drawning/DrawningWarPlane.cs
index 4b81258..23ec6f1 100644
--- a/ProjectAirFighter/ProjectAirFighter/Drawning/DrawningWarPlane.cs
+++ b/ProjectAirFighter/ProjectAirFighter/Drawning/DrawningWarPlane.cs
@@ -37,13 +37,32 @@ public class DrawningWarPlane
///
/// Ширина прорисовки самолета
///
- private int? _drawningWarPlaneWidth = 120;
+ private readonly int _drawningWarPlaneWidth = 120;
///
/// Высота прорисовки самолета
///
- private int? _drawningWarPlaneHeight = 140;
+ private readonly int _drawningWarPlaneHeight = 140;
+ ///
+ /// Координаты X объекта
+ ///
+ public int? GetPosX => _startPosX;
+
+ ///
+ /// Координаты Y объекта
+ ///
+ public int? GetPosY => _startPosY;
+
+ ///
+ /// Высота объекта
+ ///
+ public int GetHeight => _drawningWarPlaneHeight;
+
+ ///
+ /// Ширина объекта
+ ///
+ public int GetWigdth => _drawningWarPlaneWidth;
private DrawningWarPlane() {
_pictureHeight = null;
_pictureWidth = null;
diff --git a/ProjectAirFighter/ProjectAirFighter/FormAirFighter.Designer.cs b/ProjectAirFighter/ProjectAirFighter/FormAirFighter.Designer.cs
index 302d5af..71347d9 100644
--- a/ProjectAirFighter/ProjectAirFighter/FormAirFighter.Designer.cs
+++ b/ProjectAirFighter/ProjectAirFighter/FormAirFighter.Designer.cs
@@ -35,6 +35,8 @@
buttonDown = new Button();
buttonRight = new Button();
buttonCreateWarPlane = new Button();
+ comboBoxStrategy = new ComboBox();
+ buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxAirFighter).BeginInit();
SuspendLayout();
//
@@ -121,11 +123,33 @@
buttonCreateWarPlane.UseVisualStyleBackColor = true;
buttonCreateWarPlane.Click += buttonCreateWarPlane_Click;
//
+ // comboBoxStrategy
+ //
+ comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
+ comboBoxStrategy.FormattingEnabled = true;
+ comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
+ comboBoxStrategy.Location = new Point(791, 12);
+ comboBoxStrategy.Name = "comboBoxStrategy";
+ comboBoxStrategy.Size = new Size(121, 23);
+ comboBoxStrategy.TabIndex = 7;
+ //
+ // buttonStrategyStep
+ //
+ buttonStrategyStep.Location = new Point(829, 41);
+ buttonStrategyStep.Name = "buttonStrategyStep";
+ buttonStrategyStep.Size = new Size(75, 23);
+ buttonStrategyStep.TabIndex = 8;
+ buttonStrategyStep.Text = "Шаг";
+ buttonStrategyStep.UseVisualStyleBackColor = true;
+ buttonStrategyStep.Click += buttonStrategyStep_Click;
+ //
// FormAirFighter
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(924, 557);
+ Controls.Add(buttonStrategyStep);
+ Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateWarPlane);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
@@ -149,5 +173,7 @@
private Button buttonDown;
private Button buttonRight;
private Button buttonCreateWarPlane;
+ private ComboBox comboBoxStrategy;
+ private Button buttonStrategyStep;
}
}
\ No newline at end of file
diff --git a/ProjectAirFighter/ProjectAirFighter/FormAirFighter.cs b/ProjectAirFighter/ProjectAirFighter/FormAirFighter.cs
index 100e08b..6a77d56 100644
--- a/ProjectAirFighter/ProjectAirFighter/FormAirFighter.cs
+++ b/ProjectAirFighter/ProjectAirFighter/FormAirFighter.cs
@@ -9,6 +9,7 @@ using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectAirFighter.Drawning;
using ProjectAirFighter.Entities;
+using ProjectAirFighter.MovementStrategy;
namespace ProjectAirFighter
{
@@ -18,18 +19,17 @@ namespace ProjectAirFighter
{
private DrawningWarPlane? _drawningWarPlane;
-
///
/// Стратегия перемещения
///
- //private AbstractStrategy? _strategy;
+ private AbstractStrategy? _strategy;
///
/// Конструктор формы
///
public FormAirFighter()
{
InitializeComponent();
- //_strategy = null;
+ _strategy = null;
}
///
/// Метод прорисовки
@@ -70,6 +70,8 @@ namespace ProjectAirFighter
_drawningWarPlane.SetPictureSize(pictureBoxAirFighter.Width,
pictureBoxAirFighter.Height);
_drawningWarPlane.SetPosition(random.Next(10, 100), random.Next(10, 100));
+ _strategy = null;
+ comboBoxStrategy.Enabled = true;
Draw();
}
private void ButtonCreate_Click(object sender, EventArgs e)
@@ -127,6 +129,41 @@ namespace ProjectAirFighter
}
-
+ private void buttonStrategyStep_Click(object sender, EventArgs e)
+ {
+ if (_drawningWarPlane == null)
+ {
+ return;
+ }
+ if (comboBoxStrategy.Enabled)
+ {
+ _strategy = comboBoxStrategy.SelectedIndex switch
+ {
+ 0 => new MoveToCenter(),
+ 1 => new MoveToBorder(),
+ _ => null,
+ };
+ if (_strategy == null)
+ {
+ return;
+ }
+ _strategy.SetDate(new MoveableWarPlane(_drawningWarPlane), pictureBoxAirFighter.Width, pictureBoxAirFighter.Height);
+ }
+
+ if (_strategy == null)
+ {
+ return;
+ }
+
+ comboBoxStrategy.Enabled = false;
+ _strategy.MakeStep();
+ Draw();
+
+ if (_strategy.GetStatus() == StrategyStatus.Finish)
+ {
+ comboBoxStrategy.Enabled = true;
+ _strategy = null;
+ }
+ }
}
}
diff --git a/ProjectAirFighter/ProjectAirFighter/MovementStrategy/AbstractStrategy.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/AbstractStrategy.cs
new file mode 100644
index 0000000..fd5fed0
--- /dev/null
+++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/AbstractStrategy.cs
@@ -0,0 +1,123 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+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 SetDate(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/ProjectAirFighter/ProjectAirFighter/MovementStrategy/IMoveableObject.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/IMoveableObject.cs
new file mode 100644
index 0000000..4f53874
--- /dev/null
+++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/IMoveableObject.cs
@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirFighter.MovementStrategy;
+
+public interface IMoveableObject
+{
+ ///
+ /// Получение координаты объекта
+ ///
+ ObjectParameters? GetObjectPosition { get; }
+
+ ///
+ /// Шаг объекта
+ ///
+ int GetStep { get; }
+
+ bool TryMoveObject(MovementDirection direction);
+}
diff --git a/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToBorder.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToBorder.cs
new file mode 100644
index 0000000..2a83b99
--- /dev/null
+++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToBorder.cs
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirFighter.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/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToCenter.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToCenter.cs
new file mode 100644
index 0000000..13468e0
--- /dev/null
+++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToCenter.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+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/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveableWarPlane.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveableWarPlane.cs
new file mode 100644
index 0000000..1fa15ed
--- /dev/null
+++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveableWarPlane.cs
@@ -0,0 +1,67 @@
+using ProjectAirFighter.Drawning;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirFighter.MovementStrategy;
+
+///
+/// класс реализация IMoveableObject с исользованием DrawningWarPlane
+///
+public class MoveableWarPlane: IMoveableObject
+{
+ ///
+ /// Полк объекта DrawningWarPlane или его наследника
+ ///
+ private readonly DrawningWarPlane? _warPlane = null;
+
+ ///
+ /// Конструктор
+ ///
+ /// Объект класса DrawningWarPlane
+ public MoveableWarPlane(DrawningWarPlane warPlane)
+ {
+ _warPlane = warPlane;
+ }
+
+ public ObjectParameters? GetObjectPosition {
+ get
+ {
+ if (_warPlane == null || _warPlane.EntityWarPlane == null || !_warPlane.GetPosX.HasValue || !_warPlane.GetPosY.HasValue)
+ {
+ return null;
+ }
+ return new ObjectParameters(_warPlane.GetPosX.Value, _warPlane.GetPosY.Value, _warPlane.GetWigdth, _warPlane.GetHeight);
+ }
+ }
+
+ public int GetStep => (int)(_warPlane?.EntityWarPlane?.Step ?? 0);
+
+ public bool TryMoveObject(MovementDirection direction)
+ {
+ if (_warPlane == null || _warPlane.EntityWarPlane == null)
+ {
+ return false;
+ }
+ return _warPlane.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/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MovementDirection.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MovementDirection.cs
new file mode 100644
index 0000000..c22a919
--- /dev/null
+++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MovementDirection.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirFighter.MovementStrategy;
+
+public enum MovementDirection
+{
+
+ ///
+ /// Вверх
+ ///
+ Up = 1,
+
+ ///
+ /// Вниз
+ ///
+ Down = 2,
+
+ ///
+ /// Влево
+ ///
+ Left = 3,
+
+ ///
+ /// Вправо
+ ///
+ Right = 4
+}
diff --git a/ProjectAirFighter/ProjectAirFighter/MovementStrategy/ObjectParameters.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/ObjectParameters.cs
new file mode 100644
index 0000000..96d9888
--- /dev/null
+++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/ObjectParameters.cs
@@ -0,0 +1,75 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+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/ProjectAirFighter/ProjectAirFighter/MovementStrategy/StrategyStatus.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/StrategyStatus.cs
new file mode 100644
index 0000000..0cf805c
--- /dev/null
+++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/StrategyStatus.cs
@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectAirFighter.MovementStrategy;
+///
+/// Статус выполнения операции перемещения
+///
+public enum StrategyStatus
+{
+ ///
+ /// Все готово к началу
+ ///
+ NotInit,
+ ///
+ /// Выполняется
+ ///
+ InProgress,
+ ///
+ /// Завершено
+ ///
+ Finish
+
+}
diff --git a/ProjectAirFighter/ProjectAirFighter/ProjectAirFighter.csproj b/ProjectAirFighter/ProjectAirFighter/ProjectAirFighter.csproj
index af03d74..629ec08 100644
--- a/ProjectAirFighter/ProjectAirFighter/ProjectAirFighter.csproj
+++ b/ProjectAirFighter/ProjectAirFighter/ProjectAirFighter.csproj
@@ -2,7 +2,7 @@
WinExe
- net8.0-windows
+ net8.0-windows7.0
enable
true
enable