From 9f13fc6a95e17dc4e65ccf40444ecc47934eb227 Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sat, 23 Mar 2024 14:57:14 +0400 Subject: [PATCH 1/4] =?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 --- .../MovementStrategy/AbstractStrategy.cs | 141 ++++++++++++++++++ .../MovementStrategy/IMoveableObject.cs | 25 ++++ .../MovementStrategy/MoveToBorder.cs | 53 +++++++ .../MovementStrategy/MoveToCenter.cs | 55 +++++++ .../MovementStrategy/MoveablePlane.cs | 65 ++++++++ .../MovementStrategy/MovementDirection.cs | 28 ++++ .../MovementStrategy/ObjectParameters.cs | 73 +++++++++ .../MovementStrategy/StrategyStatus.cs | 23 +++ 8 files changed, 463 insertions(+) create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/AbstractStrategy.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/IMoveableObject.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveToBorder.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveToCenter.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveablePlane.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MovementDirection.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/ObjectParameters.cs create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/StrategyStatus.cs diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/AbstractStrategy.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/AbstractStrategy.cs new file mode 100644 index 0000000..9c917cb --- /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; + } + } +} \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/IMoveableObject.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/IMoveableObject.cs new file mode 100644 index 0000000..b0cf034 --- /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); + } +} \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveToBorder.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveToBorder.cs new file mode 100644 index 0000000..7795019 --- /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(); + } + } + } + } +} \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveToCenter.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveToCenter.cs new file mode 100644 index 0000000..bf15aab --- /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(); + } + } + } + } +} \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveablePlane.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveablePlane.cs new file mode 100644 index 0000000..049275d --- /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, + }; + } + } +} \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MovementDirection.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MovementDirection.cs new file mode 100644 index 0000000..4c710df --- /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 + } +} \ No newline at end of file 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..774a356 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/StrategyStatus.cs @@ -0,0 +1,23 @@ +namespace ProjectAirplaneWithRadar.MovementStrategy +{ + /// + /// Статус выполнения операции перемещения + /// + public enum StrategyStatus + { + /// + /// Все готово к началу + /// + NotInit, + + /// + /// Выполняется + /// + InProgress, + + /// + /// Завершено + /// + Finish + } +} \ No newline at end of file -- 2.25.1 From a0f64e592fa34523161fba7d0bba2ff1a8f64b7a Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sat, 23 Mar 2024 15:11:25 +0400 Subject: [PATCH 2/4] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=B0=D0=BF=D0=BA=D0=B8=20Entities?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawingAirplaneWithRadar.cs | 22 ++++----- .../Entities/EntityAirplane.cs | 48 +++++++++++++++++++ .../{ => Entities}/EntityAirplaneWithRadar.cs | 35 +++----------- .../FormAirplaneWithRadar.cs | 4 +- 4 files changed, 68 insertions(+), 41 deletions(-) create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplane.cs rename AirplaneWithRadar/ProjectAirplaneWithRadar/{ => Entities}/EntityAirplaneWithRadar.cs (56%) diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/DrawingAirplaneWithRadar.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/DrawingAirplaneWithRadar.cs index 59761f7..36f61b6 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/DrawingAirplaneWithRadar.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/DrawingAirplaneWithRadar.cs @@ -1,4 +1,4 @@ -namespace ProjectAirplaneWithRadar +namespace ProjectAirplaneWithRadar.Entities { /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности @@ -39,7 +39,7 @@ /// Высота прорисовки самолета /// public readonly int PlaneHeight = 95; - + /// /// Инициализация свойств @@ -72,18 +72,18 @@ { _pictureWidth = width; _pictureHeight = height; - if(_startPosX != null && _startPosY != null) + if (_startPosX != null && _startPosY != null) { - if(_startPosX.Value < 0) + if (_startPosX.Value < 0) { _startPosX = 0; } - if(_startPosY.Value < 0) + if (_startPosY.Value < 0) { _startPosY = 0; } - if(_startPosX.Value + PlaneWidth > _pictureWidth) + if (_startPosX.Value + PlaneWidth > _pictureWidth) { _startPosX = _pictureWidth - PlaneWidth; } @@ -116,7 +116,7 @@ { _startPosX = 0; } - if(_startPosY.Value < 0) + if (_startPosY.Value < 0) { _startPosY = 0; } @@ -162,7 +162,7 @@ break; // вправо case DirectionType.Right: - if (_startPosX.Value + step < _pictureWidth - PlaneWidth) + if (_startPosX.Value + step < _pictureWidth - PlaneWidth) { _startPosX += step; } @@ -227,8 +227,8 @@ } //Корпус - g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 50,100,30); - + g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 50, 100, 30); + //Хвост Point[] points = { new Point(_startPosX.Value + 10, _startPosY.Value + 10), @@ -259,7 +259,7 @@ //Хвостовой элерон g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 45, 30, 10); g.FillEllipse(brBlack, _startPosX.Value, _startPosY.Value + 45, 30, 10); - + } } } diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplane.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplane.cs new file mode 100644 index 0000000..868b609 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplane.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace ProjectAirplaneWithRadar.Entities +{ + /// + /// Класс-сущность "Самолет" + /// + public class EntityAirplane + { + /// + /// Скорость + /// + public int Speed { get; private set; } + + /// + /// Вес + /// + public double Weight { get; private set; } + + /// + /// Основной цвет + /// + public Color BodyColor { get; private set; } + + /// + /// Шаг перемещения автомобиля + /// + public double Step => Speed * 100 / Weight; + + /// + /// Конструктор сущности + /// + /// Скорость + /// Вес + /// Основной цвет + public EntityAirplane(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/EntityAirplaneWithRadar.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplaneWithRadar.cs similarity index 56% rename from AirplaneWithRadar/ProjectAirplaneWithRadar/EntityAirplaneWithRadar.cs rename to AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplaneWithRadar.cs index 598eda9..269b5ed 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/EntityAirplaneWithRadar.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplaneWithRadar.cs @@ -1,25 +1,10 @@ -namespace ProjectAirplaneWithRadar +namespace ProjectAirplaneWithRadar.Entities { /// /// Класс-сущность "Самолет с радаром" /// - public class EntityAirplaneWithRadar + public class EntityAirplaneWithRadar : EntityAirplane { - /// - /// Скорость - /// - public int Speed { get; private set; } - - /// - /// Вес - /// - public double Weight { get; private set; } - - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// /// Дополнительный цвет (для опциональных элементов) /// @@ -33,12 +18,7 @@ /// /// Признак (опция) наличия радар /// - public bool Radar { get; private set; } - - /// - /// Шаг перемещения автомобиля - /// - public double Step => Speed * 100 / Weight; + public bool Radar { get; private set; } /// /// Инициализация полей объекта-класса самолета с радаром @@ -49,14 +29,11 @@ /// Дополнительный цвет /// Шасси /// Радар - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool wheels, bool radar) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; + public EntityAirplaneWithRadar(int speed, double weight, Color bodyColor, Color additionalColor, bool wheels, bool radar) : base(speed, weight, bodyColor) + { AdditionalColor = additionalColor; Wheels = wheels; - Radar = radar; + Radar = radar; } } } diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs index 7ec64fb..2b353ba 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs @@ -1,4 +1,6 @@ -namespace ProjectAirplaneWithRadar +using ProjectAirplaneWithRadar.Entities; + +namespace ProjectAirplaneWithRadar { /// /// Форма работы с объектом "Самолет с радаром" -- 2.25.1 From ef5352ab0cd339a43d7b83f591300b565d5b2ae5 Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sat, 23 Mar 2024 15:26:35 +0400 Subject: [PATCH 3/4] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=B0=D0=BF=D0=BA=D0=B8=20Drawnings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ => Drawnings}/DirectionType.cs | 3 +- .../Drawnings/DrawingAirplaneWithRadar.cs | 75 +++++++++ .../DrawningAirplane.cs} | 147 +++++++++--------- .../Entities/EntityAirplane.cs | 9 +- .../FormAirplaneWithRadar.cs | 3 +- 5 files changed, 154 insertions(+), 83 deletions(-) rename AirplaneWithRadar/ProjectAirplaneWithRadar/{ => Drawnings}/DirectionType.cs (90%) create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawingAirplaneWithRadar.cs rename AirplaneWithRadar/ProjectAirplaneWithRadar/{DrawingAirplaneWithRadar.cs => Drawnings/DrawningAirplane.cs} (66%) diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/DirectionType.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DirectionType.cs similarity index 90% rename from AirplaneWithRadar/ProjectAirplaneWithRadar/DirectionType.cs rename to AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DirectionType.cs index 46519a9..65cc84f 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/DirectionType.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DirectionType.cs @@ -1,5 +1,4 @@ - -namespace ProjectAirplaneWithRadar +namespace ProjectAirplaneWithRadar.Drawnings { /// /// Направление перемещения diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawingAirplaneWithRadar.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawingAirplaneWithRadar.cs new file mode 100644 index 0000000..8ba2e38 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawingAirplaneWithRadar.cs @@ -0,0 +1,75 @@ +using ProjectAirplaneWithRadar.Entities; + +namespace ProjectAirplaneWithRadar.Drawnings +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawingAirplaneWithRadar : DrawningAirplane + { + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес + /// Основной цвет + /// Дополнительный цвет + /// Шасси + /// Радар + public DrawingAirplaneWithRadar(int speed, double weight, Color bodyColor, Color additionalColor, bool wheels, bool radar) : base(150, 93) + { + EntityAirplane = new EntityAirplaneWithRadar(speed, weight, bodyColor, additionalColor, wheels, radar); + } + + /// + /// Прорисовка объекта + /// + /// + public override void DrawTransport(Graphics g) + { + if (EntityAirplane == null || EntityAirplane is not EntityAirplaneWithRadar airplaneWithRadar || !_startPosX.HasValue || !_startPosY.HasValue) + { + return; + } + + Pen pen = new(Color.Black); + Brush additionalBrush = new SolidBrush(airplaneWithRadar.AdditionalColor); + + if (airplaneWithRadar.Wheels) + { + //Задняя стойка + g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 80, 5, 10); + g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 80, 5, 10); + + g.DrawEllipse(pen, _startPosX.Value + 20, _startPosY.Value + 85, 10, 10); + g.FillEllipse(additionalBrush, _startPosX.Value + 20, _startPosY.Value + 85, 10, 10); + + g.DrawEllipse(pen, _startPosX.Value + 35, _startPosY.Value + 85, 10, 10); + g.FillEllipse(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 85, 10, 10); + + //Передняя стойка + g.DrawRectangle(pen, _startPosX.Value + 95, _startPosY.Value + 80, 5, 10); + g.FillRectangle(additionalBrush, _startPosX.Value + 95, _startPosY.Value + 80, 5, 10); + + g.DrawEllipse(pen, _startPosX.Value + 92, _startPosY.Value + 85, 10, 10); + g.FillEllipse(additionalBrush, _startPosX.Value + 92, _startPosY.Value + 85, 10, 10); + } + + _startPosY += 10; + base.DrawTransport(g); + _startPosY -= 10; + + + //Радар + if (airplaneWithRadar.Radar) + { + g.DrawRectangle(pen, _startPosX.Value + 70, _startPosY.Value + 40, 10, 10); + g.FillRectangle(additionalBrush, _startPosX.Value + 70, _startPosY.Value + 40, 10, 10); + + g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 30, 50, 10); + g.FillEllipse(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 30, 50, 10); + } + + } + } +} \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/DrawingAirplaneWithRadar.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplane.cs similarity index 66% rename from AirplaneWithRadar/ProjectAirplaneWithRadar/DrawingAirplaneWithRadar.cs rename to AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplane.cs index 36f61b6..63981d4 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/DrawingAirplaneWithRadar.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Drawnings/DrawningAirplane.cs @@ -1,14 +1,15 @@ -namespace ProjectAirplaneWithRadar.Entities +using ProjectAirplaneWithRadar.Entities; + + +namespace ProjectAirplaneWithRadar.Drawnings { - /// - /// Класс, отвечающий за прорисовку и перемещение объекта-сущности - /// - public class DrawingAirplaneWithRadar + public class DrawningAirplane { + /// /// Класс-сущность /// - public EntityAirplaneWithRadar? EntityAirplaneWithRadar { get; private set; } + public EntityAirplane? EntityAirplane { get; protected set; } /// /// Ширина окна @@ -23,43 +24,76 @@ /// /// Левая координата прорисовки /// - private int? _startPosX; + protected int? _startPosX; /// /// Верхняя кооридната прорисовки /// - private int? _startPosY; + protected int? _startPosY; /// /// Ширина прорисовки самолета /// - public readonly int PlaneWidth = 150; + private readonly int PlaneWidth = 150; /// /// Высота прорисовки самолета /// - public readonly int PlaneHeight = 95; - + private readonly int PlaneHeight = 85; /// - /// Инициализация свойств + /// Координата X объекта /// - /// Скорость - /// Вес - /// Основной цвет - /// Дополнительный цвет - /// Шасси - /// Радар - public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool wheels, bool radar) + public int? GetPosX => _startPosX; + + /// + /// Координата Y объекта + /// + public int? GetPosY => _startPosY; + + /// + /// Ширина объекта + /// + public int GetWidth => PlaneWidth; + + /// + /// Высота объекта + /// + public int GetHeight => PlaneHeight; + + /// + /// Пустой конструктор + /// + private DrawningAirplane() { - EntityAirplaneWithRadar = new EntityAirplaneWithRadar(); - EntityAirplaneWithRadar.Init(speed, weight, bodyColor, additionalColor, wheels, radar); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + public DrawningAirplane(int speed, double weight, Color bodyColor) : this() + { + EntityAirplane = new EntityAirplane(speed, weight, bodyColor); + } + + /// + /// Конструктор для наследников + /// + /// Ширина прорисовки самолета + /// Высота прорисовки самолета + protected DrawningAirplane(int planeWidth, int planeHeight) : this() + { + PlaneWidth = planeWidth; + PlaneHeight = planeHeight; + } + /// /// Установка границ поля /// @@ -138,12 +172,12 @@ /// true - перемещене выполнено, false - перемещение невозможно public bool MoveTransport(DirectionType direction) { - if (EntityAirplaneWithRadar == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityAirplane == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } - int step = (int)EntityAirplaneWithRadar.Step; + int step = (int)EntityAirplane.Step; switch (direction) { //влево @@ -184,82 +218,51 @@ /// Прорисовка объекта /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (EntityAirplaneWithRadar == null || !_startPosX.HasValue || !_startPosY.HasValue) + if (EntityAirplane == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityAirplaneWithRadar.AdditionalColor); - //Шасси - if (EntityAirplaneWithRadar.Wheels) - { - //Задняя стойка - g.DrawRectangle(pen, _startPosX.Value + 30, _startPosY.Value + 80, 5, 10); - g.FillRectangle(additionalBrush, _startPosX.Value + 30, _startPosY.Value + 80, 5, 10); - - g.DrawEllipse(pen, _startPosX.Value + 20, _startPosY.Value + 85, 10, 10); - g.FillEllipse(additionalBrush, _startPosX.Value + 20, _startPosY.Value + 85, 10, 10); - - g.DrawEllipse(pen, _startPosX.Value + 35, _startPosY.Value + 85, 10, 10); - g.FillEllipse(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 85, 10, 10); - - //Передняя стойка - g.DrawRectangle(pen, _startPosX.Value + 95, _startPosY.Value + 80, 5, 10); - g.FillRectangle(additionalBrush, _startPosX.Value + 95, _startPosY.Value + 80, 5, 10); - - g.DrawEllipse(pen, _startPosX.Value + 92, _startPosY.Value + 85, 10, 10); - g.FillEllipse(additionalBrush, _startPosX.Value + 92, _startPosY.Value + 85, 10, 10); - } - - //Радар - if (EntityAirplaneWithRadar.Radar) - { - g.DrawRectangle(pen, _startPosX.Value + 70, _startPosY.Value + 40, 10, 10); - g.FillRectangle(additionalBrush, _startPosX.Value + 70, _startPosY.Value + 40, 10, 10); - - g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 30, 50, 10); - g.FillEllipse(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 30, 50, 10); - } //Корпус - g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 50, 100, 30); + g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 40, 100, 30); //Хвост Point[] points = { - new Point(_startPosX.Value + 10, _startPosY.Value + 10), - new Point(_startPosX.Value + 10, _startPosY.Value + 50), - new Point(_startPosX.Value + 50, _startPosY.Value + 50) + new Point(_startPosX.Value + 10, _startPosY.Value), + new Point(_startPosX.Value + 10, _startPosY.Value + 40), + new Point(_startPosX.Value + 50, _startPosY.Value + 40) }; g.DrawPolygon(pen, points); //Кабина Point[] points2 = { - new Point(_startPosX.Value + 110, _startPosY.Value + 45), - new Point(_startPosX.Value + 110, _startPosY.Value + 65), - new Point(_startPosX.Value + 150, _startPosY.Value + 65) + new Point(_startPosX.Value + 110, _startPosY.Value + 35), + new Point(_startPosX.Value + 110, _startPosY.Value + 55), + new Point(_startPosX.Value + 150, _startPosY.Value + 55) }; g.DrawPolygon(pen, points2); Point[] points3 = { - new Point(_startPosX.Value + 110, _startPosY.Value + 65), - new Point(_startPosX.Value + 110, _startPosY.Value + 85), - new Point(_startPosX.Value + 150, _startPosY.Value + 65) + new Point(_startPosX.Value + 110, _startPosY.Value + 55), + new Point(_startPosX.Value + 110, _startPosY.Value + 75), + new Point(_startPosX.Value + 150, _startPosY.Value + 55) }; g.DrawPolygon(pen, points3); //Крыло - Brush brBlack = new SolidBrush(Color.Black); - g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 60, 70, 10); - g.FillEllipse(brBlack, _startPosX.Value + 30, _startPosY.Value + 60, 70, 10); + Brush brBlack = new SolidBrush(EntityAirplane.BodyColor); + g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 50, 70, 10); + g.FillEllipse(brBlack, _startPosX.Value + 30, _startPosY.Value + 50, 70, 10); //Хвостовой элерон - g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 45, 30, 10); - g.FillEllipse(brBlack, _startPosX.Value, _startPosY.Value + 45, 30, 10); + g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 35, 30, 10); + g.FillEllipse(brBlack, _startPosX.Value, _startPosY.Value + 35, 30, 10); } } -} +} \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplane.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplane.cs index 868b609..6abeb89 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplane.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Entities/EntityAirplane.cs @@ -1,11 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using static System.Windows.Forms.VisualStyles.VisualStyleElement; - -namespace ProjectAirplaneWithRadar.Entities +namespace ProjectAirplaneWithRadar.Entities { /// /// Класс-сущность "Самолет" diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs index 2b353ba..c399391 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs @@ -1,4 +1,5 @@ -using ProjectAirplaneWithRadar.Entities; +using ProjectAirplaneWithRadar.Drawnings; +using ProjectAirplaneWithRadar.Entities; namespace ProjectAirplaneWithRadar { -- 2.25.1 From b039442feae9eb2808340c867d7b32973839b6da Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sat, 23 Mar 2024 15:58:03 +0400 Subject: [PATCH 4/4] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Drawnings/DirectionType.cs | 5 + .../FormAirplaneWithRadar.Designer.cs | 75 +++++++---- .../FormAirplaneWithRadar.cs | 118 ++++++++++++++---- .../MovementStrategy/MoveablePlane.cs | 2 +- 4 files changed, 157 insertions(+), 43 deletions(-) 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/FormAirplaneWithRadar.Designer.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.Designer.cs index 2ed78dd..b3c9650 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.Designer.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.Designer.cs @@ -34,6 +34,9 @@ buttonRight = new Button(); buttonDown = new Button(); buttonUp = new Button(); + ButtonCreateAirplane = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStrategyStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirplaneWithRadar).BeginInit(); SuspendLayout(); // @@ -41,21 +44,19 @@ // pictureBoxAirplaneWithRadar.Dock = DockStyle.Fill; pictureBoxAirplaneWithRadar.Location = new Point(0, 0); - pictureBoxAirplaneWithRadar.Margin = new Padding(3, 4, 3, 4); pictureBoxAirplaneWithRadar.Name = "pictureBoxAirplaneWithRadar"; - pictureBoxAirplaneWithRadar.Size = new Size(1128, 636); + pictureBoxAirplaneWithRadar.Size = new Size(987, 477); pictureBoxAirplaneWithRadar.TabIndex = 0; pictureBoxAirplaneWithRadar.TabStop = false; // // buttonCreate // buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(14, 589); - buttonCreate.Margin = new Padding(3, 4, 3, 4); + buttonCreate.Location = new Point(12, 442); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(86, 31); + buttonCreate.Size = new Size(199, 23); buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; + buttonCreate.Text = "Создать Самолет с радаром"; buttonCreate.UseVisualStyleBackColor = true; buttonCreate.Click += ButtonCreate_Click; // @@ -64,10 +65,9 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonLeft.BackgroundImage = Properties.Resources.Стрелка_влево; buttonLeft.BackgroundImageLayout = ImageLayout.Stretch; - buttonLeft.Location = new Point(981, 573); - buttonLeft.Margin = new Padding(3, 4, 3, 4); + buttonLeft.Location = new Point(858, 430); buttonLeft.Name = "buttonLeft"; - buttonLeft.Size = new Size(40, 47); + buttonLeft.Size = new Size(35, 35); buttonLeft.TabIndex = 2; buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += ButtonMove_Click; @@ -77,10 +77,9 @@ buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonRight.BackgroundImage = Properties.Resources.Стрелка_вправо; buttonRight.BackgroundImageLayout = ImageLayout.Stretch; - buttonRight.Location = new Point(1074, 573); - buttonRight.Margin = new Padding(3, 4, 3, 4); + buttonRight.Location = new Point(940, 430); buttonRight.Name = "buttonRight"; - buttonRight.Size = new Size(40, 47); + buttonRight.Size = new Size(35, 35); buttonRight.TabIndex = 3; buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += ButtonMove_Click; @@ -90,10 +89,9 @@ buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonDown.BackgroundImage = Properties.Resources.Стрелка_вниз; buttonDown.BackgroundImageLayout = ImageLayout.Stretch; - buttonDown.Location = new Point(1027, 573); - buttonDown.Margin = new Padding(3, 4, 3, 4); + buttonDown.Location = new Point(899, 430); buttonDown.Name = "buttonDown"; - buttonDown.Size = new Size(40, 47); + buttonDown.Size = new Size(35, 35); buttonDown.TabIndex = 4; buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += ButtonMove_Click; @@ -103,26 +101,58 @@ buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; buttonUp.BackgroundImage = Properties.Resources.Стрелка_вверх; buttonUp.BackgroundImageLayout = ImageLayout.Stretch; - buttonUp.Location = new Point(1027, 519); - buttonUp.Margin = new Padding(3, 4, 3, 4); + buttonUp.Location = new Point(899, 389); buttonUp.Name = "buttonUp"; - buttonUp.Size = new Size(40, 47); + buttonUp.Size = new Size(35, 35); buttonUp.TabIndex = 5; buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += ButtonMove_Click; // + // ButtonCreateAirplane + // + ButtonCreateAirplane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + ButtonCreateAirplane.Location = new Point(230, 442); + ButtonCreateAirplane.Name = "ButtonCreateAirplane"; + ButtonCreateAirplane.Size = new Size(199, 23); + ButtonCreateAirplane.TabIndex = 6; + ButtonCreateAirplane.Text = "Создать Самолет"; + 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(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1128, 636); + ClientSize = new Size(987, 477); + Controls.Add(buttonStrategyStep); + Controls.Add(comboBoxStrategy); + Controls.Add(ButtonCreateAirplane); Controls.Add(buttonUp); Controls.Add(buttonDown); Controls.Add(buttonRight); Controls.Add(buttonLeft); Controls.Add(buttonCreate); Controls.Add(pictureBoxAirplaneWithRadar); - Margin = new Padding(3, 4, 3, 4); Name = "FormAirplaneWithRadar"; Text = "Самолет с радаром"; ((System.ComponentModel.ISupportInitialize)pictureBoxAirplaneWithRadar).EndInit(); @@ -137,5 +167,8 @@ private Button buttonRight; 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 c399391..8fb250b 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneWithRadar.cs @@ -1,5 +1,5 @@ using ProjectAirplaneWithRadar.Drawnings; -using ProjectAirplaneWithRadar.Entities; +using ProjectAirplaneWithRadar.MovementStrategy; namespace ProjectAirplaneWithRadar { @@ -11,7 +11,12 @@ namespace ProjectAirplaneWithRadar /// /// Поле-объект для происовки объект /// - private DrawingAirplaneWithRadar? _drawingAirplaneWithRadar; + private DrawningAirplane? _drawingAirplane; + + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _strategy; /// /// Конструктор формы @@ -19,39 +24,67 @@ namespace ProjectAirplaneWithRadar public FormAirplaneWithRadar() { InitializeComponent(); + _strategy = null; } /// - /// Обработка нажатия кнопки "Создать" + /// Создание объекта класса-перемещения + /// + /// Тип создаваемого объекта + private void CreateObject(string type) + { + Random random = new(); + switch (type) + { + case nameof(DrawningAirplane): + _drawingAirplane = new DrawningAirplane(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256))); + break; + case nameof(DrawingAirplaneWithRadar): + _drawingAirplane = new DrawingAirplaneWithRadar(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), + Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); + break; + default: + return; + + } + + _drawingAirplane.SetPictureSize(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height); + _drawingAirplane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + + _strategy = null; + comboBoxStrategy.Enabled = true; + UpdatePlane(); + } + + /// + /// Обработка нажатия кнопки "Создать Самолет с радаром" /// /// /// - private void ButtonCreate_Click(object sender, EventArgs e) - { - Random random = new(); - _drawingAirplaneWithRadar = new DrawingAirplaneWithRadar(); - _drawingAirplaneWithRadar.Init(random.Next(100, 300), random.Next(1000, 3000), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), - Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2))); - _drawingAirplaneWithRadar.SetPictureSize(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height); - _drawingAirplaneWithRadar.SetPosition(random.Next(10, 100), random.Next(10, 100)); + private void ButtonCreate_Click(object sender, EventArgs e) => CreateObject(nameof(DrawingAirplaneWithRadar)); - UpdatePlane(); - } + /// + /// Обработка нажатия кнопки "Создать Самолет" + /// + /// + /// + private void ButtonCreateAirplane_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningAirplane)); /// /// Метод прорисовки самолета /// private void UpdatePlane() { - if (_drawingAirplaneWithRadar == null) + if (_drawingAirplane == null) { return; } Bitmap bmp = new(pictureBoxAirplaneWithRadar.Width, pictureBoxAirplaneWithRadar.Height); Graphics gr = Graphics.FromImage(bmp); - _drawingAirplaneWithRadar?.DrawTransport(gr); + _drawingAirplane?.DrawTransport(gr); pictureBoxAirplaneWithRadar.Image = bmp; } @@ -62,7 +95,7 @@ namespace ProjectAirplaneWithRadar /// private void ButtonMove_Click(object sender, EventArgs e) { - if (_drawingAirplaneWithRadar == null) + if (_drawingAirplane == null) { return; } @@ -90,10 +123,53 @@ namespace ProjectAirplaneWithRadar return; } - _drawingAirplaneWithRadar.MoveTransport(result); + _drawingAirplane.MoveTransport(result); 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; + } + } } -} +} \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveablePlane.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveablePlane.cs index 049275d..ce39e8c 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveablePlane.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/MovementStrategy/MoveablePlane.cs @@ -58,7 +58,7 @@ namespace ProjectAirplaneWithRadar.MovementStrategy MovementDirection.Right => DirectionType.Right, MovementDirection.Up => DirectionType.Up, MovementDirection.Down => DirectionType.Down, - _ => DirectionType.Unknow, + _ => DirectionType.Unknow }; } } -- 2.25.1