From a8fa0803252ef5ec738e310f2475961433f48778 Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Sun, 3 Mar 2024 17:28:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D1=82=D1=81=D1=8F=20=D1=81=D1=82=D1=80=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=B3=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Drawnings/DirectionType.cs | 4 + .../Drawnings/DrawningLocomotive.cs | 19 +++ .../FormlectricLocomotive.Designer.cs | 26 ++++ .../FormlectricLocomotive.cs | 56 ++++++- .../MovementStrategy/AbstractStrategy.cs | 142 ++++++++++++++++++ .../MovementStrategy/IMovableObject.cs | 32 ++++ .../MovementStrategy/MoveToBorder.cs | 57 +++++++ .../MovementStrategy/MoveToCenter.cs | 57 +++++++ .../MovementStrategy/MoveableLocomotive.cs | 65 ++++++++ .../MovementStrategy/MovementDirection.cs | 27 ++++ .../MovementStrategy/ObjectParameters.cs | 78 ++++++++++ .../MovementStrategy/StrategyStatus.cs | 23 +++ 12 files changed, 580 insertions(+), 6 deletions(-) create mode 100644 ProjectElectricLocomotive/MovementStrategy/AbstractStrategy.cs create mode 100644 ProjectElectricLocomotive/MovementStrategy/IMovableObject.cs create mode 100644 ProjectElectricLocomotive/MovementStrategy/MoveToBorder.cs create mode 100644 ProjectElectricLocomotive/MovementStrategy/MoveToCenter.cs create mode 100644 ProjectElectricLocomotive/MovementStrategy/MoveableLocomotive.cs create mode 100644 ProjectElectricLocomotive/MovementStrategy/MovementDirection.cs create mode 100644 ProjectElectricLocomotive/MovementStrategy/ObjectParameters.cs create mode 100644 ProjectElectricLocomotive/MovementStrategy/StrategyStatus.cs diff --git a/ProjectElectricLocomotive/Drawnings/DirectionType.cs b/ProjectElectricLocomotive/Drawnings/DirectionType.cs index db3f0e7..ba08627 100644 --- a/ProjectElectricLocomotive/Drawnings/DirectionType.cs +++ b/ProjectElectricLocomotive/Drawnings/DirectionType.cs @@ -8,6 +8,10 @@ namespace ProjectElectricLocomotive.Drawnings { public enum DirectionType { + /// + /// Неизвестное направление + /// + Unknow = -1, /// /// Вверх /// diff --git a/ProjectElectricLocomotive/Drawnings/DrawningLocomotive.cs b/ProjectElectricLocomotive/Drawnings/DrawningLocomotive.cs index 42fce8c..e05df50 100644 --- a/ProjectElectricLocomotive/Drawnings/DrawningLocomotive.cs +++ b/ProjectElectricLocomotive/Drawnings/DrawningLocomotive.cs @@ -39,7 +39,26 @@ public class DrawningLocomotive /// Высота прорисовки локомотива /// private readonly int _drawningLocomotiveHeight = 90; + + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningLocomotiveWidth; + /// + /// Высота объекта + /// + public int GetHeight => _drawningLocomotiveHeight; /// /// Пустой конструктор /// diff --git a/ProjectElectricLocomotive/FormlectricLocomotive.Designer.cs b/ProjectElectricLocomotive/FormlectricLocomotive.Designer.cs index 5f21c43..725b4ff 100644 --- a/ProjectElectricLocomotive/FormlectricLocomotive.Designer.cs +++ b/ProjectElectricLocomotive/FormlectricLocomotive.Designer.cs @@ -35,6 +35,8 @@ buttonRight = new Button(); buttonDown = new Button(); buttonCreateLocomotive = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxElectricLocomotive).BeginInit(); SuspendLayout(); // @@ -118,11 +120,33 @@ buttonCreateLocomotive.UseVisualStyleBackColor = true; buttonCreateLocomotive.Click += buttonCreateLocomotive_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(594, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(182, 33); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(664, 51); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(112, 34); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormlectricLocomotive // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(788, 399); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateLocomotive); Controls.Add(buttonDown); Controls.Add(buttonRight); @@ -145,5 +169,7 @@ private Button buttonRight; private Button buttonDown; private Button buttonCreateLocomotive; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/ProjectElectricLocomotive/FormlectricLocomotive.cs b/ProjectElectricLocomotive/FormlectricLocomotive.cs index 7f2d59b..68036f4 100644 --- a/ProjectElectricLocomotive/FormlectricLocomotive.cs +++ b/ProjectElectricLocomotive/FormlectricLocomotive.cs @@ -9,11 +9,23 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ProjectElectricLocomotive.Drawnings; +using ProjectElectricLocomotive.MovementStrategy; namespace ProjectElectricLocomotive { public partial class FormlectricLocomotive : Form { + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; + + public FormlectricLocomotive() + { + InitializeComponent(); + _strategy = null; + } + private void Draw() { if (_drawnningLocomotive == null) @@ -47,15 +59,13 @@ namespace ProjectElectricLocomotive } _drawnningLocomotive.SetPictureSize(pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height); _drawnningLocomotive.SetPosition(random.Next(10, 100), random.Next(10, 100)); - + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } private DrawningLocomotive? _drawnningLocomotive; - public FormlectricLocomotive() - { - InitializeComponent(); - } + /// /// Обработка кнопки создать "ЭлектроЛокомотив" @@ -104,6 +114,40 @@ namespace ProjectElectricLocomotive } - + + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawnningLocomotive == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableLocomotive(_drawnningLocomotive), + pictureBoxElectricLocomotive.Width, pictureBoxElectricLocomotive.Height); + } + if (_strategy == null) + { + return; + } + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } } diff --git a/ProjectElectricLocomotive/MovementStrategy/AbstractStrategy.cs b/ProjectElectricLocomotive/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..c1c9b32 --- /dev/null +++ b/ProjectElectricLocomotive/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.MovementStrategy; + +public abstract class AbstractStrategy +{ + /// + /// Перемещаемый объект + /// + private IMovableObject _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(IMovableObject 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/ProjectElectricLocomotive/MovementStrategy/IMovableObject.cs b/ProjectElectricLocomotive/MovementStrategy/IMovableObject.cs new file mode 100644 index 0000000..f67d1b3 --- /dev/null +++ b/ProjectElectricLocomotive/MovementStrategy/IMovableObject.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.MovementStrategy; + +/// +/// Интерфейс для работы с перемещаемыми объектами +/// +public interface IMovableObject +{ + + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// направление + /// true - объект перемещен , false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); + +} diff --git a/ProjectElectricLocomotive/MovementStrategy/MoveToBorder.cs b/ProjectElectricLocomotive/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..b03ad9e --- /dev/null +++ b/ProjectElectricLocomotive/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.MovementStrategy; + +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + + return objParams.RightBorder - GetStep() <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder - GetStep() <= 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/ProjectElectricLocomotive/MovementStrategy/MoveToCenter.cs b/ProjectElectricLocomotive/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..3203774 --- /dev/null +++ b/ProjectElectricLocomotive/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.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/ProjectElectricLocomotive/MovementStrategy/MoveableLocomotive.cs b/ProjectElectricLocomotive/MovementStrategy/MoveableLocomotive.cs new file mode 100644 index 0000000..fe58fb3 --- /dev/null +++ b/ProjectElectricLocomotive/MovementStrategy/MoveableLocomotive.cs @@ -0,0 +1,65 @@ +using ProjectElectricLocomotive.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.MovementStrategy +{ + public class MoveableLocomotive : IMovableObject + { + + private readonly DrawningLocomotive? _locomotive = null; + + public MoveableLocomotive(DrawningLocomotive locomotive) + { + _locomotive = locomotive; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_locomotive == null || _locomotive.EntityLocomotive == null || !_locomotive.GetPosX.HasValue || !_locomotive.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_locomotive.GetPosX.Value, _locomotive.GetPosY.Value, _locomotive.GetWidth, _locomotive.GetHeight); + } + } + + + + public int GetStep => (int)(_locomotive?.EntityLocomotive?.Step ?? 0); + + + + public bool TryMoveObject(MovementDirection direction) + { + if (_locomotive == null || _locomotive.EntityLocomotive == null) + { + return false; + } + + return _locomotive.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/ProjectElectricLocomotive/MovementStrategy/MovementDirection.cs b/ProjectElectricLocomotive/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..b2965ea --- /dev/null +++ b/ProjectElectricLocomotive/MovementStrategy/MovementDirection.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.MovementStrategy; + +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 +} diff --git a/ProjectElectricLocomotive/MovementStrategy/ObjectParameters.cs b/ProjectElectricLocomotive/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..9a0f6eb --- /dev/null +++ b/ProjectElectricLocomotive/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.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/ProjectElectricLocomotive/MovementStrategy/StrategyStatus.cs b/ProjectElectricLocomotive/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..737c149 --- /dev/null +++ b/ProjectElectricLocomotive/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectElectricLocomotive.MovementStrategy; + +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + /// + /// Выполняется + /// + InProgress, + /// + /// Завершено + /// + Finish +}