diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DirectionType.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DirectionType.cs index b32b3e8..a743d32 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DirectionType.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DirectionType.cs @@ -8,6 +8,10 @@ namespace SelfPropelledArtilleryUnit.Drawnings; public enum DirectionType { + /// + /// Неизвестное направление + /// + Unknow = -1, Up = 1, Down = 2, Left = 3, diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DrawningTank.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DrawningTank.cs index f4c0d4c..f063da9 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DrawningTank.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Drawnings/DrawningTank.cs @@ -41,6 +41,25 @@ public class DrawningTank /// Пустой конструктор /// + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningTankWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawningTankHeight; private DrawningTank() { _pictureWidth = null; diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormArtillery.Designer.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormArtillery.Designer.cs index 6deb537..272f29b 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormArtillery.Designer.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormArtillery.Designer.cs @@ -36,6 +36,8 @@ buttonDown = new Button(); buttonRight = new Button(); buttonCreateTank = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxArtillery).BeginInit(); SuspendLayout(); // @@ -118,11 +120,33 @@ buttonCreateTank.UseVisualStyleBackColor = true; buttonCreateTank.Click += ButtonCreateTank_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(679, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(719, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(75, 23); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += ButtonStrategyStep_Click; + // // FormArtillery // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 461); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateTank); Controls.Add(buttonRight); Controls.Add(buttonDown); @@ -145,5 +169,7 @@ private Button buttonDown; private Button buttonRight; private Button buttonCreateTank; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormArtillery.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormArtillery.cs index 2fbc273..dba5af8 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormArtillery.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormArtillery.cs @@ -8,16 +8,24 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using SelfPropelledArtilleryUnit.Drawnings; +using SelfPropelledArtilleryUnit.MovementStrategy; namespace SelfPropelledArtilleryUnit { public partial class FormArtillery : Form { + /// + /// Поле-объект для прорисовки объекта + /// private DrawningTank? _drawningTank; - + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; public FormArtillery() { InitializeComponent(); + _strategy = null; } @@ -53,6 +61,8 @@ namespace SelfPropelledArtilleryUnit } _drawningTank.SetPictureSize(pictureBoxArtillery.Width, pictureBoxArtillery.Height); _drawningTank.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } @@ -69,8 +79,8 @@ namespace SelfPropelledArtilleryUnit /// /// private void ButtonCreateTank_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTank)); - - private void ButtonMove_Click(object sender, EventArgs e) + + private void ButtonMove_Click(object sender, EventArgs e) { if (_drawningTank == null) { @@ -100,7 +110,47 @@ namespace SelfPropelledArtilleryUnit Draw(); } } + /// + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawningTank == null) + { + return; + } - + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableTank(_drawningTank), pictureBoxArtillery.Width, pictureBoxArtillery.Height); + } + + if (_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/AbstractStrategy.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..1e9d280 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.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 (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/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/IMoveableObjectcs.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/IMoveableObjectcs.cs new file mode 100644 index 0000000..89758de --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/IMoveableObjectcs.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.MovementStrategy; + +/// +/// Интерфейс для работы с перемещаемым объектом +/// +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); +} diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/MoveToBorder.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..bd9531b --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.MovementStrategy; +public class MoveToBorder : AbstractStrategy +{ + protected override bool IsTargetDestinaion() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.LeftBorder - GetStep() <= 0 || objParams.RightBorder + GetStep() >= FieldWidth || + objParams.TopBorder - GetStep() <= 0 || objParams.ObjectMiddleVertical + GetStep() >= FieldHeight; + } + + protected override void MoveToTarget() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + + int x = objParams.RightBorder; + if (x + GetStep() < FieldWidth) MoveRight(); + int y = objParams.DownBorder; + if (y + GetStep() < FieldHeight) MoveDown(); + } +} \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/MoveToCenter.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..6a5aace --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.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/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/MoveableTank.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/MoveableTank.cs new file mode 100644 index 0000000..b7e8253 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/MoveableTank.cs @@ -0,0 +1,56 @@ +using SelfPropelledArtilleryUnit.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.MovementStrategy; + +public class MoveableTank : IMoveableObject +{ + private readonly DrawningTank? _tank; + public MoveableTank(DrawningTank tank) + { + _tank = tank; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if (_tank == null || _tank.EntityTank == null || !_tank.GetPosX.HasValue || !_tank.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_tank.GetPosX.Value, _tank.GetPosY.Value, _tank.GetWidth, _tank.GetHeight); + } + } + public int GetStep => (int)(_tank?.EntityTank?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_tank == null || _tank.EntityTank == null) + { + return false; + } + + return _tank.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/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/MovementDirection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..3598b83 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/MovementDirection.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.MovementStrategy; + +public enum MovementDirection +{ + Up = 1, + Down = 2, + Left = 3, + Right = 4 +} diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/ObjectsParameters.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/ObjectsParameters.cs new file mode 100644 index 0000000..fb9548a --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/ObjectsParameters.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.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/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/StrategyStatus.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..0b021f5 --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SelfPropelledArtilleryUnit.MovementStrategy; + +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + + /// + /// Выполняется + /// + InProgress, + + /// + /// Завершено + /// + Finish +}