From 1bde4734fcd08a7bc4a2a2846d70b6226c9a182b Mon Sep 17 00:00:00 2001
From: Almaz <79022113685@mail.ru>
Date: Tue, 9 Apr 2024 15:02:30 +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
---
.../ProjectCruiser/Drawings/DirectionType.cs | 8 +
.../Drawings/DrawningCruiser.cs | 21 +++
.../ProjectCruiser/FormCruiser.Designer.cs | 28 ++++
ProjectCruiser/ProjectCruiser/FormCruiser.cs | 60 +++++++-
.../MovementStrategy/AbstactStrategy.cs | 138 ++++++++++++++++++
.../MovementStrategy/IMoveableObject.cs | 30 ++++
.../MovementStrategy/MoveToBorder.cs | 50 +++++++
.../MovementStrategy/MoveToCenter.cs | 54 +++++++
.../MovementStrategy/MoveableCruiser.cs | 68 +++++++++
.../MovementStrategy/MovementDirection.cs | 24 +++
.../MovementStrategy/ObjectParameters.cs | 77 ++++++++++
.../MovementStrategy/StrategyStatus.cs | 26 ++++
12 files changed, 576 insertions(+), 8 deletions(-)
create mode 100644 ProjectCruiser/ProjectCruiser/MovementStrategy/AbstactStrategy.cs
create mode 100644 ProjectCruiser/ProjectCruiser/MovementStrategy/IMoveableObject.cs
create mode 100644 ProjectCruiser/ProjectCruiser/MovementStrategy/MoveToBorder.cs
create mode 100644 ProjectCruiser/ProjectCruiser/MovementStrategy/MoveToCenter.cs
create mode 100644 ProjectCruiser/ProjectCruiser/MovementStrategy/MoveableCruiser.cs
create mode 100644 ProjectCruiser/ProjectCruiser/MovementStrategy/MovementDirection.cs
create mode 100644 ProjectCruiser/ProjectCruiser/MovementStrategy/ObjectParameters.cs
create mode 100644 ProjectCruiser/ProjectCruiser/MovementStrategy/StrategyStatus.cs
diff --git a/ProjectCruiser/ProjectCruiser/Drawings/DirectionType.cs b/ProjectCruiser/ProjectCruiser/Drawings/DirectionType.cs
index 3ea0799..4f4876b 100644
--- a/ProjectCruiser/ProjectCruiser/Drawings/DirectionType.cs
+++ b/ProjectCruiser/ProjectCruiser/Drawings/DirectionType.cs
@@ -11,18 +11,26 @@ namespace ProjectCruiser.Drawings;
///
public enum DirectionType
{
+ ///
+ /// Неизвестное направление
+ ///
+ Unknow = -1,
+
///
/// Вверх
///
Up = 1,
+
///
/// Вниз
///
Down = 2,
+
///
/// Влева
///
Left = 3,
+
///
/// Вправо
///
diff --git a/ProjectCruiser/ProjectCruiser/Drawings/DrawningCruiser.cs b/ProjectCruiser/ProjectCruiser/Drawings/DrawningCruiser.cs
index b953713..ea8c06f 100644
--- a/ProjectCruiser/ProjectCruiser/Drawings/DrawningCruiser.cs
+++ b/ProjectCruiser/ProjectCruiser/Drawings/DrawningCruiser.cs
@@ -24,6 +24,7 @@ public class DrawningCruiser
///
/// Левая координата прорисовки крейсера
///
+
protected int? _startPosX;
///
/// Верхняя координата прорисовки крейсера
@@ -39,6 +40,26 @@ public class DrawningCruiser
///
private readonly int _drawingCruiserHeight = 50;
+ ///
+ /// Координата X объекта
+ ///
+ public int? GetPosX => _startPosX;
+
+ ///
+ /// Координата Y объекта
+ ///
+ public int? GetPosY => _startPosY;
+
+ ///
+ /// Ширина объекта
+ ///
+ public int GetWidth => _drawningCruiserWidth;
+
+ ///
+ /// Высота объекта
+ ///
+ public int GetHeight => _drawingCruiserHeight;
+
///
/// Пустой конструктор
///
diff --git a/ProjectCruiser/ProjectCruiser/FormCruiser.Designer.cs b/ProjectCruiser/ProjectCruiser/FormCruiser.Designer.cs
index ec630ad..9b8c644 100644
--- a/ProjectCruiser/ProjectCruiser/FormCruiser.Designer.cs
+++ b/ProjectCruiser/ProjectCruiser/FormCruiser.Designer.cs
@@ -35,6 +35,8 @@
buttonRight = new Button();
buttonLeft = new Button();
buttonCreateCruiser = new Button();
+ comboBoxStrategy = new ComboBox();
+ buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCruiser).BeginInit();
SuspendLayout();
//
@@ -117,11 +119,35 @@
buttonCreateCruiser.UseVisualStyleBackColor = true;
buttonCreateCruiser.Click += buttonCreateCruiser_Click;
//
+ // comboBoxStrategy
+ //
+ comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
+ 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.Anchor = AnchorStyles.Top | AnchorStyles.Right;
+ buttonStrategyStep.Location = new Point(694, 46);
+ buttonStrategyStep.Name = "buttonStrategyStep";
+ buttonStrategyStep.Size = new Size(94, 29);
+ buttonStrategyStep.TabIndex = 8;
+ buttonStrategyStep.Text = "Шаг";
+ buttonStrategyStep.UseVisualStyleBackColor = true;
+ buttonStrategyStep.Click += ButtonStrategyStep_Click;
+ //
// FormCruiser
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
+ Controls.Add(buttonStrategyStep);
+ Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateCruiser);
Controls.Add(buttonLeft);
Controls.Add(buttonRight);
@@ -144,5 +170,7 @@
private Button buttonRight;
private Button buttonLeft;
private Button buttonCreateCruiser;
+ private ComboBox comboBoxStrategy;
+ private Button buttonStrategyStep;
}
}
\ No newline at end of file
diff --git a/ProjectCruiser/ProjectCruiser/FormCruiser.cs b/ProjectCruiser/ProjectCruiser/FormCruiser.cs
index af4d3d6..1cce72e 100644
--- a/ProjectCruiser/ProjectCruiser/FormCruiser.cs
+++ b/ProjectCruiser/ProjectCruiser/FormCruiser.cs
@@ -1,4 +1,5 @@
using ProjectCruiser.Drawings;
+using ProjectCruiser.MovementStrategy;
namespace ProjectCruiser
{
@@ -6,9 +7,12 @@ namespace ProjectCruiser
{
private DrawningCruiser? _drawingCruiser;
+
+ private AbstactStrategy? _strategy;
public FormCruiser()
{
InitializeComponent();
+ _strategy = null;
}
private void Draw()
@@ -37,13 +41,13 @@ namespace ProjectCruiser
case nameof(DrawningMilitaryCruiser):
- _drawingCruiser = new DrawningMilitaryCruiser(random.Next(100, 300),
- 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)));
+ _drawingCruiser = new DrawningMilitaryCruiser(random.Next(100, 300),
+ 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)));
break;
@@ -53,7 +57,8 @@ namespace ProjectCruiser
_drawingCruiser.SetPictireSize(pictureBoxCruiser.Width, pictureBoxCruiser.Height);
_drawingCruiser.SetPosition(random.Next(10, 100), random.Next(10, 100));
-
+ _strategy = null;
+ comboBoxStrategy.Enabled = true;
Draw();
}
@@ -102,5 +107,44 @@ namespace ProjectCruiser
}
}
+ ///
+ /// Обработка нажатия кнопки "Шаг"
+ ///
+ ///
+ ///
+ private void ButtonStrategyStep_Click(object sender, EventArgs e)
+ {
+ if (_drawingCruiser == null) { return; }
+
+ if (comboBoxStrategy.Enabled)
+ {
+ _strategy = comboBoxStrategy.SelectedIndex switch
+ {
+ 0 => new MoveToCenter(),
+ 1 => new MoveToBorder(),
+ _ => null,
+ };
+ if (_strategy == null)
+ {
+ return ;
+ }
+ _strategy.SetData(new MoveableCruiser(_drawingCruiser), pictureBoxCruiser.Width, pictureBoxCruiser.Height);
+ }
+
+ if (_strategy == null)
+ {
+ return;
+ }
+
+ comboBoxStrategy.Enabled = false;
+ _strategy.MakeStep();
+ Draw();
+
+ if (_strategy.GetStatus() == StrategyStatus.Finish)
+ {
+ comboBoxStrategy.Enabled = true;
+ _strategy = null;
+ }
+ }
}
}
diff --git a/ProjectCruiser/ProjectCruiser/MovementStrategy/AbstactStrategy.cs b/ProjectCruiser/ProjectCruiser/MovementStrategy/AbstactStrategy.cs
new file mode 100644
index 0000000..b38a124
--- /dev/null
+++ b/ProjectCruiser/ProjectCruiser/MovementStrategy/AbstactStrategy.cs
@@ -0,0 +1,138 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectCruiser.MovementStrategy;
+
+///
+/// Класс-стратегия перемещения объекта
+///
+public abstract class AbstactStrategy
+{
+ ///
+ /// Перемещаемы объект
+ ///
+ 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 (IsTargetDestination())
+ {
+ _state = StrategyStatus.Finish;
+ return;
+ }
+
+ MoveToTarget();
+ }
+
+ ///
+ /// Перемещение влево
+ ///
+ ///
+ protected bool MoveLeft() => MoveTo(MovementDirection.Left);
+
+ ///
+ /// Перемещение вправо
+ ///
+ ///
+ protected bool MoveRight() => MoveTo(MovementDirection.Right);
+
+ ///
+ /// Перемещение вверх
+ ///
+ ///
+ protected bool MoveUp() => MoveTo(MovementDirection.Up);
+
+ ///
+ /// Перемещение вниз
+ ///
+ ///
+ 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 IsTargetDestination();
+
+
+ private bool MoveTo(MovementDirection direction)
+ {
+ if (_state != StrategyStatus.InProgress)
+ {
+ return false;
+ }
+ return _moveableObject?.TryMoveObject(direction) ?? false;
+ }
+
+}
diff --git a/ProjectCruiser/ProjectCruiser/MovementStrategy/IMoveableObject.cs b/ProjectCruiser/ProjectCruiser/MovementStrategy/IMoveableObject.cs
new file mode 100644
index 0000000..fd23511
--- /dev/null
+++ b/ProjectCruiser/ProjectCruiser/MovementStrategy/IMoveableObject.cs
@@ -0,0 +1,30 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectCruiser.MovementStrategy;
+
+///
+/// Интерфейс для работы с перемещением объектов
+///
+public interface IMoveableObject
+{
+ ///
+ /// Получение координаты объекта
+ ///
+ ObjectParameters? GetObjectPosition { get; }
+
+ ///
+ /// Шаг объекта
+ ///
+ int GetStep { get; }
+
+ ///
+ /// Попытка переместить объект в указанном направлении
+ ///
+ /// Направление
+ ///
+ bool TryMoveObject(MovementDirection direction);
+}
diff --git a/ProjectCruiser/ProjectCruiser/MovementStrategy/MoveToBorder.cs b/ProjectCruiser/ProjectCruiser/MovementStrategy/MoveToBorder.cs
new file mode 100644
index 0000000..8469f08
--- /dev/null
+++ b/ProjectCruiser/ProjectCruiser/MovementStrategy/MoveToBorder.cs
@@ -0,0 +1,50 @@
+namespace ProjectCruiser.MovementStrategy;
+
+public class MoveToBorder : AbstactStrategy
+{
+ protected override bool IsTargetDestination()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null)
+ {
+ return false;
+ }
+ return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth &&
+ objParams.ObjectMiddleVertical - GetStep() <= FieldHeight && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight;
+ }
+
+ protected override void MoveToTarget()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null)
+ {
+ return;
+ }
+
+ int diffX = objParams.ObjectMiddleHorizontal - FieldWidth;
+ if (Math.Abs(diffX) > GetStep())
+ {
+ if (diffX > 0)
+ {
+ MoveLeft();
+ }
+ else
+ {
+ MoveRight();
+ }
+ }
+
+ int diffY = objParams.ObjectMiddleVertical - FieldHeight;
+ if (Math.Abs(diffY) > GetStep())
+ {
+ if (diffY > 0)
+ {
+ MoveUp();
+ }
+ else
+ {
+ MoveDown();
+ }
+ }
+ }
+}
diff --git a/ProjectCruiser/ProjectCruiser/MovementStrategy/MoveToCenter.cs b/ProjectCruiser/ProjectCruiser/MovementStrategy/MoveToCenter.cs
new file mode 100644
index 0000000..c88b51e
--- /dev/null
+++ b/ProjectCruiser/ProjectCruiser/MovementStrategy/MoveToCenter.cs
@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectCruiser.MovementStrategy;
+
+public class MoveToCenter : AbstactStrategy
+{
+ protected override bool IsTargetDestination()
+ {
+ 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/ProjectCruiser/ProjectCruiser/MovementStrategy/MoveableCruiser.cs b/ProjectCruiser/ProjectCruiser/MovementStrategy/MoveableCruiser.cs
new file mode 100644
index 0000000..4755f4d
--- /dev/null
+++ b/ProjectCruiser/ProjectCruiser/MovementStrategy/MoveableCruiser.cs
@@ -0,0 +1,68 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using ProjectCruiser.Drawings;
+
+namespace ProjectCruiser.MovementStrategy;
+
+///
+/// Класс реализация IMoveableObject с использованием DrawningCruiser
+///
+public class MoveableCruiser : IMoveableObject
+{
+ ///
+ /// Поле-объект класса DrawningCruiser или его наследника
+ ///
+ private readonly DrawningCruiser? _cruiser = null;
+
+ ///
+ /// Конструктор
+ ///
+ ///
+ public MoveableCruiser(DrawningCruiser? cruiser)
+ {
+ _cruiser = cruiser;
+ }
+
+ public ObjectParameters? GetObjectPosition
+ {
+ get
+ {
+ if (_cruiser == null || _cruiser.EntityCruiser == null || !_cruiser.GetPosX.HasValue || !_cruiser.GetPosY.HasValue)
+ {
+ return null;
+ }
+ return new ObjectParameters(_cruiser.GetPosX.Value, _cruiser.GetPosY.Value, _cruiser.GetWidth, _cruiser.GetHeight);
+ }
+ }
+
+ public int GetStep => (int)(_cruiser?.EntityCruiser?.Step ?? 0);
+
+ public bool TryMoveObject(MovementDirection direction)
+ {
+ if (_cruiser == null || _cruiser.EntityCruiser == null)
+ {
+ return false;
+ }
+ return _cruiser.MoveTransport(GetDirectionType(direction));
+ }
+
+ ///
+ /// Конвертация из 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/ProjectCruiser/ProjectCruiser/MovementStrategy/MovementDirection.cs b/ProjectCruiser/ProjectCruiser/MovementStrategy/MovementDirection.cs
new file mode 100644
index 0000000..2d6fe5f
--- /dev/null
+++ b/ProjectCruiser/ProjectCruiser/MovementStrategy/MovementDirection.cs
@@ -0,0 +1,24 @@
+namespace ProjectCruiser.MovementStrategy;
+
+///
+/// Направление перемещения
+///
+public enum MovementDirection
+{
+ ///
+ /// Вверх
+ ///
+ Up = 1,
+ ///
+ /// Вниз
+ ///
+ Down = 2,
+ ///
+ /// Влева
+ ///
+ Left = 3,
+ ///
+ /// Вправо
+ ///
+ Right = 4
+}
diff --git a/ProjectCruiser/ProjectCruiser/MovementStrategy/ObjectParameters.cs b/ProjectCruiser/ProjectCruiser/MovementStrategy/ObjectParameters.cs
new file mode 100644
index 0000000..dd28ac0
--- /dev/null
+++ b/ProjectCruiser/ProjectCruiser/MovementStrategy/ObjectParameters.cs
@@ -0,0 +1,77 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectCruiser.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;
+
+ ///
+ /// Конструктор
+ ///
+ ///
+ ///
+ ///
+ ///
+ public ObjectParameters(int x, int y, int width, int height)
+ {
+ _x = x;
+ _y = y;
+ _width = width;
+ _height = height;
+ }
+}
diff --git a/ProjectCruiser/ProjectCruiser/MovementStrategy/StrategyStatus.cs b/ProjectCruiser/ProjectCruiser/MovementStrategy/StrategyStatus.cs
new file mode 100644
index 0000000..d769b40
--- /dev/null
+++ b/ProjectCruiser/ProjectCruiser/MovementStrategy/StrategyStatus.cs
@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectCruiser.MovementStrategy;
+
+///
+/// Статус выполнения операции перемещения
+///
+public enum StrategyStatus
+{
+ ///
+ /// Все готово к началу
+ ///
+ NotInit,
+ ///
+ /// Выполняется
+ ///
+ InProgress,
+ ///
+ /// Завершено
+ ///
+ Finish
+}