From 39429dea0f468f6614ea74c86e92136b5840dd81 Mon Sep 17 00:00:00 2001 From: VirBiuM Date: Mon, 11 Mar 2024 14:25:54 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D1=82=D1=80=D0=B0=D1=82=D0=B5=D0=B3?= =?UTF-8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Drawnings/DirectionType.cs | 5 + .../Drawnings/DrawningWarPlane.cs | 20 +++ .../FormAirFighter.Designer.cs | 26 ++++ .../ProjectAirFighter/FormAirFighter.cs | 55 ++++++- .../MovementStrategy/AbstractStrategy.cs | 135 ++++++++++++++++++ .../MovementStrategy/IMoveableObjetc.cs | 24 ++++ .../MovementStrategy/MoveToBorder.cs | 36 +++++ .../MovementStrategy/MoveToCenter.cs | 53 +++++++ .../MovementStrategy/MoveableWarPlane.cs | 57 ++++++++ .../MovementStrategy/MovementDirection.cs | 24 ++++ .../MovementStrategy/ObjectParameters.cs | 69 +++++++++ .../MovementStrategy/StrategyStatus.cs | 17 +++ 12 files changed, 519 insertions(+), 2 deletions(-) create mode 100644 ProjectAirFighter/ProjectAirFighter/MovementStrategy/AbstractStrategy.cs create mode 100644 ProjectAirFighter/ProjectAirFighter/MovementStrategy/IMoveableObjetc.cs create mode 100644 ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToBorder.cs create mode 100644 ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToCenter.cs create mode 100644 ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveableWarPlane.cs create mode 100644 ProjectAirFighter/ProjectAirFighter/MovementStrategy/MovementDirection.cs create mode 100644 ProjectAirFighter/ProjectAirFighter/MovementStrategy/ObjectParameters.cs create mode 100644 ProjectAirFighter/ProjectAirFighter/MovementStrategy/StrategyStatus.cs diff --git a/ProjectAirFighter/ProjectAirFighter/Drawnings/DirectionType.cs b/ProjectAirFighter/ProjectAirFighter/Drawnings/DirectionType.cs index 7db34d9..8ddd95a 100644 --- a/ProjectAirFighter/ProjectAirFighter/Drawnings/DirectionType.cs +++ b/ProjectAirFighter/ProjectAirFighter/Drawnings/DirectionType.cs @@ -5,6 +5,11 @@ /// public enum DirectionType { + /// + /// Неизвестное направление + /// + Unknow = -1, + /// /// Вверх /// diff --git a/ProjectAirFighter/ProjectAirFighter/Drawnings/DrawningWarPlane.cs b/ProjectAirFighter/ProjectAirFighter/Drawnings/DrawningWarPlane.cs index c782dd7..67d604e 100644 --- a/ProjectAirFighter/ProjectAirFighter/Drawnings/DrawningWarPlane.cs +++ b/ProjectAirFighter/ProjectAirFighter/Drawnings/DrawningWarPlane.cs @@ -39,6 +39,26 @@ public class DrawningWarPlane /// private readonly int _drawningPlaneHeight = 150; + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningPlaneWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawningPlaneHeight; + /// /// Пустой конструктор /// diff --git a/ProjectAirFighter/ProjectAirFighter/FormAirFighter.Designer.cs b/ProjectAirFighter/ProjectAirFighter/FormAirFighter.Designer.cs index c19d18d..9504882 100644 --- a/ProjectAirFighter/ProjectAirFighter/FormAirFighter.Designer.cs +++ b/ProjectAirFighter/ProjectAirFighter/FormAirFighter.Designer.cs @@ -35,6 +35,8 @@ buttonRight = new Button(); buttonDown = new Button(); buttonCreateWarPlane = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirFighter).BeginInit(); SuspendLayout(); // @@ -124,11 +126,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(719, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(775, 46); + 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(882, 673); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateWarPlane); Controls.Add(buttonDown); Controls.Add(buttonRight); @@ -152,5 +176,7 @@ private Button buttonRight; private Button buttonDown; 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 0a2e039..216be7d 100644 --- a/ProjectAirFighter/ProjectAirFighter/FormAirFighter.cs +++ b/ProjectAirFighter/ProjectAirFighter/FormAirFighter.cs @@ -1,4 +1,5 @@ using ProjectAirFighter.Drawnings; +using ProjectAirFighter.MovementStrategy; namespace ProjectAirFighter; @@ -12,12 +13,18 @@ public partial class FormAirFighter : Form /// private DrawningWarPlane? _drawningWarPlane; + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; + /// /// Конструктор формы /// public FormAirFighter() { InitializeComponent(); + _strategy = null; } /// @@ -42,11 +49,11 @@ public partial class FormAirFighter : Form switch (type) { case nameof(DrawningWarPlane): - _drawningWarPlane = new DrawningWarPlane(random.Next(100, 300), random.Next(1000, 3000), + _drawningWarPlane = new DrawningWarPlane(random.Next(300, 600), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); break; case nameof(DrawningAirFighter): - _drawningWarPlane = new DrawningAirFighter(random.Next(100, 300), random.Next(1000, 3000), + _drawningWarPlane = new DrawningAirFighter(random.Next(300, 600), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); @@ -56,6 +63,8 @@ public partial class FormAirFighter : Form } _drawningWarPlane.SetPictureSize(pictureBoxAirFighter.Width, pictureBoxAirFighter.Height); _drawningWarPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } @@ -108,4 +117,46 @@ public partial class FormAirFighter : Form Draw(); } } + + /// + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + 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.SetData(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; + } + } } \ No newline at end of file diff --git a/ProjectAirFighter/ProjectAirFighter/MovementStrategy/AbstractStrategy.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..534a52a --- /dev/null +++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,135 @@ +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; + } +} \ No newline at end of file diff --git a/ProjectAirFighter/ProjectAirFighter/MovementStrategy/IMoveableObjetc.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/IMoveableObjetc.cs new file mode 100644 index 0000000..7482eef --- /dev/null +++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/IMoveableObjetc.cs @@ -0,0 +1,24 @@ +namespace ProjectAirFighter.MovementStrategy; + +/// +/// Интерфейс для работы с перемещаемым объектом +/// +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} \ No newline at end of file diff --git a/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToBorder.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..8f32d86 --- /dev/null +++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,36 @@ +namespace ProjectAirFighter.MovementStrategy; + +/// +/// Стратегия перемещения объекта в правый нижний угол +/// +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.RightBorder + GetStep() >= FieldWidth + && objParams.DownBorder + GetStep() >= FieldHeight; + } + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + int diffX = objParams.LeftBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + MoveRight(); + } + int diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + MoveDown(); + } + } +} \ No newline at end of file diff --git a/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToCenter.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..337aaf4 --- /dev/null +++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,53 @@ +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(); + } + } + } +} \ No newline at end of file diff --git a/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveableWarPlane.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveableWarPlane.cs new file mode 100644 index 0000000..a8437ef --- /dev/null +++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MoveableWarPlane.cs @@ -0,0 +1,57 @@ +using ProjectAirFighter.Drawnings; + +namespace ProjectAirFighter.MovementStrategy; + +/// +/// Класс-реализация IMoveableObject с использованием DrawningWarPlane +/// +public class MoveableWarPlane : IMoveableObject +{ + private readonly DrawningWarPlane? _warPlane = null; + + 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.GetWidth, _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, + }; + } +} \ No newline at end of file diff --git a/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MovementDirection.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..bb15cc8 --- /dev/null +++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/MovementDirection.cs @@ -0,0 +1,24 @@ +namespace ProjectAirFighter.MovementStrategy; + +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + + /// + /// Вниз + /// + Down = 2, + + /// + /// Влево + /// + Left = 3, + + /// + /// Вправо + /// + Right = 4 +} \ No newline at end of file diff --git a/ProjectAirFighter/ProjectAirFighter/MovementStrategy/ObjectParameters.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..465af28 --- /dev/null +++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,69 @@ +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; + } +} \ No newline at end of file diff --git a/ProjectAirFighter/ProjectAirFighter/MovementStrategy/StrategyStatus.cs b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..6273fe6 --- /dev/null +++ b/ProjectAirFighter/ProjectAirFighter/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,17 @@ +namespace ProjectAirFighter.MovementStrategy; + +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + /// + /// Выполняется + /// + InProgress, + /// + /// Завершено + /// + Finish +} \ No newline at end of file