diff --git a/ProjectCatamaran/ProjectCatamaran/Drawnings/DirectionType.cs b/ProjectCatamaran/ProjectCatamaran/Drawnings/DirectionType.cs
index 6d2f75e..15c6362 100644
--- a/ProjectCatamaran/ProjectCatamaran/Drawnings/DirectionType.cs
+++ b/ProjectCatamaran/ProjectCatamaran/Drawnings/DirectionType.cs
@@ -8,6 +8,10 @@ namespace ProjectCatamaran.Drawnings;
public enum DirectionType
{
+ ///
+ /// неизвестное направление
+ ///
+ Unknown = -1,
///
/// Вверх
///
diff --git a/ProjectCatamaran/ProjectCatamaran/Drawnings/DrawningCatamaranBase.cs b/ProjectCatamaran/ProjectCatamaran/Drawnings/DrawningCatamaranBase.cs
index 77c1556..9ce8fcd 100644
--- a/ProjectCatamaran/ProjectCatamaran/Drawnings/DrawningCatamaranBase.cs
+++ b/ProjectCatamaran/ProjectCatamaran/Drawnings/DrawningCatamaranBase.cs
@@ -37,6 +37,23 @@ public class DrawningCatamaranBase
/// Высота прорисовки катамарана
///
private readonly int _drawningCatamaranHeight = 25;
+ ///
+ /// координата х обьекта
+ ///
+ public int? GetPosX => _startPosX;
+ ///
+ /// координата Y обьекта
+ ///
+ public int? GetPosY => _startPosY;
+ ///
+ /// ширина обьекта
+ ///
+ public int GetWidth => _drawningCatamaranWidth;
+ ///
+ /// высота обьекта
+ ///
+ public int GetHeight => _drawningCatamaranHeight;
+
///
/// пустой конструктор
///
diff --git a/ProjectCatamaran/ProjectCatamaran/FormCatamaran.Designer.cs b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.Designer.cs
index 4107701..3ceeaf4 100644
--- a/ProjectCatamaran/ProjectCatamaran/FormCatamaran.Designer.cs
+++ b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.Designer.cs
@@ -35,6 +35,8 @@
buttonLeft = new Button();
buttonDown = new Button();
buttonCreateCatamaranBase = new Button();
+ comboBoxStrategy = new ComboBox();
+ buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxCatamaran).BeginInit();
SuspendLayout();
//
@@ -117,11 +119,34 @@
buttonCreateCatamaranBase.UseVisualStyleBackColor = true;
buttonCreateCatamaranBase.Click += ButtonCreateCatamaranBase_Click;
//
+ // comboBoxStrategy
+ //
+ comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
+ comboBoxStrategy.FormattingEnabled = true;
+ comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
+ comboBoxStrategy.Location = new Point(668, 12);
+ comboBoxStrategy.Name = "comboBoxStrategy";
+ comboBoxStrategy.Size = new Size(121, 23);
+ comboBoxStrategy.TabIndex = 7;
+ //
+ // buttonStrategyStep
+ //
+ buttonStrategyStep.Location = new Point(709, 41);
+ buttonStrategyStep.Name = "buttonStrategyStep";
+ buttonStrategyStep.RightToLeft = RightToLeft.Yes;
+ buttonStrategyStep.Size = new Size(75, 23);
+ buttonStrategyStep.TabIndex = 8;
+ buttonStrategyStep.Text = "Шаг";
+ buttonStrategyStep.UseVisualStyleBackColor = true;
+ buttonStrategyStep.Click += ButtonStrategyStep_Click;
+ //
// FormCatamaran
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
+ Controls.Add(buttonStrategyStep);
+ Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateCatamaranBase);
Controls.Add(buttonDown);
Controls.Add(buttonLeft);
@@ -144,5 +169,7 @@
private Button buttonLeft;
private Button buttonDown;
private Button buttonCreateCatamaranBase;
+ private ComboBox comboBoxStrategy;
+ private Button buttonStrategyStep;
}
}
\ No newline at end of file
diff --git a/ProjectCatamaran/ProjectCatamaran/FormCatamaran.cs b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.cs
index ba6fb78..d6ab145 100644
--- a/ProjectCatamaran/ProjectCatamaran/FormCatamaran.cs
+++ b/ProjectCatamaran/ProjectCatamaran/FormCatamaran.cs
@@ -9,15 +9,18 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
+using ProjectCatamaran.MovementStrategy;
namespace ProjectCatamaran
{
public partial class FormCatamaran : Form
{
private DrawningCatamaranBase? _drawningCatamaranBase;
+ private AbstractStrategy? _strategy;
public FormCatamaran()
{
InitializeComponent();
+ _strategy = null;
}
private void Draw()
@@ -81,8 +84,9 @@ namespace ProjectCatamaran
}
_drawningCatamaranBase.SetPictureSize(pictureBoxCatamaran.Width, pictureBoxCatamaran.Height);
_drawningCatamaranBase.SetPosition(random.Next(10, 100), random.Next(10, 100));
+ _strategy = null;
+ comboBoxStrategy.Enabled = true;
Draw();
-
}
private void ButtonCreateCatamaran_Click(object sender, EventArgs e)
{
@@ -93,5 +97,44 @@ namespace ProjectCatamaran
{
CreateObject(nameof(DrawningCatamaranBase));
}
+ ///
+ /// Обработка нажатия кнопки "Шаг"
+ ///
+ ///
+ ///
+ private void ButtonStrategyStep_Click(object sender, EventArgs e)
+ {
+ if (_drawningCatamaranBase == null)
+ {
+ return;
+ }
+ if (comboBoxStrategy.Enabled)
+ {
+ _strategy = comboBoxStrategy.SelectedIndex switch
+ {
+ 0 => new MoveToCenter(),
+ 1 => new MoveToBorder(),
+ _ => null,
+ };
+ if (_strategy == null)
+ {
+ return;
+ }
+ _strategy.SetData(new MoveableCatamaranBase(_drawningCatamaranBase),
+ pictureBoxCatamaran.Width, pictureBoxCatamaran.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/ProjectCatamaran/ProjectCatamaran/MovementStrategy/AbstractStrategy.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/AbstractStrategy.cs
new file mode 100644
index 0000000..d2395be
--- /dev/null
+++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/AbstractStrategy.cs
@@ -0,0 +1,124 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectCatamaran.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 (IsTargetDestination())
+ {
+ _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 IsTargetDestination();
+ ///
+ /// Попытка перемещения в требуемом направлении
+ ///
+ /// Направление
+ /// Результат попытки (true - удалось переместиться, false - неудача)
+ private bool MoveTo(MovementDirection movementDirection)
+ {
+ if (_state != StrategyStatus.InProgress)
+ {
+ return false;
+ }
+ return _moveableObject?.TryMoveObject(movementDirection) ?? false;
+ }
+}
+
diff --git a/ProjectCatamaran/ProjectCatamaran/MovementStrategy/IMoveableObjects.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/IMoveableObjects.cs
new file mode 100644
index 0000000..49cfdbe
--- /dev/null
+++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/IMoveableObjects.cs
@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectCatamaran.MovementStrategy;
+public interface IMoveableObject
+{
+ ///
+ /// Получение координаты объекта
+ ///
+ ObjectParameters? GetObjectPosition { get; }
+ ///
+ /// Шаг объекта
+ ///
+ int GetStep { get; }
+ ///
+ /// Попытка переместить объект в указанном направлении
+ ///
+ /// Направление
+ /// true - объект перемещен, false - перемещение невозможно
+ bool TryMoveObject(MovementDirection direction);
+}
+
diff --git a/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveToBorder.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveToBorder.cs
new file mode 100644
index 0000000..ba16109
--- /dev/null
+++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveToBorder.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectCatamaran.MovementStrategy;
+public class MoveToBorder : AbstractStrategy
+{
+ ///
+ /// движение до правого края
+ ///
+ protected override void MoveToTarget()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null)
+ return;
+ if (objParams.RightBorder + GetStep() < FieldWidth)
+ MoveRight();
+ if (objParams.DownBorder + GetStep() < FieldHeight)
+ MoveDown();
+
+ }
+ ///
+ /// проверка достижения цели
+ ///
+ /// true - достиг , false - нет
+ protected override bool IsTargetDestination()
+ {
+ ObjectParameters? objParams = GetObjectParameters;
+ if (objParams == null)
+ return false;
+ return objParams.RightBorder + GetStep() >= FieldWidth &&
+ objParams.DownBorder + GetStep() >= FieldHeight ;
+ }
+}
+
diff --git a/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveToCenter.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveToCenter.cs
new file mode 100644
index 0000000..280f6f3
--- /dev/null
+++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveToCenter.cs
@@ -0,0 +1,60 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectCatamaran.MovementStrategy;
+///
+/// стратегия перемещения обьекта в центр экрана
+///
+public class MoveToCenter : AbstractStrategy
+{
+ 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();
+ }
+ }
+ }
+
+ 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;
+ }
+
+}
+
diff --git a/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveableCatamaranBase.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveableCatamaranBase.cs
new file mode 100644
index 0000000..d13cde3
--- /dev/null
+++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MoveableCatamaranBase.cs
@@ -0,0 +1,67 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using ProjectCatamaran.Drawnings;
+
+namespace ProjectCatamaran.MovementStrategy
+{
+ internal class MoveableCatamaranBase : IMoveableObject
+ {
+ ///
+ /// Поле-объект класса DrawningCatamaranBase или его наследника
+ ///
+ private DrawningCatamaranBase _catamaranBase = null;
+ ///
+ /// Конструктор
+ ///
+ /// Объект класса DrawningCatamaranBase
+ public MoveableCatamaranBase(DrawningCatamaranBase catamaranBase)
+ {
+ _catamaranBase = catamaranBase;
+ }
+
+ public ObjectParameters? GetObjectPosition
+ {
+ get
+ {
+ if (_catamaranBase == null || _catamaranBase.EntityCatamaranBase == null || !_catamaranBase.GetPosX.HasValue
+ || !_catamaranBase.GetPosY.HasValue)
+ {
+ return null;
+ }
+
+ return new ObjectParameters(_catamaranBase.GetPosX.Value, _catamaranBase.GetPosY.Value,
+ _catamaranBase.GetWidth, _catamaranBase.GetHeight);
+ }
+ }
+
+ public int GetStep => (int)(_catamaranBase?.EntityCatamaranBase?.Step ?? 0);
+ public bool TryMoveObject(MovementDirection direction)
+ {
+ if (_catamaranBase == null || _catamaranBase.EntityCatamaranBase == null)
+ {
+ return false;
+ }
+
+ return _catamaranBase.MoveTransport(GetDirectionType(direction));
+ }
+ ///
+ /// Конвертация из MovementDirection в DirectionType
+ ///
+ ///
+ /// 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.Unknown
+ };
+ }
+ }
+}
diff --git a/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MovementDirection.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MovementDirection.cs
new file mode 100644
index 0000000..0c99da6
--- /dev/null
+++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/MovementDirection.cs
@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectCatamaran.MovementStrategy;
+public enum MovementDirection
+{
+
+ ///
+ /// Вверх
+ ///
+ Up = 1,
+ ///
+ /// Вниз
+ ///
+ Down = 2,
+ ///
+ /// Влево
+ ///
+ Left = 3,
+ ///
+ /// Вправо
+ ///
+ Right = 4
+}
diff --git a/ProjectCatamaran/ProjectCatamaran/MovementStrategy/ObjectParameters.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/ObjectParameters.cs
new file mode 100644
index 0000000..b9d9e0b
--- /dev/null
+++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/ObjectParameters.cs
@@ -0,0 +1,66 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectCatamaran.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/ProjectCatamaran/ProjectCatamaran/MovementStrategy/StrategyStatus.cs b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/StrategyStatus.cs
new file mode 100644
index 0000000..fe0389a
--- /dev/null
+++ b/ProjectCatamaran/ProjectCatamaran/MovementStrategy/StrategyStatus.cs
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectCatamaran.MovementStrategy;
+public enum StrategyStatus
+{
+ NotInit,
+ InProgress,
+ Finish
+}