From 255b862e89cddde0486625bc9c9158e17560c5d5 Mon Sep 17 00:00:00 2001 From: nikos77781 Date: Thu, 28 Mar 2024 18:14:53 +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 --- .../Excavator/Drawnings/DirectionType.cs | 7 +- .../Drawnings/DrawningSimpleExcavator.cs | 20 +++ Excavator/Excavator/FormExcavator.Designer.cs | 27 ++++ Excavator/Excavator/FormExcavator.cs | 50 ++++++- .../MovementSrtategy/AbstractStrategy.cs | 139 ++++++++++++++++++ .../MovementSrtategy/IMoveableObject.cs | 24 +++ .../MovementSrtategy/MoveToBorder.cs | 39 +++++ .../MovementSrtategy/MoveToCenter.cs | 55 +++++++ .../MoveableSimpleExcavator.cs | 65 ++++++++ .../MovementSrtategy/MovementDirection.cs | 27 ++++ .../MovementSrtategy/ObjectParameters.cs | 70 +++++++++ .../MovementSrtategy/StrategyStatus.cs | 21 +++ 12 files changed, 542 insertions(+), 2 deletions(-) create mode 100644 Excavator/Excavator/MovementSrtategy/AbstractStrategy.cs create mode 100644 Excavator/Excavator/MovementSrtategy/IMoveableObject.cs create mode 100644 Excavator/Excavator/MovementSrtategy/MoveToBorder.cs create mode 100644 Excavator/Excavator/MovementSrtategy/MoveToCenter.cs create mode 100644 Excavator/Excavator/MovementSrtategy/MoveableSimpleExcavator.cs create mode 100644 Excavator/Excavator/MovementSrtategy/MovementDirection.cs create mode 100644 Excavator/Excavator/MovementSrtategy/ObjectParameters.cs create mode 100644 Excavator/Excavator/MovementSrtategy/StrategyStatus.cs diff --git a/Excavator/Excavator/Drawnings/DirectionType.cs b/Excavator/Excavator/Drawnings/DirectionType.cs index 9302014..c1ae261 100644 --- a/Excavator/Excavator/Drawnings/DirectionType.cs +++ b/Excavator/Excavator/Drawnings/DirectionType.cs @@ -23,5 +23,10 @@ public enum DirectionType /// /// Вправо /// - Right = 4 + Right = 4, + + /// + /// Неизвестное направление + /// + Unknow = -1 } diff --git a/Excavator/Excavator/Drawnings/DrawningSimpleExcavator.cs b/Excavator/Excavator/Drawnings/DrawningSimpleExcavator.cs index cd4fb06..1ad2b49 100644 --- a/Excavator/Excavator/Drawnings/DrawningSimpleExcavator.cs +++ b/Excavator/Excavator/Drawnings/DrawningSimpleExcavator.cs @@ -45,6 +45,26 @@ public class DrawningSimpleExcavator /// private readonly int _drawningExcavatorHeight = 80; + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningExcavatorWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawningExcavatorHeight; + /// /// Пустой конструктор /// diff --git a/Excavator/Excavator/FormExcavator.Designer.cs b/Excavator/Excavator/FormExcavator.Designer.cs index 4bc84c9..a652b92 100644 --- a/Excavator/Excavator/FormExcavator.Designer.cs +++ b/Excavator/Excavator/FormExcavator.Designer.cs @@ -35,6 +35,8 @@ buttonDown = new Button(); buttonRight = new Button(); buttonCreateSimpleExcavator = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxExcavator).BeginInit(); SuspendLayout(); // @@ -117,11 +119,34 @@ buttonCreateSimpleExcavator.UseVisualStyleBackColor = true; buttonCreateSimpleExcavator.Click += ButtonCreateSimpleExcavator_Click; // + // comboBoxStrategy + // + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю " }); + comboBoxStrategy.Location = new Point(790, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStrategyStep.Location = new Point(790, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(121, 23); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += ButtonStrategyStep_Click; + // // FormExcavator // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(923, 597); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateSimpleExcavator); Controls.Add(buttonRight); Controls.Add(buttonDown); @@ -144,5 +169,7 @@ private Button buttonDown; private Button buttonRight; private Button buttonCreateSimpleExcavator; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/Excavator/Excavator/FormExcavator.cs b/Excavator/Excavator/FormExcavator.cs index d645e7d..27aa00b 100644 --- a/Excavator/Excavator/FormExcavator.cs +++ b/Excavator/Excavator/FormExcavator.cs @@ -1,4 +1,6 @@ using Excavator.Drawnings; +using Excavator.Entities; +using Excavator.MovementSrtategy; namespace Excavator; @@ -12,12 +14,15 @@ public partial class FormExcavator : Form /// private DrawningSimpleExcavator? _drawningSimpleExcavator; + private AbstractStrategy? _strategy; + /// /// Конструктор формы /// public FormExcavator() { InitializeComponent(); + _strategy = null; } /// @@ -61,7 +66,7 @@ public partial class FormExcavator : Form _drawningSimpleExcavator.SetPictureSize(pictureBoxExcavator.Width, pictureBoxExcavator.Height); _drawningSimpleExcavator.SetPosition(random.Next(10, 100), random.Next(10, 100)); - //_strategy = null; + _strategy = null; //comboBoxStrategy.Enabled = true; Draw(); @@ -122,4 +127,47 @@ public partial class FormExcavator : Form Draw(); } } + /// + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningSimpleExcavator == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableSimpleExcavator(_drawningSimpleExcavator), pictureBoxExcavator.Width, pictureBoxExcavator.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/Excavator/Excavator/MovementSrtategy/AbstractStrategy.cs b/Excavator/Excavator/MovementSrtategy/AbstractStrategy.cs new file mode 100644 index 0000000..237a4b5 --- /dev/null +++ b/Excavator/Excavator/MovementSrtategy/AbstractStrategy.cs @@ -0,0 +1,139 @@ +using Excavator.MovementStrategy; + +namespace Excavator.MovementSrtategy; + +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?.GetObjectParameters; + + /// + /// Шаг объекта + /// + /// + 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/Excavator/Excavator/MovementSrtategy/IMoveableObject.cs b/Excavator/Excavator/MovementSrtategy/IMoveableObject.cs new file mode 100644 index 0000000..6e56817 --- /dev/null +++ b/Excavator/Excavator/MovementSrtategy/IMoveableObject.cs @@ -0,0 +1,24 @@ +using Excavator.MovementStrategy; + +namespace Excavator.MovementSrtategy; + +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectParameters { get; } + + /// + /// Шаг + /// + int GetStep { get; } + + /// + /// Попытка перемстить объект + /// + /// Направление + /// + bool TryMoveObject(MovementDirection direction); + +} diff --git a/Excavator/Excavator/MovementSrtategy/MoveToBorder.cs b/Excavator/Excavator/MovementSrtategy/MoveToBorder.cs new file mode 100644 index 0000000..51ab69f --- /dev/null +++ b/Excavator/Excavator/MovementSrtategy/MoveToBorder.cs @@ -0,0 +1,39 @@ +namespace Excavator.MovementSrtategy; + +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.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + MoveRight(); + } + + int diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + MoveDown(); + } + } +} + diff --git a/Excavator/Excavator/MovementSrtategy/MoveToCenter.cs b/Excavator/Excavator/MovementSrtategy/MoveToCenter.cs new file mode 100644 index 0000000..66cbae5 --- /dev/null +++ b/Excavator/Excavator/MovementSrtategy/MoveToCenter.cs @@ -0,0 +1,55 @@ +namespace Excavator.MovementSrtategy; + +/// +/// Стратегия перемещения объекта в центр экрана +/// +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/Excavator/Excavator/MovementSrtategy/MoveableSimpleExcavator.cs b/Excavator/Excavator/MovementSrtategy/MoveableSimpleExcavator.cs new file mode 100644 index 0000000..e40dd29 --- /dev/null +++ b/Excavator/Excavator/MovementSrtategy/MoveableSimpleExcavator.cs @@ -0,0 +1,65 @@ +using Excavator.Drawnings; +using Excavator.MovementStrategy; + +namespace Excavator.MovementSrtategy; + +/// +/// Класс-реализация IMoveableObject с использованием Drawningexcavator +/// +public class MoveableSimpleExcavator : IMoveableObject +{ + /// + /// Поле-объект класса Drawningexcavator или его наследника + /// + private readonly DrawningSimpleExcavator? _excavator = null; + + /// + /// Конструктор + /// + /// Объект класса Drawningexcavator + public MoveableSimpleExcavator(DrawningSimpleExcavator excavator) + { + _excavator = excavator; + } + + public ObjectParameters? GetObjectParameters + { + get + { + if (_excavator == null || _excavator.EntitySimpleExcavator == null || !_excavator.GetPosX.HasValue || !_excavator.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_excavator.GetPosX.Value, _excavator.GetPosY.Value, _excavator.GetWidth, _excavator.GetHeight); + } + } + + public int GetStep => (int)(_excavator?.EntitySimpleExcavator?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_excavator == null || _excavator.EntitySimpleExcavator == null) + { + return false; + } + + return _excavator.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/Excavator/Excavator/MovementSrtategy/MovementDirection.cs b/Excavator/Excavator/MovementSrtategy/MovementDirection.cs new file mode 100644 index 0000000..68bbaee --- /dev/null +++ b/Excavator/Excavator/MovementSrtategy/MovementDirection.cs @@ -0,0 +1,27 @@ +namespace Excavator.MovementStrategy; + +/// +/// Направление перемещения +/// +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + + /// + /// Вниз + /// + Down = 2, + + /// + /// Влево + /// + Left = 3, + + /// + /// Вправо + /// + Right = 4 +} \ No newline at end of file diff --git a/Excavator/Excavator/MovementSrtategy/ObjectParameters.cs b/Excavator/Excavator/MovementSrtategy/ObjectParameters.cs new file mode 100644 index 0000000..6ac5eea --- /dev/null +++ b/Excavator/Excavator/MovementSrtategy/ObjectParameters.cs @@ -0,0 +1,70 @@ + +namespace Excavator.MovementSrtategy; + +public class ObjectParameters +{ + /// + /// Координата Х + /// + 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/Excavator/Excavator/MovementSrtategy/StrategyStatus.cs b/Excavator/Excavator/MovementSrtategy/StrategyStatus.cs new file mode 100644 index 0000000..b94874d --- /dev/null +++ b/Excavator/Excavator/MovementSrtategy/StrategyStatus.cs @@ -0,0 +1,21 @@ +namespace Excavator.MovementSrtategy; + + +public enum StrategyStatus +{ + /// + /// Всё готово к началу + /// + NotInit, + + /// + /// Выполняется + /// + InProgress, + + /// + /// Завершено + /// + Finish + +}