diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DirectionType.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DirectionType.cs index 65cc84f..668795f 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DirectionType.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DirectionType.cs @@ -5,6 +5,11 @@ /// public enum DirectionType { + /// + /// Неизвестное направление + /// + Unknow = -1, + /// /// Вверх /// diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawingAirplaneWithRadar.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawingAirplaneWithRadar.cs index 05b052c..d410403 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawingAirplaneWithRadar.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawingAirplaneWithRadar.cs @@ -16,7 +16,7 @@ namespace ProjectAirplaneWithRadar.Drawnings /// Дополнительный цвет /// Шасси /// Ракета - public DrawingAirplaneWithRadar(int speed, double weight, Color bodyColor, Color additionalColor, bool wheels, bool rocket) : base(150, 85) + public DrawingAirplaneWithRadar(int speed, double weight, Color bodyColor, Color additionalColor, bool wheels, bool rocket) : base(150, 93) { EntityAirplane = new EntityAirplaneWithRadar(speed, weight, bodyColor, additionalColor, wheels, rocket); } diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplane.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplane.cs index a551108..14607f3 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplane.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplane.cs @@ -42,6 +42,26 @@ namespace ProjectAirplaneWithRadar.Drawnings /// public readonly int PlaneHeight = 85; + /// + /// Координата X объекта + /// + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => PlaneWidth; + + /// + /// Высота объекта + /// + public int GetHeight => PlaneHeight; + /// /// Пустой конструктор /// @@ -214,7 +234,7 @@ namespace ProjectAirplaneWithRadar.Drawnings //Хвост Point[] points = { - new Point(_startPosX.Value + 10, _startPosY.Value + 10), + new Point(_startPosX.Value + 10, _startPosY.Value), new Point(_startPosX.Value + 10, _startPosY.Value + 50), new Point(_startPosX.Value + 50, _startPosY.Value + 50) }; diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.Designer.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.Designer.cs index 3f66348..80becfd 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.Designer.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.Designer.cs @@ -35,6 +35,8 @@ buttonDown = new Button(); buttonUp = new Button(); ButtonCreateAirplane = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirplaneWithRadar).BeginInit(); SuspendLayout(); // @@ -117,11 +119,33 @@ ButtonCreateAirplane.UseVisualStyleBackColor = true; ButtonCreateAirplane.Click += ButtonCreateAirplane_Click; // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" }); + comboBoxStrategy.Location = new Point(854, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(121, 23); + comboBoxStrategy.TabIndex = 7; + // + // buttonStrategyStep + // + buttonStrategyStep.Location = new Point(900, 41); + buttonStrategyStep.Name = "buttonStrategyStep"; + buttonStrategyStep.Size = new Size(75, 23); + buttonStrategyStep.TabIndex = 8; + buttonStrategyStep.Text = "Шаг"; + buttonStrategyStep.UseVisualStyleBackColor = true; + buttonStrategyStep.Click += buttonStrategyStep_Click; + // // FormAirplaneWithRadar // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(987, 477); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); Controls.Add(ButtonCreateAirplane); Controls.Add(buttonUp); Controls.Add(buttonDown); @@ -144,5 +168,7 @@ private Button buttonDown; private Button buttonUp; private Button ButtonCreateAirplane; + private ComboBox comboBoxStrategy; + private Button buttonStrategyStep; } } \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs index 4cfb2b0..4809538 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs @@ -1,4 +1,5 @@ using ProjectAirplaneWithRadar.Drawnings; +using ProjectAirplaneWithRadar.MovementStrategy; namespace ProjectAirplaneWithRadar { @@ -12,12 +13,18 @@ namespace ProjectAirplaneWithRadar /// private DrawningAirplane? _drawingAirplane; + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; + /// /// Конструктор формы /// public FormAirplaneWithRadar() { InitializeComponent(); + _strategy = null; } /// @@ -47,6 +54,8 @@ namespace ProjectAirplaneWithRadar _drawingAirplane.SetPictureSize(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height); _drawingAirplane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _strategy = null; + comboBoxStrategy.Enabled = true; UpdatePlane(); } @@ -77,7 +86,7 @@ namespace ProjectAirplaneWithRadar Graphics gr = Graphics.FromImage(bmp); _drawingAirplane?.DrawTransport(gr); pictureBoxAirplaneWithRadar.Image = bmp; - } + } /// /// Перемещение объекта по форме (нажатие кнопок навигации) @@ -119,5 +128,48 @@ namespace ProjectAirplaneWithRadar UpdatePlane(); } } + + /// + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + private void buttonStrategyStep_Click(object sender, EventArgs e) + { + if(_drawingAirplane == null) + { + return; + } + + if (comboBoxStrategy.Enabled) + { + _strategy = comboBoxStrategy.SelectedIndex switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_strategy == null) + { + return; + } + _strategy.SetData(new MoveablePlane(_drawingAirplane), pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height); + } + + if(_strategy == null) + { + return; + } + + comboBoxStrategy.Enabled = false; + _strategy.MakeStep(); + UpdatePlane(); + + if(_strategy.GetStatus() == StrategyStatus.Finish) + { + comboBoxStrategy.Enabled = true; + _strategy = null; + } + } } } diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/AbstractStrategy.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..66d9230 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/AbstractStrategy.cs @@ -0,0 +1,141 @@ +namespace ProjectAirplaneWithRadar.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 (IsTargetDestination()) + { + _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 IsTargetDestination(); + + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось, false - неудача) + private bool MoveTo(MovementDirection movementDirection) + { + if(_state != StrategyStatus.InProgress) + { + return false; + } + + return _moveableObject?.TryMoveObject(movementDirection) ?? false; + } + } +} diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/IMoveableObject.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..8a47a23 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/IMoveableObject.cs @@ -0,0 +1,25 @@ +namespace ProjectAirplaneWithRadar.MovementStrategy +{ + /// + /// Интерфейс для работы с перемещаемым объектом + /// + public interface IMoveableObject + { + /// + /// Получение координаты объекта + /// + ObjectParameters? GetObjectPosition { get; } + + /// + /// Шаг объекта + /// + int GetStep { get; } + + /// + /// Попытка переместить объект в указанном направлении + /// + /// Направление + /// true - объект перемещен, false - перемещение невозможно + bool TryMoveObject(MovementDirection direction); + } +} diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveToBorder.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..1459d44 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveToBorder.cs @@ -0,0 +1,53 @@ +namespace ProjectAirplaneWithRadar.MovementStrategy +{ + /// + /// Стратегия перемещения объекта к правой нижней границы + /// + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestination() + { + ObjectParameters? objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + + return objParams.RightBorder <= FieldWidth && objParams.RightBorder + GetStep() >= FieldWidth && + objParams.DownBorder <= FieldHeight && 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/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveToCenter.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..d7dc9ca --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveToCenter.cs @@ -0,0 +1,55 @@ +namespace ProjectAirplaneWithRadar.MovementStrategy +{ + /// + /// Стратегия перемещения объекта в центр экрана + /// + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestination() + { + 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/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveablePlane.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveablePlane.cs new file mode 100644 index 0000000..404457d --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveablePlane.cs @@ -0,0 +1,65 @@ +using ProjectAirplaneWithRadar.Drawnings; + +namespace ProjectAirplaneWithRadar.MovementStrategy +{ + /// + /// Класс-реализация IMoveableObject с использованием DrawningAirplane + /// + public class MoveablePlane : IMoveableObject + { + /// + /// Поле-объект класса DrawningAirplane или его наследника + /// + private readonly DrawningAirplane? _airplane = null; + + /// + /// Конструктор + /// + /// Объект класса DrawningAirplane + public MoveablePlane(DrawningAirplane airplane) + { + _airplane = airplane; + } + + public ObjectParameters? GetObjectPosition + { + get + { + if(_airplane == null || _airplane.EntityAirplane == null || !_airplane.GetPosX.HasValue || !_airplane.GetPosY.HasValue) + { + return null; + } + return new ObjectParameters(_airplane.GetPosX.Value, _airplane.GetPosY.Value, _airplane.GetWidth, _airplane.GetHeight); + } + } + + public int GetStep => (int)(_airplane?.EntityAirplane?.Step ?? 0); + + public bool TryMoveObject(MovementDirection direction) + { + if(_airplane == null || _airplane.EntityAirplane == null) + { + return false; + } + + return _airplane.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/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MovementDirection.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..4ba5518 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MovementDirection.cs @@ -0,0 +1,28 @@ +namespace ProjectAirplaneWithRadar.MovementStrategy +{ + /// + /// Направление перемещения + /// + public enum MovementDirection + { + /// + /// Вверх + /// + Up = 1, + + /// + /// Вниз + /// + Down = 2, + + /// + /// Влево + /// + Left = 3, + + /// + /// Вправо + /// + Right = 4 + } +} diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/ObjectParameters.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/ObjectParameters.cs new file mode 100644 index 0000000..c76a32a --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/ObjectParameters.cs @@ -0,0 +1,73 @@ +namespace ProjectAirplaneWithRadar.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/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/StrategyStatus.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/StrategyStatus.cs new file mode 100644 index 0000000..4da3f17 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,23 @@ +namespace ProjectAirplaneWithRadar.MovementStrategy +{ + /// + /// Статус выполнения операции перемещения + /// + public enum StrategyStatus + { + /// + /// Все готово к началу + /// + NotInit, + + /// + /// Выполняется + /// + InProgress, + + /// + /// Завершено + /// + Finish + } +}