diff --git a/AirBus/AirBus/Drawings/DirectionType.cs b/AirBus/AirBus/Drawings/DirectionType.cs index 5bad0b0..86cbec0 100644 --- a/AirBus/AirBus/Drawings/DirectionType.cs +++ b/AirBus/AirBus/Drawings/DirectionType.cs @@ -11,6 +11,11 @@ namespace AirBus.Drawings /// public enum DirectionType { + /// + /// Неизвестное направление + /// + Unknow = -1, + /// /// Вверх /// diff --git a/AirBus/AirBus/Drawings/DrawingAirBus.cs b/AirBus/AirBus/Drawings/DrawingAirBus.cs index 7a495dc..3948e31 100644 --- a/AirBus/AirBus/Drawings/DrawingAirBus.cs +++ b/AirBus/AirBus/Drawings/DrawingAirBus.cs @@ -38,9 +38,7 @@ namespace AirBus.Drawings Pen pen = new(Color.Black, 1); Brush additionalBrush = new SolidBrush(airBus.AdditionalColor); - Brush br = new SolidBrush(airBus.BodyColor); - Brush brBlack = new SolidBrush(Color.Black); - + base.DrawTransport(g); if (airBus.ExtraEngine) // двигатели опционально @@ -59,17 +57,7 @@ namespace AirBus.Drawings g.FillRectangle(additionalBrush, _startPosX.Value + 90, _startPosY.Value + 25, 50, 10); g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value + 25, 50, 10); - - } - } - - - - - - - - + } } } diff --git a/AirBus/AirBus/Drawings/DrawingPlane.cs b/AirBus/AirBus/Drawings/DrawingPlane.cs index 01af9a3..9254f2a 100644 --- a/AirBus/AirBus/Drawings/DrawingPlane.cs +++ b/AirBus/AirBus/Drawings/DrawingPlane.cs @@ -44,6 +44,26 @@ namespace AirBus.Drawings /// private readonly int _drawingAirBusHeight = 72; + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => _drawingAirBusWidth; + + /// + /// Высота объекта + /// + public int GetHeight => _drawingAirBusHeight; + /// /// Пустой конструктор /// @@ -56,7 +76,7 @@ namespace AirBus.Drawings } /// - /// Конструктор + /// Конструктор для наследников /// /// Ширина прорисовки /// Высота прорисовки diff --git a/AirBus/AirBus/Entities/EntityAirBus.cs b/AirBus/AirBus/Entities/EntityAirBus.cs index 31e1c03..61c1aab 100644 --- a/AirBus/AirBus/Entities/EntityAirBus.cs +++ b/AirBus/AirBus/Entities/EntityAirBus.cs @@ -27,7 +27,7 @@ namespace AirBus.Entities public bool SecondFloor { get; private set; } /// - /// Инициализация полей объекта-класса самолёта "Аэробус" + /// Конструктор объекта-класса самолёта "Аэробус" /// /// Скорость /// Вес diff --git a/AirBus/AirBus/FormAirBus.Designer.cs b/AirBus/AirBus/FormAirBus.Designer.cs index 4b36d44..898a2d6 100644 --- a/AirBus/AirBus/FormAirBus.Designer.cs +++ b/AirBus/AirBus/FormAirBus.Designer.cs @@ -36,6 +36,8 @@ buttonDown = new Button(); buttonRight = new Button(); buttonCreatePlane = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirBus).BeginInit(); SuspendLayout(); // @@ -121,11 +123,33 @@ buttonCreatePlane.UseVisualStyleBackColor = true; buttonCreatePlane.Click += buttonCreateAirBus_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(841, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 3; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(898, 46); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(94, 29); + buttonStrategyStep.TabIndex = 4; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormAirBus // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(1004, 468); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonUp); @@ -148,5 +172,7 @@ private Button buttonDown; private Button buttonRight; private Button buttonCreatePlane; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/AirBus/AirBus/FormAirBus.cs b/AirBus/AirBus/FormAirBus.cs index 5b5d0e9..554a482 100644 --- a/AirBus/AirBus/FormAirBus.cs +++ b/AirBus/AirBus/FormAirBus.cs @@ -8,15 +8,19 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using AirBus.Drawings; +using AirBus.MovementStrategy; namespace AirBus; public partial class FormAirBus : Form { private DrawingPlane? _drawingPlane; + + private AbstractStrategy? _strategy; public FormAirBus() { InitializeComponent(); + _strategy = null; } private void Draw() @@ -36,7 +40,7 @@ public partial class FormAirBus : Form { Random random = new(); switch (type) - { + { case nameof(DrawingPlane): _drawingPlane = new DrawingPlane(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); @@ -52,28 +56,24 @@ public partial class FormAirBus : Form } _drawingPlane.SetPictureSize(pictureBoxAirBus.Width, pictureBoxAirBus.Height); _drawingPlane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; Draw(); } - + /// /// Обработка нажатия кнопки "Создать Аеробус" /// /// /// - private void buttonCreateAirBus_Click(object sender, EventArgs e) - { - CreateObject(nameof(DrawingAirBus)); - } + private void buttonCreateAirBus_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingAirBus)); /// /// Обработка нажатия кнопки "Создать самолёт" /// /// /// - private void buttonCreatePlane_Click(object sender, EventArgs e) - { - CreateObject(nameof(DrawingPlane)); - } + private void buttonCreatePlane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingPlane)); private void ButtonMove_Click(object sender, EventArgs e) { @@ -100,9 +100,52 @@ public partial class FormAirBus : Form break; } - if(result) + if (result) { Draw(); } } + + /// + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if (_drawingPlane == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveablePlane(_drawingPlane), + pictureBoxAirBus.Width, pictureBoxAirBus.Height); + } + if (_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + Draw(); + + if (_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } + diff --git a/AirBus/AirBus/MovementStrategy/AbstractStrategy.cs b/AirBus/AirBus/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..5405370 --- /dev/null +++ b/AirBus/AirBus/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBus.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/AirBus/AirBus/MovementStrategy/IMoveableObject.cs b/AirBus/AirBus/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..2226b3e --- /dev/null +++ b/AirBus/AirBus/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBus.MovementStrategy; + +/// +/// Интерфейс для работы с перемещаемым объектом +/// +public interface IMoveableObject +{ + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно +bool TryMoveObject(MovementDirection direction); +} diff --git a/AirBus/AirBus/MovementStrategy/MoveToBorder.cs b/AirBus/AirBus/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..6ffded4 --- /dev/null +++ b/AirBus/AirBus/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBus.MovementStrategy; + +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()) + { + 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/AirBus/AirBus/MovementStrategy/MoveToCenter.cs b/AirBus/AirBus/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..501479d --- /dev/null +++ b/AirBus/AirBus/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBus.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/AirBus/AirBus/MovementStrategy/MoveablePlane.cs b/AirBus/AirBus/MovementStrategy/MoveablePlane.cs new file mode 100644 index 0000000..137f6fd --- /dev/null +++ b/AirBus/AirBus/MovementStrategy/MoveablePlane.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirBus.Drawings; + +namespace AirBus.MovementStrategy; + +/// +/// Класс реализация IMoveableObject с использованияем DrawingPlane +/// +public class MoveablePlane : IMoveableObject +{ + + /// + /// Поле-объект класса DrawingPlane или его наследника + /// + private readonly DrawingPlane? _plane = null; + /// + /// Конструктор + /// + /// Объект класса DrawingPlane + public MoveablePlane(DrawingPlane plane) + { + _plane = plane; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_plane == null || _plane.EntityPlane == null || !_plane.GetPosX.HasValue || !_plane.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_plane.GetPosX.Value, + _plane.GetPosY.Value, _plane.GetWidth, _plane.GetHeight); + } + } + public int GetStep => (int)(_plane?.EntityPlane?.Step ?? 0); + public bool TryMoveObject(MovementDirection direction) + { + if (_plane == null || _plane.EntityPlane == null) + { + return false; + } + return _plane.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/AirBus/AirBus/MovementStrategy/MovementDirection.cs b/AirBus/AirBus/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..d5f84e9 --- /dev/null +++ b/AirBus/AirBus/MovementStrategy/MovementDirection.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBus.MovementStrategy; + +/// +/// Направление перемещения +/// +public enum MovementDirection +{ + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 +} diff --git a/AirBus/AirBus/MovementStrategy/ObjectParameters.cs b/AirBus/AirBus/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..bea3981 --- /dev/null +++ b/AirBus/AirBus/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBus.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/AirBus/AirBus/MovementStrategy/StrategyStatus.cs b/AirBus/AirBus/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..497ff69 --- /dev/null +++ b/AirBus/AirBus/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirBus.MovementStrategy; + +/// +/// Статус выполнения операции перемещения +/// +public enum StrategyStatus +{ + /// + /// Все готово к началу + /// + NotInit, + /// + /// Выполняется + /// + InProgress, + /// + /// Завершено + /// + Finish +}