From a6ecfa293fe3a76b3721cfe89026aa1418af28bf Mon Sep 17 00:00:00 2001 From: "a.puchkina" Date: Wed, 27 Sep 2023 11:20:19 +0400 Subject: [PATCH 1/2] =?UTF-8?q?Entities=20=D0=B8=20DrawningOblects=20?= =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AirplaneWithRadarForm.cs | 14 ++- .../AirplaneWithRadar/DrawningAirplane.cs | 100 +++++++++--------- .../DrawningAirplaneWithRadar.cs | 71 +++++++++++++ .../{ProjectAirplane.cs => EntityAirplane.cs} | 29 ++--- .../EntityAirplaneWithRadar.cs | 44 ++++++++ 5 files changed, 178 insertions(+), 80 deletions(-) create mode 100644 AirplaneWithRadar/AirplaneWithRadar/DrawningAirplaneWithRadar.cs rename AirplaneWithRadar/AirplaneWithRadar/{ProjectAirplane.cs => EntityAirplane.cs} (59%) create mode 100644 AirplaneWithRadar/AirplaneWithRadar/EntityAirplaneWithRadar.cs diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.cs index b79188d..2437abd 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using AirplaneWithRadar.DrawningObjects; namespace AirplaneWithRadar { @@ -50,14 +51,11 @@ namespace AirplaneWithRadar private void buttonCreateAirplane_Click(object sender, EventArgs e) { Random random = new(); - _drawningAirplane = new DrawningAirplane(); - _drawningAirplane.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)), Convert.ToBoolean(random.Next(0, 2)), pictureBoxAirplane.Width, pictureBoxAirplane.Height); - _drawningAirplane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _drawningAirplane = new DrawningAirplane(random.Next(100,100),random.Next(1000,3000), Color.FromArgbb(random.Next(0, 256), random.Next(0, 256), + random.Next(0, 256)), + pictureBoxAirplane.Width, pictureBoxAirplane.Height); + _drawningAirplane.SetPosition(random.Next(10, 100), random.Next(10, + 100)); Draw(); } diff --git a/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplane.cs index 0fd77ef..e984bd9 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplane.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplane.cs @@ -4,8 +4,10 @@ using System.Linq; using System.Reflection.Metadata.Ecma335; using System.Text; using System.Threading.Tasks; +using AirplaneWithRadar.Entities; -namespace AirplaneWithRadar + +namespace AirplaneWithRadar.DrawningObjects {/// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// @@ -14,7 +16,7 @@ namespace AirplaneWithRadar /// /// Класс-сущность /// - public EntityAirplaneWithRadar? EntityAirplaneWithRadar{ get; private set; } + public EntityAirplane? EntityAirplane{ get; protected set; } /// /// Ширина окна /// @@ -22,23 +24,23 @@ namespace AirplaneWithRadar /// /// Высота окна /// - private int _pictureHeight; + private int _pictureHeight; /// /// /// Левая координата прорисовки самолета /// - private int _startPosX; + protected int _startPosX; /// /// Верхняя кооридната прорисовки самолета /// - private int _startPosY; + protected int _startPosY; /// /// Ширина прорисовки самолета /// - private readonly int _airplaneWidth = 200; + protected readonly int _airplaneWidth = 200; /// /// Высота прорисовки самолета /// - private readonly int _airplaneHeight = 85; + protected readonly int _airplaneHeight = 85; /// /// Инициализация свойств /// @@ -46,24 +48,43 @@ namespace AirplaneWithRadar /// Вес /// Цвет фюзеляжа /// Дополнительный цвет - /// Признак наличия радара - /// Признак наличия доп.бака - /// Признак наличия штыря /// Ширина картинки /// Высота картинки /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах - public bool Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool radar, bool tank, bool pin, int width, int height) + public DrawningAirplane(int speed, double weight, Color bodyColor, Color + additionalColor, int width, int height) { _pictureWidth = width; _pictureHeight = height; if (_pictureWidth < _airplaneWidth || _pictureHeight < _airplaneHeight) { - return false; + return; } - EntityAirplaneWithRadar = new EntityAirplaneWithRadar(); - EntityAirplaneWithRadar.Init(speed, weight, bodyColor, additionalColor, radar, tank, pin); - return true; + EntityAirplane = new EntityAirplane(speed, weight, bodyColor, additionalColor); + + } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки автомобиля + /// Высота прорисовки автомобиля + protected DrawningAirplane(int speed, double weight, Color bodyColor, Color additionalColor, int + width, int height, int airplaneWidth, int airplaneHeight) + { + _pictureWidth = width; + _pictureHeight = height; + _airplaneWidth = airplaneWidth; + _airplaneHeight = airplaneHeight; + if (_pictureWidth < _airplaneWidth || _pictureHeight < _airplaneHeight) + { + return; + } + EntityAirplane = new EntityAirplane(speed, weight, bodyColor, additionalColor); } /// /// Установка позиции @@ -81,7 +102,7 @@ namespace AirplaneWithRadar /// Направление public void MoveTransport(DirectionType direction) { - if (EntityAirplaneWithRadar == null) + if (EntityAirplane == null) { return; } @@ -89,30 +110,30 @@ namespace AirplaneWithRadar { //влево case DirectionType.Left: - if (_startPosX - EntityAirplaneWithRadar.Step > 0) + if (_startPosX - EntityAirplane.Step > 0) { - _startPosX -= (int)EntityAirplaneWithRadar.Step; + _startPosX -= (int)EntityAirplane.Step; } break; //вверх case DirectionType.Up: - if (_startPosY - EntityAirplaneWithRadar.Step > 0) + if (_startPosY - EntityAirplane.Step > 0) { - _startPosY -= (int)EntityAirplaneWithRadar.Step; + _startPosY -= (int)EntityAirplane.Step; } break; // вправо case DirectionType.Right: - if (_startPosX + EntityAirplaneWithRadar.Step + _airplaneWidth < _pictureWidth) + if (_startPosX + EntityAirplane.Step + _airplaneWidth < _pictureWidth) { - _startPosX += (int)EntityAirplaneWithRadar.Step; + _startPosX += (int)EntityAirplane.Step; } break; //вниз case DirectionType.Down: - if (_startPosY + EntityAirplaneWithRadar.Step + _airplaneHeight < _pictureHeight) + if (_startPosY + EntityAirplane.Step + _airplaneHeight < _pictureHeight) { - _startPosY += (int)EntityAirplaneWithRadar.Step; + _startPosY += (int)EntityAirplane.Step; } break; } @@ -121,24 +142,16 @@ namespace AirplaneWithRadar /// Прорисовка объекта /// /// - public void DrawTransport(Graphics g) + public virtual void DrawTransport(Graphics g) { - if (EntityAirplaneWithRadar == null) + if (EntityAirplane == null) { return; } Pen penBlack = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityAirplaneWithRadar.AdditionalColor); - Brush bodyBrush = new SolidBrush(EntityAirplaneWithRadar.BodyColor); + Brush additionalBrush = new SolidBrush(EntityAirplane.AdditionalColor); + Brush bodyBrush = new SolidBrush(EntityAirplane.BodyColor); - // радар - if (EntityAirplaneWithRadar.Radar) - { - g.FillEllipse(additionalBrush, _startPosX + 65, _startPosY + 25, 50, 10); - g.DrawEllipse(penBlack, _startPosX + 65, _startPosY + 25, 50, 10); - g.FillRectangle(additionalBrush, _startPosX + 85, _startPosY + 35, 10, 5); - g.DrawRectangle(penBlack, _startPosX + 85, _startPosY + 35, 10, 5); - } //фюзеляж g.FillRectangle(bodyBrush, _startPosX + 4, _startPosY + 40, 150, 30); g.DrawRectangle(penBlack, _startPosX + 4, _startPosY + 40, 150, 30); @@ -175,19 +188,6 @@ namespace AirplaneWithRadar g.DrawLine(penBlack, new Point(_startPosX + 150, _startPosY + 72), new Point(_startPosX + 150, _startPosY + 55)); g.DrawLine(penBlack, new Point(_startPosX + 150, _startPosY + 72), new Point(_startPosX + 190, _startPosY + 55)); - // Штырь - if (EntityAirplaneWithRadar.Pin) - { - g.DrawLine(penGray, new Point(_startPosX + 190, _startPosY + 55), new Point(_startPosX + 200, _startPosY + 55)); - } - - // бак - if (EntityAirplaneWithRadar.Tank) - { - g.FillRectangle(additionalBrush, _startPosX + 70, _startPosY + 65, 45, 15); - g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX + 70, _startPosY + 65), new Point(_startPosX + 60, _startPosY + 72), new Point(_startPosX + 70, _startPosY + 80) }); - g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX + 115, _startPosY + 65), new Point(_startPosX + 125, _startPosY + 72), new Point(_startPosX + 115, _startPosY + 80) }); - } } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplaneWithRadar.cs b/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplaneWithRadar.cs new file mode 100644 index 0000000..63e41a2 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplaneWithRadar.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirplaneWithRadar.Entities; + +namespace AirplaneWithRadar.DrawningObjects +{ + public class DrawningAirplaneWithRadar : DrawningAirplane + { + /// + /// Инициализация свойств + /// + /// Скорость + /// Вес + /// Цвет фюзеляжа + /// Дополнительный цвет + /// Признак наличия радара + /// Признак наличия бака + /// Признак наличия штыря + /// Ширина картинки + /// Высота картинки + /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах + public DrawningAirplaneWithRadar(int speed, double weight, Color bodyColor, Color + additionalColor, bool radar, bool tank, bool pin, int width, int height) : + base(speed, weight, bodyColor, additionalColor, width, height, 200, 85) + { + if (EntityAirplane != null) + { + EntityAirplane = new EntityAirplaneWithRadar(speed, weight, bodyColor, + additionalColor, radar, tank, pin); + } + } + public override void DrawTransport(Graphics g) + { + if (EntityAirplane is not EntityAirplaneWithRadar airplaneWithRadar) + { + return; + } + Pen penBlack = new(Color.Black); + Brush additionalBrush = new SolidBrush(EntityAirplane.AdditionalColor); + Brush bodyBrush = new SolidBrush(EntityAirplane.BodyColor); + Pen penGray = new Pen(Color.Gray); + // радар + if (airplaneWithRadar.Radar) + { + g.FillEllipse(additionalBrush, _startPosX + 65, _startPosY + 25, 50, 10); + g.DrawEllipse(penBlack, _startPosX + 65, _startPosY + 25, 50, 10); + g.FillRectangle(additionalBrush, _startPosX + 85, _startPosY + 35, 10, 5); + g.DrawRectangle(penBlack, _startPosX + 85, _startPosY + 35, 10, 5); + } + base.DrawTransport(g); + // Штырь + if (airplaneWithRadar.Pin) + { + g.DrawLine(penGray, new Point(_startPosX + 190, _startPosY + 55), new Point(_startPosX + 200, _startPosY + 55)); + } + + // бак + if (airplaneWithRadar.Tank) + { + g.FillRectangle(additionalBrush, _startPosX + 70, _startPosY + 65, 45, 15); + g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX + 70, _startPosY + 65), new Point(_startPosX + 60, _startPosY + 72), new Point(_startPosX + 70, _startPosY + 80) }); + g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX + 115, _startPosY + 65), new Point(_startPosX + 125, _startPosY + 72), new Point(_startPosX + 115, _startPosY + 80) }); + } + } + } +} + + diff --git a/AirplaneWithRadar/AirplaneWithRadar/ProjectAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/EntityAirplane.cs similarity index 59% rename from AirplaneWithRadar/AirplaneWithRadar/ProjectAirplane.cs rename to AirplaneWithRadar/AirplaneWithRadar/EntityAirplane.cs index b1afeed..6eb305a 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/ProjectAirplane.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/EntityAirplane.cs @@ -4,9 +4,12 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AirplaneWithRadar +namespace AirplaneWithRadar.Entities { - public class EntityAirplaneWithRadar + /// + /// Класс-сущность "Самолет" + /// + public class EntityAirplane { /// /// Скорость @@ -25,18 +28,6 @@ namespace AirplaneWithRadar /// public Color AdditionalColor { get; private set; } /// - /// Признак (опция) наличия радара - /// - public bool Radar { get; private set; } - /// - /// Признак (опция) наличия доп.бака - /// - public bool Tank { get; private set; } - /// - /// Признак (опция) наличия штыря - /// - public bool Pin { get; private set; } - /// /// Шаг перемещения самолета /// public double Step => (double)Speed * 100 / Weight; @@ -47,19 +38,13 @@ namespace AirplaneWithRadar /// Вес самолета /// Основной цвет /// Дополнительный цвет - /// Признак наличия радара - /// Признак наличия доп.бака - /// Признак наличия штыря - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool radar, bool tank, bool pin) + public EntityAirplane(int speed, double weight, Color bodyColor, Color + additionalColor) { Speed = speed; Weight = weight; BodyColor = bodyColor; AdditionalColor = additionalColor; - Radar = radar; - Tank = tank; - Pin = pin; } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/EntityAirplaneWithRadar.cs b/AirplaneWithRadar/AirplaneWithRadar/EntityAirplaneWithRadar.cs new file mode 100644 index 0000000..183b517 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/EntityAirplaneWithRadar.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.Entities +{ + /// + /// Класс-сущность "Самолет с радаром" + /// + public class EntityAirplaneWithRadar : EntityAirplane + { + /// + /// Признак (опция) наличия радара + /// + public bool Radar { get; private set; } + /// + /// Признак (опция) наличия доп.бака + /// + public bool Tank { get; private set; } + /// + /// Признак (опция) наличия штыря + /// + public bool Pin { get; private set; } + /// + /// Инициализация полей объекта-класса самолета с радаром + /// + /// Скорость + /// Вес самолета + /// Основной цвет + /// Дополнительный цвет + /// Признак наличия радара + /// Признак наличия доп.бака + /// Признак наличия штыря + public EntityAirplaneWithRadar(int speed, double weight, Color bodyColor, Color + additionalColor, bool radar, bool tank, bool pin) + { + Radar = radar; + Tank = tank; + Pin = pin; + } + } +} From d4c53d054355e8f378608e951bb717b4d2dfda4a Mon Sep 17 00:00:00 2001 From: "a.puchkina" Date: Tue, 10 Oct 2023 21:32:48 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0=202=20=D1=81=D0=BE?= =?UTF-8?q?=D0=B7=D0=B4=D0=B0=D0=BB=D0=B0=20=D0=B4=D0=BE=D0=BF=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=D1=8B,=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=D0=B0=20=D0=BC=D0=B5=D1=82=D0=BE?= =?UTF-8?q?=D0=B4=D1=8B,=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB=D0=B0=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20=D0=B4=D0=B2=D0=B8=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=BA=20=D0=B3=D1=80=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=86=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AirplaneWithRadar/AbstractStrategy.cs | 136 ++++++++++++++++++ .../AirplaneWithRadarForm.Designer.cs | 42 ++++++ .../AirplaneWithRadarForm.cs | 77 +++++++++- .../AirplaneWithRadarForm.resx | 4 +- .../AirplaneWithRadar/DrawningAirplane.cs | 57 ++++++-- .../DrawningAirplaneWithRadar.cs | 4 +- .../DrawningObjectAirplane.cs | 40 ++++++ .../EntityAirplaneWithRadar.cs | 2 +- .../AirplaneWithRadar/IMoveableObject.cs | 36 +++++ .../AirplaneWithRadar/MoveToBorder.cs | 57 ++++++++ .../AirplaneWithRadar/MoveToCenter.cs | 57 ++++++++ .../AirplaneWithRadar/ObjectParameters.cs | 58 ++++++++ AirplaneWithRadar/AirplaneWithRadar/Status.cs | 15 ++ 13 files changed, 568 insertions(+), 17 deletions(-) create mode 100644 AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs create mode 100644 AirplaneWithRadar/AirplaneWithRadar/Status.cs diff --git a/AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs b/AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs new file mode 100644 index 0000000..3bec3e8 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/AbstractStrategy.cs @@ -0,0 +1,136 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirplaneWithRadar.DrawningObjects; + +namespace AirplaneWithRadar.MovementStrategy +{ + /// + /// Класс-стратегия перемещения объекта + /// + public abstract class AbstractStrategy + { + /// + /// Перемещаемый объект + /// + private IMoveableObject? _moveableObject; + /// + /// Статус перемещения + /// + private Status _state = Status.NotInit; + /// + /// Ширина поля + /// + protected int FieldWidth { get; private set; } + /// + /// Высота поля + /// + protected int FieldHeight { get; private set; } + /// + /// Статус перемещения + /// + public Status GetStatus() { return _state; } + /// + /// Установка данных + /// + /// Перемещаемый объект + /// Ширина поля + /// Высота поля + public void SetData(IMoveableObject moveableObject, int width, int + height) + { + if (moveableObject == null) + { + _state = Status.NotInit; + return; + } + _state = Status.InProgress; + _moveableObject = moveableObject; + FieldWidth = width; + FieldHeight = height; + } + /// + /// Шаг перемещения + /// + public void MakeStep() + { + if (_state != Status.InProgress) + { + return; + } + if (IsTargetDestinaion()) + { + _state = Status.Finish; + return; + } + MoveToTarget(); + } + /// + /// Перемещение влево + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveLeft() => MoveTo(DirectionType.Left); + /// + /// Перемещение вправо + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveRight() => MoveTo(DirectionType.Right); + /// + /// Перемещение вверх + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveUp() => MoveTo(DirectionType.Up); + /// + /// Перемещение вниз + /// + /// Результат перемещения (true - удалось переместиться, false - неудача) + protected bool MoveDown() => MoveTo(DirectionType.Down); + /// + /// Параметры объекта + /// + protected ObjectParameters? GetObjectParameters => + _moveableObject?.GetObjectPosition; + /// + /// Шаг объекта + /// + /// + protected int? GetStep() + { + if (_state != Status.InProgress) + { + return null; + } + return _moveableObject?.GetStep; + } + /// + /// Перемещение к цели + /// + protected abstract void MoveToTarget(); + /// + /// Достигнута ли цель + /// + /// + protected abstract bool IsTargetDestinaion(); + /// + /// Попытка перемещения в требуемом направлении + /// + /// Направление + /// Результат попытки (true - удалось переместиться, false - неудача) + private bool MoveTo(DirectionType directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } + +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.Designer.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.Designer.cs index e342c34..649b9c3 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.Designer.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.Designer.cs @@ -35,6 +35,9 @@ buttonLeft = new Button(); buttonRight = new Button(); buttonDown = new Button(); + buttonCreateAirplaneWithRadar = new Button(); + buttonStep = new Button(); + comboBoxStrategy = new ComboBox(); ((System.ComponentModel.ISupportInitialize)pictureBoxAirplane).BeginInit(); SuspendLayout(); // @@ -107,11 +110,47 @@ buttonDown.UseVisualStyleBackColor = true; buttonDown.Click += buttonMove_Click; // + // buttonCreateAirplaneWithRadar + // + buttonCreateAirplaneWithRadar.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateAirplaneWithRadar.Location = new Point(112, 411); + buttonCreateAirplaneWithRadar.Name = "buttonCreateAirplaneWithRadar"; + buttonCreateAirplaneWithRadar.Size = new Size(198, 30); + buttonCreateAirplaneWithRadar.TabIndex = 6; + buttonCreateAirplaneWithRadar.Text = "Создать с дополнениями"; + buttonCreateAirplaneWithRadar.UseVisualStyleBackColor = true; + buttonCreateAirplaneWithRadar.Click += buttonCreateAirplaneWithRadar_Click; + // + // buttonStep + // + buttonStep.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonStep.Location = new Point(788, 34); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(94, 29); + buttonStep.TabIndex = 7; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += buttonStep_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right; + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "в центр", "к гарнице" }); + comboBoxStrategy.Location = new Point(731, 0); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 8; + // // AirplaneWithRadarForm // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(882, 453); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonStep); + Controls.Add(buttonCreateAirplaneWithRadar); Controls.Add(buttonDown); Controls.Add(buttonRight); Controls.Add(buttonLeft); @@ -134,5 +173,8 @@ private Button buttonLeft; private Button buttonRight; private Button buttonDown; + private Button buttonCreateAirplaneWithRadar; + private Button buttonStep; + private ComboBox comboBoxStrategy; } } \ No newline at end of file diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.cs b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.cs index 2437abd..91403fd 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using AirplaneWithRadar.DrawningObjects; +using AirplaneWithRadar.MovementStrategy; namespace AirplaneWithRadar { @@ -20,6 +21,11 @@ namespace AirplaneWithRadar /// Поле-объект для прорисовки объекта /// private DrawningAirplane? _drawningAirplane; + /// + /// Стратегия перемещения + /// + private AbstractStrategy? _abstractStrategy; + /// /// Инициализация формы /// @@ -44,23 +50,42 @@ namespace AirplaneWithRadar } /// - /// Обработка нажатия кнопки "Создать" + /// Обработка нажатия кнопки "Создать самолет" /// /// /// private void buttonCreateAirplane_Click(object sender, EventArgs e) { Random random = new(); - _drawningAirplane = new DrawningAirplane(random.Next(100,100),random.Next(1000,3000), Color.FromArgbb(random.Next(0, 256), random.Next(0, 256), + _drawningAirplane = new DrawningAirplane(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)), pictureBoxAirplane.Width, pictureBoxAirplane.Height); - _drawningAirplane.SetPosition(random.Next(10, 100), random.Next(10, + _drawningAirplane.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } + private void buttonCreateAirplaneWithRadar_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningAirplane = new DrawningAirplaneWithRadar(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)), Convert.ToBoolean(random.Next(0, 2)), + pictureBoxAirplane.Width, pictureBoxAirplane.Height); + _drawningAirplane.SetPosition(random.Next(10, 100), random.Next(10, + 100)); + Draw(); + } + /// - /// Изменение размеров формы + /// Изменение положения самолета /// /// /// @@ -88,5 +113,49 @@ namespace AirplaneWithRadar } Draw(); } + /// + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + private void buttonStep_Click(object sender, EventArgs e) + { + if (_drawningAirplane == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectAirplane(_drawningAirplane), pictureBoxAirplane.Width, + pictureBoxAirplane.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + + } + } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.resx b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.resx index 16c89f7..416b05a 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.resx +++ b/AirplaneWithRadar/AirplaneWithRadar/AirplaneWithRadarForm.resx @@ -121,7 +121,7 @@ iVBORw0KGgoAAAANSUhEUgAAA4QAAAOEBAMAAAALYOIIAAAABGdBTUEAALGPC/xhBQAAABJQTFRF5ubm - AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOvgAADr4B6kKxwAAAFy1JREFUeNrtnV2S6ka2RkFB + AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOvAAADrwBlbxySQAAFy1JREFUeNrtnV2S6ka2RkFB v7ese98VCmkA6hwBlzsB4tjzn0qTPxLC5lBCKH++qsVDVy932yf5VmxD1lbmPnT+1R78C5RD4pBH4pBH 4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pBH4pDH6VWFvw6qInHII3HII3HII3HII3HII3HI I3HII3HII3HII3HII3HII3HII3HII3EIY/hJ700WiUMeiUMeiUMeiUMeiUMeiUMeiUMeiUMeiUMeiUMe @@ -328,7 +328,7 @@ iVBORw0KGgoAAAANSUhEUgAAA4QAAAOEBAMAAAALYOIIAAAABGdBTUEAALGPC/xhBQAAABJQTFRF5ubm - AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOvgAADr4B6kKxwAAAGBNJREFUeNrt3V9y+r7Vx3Fj + AQEB////AAAAa2trubm55kNtzgAAAAlwSFlzAAAOvAAADrwBlbxySQAAGBNJREFUeNrt3V9y+r7Vx3Fj NoARuTeacB9VYQGPM92A2f9eimTZSb5PSAD/05Hens7QTy/6i/WqjqFHkotwlbq7oosf1fWy3WWq7lo+ WvsW4+B8jRBCmDahglA6oYUQQgghhBBCCCGEEEIIIRwT+V0IIYQQQjg2KgilE1bqJV7C8FmH/zy2WH5c i5gNI6rCkK4Qq5e6iHSsIIQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGE8K+oIJROaM/xEoaLfuGvka49 diff --git a/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplane.cs index e984bd9..655ec6d 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplane.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplane.cs @@ -16,7 +16,7 @@ namespace AirplaneWithRadar.DrawningObjects /// /// Класс-сущность /// - public EntityAirplane? EntityAirplane{ get; protected set; } + public EntityAirplane? EntityAirplane { get; protected set; } /// /// Ширина окна /// @@ -24,7 +24,7 @@ namespace AirplaneWithRadar.DrawningObjects /// /// Высота окна /// - private int _pictureHeight; + private int _pictureHeight; /// /// /// Левая координата прорисовки самолета /// @@ -61,7 +61,7 @@ namespace AirplaneWithRadar.DrawningObjects return; } EntityAirplane = new EntityAirplane(speed, weight, bodyColor, additionalColor); - + } /// /// Конструктор @@ -96,13 +96,55 @@ namespace AirplaneWithRadar.DrawningObjects _startPosX = Math.Min(x, _pictureWidth - _airplaneWidth); _startPosY = Math.Min(y, _pictureHeight - _airplaneHeight); } + /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объект + /// + public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _airplaneWidth; + /// + /// Высота объекта + /// + public int GetHeight => _airplaneHeight; + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(DirectionType direction) + { + if (EntityAirplane == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityAirplane.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityAirplane.Step > 0, + // вправо + DirectionType.Right => _startPosX + EntityAirplane.Step < _pictureWidth + _airplaneWidth, + //вниз + DirectionType.Down => _startPosY + EntityAirplane.Step < _pictureHeight + _airplaneHeight, + _ => false, + }; + } + + /// /// Изменение направления перемещения /// /// Направление public void MoveTransport(DirectionType direction) { - if (EntityAirplane == null) + if (!CanMove(direction) || EntityAirplane == null) { return; } @@ -120,7 +162,7 @@ namespace AirplaneWithRadar.DrawningObjects if (_startPosY - EntityAirplane.Step > 0) { _startPosY -= (int)EntityAirplane.Step; - } + } break; // вправо case DirectionType.Right: @@ -157,9 +199,9 @@ namespace AirplaneWithRadar.DrawningObjects g.DrawRectangle(penBlack, _startPosX + 4, _startPosY + 40, 150, 30); //киль - g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX + 4, _startPosY + 40), new Point(_startPosX + 4, _startPosY + 0), new Point(_startPosX + 50, _startPosY + 40)}); + g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX + 4, _startPosY + 40), new Point(_startPosX + 4, _startPosY + 0), new Point(_startPosX + 50, _startPosY + 40) }); g.DrawPolygon(penBlack, new Point[] { new Point(_startPosX + 4, _startPosY + 40), new Point(_startPosX + 4, _startPosY + 0), new Point(_startPosX + 50, _startPosY + 40) }); - + //стабилизатор и крыло Brush brSilver = new SolidBrush(Color.Silver); g.FillEllipse(brSilver, _startPosX, _startPosY + 35, 55, 10); @@ -187,7 +229,6 @@ namespace AirplaneWithRadar.DrawningObjects g.DrawLine(penBlue, new Point(_startPosX + 190, _startPosY + 55), new Point(_startPosX + 150, _startPosY + 55)); g.DrawLine(penBlack, new Point(_startPosX + 150, _startPosY + 72), new Point(_startPosX + 150, _startPosY + 55)); g.DrawLine(penBlack, new Point(_startPosX + 150, _startPosY + 72), new Point(_startPosX + 190, _startPosY + 55)); - } } } diff --git a/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplaneWithRadar.cs b/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplaneWithRadar.cs index 63e41a2..96498a9 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplaneWithRadar.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/DrawningAirplaneWithRadar.cs @@ -24,7 +24,7 @@ namespace AirplaneWithRadar.DrawningObjects /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах public DrawningAirplaneWithRadar(int speed, double weight, Color bodyColor, Color additionalColor, bool radar, bool tank, bool pin, int width, int height) : - base(speed, weight, bodyColor, additionalColor, width, height, 200, 85) + base(speed, weight, bodyColor, additionalColor, width, height) { if (EntityAirplane != null) { @@ -42,6 +42,7 @@ namespace AirplaneWithRadar.DrawningObjects Brush additionalBrush = new SolidBrush(EntityAirplane.AdditionalColor); Brush bodyBrush = new SolidBrush(EntityAirplane.BodyColor); Pen penGray = new Pen(Color.Gray); + // радар if (airplaneWithRadar.Radar) { @@ -56,7 +57,6 @@ namespace AirplaneWithRadar.DrawningObjects { g.DrawLine(penGray, new Point(_startPosX + 190, _startPosY + 55), new Point(_startPosX + 200, _startPosY + 55)); } - // бак if (airplaneWithRadar.Tank) { diff --git a/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs b/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs new file mode 100644 index 0000000..9b68624 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/DrawningObjectAirplane.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirplaneWithRadar.MovementStrategy; +using AirplaneWithRadar.DrawningObjects; + +namespace AirplaneWithRadar.MovementStrategy +{ + /// + /// Реализация интерфейса IDrawningObject для работы с объектом DrawningCar + /// + public class DrawningObjectAirplane : IMoveableObject + { + private readonly DrawningAirplane? _drawningAirplane = null; + public DrawningObjectAirplane(DrawningAirplane drawningAirplane) + { + _drawningAirplane = drawningAirplane; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningAirplane == null || _drawningAirplane.EntityAirplane == + null) + { + return null; + } + return new ObjectParameters(_drawningAirplane.GetPosX, + _drawningAirplane.GetPosY, _drawningAirplane.GetWidth, _drawningAirplane.GetHeight); + } + } + public int GetStep => (int)(_drawningAirplane?.EntityAirplane?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => + _drawningAirplane?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => + _drawningAirplane?.MoveTransport(direction); + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/EntityAirplaneWithRadar.cs b/AirplaneWithRadar/AirplaneWithRadar/EntityAirplaneWithRadar.cs index 183b517..1c29893 100644 --- a/AirplaneWithRadar/AirplaneWithRadar/EntityAirplaneWithRadar.cs +++ b/AirplaneWithRadar/AirplaneWithRadar/EntityAirplaneWithRadar.cs @@ -34,7 +34,7 @@ namespace AirplaneWithRadar.Entities /// Признак наличия доп.бака /// Признак наличия штыря public EntityAirplaneWithRadar(int speed, double weight, Color bodyColor, Color - additionalColor, bool radar, bool tank, bool pin) + additionalColor, bool radar, bool tank, bool pin) : base (speed, weight, bodyColor, additionalColor) { Radar = radar; Tank = tank; diff --git a/AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs b/AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs new file mode 100644 index 0000000..667f747 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/IMoveableObject.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AirplaneWithRadar; + + +namespace AirplaneWithRadar.MovementStrategy +{ + /// + /// Интерфейс для работы с перемещаемым объектом + /// + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs b/AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs new file mode 100644 index 0000000..531f560 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/MoveToBorder.cs @@ -0,0 +1,57 @@ +using AirplaneWithRadar.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.MovementStrategy +{ + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal <= FieldWidth && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth && + objParams.ObjectMiddleVertical <= FieldHeight && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs b/AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs new file mode 100644 index 0000000..1dec3dd --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/MoveToCenter.cs @@ -0,0 +1,57 @@ +using AirplaneWithRadar.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.MovementStrategy +{ + public class MoveToCenter : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectMiddleHorizontal <= FieldWidth / 2 && + objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 && + objParams.ObjectMiddleVertical <= FieldHeight / 2 && + objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectMiddleVertical - FieldHeight / 2; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs b/AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs new file mode 100644 index 0000000..dbe9416 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/ObjectParameters.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.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; + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина + /// Высота + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + + } +} diff --git a/AirplaneWithRadar/AirplaneWithRadar/Status.cs b/AirplaneWithRadar/AirplaneWithRadar/Status.cs new file mode 100644 index 0000000..98eb831 --- /dev/null +++ b/AirplaneWithRadar/AirplaneWithRadar/Status.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AirplaneWithRadar.MovementStrategy +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +}