From 2f4c58fbc63fb0a27f367d80dc7c17e6c5d8f86c Mon Sep 17 00:00:00 2001 From: ilyaryabovv Date: Sun, 18 Feb 2024 19:26:51 +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 --- .../Drawnings/DirectionType.cs | 4 + .../Drawnings/DrawningBaseStormtrooper.cs | 20 +++ .../FormStormtrooper.Designer.cs | 26 ++++ .../ProjectStormtrooper/FormStormtrooper.cs | 46 +++++- .../MovementStrategy/AbstractStrategy.cs | 140 ++++++++++++++++++ .../MovementStrategy/IMoveableObjects.cs | 24 +++ .../MovementStrategy/MoveToBorder.cs | 31 ++++ .../MovementStrategy/MoveToCenter.cs | 53 +++++++ .../MovementStrategy/MoveableStormtrooper.cs | 56 +++++++ .../MovementStrategy/MovementDirection.cs | 25 ++++ .../MovementStrategy/ObjectParameters.cs | 72 +++++++++ .../MovementStrategy/StrategyStatus.cs | 18 +++ 12 files changed, 514 insertions(+), 1 deletion(-) create mode 100644 ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/AbstractStrategy.cs create mode 100644 ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/IMoveableObjects.cs create mode 100644 ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MoveToBorder.cs create mode 100644 ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MoveToCenter.cs create mode 100644 ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MoveableStormtrooper.cs create mode 100644 ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MovementDirection.cs create mode 100644 ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/ObjectParameters.cs create mode 100644 ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/StrategyStatus.cs diff --git a/ProjectStormtrooper/ProjectStormtrooper/Drawnings/DirectionType.cs b/ProjectStormtrooper/ProjectStormtrooper/Drawnings/DirectionType.cs index c1a12a6..6240396 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/Drawnings/DirectionType.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/Drawnings/DirectionType.cs @@ -5,6 +5,10 @@ /// public enum DirectionType { + /// + /// Неизвестное значение + /// + Unknow= -1, /// /// Вверх /// diff --git a/ProjectStormtrooper/ProjectStormtrooper/Drawnings/DrawningBaseStormtrooper.cs b/ProjectStormtrooper/ProjectStormtrooper/Drawnings/DrawningBaseStormtrooper.cs index d1b3255..5aaf5b3 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/Drawnings/DrawningBaseStormtrooper.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/Drawnings/DrawningBaseStormtrooper.cs @@ -35,6 +35,26 @@ public class DrawningBaseStormtrooper /// private readonly int _drawningStormtrooperHeight = 135; + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawningStormtrooperWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawningStormtrooperHeight; + /// /// Пустой конструктор /// diff --git a/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.Designer.cs b/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.Designer.cs index 596bc70..702bd9e 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.Designer.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.Designer.cs @@ -35,6 +35,8 @@ buttonLeft = new Button(); pictureBoxStormtrooper = new PictureBox(); buttonCreateBaseStormtrooper = new Button(); + comboBoxStrategy = new ComboBox(); + ButtonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxStormtrooper).BeginInit(); SuspendLayout(); // @@ -118,11 +120,33 @@ buttonCreateBaseStormtrooper.UseVisualStyleBackColor = true; buttonCreateBaseStormtrooper.Click += buttonCreateBaseStormtrooper_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(1127, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // ButtonStrategyStep + // + ButtonStrategyStep.Location = new Point(1173, 41); + ButtonStrategyStep.Name = "ButtonStrategyStep"; + ButtonStrategyStep.Size = new Size(75, 23); + ButtonStrategyStep.TabIndex = 8; + ButtonStrategyStep.Text = "Шаг"; + ButtonStrategyStep.UseVisualStyleBackColor = true; + ButtonStrategyStep.Click += ButtonStrategyStep_Click; + // // FormStormtrooper // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1260, 737); + Controls.Add(ButtonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateBaseStormtrooper); Controls.Add(buttonLeft); Controls.Add(buttonDown); @@ -145,5 +169,7 @@ private Button buttonLeft; private PictureBox pictureBoxStormtrooper; private Button buttonCreateBaseStormtrooper; + private ComboBox comboBoxStrategy; + private Button ButtonStrategyStep; } } \ No newline at end of file diff --git a/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.cs b/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.cs index af82085..d138b22 100644 --- a/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.cs +++ b/ProjectStormtrooper/ProjectStormtrooper/FormStormtrooper.cs @@ -1,4 +1,5 @@ using ProjectStormtrooper.Drawnings; +using ProjectStormtrooper.MovementStrategy; namespace ProjectStormtrooper { @@ -12,12 +13,15 @@ namespace ProjectStormtrooper /// private DrawningBaseStormtrooper? _drawningBaseStormtrooper; + private AbstractStrategy? _strategy; + /// /// Конструктор формы /// public FormStormtrooper() { InitializeComponent(); + _strategy = null; } @@ -62,6 +66,8 @@ namespace ProjectStormtrooper _drawningBaseStormtrooper.SetPictureSize(pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height); _drawningBaseStormtrooper.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } /// @@ -86,6 +92,7 @@ namespace ProjectStormtrooper /// private void ButtonMove_Click(object sender, EventArgs e) { + if (_drawningBaseStormtrooper == null) { return; @@ -117,6 +124,43 @@ namespace ProjectStormtrooper } } - + private void ButtonStrategyStep_Click(object sender, EventArgs e) + { + + if (_drawningBaseStormtrooper == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableStormtrooper(_drawningBaseStormtrooper), pictureBoxStormtrooper.Width, pictureBoxStormtrooper.Height); + } + + if (_strategy == null) + { + return; + } + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } + } diff --git a/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/AbstractStrategy.cs b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..04507a6 --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,140 @@ +namespace ProjectStormtrooper.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; + } +} \ No newline at end of file diff --git a/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/IMoveableObjects.cs b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/IMoveableObjects.cs new file mode 100644 index 0000000..9886158 --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/IMoveableObjects.cs @@ -0,0 +1,24 @@ +namespace ProjectStormtrooper.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/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MoveToBorder.cs b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..d13a51d --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,31 @@ +namespace ProjectStormtrooper.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(); + } +} diff --git a/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MoveToCenter.cs b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..8d996b6 --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,53 @@ +using ProjectStormtrooper.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(); + } + } + } +} \ No newline at end of file diff --git a/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MoveableStormtrooper.cs b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MoveableStormtrooper.cs new file mode 100644 index 0000000..7c461c3 --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MoveableStormtrooper.cs @@ -0,0 +1,56 @@ +using ProjectStormtrooper.Drawnings; + +namespace ProjectStormtrooper.MovementStrategy; +/// +/// Класс реализация IMoveableObject с использованием DrawningBaseStormtrooper +/// +public class MoveableStormtrooper : IMoveableObject +{ + private DrawningBaseStormtrooper? _stormtrooper ; + + public MoveableStormtrooper(DrawningBaseStormtrooper stormtrooper) + { + _stormtrooper = stormtrooper; + } + public ObjectParameters? GetObjectPosition + { + get + { + if( _stormtrooper == null||_stormtrooper.EntityBaseStormtrooper==null || !_stormtrooper.GetPosX.HasValue || !_stormtrooper.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_stormtrooper.GetPosX.Value, _stormtrooper.GetPosY.Value, _stormtrooper.GetWidth, _stormtrooper.GetHeight); + } + } + public int GetStep =>(int)(_stormtrooper?.EntityBaseStormtrooper?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_stormtrooper == null || _stormtrooper.EntityBaseStormtrooper == null) + { + return false; + } + + return _stormtrooper.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/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MovementDirection.cs b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..2124b25 --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/MovementDirection.cs @@ -0,0 +1,25 @@ + +namespace ProjectStormtrooper.MovementStrategy; +/// +/// Направление перемещения +/// +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 +} + diff --git a/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/ObjectParameters.cs b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..85dfddf --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,72 @@ +namespace ProjectStormtrooper.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/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/StrategyStatus.cs b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..725fb1d --- /dev/null +++ b/ProjectStormtrooper/ProjectStormtrooper/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,18 @@ + +namespace ProjectStormtrooper.MovementStrategy; + +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + /// + /// Выполняется + /// + InProgress, + /// + /// Завершено + /// + Finish +}