diff --git a/solution/lab1/Drawnings/DirectionType.cs b/solution/lab1/Drawnings/DirectionType.cs index 89abcbf..b049492 100644 --- a/solution/lab1/Drawnings/DirectionType.cs +++ b/solution/lab1/Drawnings/DirectionType.cs @@ -8,6 +8,10 @@ namespace lab1.Drawnings; public enum DirectionType { + /// + /// Неизвестное направление + /// + Unknow = -1, /// /// Вверх /// diff --git a/solution/lab1/Drawnings/DrawningTrackedVehicle.cs b/solution/lab1/Drawnings/DrawningTrackedVehicle.cs index 6ae79f9..c943928 100644 --- a/solution/lab1/Drawnings/DrawningTrackedVehicle.cs +++ b/solution/lab1/Drawnings/DrawningTrackedVehicle.cs @@ -41,11 +41,35 @@ public class DrawningTrackedVehicle /// /// Высота прорисовки истребителя /// - private readonly int _drawningFighterHeight = 54; + private readonly int _drawningFighterHeight = 69; + + /// + /// Координата Х объекта + /// + /// + public int? GetPosX => _startPosX; + /// + /// Координата У объекта + /// + /// + + public int? GetPosY => _startPosY; + /// + /// Ширина объекта + /// + /// + public int GetWidth => _drawningFighterWidth; + /// + /// Высота объекта + /// + /// + public int GetHeight => _drawningFighterHeight; + + /// /// Пустой конструктор /// - private DrawningTrackedVehicle() + private DrawningTrackedVehicle() { _pictureWidth = null; _pictureHeight = null; @@ -73,10 +97,10 @@ public class DrawningTrackedVehicle /// >Высота прорисовки истребителя - protected DrawningTrackedVehicle(int _drawningFighterWidth, int _drawningFighterHeight) : this() + protected DrawningTrackedVehicle(int drawningFighterWidth, int drawningFighterHeight) : this() { - _drawningFighterWidth = _drawningFighterWidth; - _drawningFighterHeight = _drawningFighterHeight; + _drawningFighterWidth = drawningFighterWidth; + _drawningFighterHeight = drawningFighterHeight; } diff --git a/solution/lab1/FormFighter.Designer.cs b/solution/lab1/FormFighter.Designer.cs index 4888291..ce7b400 100644 --- a/solution/lab1/FormFighter.Designer.cs +++ b/solution/lab1/FormFighter.Designer.cs @@ -35,6 +35,8 @@ buttonUp = new Button(); buttonRight = new Button(); buttonCreateTrackedVehicle = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxFighter).BeginInit(); SuspendLayout(); // @@ -43,9 +45,10 @@ pictureBoxFighter.Dock = DockStyle.Fill; pictureBoxFighter.Location = new Point(0, 0); pictureBoxFighter.Name = "pictureBoxFighter"; - pictureBoxFighter.Size = new Size(1015, 584); + pictureBoxFighter.Size = new Size(1164, 584); pictureBoxFighter.TabIndex = 0; pictureBoxFighter.TabStop = false; + pictureBoxFighter.Click += pictureBoxFighter_Click; // // buttonCreate // @@ -64,7 +67,7 @@ buttonLeft.BackColor = SystemColors.Control; buttonLeft.BackgroundImage = Properties.Resources.arrowLeft; buttonLeft.BackgroundImageLayout = ImageLayout.Stretch; - buttonLeft.Location = new Point(796, 505); + buttonLeft.Location = new Point(945, 505); buttonLeft.Name = "buttonLeft"; buttonLeft.Size = new Size(52, 52); buttonLeft.TabIndex = 2; @@ -77,7 +80,7 @@ buttonDown.BackColor = SystemColors.Control; buttonDown.BackgroundImage = Properties.Resources.arrowDown; buttonDown.BackgroundImageLayout = ImageLayout.Stretch; - buttonDown.Location = new Point(854, 505); + buttonDown.Location = new Point(1003, 505); buttonDown.Name = "buttonDown"; buttonDown.Size = new Size(52, 52); buttonDown.TabIndex = 3; @@ -90,7 +93,7 @@ buttonUp.BackColor = SystemColors.Control; buttonUp.BackgroundImage = Properties.Resources.arrowUp; buttonUp.BackgroundImageLayout = ImageLayout.Stretch; - buttonUp.Location = new Point(854, 447); + buttonUp.Location = new Point(1003, 447); buttonUp.Name = "buttonUp"; buttonUp.Size = new Size(52, 52); buttonUp.TabIndex = 4; @@ -103,7 +106,7 @@ buttonRight.BackColor = SystemColors.Control; buttonRight.BackgroundImage = Properties.Resources.arrowRight; buttonRight.BackgroundImageLayout = ImageLayout.Stretch; - buttonRight.Location = new Point(912, 505); + buttonRight.Location = new Point(1061, 505); buttonRight.Name = "buttonRight"; buttonRight.Size = new Size(52, 52); buttonRight.TabIndex = 5; @@ -121,11 +124,33 @@ buttonCreateTrackedVehicle.UseVisualStyleBackColor = true; buttonCreateTrackedVehicle.Click += ButtonCreateTrackedVehicle_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(970, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(182, 33); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(1040, 51); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(112, 34); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += button1_Click; + // // FormFighter // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1015, 584); + ClientSize = new Size(1164, 584); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonCreateTrackedVehicle); Controls.Add(buttonRight); Controls.Add(buttonUp); @@ -149,5 +174,7 @@ private Button buttonUp; private Button buttonRight; private Button buttonCreateTrackedVehicle; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/solution/lab1/FormFighter.cs b/solution/lab1/FormFighter.cs index 5b52c83..bd91746 100644 --- a/solution/lab1/FormFighter.cs +++ b/solution/lab1/FormFighter.cs @@ -1,5 +1,6 @@ using lab1; using lab1.Drawnings; +using lab1.MovementStrategy; using System; using System.Collections.Generic; using System.ComponentModel; @@ -17,9 +18,15 @@ namespace lab1; public partial class FormFighter : Form { private DrawningTrackedVehicle? _drawningTrackedVehicle; + /// + /// Стратегия перемещения + /// + private AbstractSrategy? _strategy; public FormFighter() { InitializeComponent(); + _strategy = null; + comboBoxStrategy.Enabled = true; } private void Draw() @@ -60,6 +67,8 @@ public partial class FormFighter : Form } _drawningTrackedVehicle.SetPictureSize(pictureBoxFighter.Width, pictureBoxFighter.Height); _drawningTrackedVehicle.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + Draw(); } @@ -79,15 +88,15 @@ public partial class FormFighter : Form /// /// private void ButtonCreateTrackedVehicle_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTrackedVehicle)); - - - - - + + + + + private void ButtonMove_Click(object sender, EventArgs e) { @@ -120,12 +129,58 @@ public partial class FormFighter : Form } } - + private void FormFighter_Load(object sender, EventArgs e) { } - + private void checkBox1_CheckedChanged(object sender, EventArgs e) + { + + } + + private void pictureBoxFighter_Click(object sender, EventArgs e) + { + + } + + private void button1_Click(object sender, EventArgs e) + { + if (_drawningTrackedVehicle == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveableTrackedVehicle(_drawningTrackedVehicle), pictureBoxFighter.Width, pictureBoxFighter.Height); + } + + if (_strategy == null) + { + return; + } + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } + diff --git a/solution/lab1/MovementStrategy/AbstractSrategy.cs b/solution/lab1/MovementStrategy/AbstractSrategy.cs new file mode 100644 index 0000000..ab7621f --- /dev/null +++ b/solution/lab1/MovementStrategy/AbstractSrategy.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace lab1.MovementStrategy; + +public abstract class AbstractSrategy +{ + /// + /// Перемещаемый объект + /// + 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 moveableObjects, int width, int height) + { + if (moveableObjects == null) + { + _state = StrategyStatus.NotInit; + return; + } + _state = StrategyStatus.InProgress; + _moveableObject = moveableObjects; + 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/solution/lab1/MovementStrategy/IMoveableObject.cs b/solution/lab1/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..ed670ac --- /dev/null +++ b/solution/lab1/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace lab1.MovementStrategy; + +public interface IMoveableObject +{ + /// + /// Получение координат объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Попытка переместить объект в указанном направлении + /// + /// + /// + bool TryMoveObject(MovementDirection direction); +} diff --git a/solution/lab1/MovementStrategy/MoveToBorder.cs b/solution/lab1/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..c8dd957 --- /dev/null +++ b/solution/lab1/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace lab1.MovementStrategy; + +public class MoveToBorder : AbstractSrategy +{ + 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/solution/lab1/MovementStrategy/MoveToCenter.cs b/solution/lab1/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..21ee4ee --- /dev/null +++ b/solution/lab1/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace lab1.MovementStrategy; + +public class MoveToCenter : AbstractSrategy +{ + 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/solution/lab1/MovementStrategy/MoveableTrackedVehicle.cs b/solution/lab1/MovementStrategy/MoveableTrackedVehicle.cs new file mode 100644 index 0000000..5bb30e9 --- /dev/null +++ b/solution/lab1/MovementStrategy/MoveableTrackedVehicle.cs @@ -0,0 +1,68 @@ +using lab1.Drawnings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace lab1.MovementStrategy; +/// +/// Класс-реализации IMoveableObject с использованием DrawingTrackedVenicle +/// +public class MoveableTrackedVehicle : IMoveableObject +{ + /// + /// Поле-объект класса DrawningTrackedVehicle или его наследников + /// + private readonly DrawningTrackedVehicle? _trackedVehicle = null; + /// + /// Конструктор + /// + /// Объект класса DrawningStormtrooperBase + public MoveableTrackedVehicle(DrawningTrackedVehicle trackedVehicle) + { + _trackedVehicle = trackedVehicle; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_trackedVehicle == null || _trackedVehicle.EntityTrackedVehicle == null || !_trackedVehicle.GetPosX.HasValue || !_trackedVehicle.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_trackedVehicle.GetPosX.Value, _trackedVehicle.GetPosY.Value, _trackedVehicle.GetWidth, _trackedVehicle.GetHeight); + } + } + + + + public int GetStep => (int)(_trackedVehicle?.EntityTrackedVehicle?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if (_trackedVehicle == null || _trackedVehicle.EntityTrackedVehicle == null) + { + return false; + } + return _trackedVehicle.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/solution/lab1/MovementStrategy/MovementDirection.cs b/solution/lab1/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..3e7c4aa --- /dev/null +++ b/solution/lab1/MovementStrategy/MovementDirection.cs @@ -0,0 +1,24 @@ + +namespace lab1.MovementStrategy; +/// +/// Направление перемещения +/// +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4, +} diff --git a/solution/lab1/MovementStrategy/ObjectParameters.cs b/solution/lab1/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..bd220a3 --- /dev/null +++ b/solution/lab1/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace lab1.MovementStrategy; + +public class ObjectParameters +{ + /// + /// Координатва х + /// + private readonly int _x; + /// + /// Координатва у + /// + 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/solution/lab1/MovementStrategy/StrategyStatus.cs b/solution/lab1/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..a7c6bd6 --- /dev/null +++ b/solution/lab1/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace lab1.MovementStrategy; + +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + /// + /// Выполняется + /// + InProgress, + /// + /// Завершено + /// + Finish +}