diff --git a/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs b/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs
index cc78a5a..bca182c 100644
--- a/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs
+++ b/ProjectLiner/ProjectLiner/Drawnings/DirectionType.cs
@@ -5,6 +5,11 @@
///
public enum DirectionType
{
+ ///
+ /// Неизвестное направление
+ ///
+ Unknow = -1,
+
///
/// Вверх
///
diff --git a/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs b/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs
index 2bc1885..f3e6fe6 100644
--- a/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs
+++ b/ProjectLiner/ProjectLiner/Drawnings/DrawningCommonLiner.cs
@@ -43,6 +43,26 @@ public class DrawningCommonLiner
///
private readonly int _drawningLinerHeight = 90;
+ ///
+ /// Координата X объекта
+ ///
+ public int? GetPosX => _startPosX;
+
+ ///
+ /// Координата Y объекта
+ ///
+ public int? GetPosY => _startPosY;
+
+ ///
+ /// Ширина объекта
+ ///
+ public int GetWidth => _drawningLinerWidth;
+
+ ///
+ /// Высота объекта
+ ///
+ public int GetHeight => _drawningLinerHeight;
+
///
/// Пустой конструктор
///
diff --git a/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs b/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs
index a2ce841..46a4021 100644
--- a/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs
+++ b/ProjectLiner/ProjectLiner/Drawnings/DrawningLiner.cs
@@ -12,7 +12,7 @@ public class DrawningLiner : DrawningCommonLiner
///
/// Скорость
/// Вес
- /// Основной цвет
+ /// Основной цвет
/// Дополнительный цвет
/// Признак наличия якоря
/// Признак наличия шлюпок
@@ -65,7 +65,7 @@ public class DrawningLiner : DrawningCommonLiner
Point point11 = new Point(_startPosX.Value + 115, _startPosY.Value + 40);
Point point12 = new Point(_startPosX.Value + 115, _startPosY.Value + 10);
Point[] curvePoints3 = { point9, point10, point11, point12 };
- g.FillPolygon(br, curvePoints3);
+ g.FillPolygon(additionalBrush, curvePoints3);
}
}
diff --git a/ProjectLiner/ProjectLiner/FormLiner.Designer.cs b/ProjectLiner/ProjectLiner/FormLiner.Designer.cs
index 2806d52..c21ee63 100644
--- a/ProjectLiner/ProjectLiner/FormLiner.Designer.cs
+++ b/ProjectLiner/ProjectLiner/FormLiner.Designer.cs
@@ -35,6 +35,8 @@
buttonLeft = new Button();
buttonRight = new Button();
buttonCreateCommonLiner = new Button();
+ comboBoxStrategy = new ComboBox();
+ buttonStrategyStep = new Button();
((System.ComponentModel.ISupportInitialize)pictureBoxLiner).BeginInit();
SuspendLayout();
//
@@ -118,11 +120,33 @@
buttonCreateCommonLiner.UseVisualStyleBackColor = true;
buttonCreateCommonLiner.Click += buttonCreateCommonLiner_Click;
//
+ // comboBoxStrategy
+ //
+ comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
+ comboBoxStrategy.FormattingEnabled = true;
+ comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
+ comboBoxStrategy.Location = new Point(742, 12);
+ comboBoxStrategy.Name = "comboBoxStrategy";
+ comboBoxStrategy.Size = new Size(194, 33);
+ comboBoxStrategy.TabIndex = 11;
+ //
+ // buttonStrategyStep
+ //
+ buttonStrategyStep.Location = new Point(816, 51);
+ buttonStrategyStep.Name = "buttonStrategyStep";
+ buttonStrategyStep.Size = new Size(120, 37);
+ buttonStrategyStep.TabIndex = 12;
+ buttonStrategyStep.Text = "Шаг";
+ buttonStrategyStep.UseVisualStyleBackColor = true;
+ buttonStrategyStep.Click += buttonStrategyStep_Click;
+ //
// FormLiner
//
AutoScaleDimensions = new SizeF(11F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(948, 614);
+ Controls.Add(buttonStrategyStep);
+ Controls.Add(comboBoxStrategy);
Controls.Add(buttonCreateCommonLiner);
Controls.Add(buttonRight);
Controls.Add(buttonLeft);
@@ -145,5 +169,7 @@
private Button buttonLeft;
private Button buttonRight;
private Button buttonCreateCommonLiner;
+ private ComboBox comboBoxStrategy;
+ private Button buttonStrategyStep;
}
}
\ No newline at end of file
diff --git a/ProjectLiner/ProjectLiner/FormLiner.cs b/ProjectLiner/ProjectLiner/FormLiner.cs
index 8597fce..aa13498 100644
--- a/ProjectLiner/ProjectLiner/FormLiner.cs
+++ b/ProjectLiner/ProjectLiner/FormLiner.cs
@@ -8,15 +8,19 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectLiner.Drawnings;
+using ProjectLiner.MovementStrategy;
namespace ProjectLiner
{
public partial class FormLiner : Form
{
private DrawningCommonLiner? _drawningCommonLiner;
+
+ private AbstractStrategy? _strategy;
public FormLiner()
{
InitializeComponent();
+ _strategy = null;
}
///
@@ -61,7 +65,8 @@ namespace ProjectLiner
_drawningCommonLiner.SetPictureSize(pictureBoxLiner.Width, pictureBoxLiner.Height);
_drawningCommonLiner.SetPosition(random.Next(10, 100), random.Next(10, 100));
-
+ _strategy = null;
+ comboBoxStrategy.Enabled = true;
Draw();
}
@@ -122,5 +127,47 @@ namespace ProjectLiner
}
}
+ ///
+ /// Обработка нажатия кнопки "Шаг"
+ ///
+ ///
+ ///
+ private void buttonStrategyStep_Click(object sender, EventArgs e)
+ {
+ if (_drawningCommonLiner == null)
+ {
+ return;
+ }
+
+ if (comboBoxStrategy.Enabled)
+ {
+ _strategy = comboBoxStrategy.SelectedIndex switch
+ {
+ 0 => new MoveToCenter(),
+ 1 => new MoveToBorder(),
+ _ => null,
+ };
+ if (_strategy == null)
+ {
+ return;
+ }
+ _strategy.SetData(new MoveableLiner(_drawningCommonLiner), pictureBoxLiner.Width, pictureBoxLiner.Height);
+ }
+
+ if (_strategy == null)
+ {
+ return;
+ }
+
+ comboBoxStrategy.Enabled = false;
+ _strategy.MakeStep();
+ Draw();
+
+ if (_strategy.GetStatus() == StrategyStatus.Finish)
+ {
+ comboBoxStrategy.Enabled = true;
+ _strategy = null;
+ }
+ }
}
}
diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/AbstractStrategy.cs b/ProjectLiner/ProjectLiner/MovementStrategy/AbstractStrategy.cs
new file mode 100644
index 0000000..ca37c66
--- /dev/null
+++ b/ProjectLiner/ProjectLiner/MovementStrategy/AbstractStrategy.cs
@@ -0,0 +1,145 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectLiner.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;
+ }
+}
\ No newline at end of file
diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/IMoveableObjectcs.cs b/ProjectLiner/ProjectLiner/MovementStrategy/IMoveableObjectcs.cs
new file mode 100644
index 0000000..a5ace3f
--- /dev/null
+++ b/ProjectLiner/ProjectLiner/MovementStrategy/IMoveableObjectcs.cs
@@ -0,0 +1,30 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectLiner.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/ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs b/ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs
new file mode 100644
index 0000000..37eced3
--- /dev/null
+++ b/ProjectLiner/ProjectLiner/MovementStrategy/MoveToBorder.cs
@@ -0,0 +1,58 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectLiner.MovementStrategy;
+
+///
+/// Стратегия перемещения объекта к правой нижней границы
+///
+public class MoveToBorder : AbstractStrategy
+{
+ protected override bool IsTargetDestination()
+ {
+ 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/ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs b/ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs
new file mode 100644
index 0000000..afe820c
--- /dev/null
+++ b/ProjectLiner/ProjectLiner/MovementStrategy/MoveToCenter.cs
@@ -0,0 +1,60 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectLiner.MovementStrategy;
+
+///
+/// Стратегия перемещения объекта в центр экрана
+///
+public class MoveToCenter : AbstractStrategy
+{
+ 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/ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs b/ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs
new file mode 100644
index 0000000..c87517a
--- /dev/null
+++ b/ProjectLiner/ProjectLiner/MovementStrategy/MoveableLiner.cs
@@ -0,0 +1,69 @@
+using ProjectLiner.Drawnings;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectLiner.MovementStrategy;
+
+///
+/// Класс-реализация IMoveableObject с использованием DrawningCommonLiner
+///
+public class MoveableLiner : IMoveableObject
+{
+ ///
+ /// Поле-объект класса DrawningCommonLiner или его наследника
+ ///
+ private readonly DrawningCommonLiner? _CommonLiner = null;
+
+ ///
+ /// Конструктор
+ ///
+ /// Объект класса DrawningCommonLiner
+ public MoveableLiner(DrawningCommonLiner CommonLiner)
+ {
+ _CommonLiner = CommonLiner;
+ }
+
+ public ObjectParameters? GetObjectPosition
+ {
+ get
+ {
+ if (_CommonLiner == null || _CommonLiner.EntityCommonLiner == null || !_CommonLiner.GetPosX.HasValue || !_CommonLiner.GetPosY.HasValue)
+ {
+ return null;
+ }
+ return new ObjectParameters(_CommonLiner.GetPosX.Value, _CommonLiner.GetPosY.Value, _CommonLiner.GetWidth, _CommonLiner.GetHeight);
+ }
+ }
+
+ public int GetStep => (int)(_CommonLiner?.EntityCommonLiner?.Step ?? 0);
+
+ public bool TryMoveObject(MovementDirection direction)
+ {
+ if (_CommonLiner == null || _CommonLiner.EntityCommonLiner == null)
+ {
+ return false;
+ }
+
+ return _CommonLiner.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/ProjectLiner/ProjectLiner/MovementStrategy/MovementDirection.cs b/ProjectLiner/ProjectLiner/MovementStrategy/MovementDirection.cs
new file mode 100644
index 0000000..3981ef6
--- /dev/null
+++ b/ProjectLiner/ProjectLiner/MovementStrategy/MovementDirection.cs
@@ -0,0 +1,30 @@
+namespace ProjectLiner.MovementStrategy;
+
+
+
+///
+/// Направление перемещения
+///
+public enum MovementDirection
+{
+ ///
+ /// Вверх
+ ///
+ Up = 1,
+
+ ///
+ /// Вниз
+ ///
+ Down = 2,
+
+ ///
+ /// Влево
+ ///
+ Left = 3,
+
+ ///
+ /// Вправо
+ ///
+ Right = 4
+}
+
diff --git a/ProjectLiner/ProjectLiner/MovementStrategy/ObjectParameters.cs b/ProjectLiner/ProjectLiner/MovementStrategy/ObjectParameters.cs
new file mode 100644
index 0000000..251aa9a
--- /dev/null
+++ b/ProjectLiner/ProjectLiner/MovementStrategy/ObjectParameters.cs
@@ -0,0 +1,78 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectLiner.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/ProjectLiner/ProjectLiner/MovementStrategy/StrategyStatus.cs b/ProjectLiner/ProjectLiner/MovementStrategy/StrategyStatus.cs
new file mode 100644
index 0000000..b221f83
--- /dev/null
+++ b/ProjectLiner/ProjectLiner/MovementStrategy/StrategyStatus.cs
@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ProjectLiner.MovementStrategy;
+///
+/// Статус выполнения операции перемещения
+///
+public enum StrategyStatus
+{
+ ///
+ /// Все готово к началу
+ ///
+ NotInit,
+
+ ///
+ /// Выполняется
+ ///
+ InProgress,
+
+ ///
+ /// Завершено
+ ///
+ Finish
+}
\ No newline at end of file