From e5c15c66a15948d087e7fbad9ac841dbf5114486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Mon, 27 Nov 2023 21:19:11 +0400 Subject: [PATCH 01/13] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0,=20=D1=85?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D1=8F=D1=89=D0=B5=D0=B3=D0=BE=20=D0=BF=D0=B0?= =?UTF-8?q?=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=D1=8B-=D0=BA=D0=BE=D0=BE?= =?UTF-8?q?=D1=80=D0=B4=D0=B8=D0=BD=D0=B0=D1=82=D1=8B=20=D0=BE=D0=B1=D1=8A?= =?UTF-8?q?=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/ObjectParameters.cs | 57 +++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Sailboat/Sailboat/ObjectParameters.cs diff --git a/Sailboat/Sailboat/ObjectParameters.cs b/Sailboat/Sailboat/ObjectParameters.cs new file mode 100644 index 0000000..3c121b0 --- /dev/null +++ b/Sailboat/Sailboat/ObjectParameters.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.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; + } + } +} \ No newline at end of file -- 2.25.1 From 3113f2defd1060fdbb3535b7b6791c037f90535a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Mon, 27 Nov 2023 21:23:17 +0400 Subject: [PATCH 02/13] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0,=20=D1=85?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D1=8F=D1=89=D0=B5=D0=B3=D0=BE=20=D0=B2=D0=BE?= =?UTF-8?q?=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D1=8B=D0=B5=20=D1=81=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D0=BE=D1=8F=D0=BD=D0=B8=D1=8F=20=D0=BF=D1=80=D0=BE=D1=86?= =?UTF-8?q?=D0=B5=D1=81=D1=81=D0=B0=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5?= =?UTF-8?q?=D1=89=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/Status.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Sailboat/Sailboat/Status.cs diff --git a/Sailboat/Sailboat/Status.cs b/Sailboat/Sailboat/Status.cs new file mode 100644 index 0000000..e2464e2 --- /dev/null +++ b/Sailboat/Sailboat/Status.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.MovementStrategy +{ + /// + /// Статус выполнения операции перемещения + /// + public enum Status + { + NotInit, + InProgress, + Finish + } +} \ No newline at end of file -- 2.25.1 From 803810eb19b61ce9a31eeff43de80e71fe79866b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Mon, 27 Nov 2023 21:26:53 +0400 Subject: [PATCH 03/13] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5=D0=B9?= =?UTF-8?q?=D1=81=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D1=8B=20=D1=81=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5?= =?UTF-8?q?=D1=89=D0=B0=D0=B5=D0=BC=D1=8B=D0=BC=20=D0=BE=D0=B1=D1=8A=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/IMoveableObject.cs | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Sailboat/Sailboat/IMoveableObject.cs diff --git a/Sailboat/Sailboat/IMoveableObject.cs b/Sailboat/Sailboat/IMoveableObject.cs new file mode 100644 index 0000000..06cf7f2 --- /dev/null +++ b/Sailboat/Sailboat/IMoveableObject.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; + +namespace Sailboat.MovementStrategy +{ + /// + /// Интерфейс для работы с перемещаемым объектом + /// + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} -- 2.25.1 From 7a89f52f40d5192b39763f18b740e9e6a43ed2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Mon, 27 Nov 2023 21:30:33 +0400 Subject: [PATCH 04/13] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B0=D0=B1=D1=81=D1=82=D1=80=D0=B0=D0=BA=D1=82?= =?UTF-8?q?=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0?= =?UTF-8?q?,=20=D0=BE=D0=BF=D0=B8=D1=81=D1=8B=D0=B2=D0=B0=D1=8E=D1=89?= =?UTF-8?q?=D0=B5=D0=B3=D0=BE=20=D1=81=D1=82=D1=80=D0=B0=D1=82=D0=B5=D0=B3?= =?UTF-8?q?=D0=B8=D1=8E=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D1=89=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/AbstractStrategy.cs | 135 ++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 Sailboat/Sailboat/AbstractStrategy.cs diff --git a/Sailboat/Sailboat/AbstractStrategy.cs b/Sailboat/Sailboat/AbstractStrategy.cs new file mode 100644 index 0000000..95de1f1 --- /dev/null +++ b/Sailboat/Sailboat/AbstractStrategy.cs @@ -0,0 +1,135 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; +namespace Sailboat.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; + } + } +} \ No newline at end of file -- 2.25.1 From 72c17b847d77f4703d2a4971148560c502a3177f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Mon, 27 Nov 2023 21:36:15 +0400 Subject: [PATCH 05/13] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=B1?= =?UTF-8?q?=D0=B0=D0=B7=D0=BE=D0=B2=D0=BE=D0=B9=20=D1=81=D1=83=D1=89=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D0=B8=20=D1=81=20=D0=BA=D0=BE=D0=BD=D1=81?= =?UTF-8?q?=D1=82=D1=80=D1=83=D0=BA=D1=82=D0=BE=D1=80=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/EntityBoat.cs | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Sailboat/Sailboat/EntityBoat.cs diff --git a/Sailboat/Sailboat/EntityBoat.cs b/Sailboat/Sailboat/EntityBoat.cs new file mode 100644 index 0000000..53f3dca --- /dev/null +++ b/Sailboat/Sailboat/EntityBoat.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.Entities +{ + /// + /// Класс-сущность "Лодка" + /// + public class EntityBoat + { + /// + /// Скорость + /// + public int Speed { get; private set; } + + /// + /// Вес + /// + public double Weight { get; private set; } + + /// + /// Основной цвет + /// + public Color BodyColor { get; private set; } + + /// + /// Шаг перемещения лодки + /// + public double Step => (double)Speed * 100 / Weight; + + /// + /// Конструктор с параметрами + /// + /// Скорость + /// Вес лодки + /// Основной цвет + public EntityBoat(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} \ No newline at end of file -- 2.25.1 From 080dd52d11fe24528d4436a6b56d274eec030905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Mon, 27 Nov 2023 21:46:16 +0400 Subject: [PATCH 06/13] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=80=D0=B8=D1=81=D0=BE=D0=B2=D0=BA=D0=B8=20=D0=B1?= =?UTF-8?q?=D0=B0=D0=B7=D0=BE=D0=B2=D0=BE=D0=B9=20=D1=81=D1=83=D1=89=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/DrawingBoat.cs | 210 +++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 Sailboat/Sailboat/DrawingBoat.cs diff --git a/Sailboat/Sailboat/DrawingBoat.cs b/Sailboat/Sailboat/DrawingBoat.cs new file mode 100644 index 0000000..0c73766 --- /dev/null +++ b/Sailboat/Sailboat/DrawingBoat.cs @@ -0,0 +1,210 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.Entities; + +namespace Sailboat.DrawingObjects +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawingBoat + { + /// + /// Класс-сущность + /// + public EntityBoat? EntityBoat { get; protected set; } + + /// + /// Ширина окна + /// + private int _pictureWidth; + + /// + /// Высота окна + /// + private int _pictureHeight; + + /// + /// Левая координата прорисовки лодки + /// + protected int _startPosX; + + /// + /// Верхняя кооридната прорисовки лодки + /// + protected int _startPosY; + + /// + /// Верхняя кооридната прорисовки лодки + /// + private readonly int _boatWidth = 185; + + /// + /// Высота прорисовки лодки + /// + private readonly int _boatHeight = 160; + + public int GetPosX => _startPosX; + public int GetPosY => _startPosY; + public int GetWidth => _boatWidth; + public int GetHeight => _boatHeight; + + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + public DrawingBoat(int speed, double weight, Color bodyColor, int width, int height) + { + if (width < _boatWidth || height < _boatHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + EntityBoat = new EntityBoat(speed, weight, bodyColor); + } + + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки лодки + /// Высота прорисовки лодки + protected DrawingBoat(int speed, double weight, Color bodyColor, int width, int height, int boatWidth, int boatHeight) + { + if (width < _boatWidth || height < _boatHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _boatWidth = boatWidth; + _boatHeight = boatHeight; + EntityBoat = new EntityBoat(speed, weight, bodyColor); + } + + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + if (x < 0 || x + _boatWidth > _pictureWidth) + { + x = 0; + } + if (y < 0 || y + _boatHeight > _pictureHeight) + { + y = 0; + } + _startPosX = x; + _startPosY = y; + } + + public bool CanMove(DirectionType direction) + { + if (EntityBoat == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityBoat.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityBoat.Step > 0, + // вправо + DirectionType.Right => _startPosX + EntityBoat.Step < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + EntityBoat.Step < _pictureHeight, + _ => false + }; + } + + /// + /// Изменение направления перемещения + /// + /// Направление + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || EntityBoat == null) + { + return; + } + switch (direction) + { + case DirectionType.Left: + if (_startPosX - EntityBoat.Step > 0) + { + _startPosX -= (int)EntityBoat.Step; + } + break; + case DirectionType.Up: + if (_startPosY - EntityBoat.Step > 0) + { + _startPosY -= (int)EntityBoat.Step; + } + break; + case DirectionType.Right: + if (_startPosX + EntityBoat.Step + _boatWidth < _pictureWidth) + { + _startPosX += (int)EntityBoat.Step; + } + break; + case DirectionType.Down: + if (_startPosY + EntityBoat.Step + _boatHeight < _pictureHeight) + { + _startPosY += (int)EntityBoat.Step; + } + break; + } + } + + /// + /// Прорисовка объекта + /// + /// + public virtual void DrawTransport(Graphics g) + { + if (EntityBoat == null) + { + return; + } + Pen pen = new(Color.Black); + + // Основной корпус парусника + Brush Brush = new + SolidBrush(EntityBoat.BodyColor); + + Point[] hull = new Point[] + { + new Point(_startPosX + 5, _startPosY + 90), + new Point(_startPosX + 120, _startPosY + 90), + new Point(_startPosX + 180, _startPosY + 120), + new Point(_startPosX + 120, _startPosY + 150), + new Point(_startPosX + 5, _startPosY + 150) + }; + g.FillPolygon(Brush, hull); + g.DrawPolygon(pen, hull); + + Brush addBrush = new + SolidBrush(Color.Green); + + g.FillEllipse(addBrush, _startPosX + 20, _startPosY + 100, 100, 40); + g.DrawEllipse(pen, _startPosX + 20, _startPosY + 100, 100, 40); + } + } +} -- 2.25.1 From ba3b017e155e0349001376a6149d2bb5c98e2ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Mon, 27 Nov 2023 22:11:30 +0400 Subject: [PATCH 07/13] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0?= =?UTF-8?q?-=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20=C2=AB?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B4=D0=B2=D0=B8=D0=BD=D1=83=D1=82=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=C2=BB=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/EntitySailboat.cs | 33 +++++++++-------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/Sailboat/Sailboat/EntitySailboat.cs b/Sailboat/Sailboat/EntitySailboat.cs index b9e4614..4568ffe 100644 --- a/Sailboat/Sailboat/EntitySailboat.cs +++ b/Sailboat/Sailboat/EntitySailboat.cs @@ -4,38 +4,28 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Sailboat +namespace Sailboat.Entities { - public class EntitySailboat + /// + /// Класс-сущность "Парусная лодка" + /// + public class EntitySailboat : EntityBoat { - /// - /// Скорость - /// - public int Speed { get; private set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } /// /// Дополнительный цвет (для опциональных элементов) /// public Color AdditionalColor { get; private set; } + /// /// Признак (опция) наличия усиленного корпуса /// public bool Hull { get; private set; } + /// /// Признак (опция) наличия паруса /// public bool Sail { get; private set; } - /// - /// Шаг перемещения лодки - /// - public double Step => (double)Speed * 100 / Weight; + /// /// Инициализация полей объекта-класса парусной лодки /// @@ -45,12 +35,9 @@ namespace Sailboat /// Дополнительный цвет /// Признак наличия усиленного корпуса /// Признак наличия паруса - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool hull, bool sail) + public EntitySailboat(int speed, double weight, Color bodyColor, Color + additionalColor, bool hull, bool sail) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; Hull = hull; Sail = sail; -- 2.25.1 From 92af89780493c3567c37f9e665e6f6937291f7de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Mon, 27 Nov 2023 22:21:26 +0400 Subject: [PATCH 08/13] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D1=80=D0=B8=D1=81=D0=BE=D0=B2=D0=BA=D0=B8?= =?UTF-8?q?=20=C2=AB=D0=BF=D1=80=D0=BE=D0=B4=D0=B2=D0=B8=D0=BD=D1=83=D1=82?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=C2=BB=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/DrawningSailboat.cs | 170 ++++---------------------- 1 file changed, 22 insertions(+), 148 deletions(-) diff --git a/Sailboat/Sailboat/DrawningSailboat.cs b/Sailboat/Sailboat/DrawningSailboat.cs index 01b6837..c9dda29 100644 --- a/Sailboat/Sailboat/DrawningSailboat.cs +++ b/Sailboat/Sailboat/DrawningSailboat.cs @@ -1,157 +1,51 @@ using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Sailboat +using Sailboat.Entities; + +namespace Sailboat.DrawingObjects { /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// - public class DrawingSailboat + public class DrawingSailboat : DrawingBoat { /// - /// Класс-сущность - /// - public EntitySailboat? EntitySailboat { get; private set; } - - /// - /// Ширина окна - /// - private int _pictureWidth; - - /// - /// Высота окна - /// - private int _pictureHeight; - - /// - /// Левая координата прорисовки лодки - /// - private int _startPosX; - - /// - /// Верхняя координата прорисовки лодки - /// - private int _startPosY; - - /// - /// Ширина прорисовки лодки - /// - private readonly int _boatWidth = 185; - - /// - /// Высота прорисовки лодки - /// - private readonly int _boatHeight = 160; - - /// - /// Инициализация свойств + /// Конструктор /// /// Скорость /// Вес - /// Цвет кузова + /// Основной цвет /// Дополнительный цвет - /// Признак наличия усиленного корпуса парусника - /// Признак наличия основного корпуса парусника + /// Признак наличия усиленного корпуса /// Признак наличия паруса /// Ширина картинки /// Высота картинки - /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах - public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool hullCooler, bool hull, bool sail, int width, int height) + public DrawingSailboat(int speed, double weight, Color bodyColor, Color additionalColor, bool hull, bool sail, int width, int height) : + base(speed, weight, bodyColor, width, height, 200, 160) { - // Проверки - if (width < _boatWidth || height < _boatHeight) + if (EntityBoat != null) { - return false; + EntityBoat = new EntitySailboat(speed, weight, bodyColor, + additionalColor, hull, sail); } - _pictureWidth = width; - _pictureHeight = height; - EntitySailboat = new EntitySailboat(); - EntitySailboat.Init(speed, weight, bodyColor, additionalColor, hull, sail); - return true; } - - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) + public override void DrawTransport(Graphics g) { - // Изменение x, y - if (x < 0 || x + _boatWidth > _pictureWidth) - { - x = 0; - } - if (y < 0 || y + _boatHeight > _pictureHeight) - { - y = 0; - } - _startPosX = x; - _startPosY = y; - } - - /// - /// Изменение направления перемещения - /// - /// Направление - public void MoveTransport(DirectionType direction) - { - if (EntitySailboat == null) + if (EntityBoat is not EntitySailboat sailboat) { return; } - switch (direction) - { - //влево - case DirectionType.Left: - if (_startPosX - EntitySailboat.Step > 0) - { - _startPosX -= (int)EntitySailboat.Step; - } - break; - //вверх - case DirectionType.Up: - if (_startPosY - EntitySailboat.Step > 0) - { - _startPosY -= (int)EntitySailboat.Step; - } - break; - //вправо - case DirectionType.Right: - if (_startPosX + EntitySailboat.Step + _boatWidth < _pictureWidth) - { - _startPosX += (int)EntitySailboat.Step; - } - break; - //вниз - case DirectionType.Down: - if (_startPosY + EntitySailboat.Step + _boatHeight < _pictureHeight) - { - _startPosY += (int)EntitySailboat.Step; - } - break; - } - } - - /// - /// Прорисовка объекта - /// - /// - public void DrawTransport(Graphics g) - { - if (EntitySailboat == null) - { - return; - } - Pen pen = new(Color.Black, 4); + Pen pen = new(Color.Black); Brush additionalBrush = new - SolidBrush(EntitySailboat.AdditionalColor); + SolidBrush(sailboat.AdditionalColor); // Усиленный корпус парусника - if (EntitySailboat.Hull) + if (sailboat.Hull) { Point[] hullCooler = new Point[] { @@ -165,38 +59,18 @@ namespace Sailboat g.DrawPolygon(pen, hullCooler); } - - // Основной корпус парусника - Brush Brush = new - SolidBrush(EntitySailboat.BodyColor); - - Point[] hull = new Point[] - { - new Point(_startPosX + 5, _startPosY + 90), - new Point(_startPosX + 120, _startPosY + 90), - new Point(_startPosX + 180, _startPosY + 120), - new Point(_startPosX + 120, _startPosY + 150), - new Point(_startPosX + 5, _startPosY + 150) - }; - g.FillPolygon(Brush, hull); - g.DrawPolygon(pen, hull); - - Brush addBrush = new - SolidBrush(Color.Green); - - g.FillEllipse(addBrush, _startPosX + 20, _startPosY + 100, 100, 40); - g.DrawEllipse(pen, _startPosX + 20, _startPosY + 100, 100, 40); + base.DrawTransport(g); // Парус - if (EntitySailboat.Sail) + if (sailboat.Sail) { Brush sailBrush = new - SolidBrush(EntitySailboat.AdditionalColor); + SolidBrush(sailboat.AdditionalColor); Point[] sail = new Point[] { new Point(_startPosX + 65, _startPosY), - new Point(_startPosX + 150, _startPosY + 120), + new Point(_startPosX + 130, _startPosY + 120), new Point(_startPosX + 15, _startPosY + 120) }; g.FillPolygon(sailBrush, sail); -- 2.25.1 From 810b4f70a6bc7591d911e98e823a070956f7e06e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Mon, 27 Nov 2023 22:33:02 +0400 Subject: [PATCH 09/13] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BD=D0=BE=D0=B2=D1=8B=D1=85=20?= =?UTF-8?q?=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=BE=D0=B2=20=D0=BA=D0=BB=D0=B0?= =?UTF-8?q?=D1=81=D1=81=D0=B0=20DrawningBoat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/DrawingBoat.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Sailboat/Sailboat/DrawingBoat.cs b/Sailboat/Sailboat/DrawingBoat.cs index 0c73766..b004686 100644 --- a/Sailboat/Sailboat/DrawingBoat.cs +++ b/Sailboat/Sailboat/DrawingBoat.cs @@ -48,9 +48,21 @@ namespace Sailboat.DrawingObjects /// private readonly int _boatHeight = 160; + /// + /// Координата X объекта + /// public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// public int GetWidth => _boatWidth; + /// + /// Высота объекта + /// public int GetHeight => _boatHeight; /// @@ -114,6 +126,11 @@ namespace Sailboat.DrawingObjects _startPosY = y; } + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению public bool CanMove(DirectionType direction) { if (EntityBoat == null) @@ -146,24 +163,28 @@ namespace Sailboat.DrawingObjects } switch (direction) { + //влево case DirectionType.Left: if (_startPosX - EntityBoat.Step > 0) { _startPosX -= (int)EntityBoat.Step; } break; + //вверх case DirectionType.Up: if (_startPosY - EntityBoat.Step > 0) { _startPosY -= (int)EntityBoat.Step; } break; + //вправо case DirectionType.Right: if (_startPosX + EntityBoat.Step + _boatWidth < _pictureWidth) { _startPosX += (int)EntityBoat.Step; } break; + //вниз case DirectionType.Down: if (_startPosY + EntityBoat.Step + _boatHeight < _pictureHeight) { -- 2.25.1 From a52c951843037f80da3590f9f8a0caa82e12dab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Mon, 27 Nov 2023 22:36:02 +0400 Subject: [PATCH 10/13] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20DrawningO?= =?UTF-8?q?bjectBoat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/DrawingObjectBoat.cs | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Sailboat/Sailboat/DrawingObjectBoat.cs diff --git a/Sailboat/Sailboat/DrawingObjectBoat.cs b/Sailboat/Sailboat/DrawingObjectBoat.cs new file mode 100644 index 0000000..7aaaa0d --- /dev/null +++ b/Sailboat/Sailboat/DrawingObjectBoat.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using Sailboat.DrawingObjects; + +namespace Sailboat.MovementStrategy +{ + /// + /// Реализация интерфейса IDrawningObject для работы с объектом DrawningBoat (паттерн Adapter) + /// + public class DrawingObjectBoat : IMoveableObject + { + private readonly DrawingBoat? _drawingBoat = null; + public DrawingObjectBoat(DrawingBoat drawingBoat) + { + _drawingBoat = drawingBoat; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawingBoat == null || _drawingBoat.EntityBoat == + null) + { + return null; + } + return new ObjectParameters(_drawingBoat.GetPosX, _drawingBoat.GetPosY, _drawingBoat.GetWidth, _drawingBoat.GetHeight); + } + } + public int GetStep => (int)(_drawingBoat?.EntityBoat?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => + _drawingBoat?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => + _drawingBoat?.MoveTransport(direction); + + } +} \ No newline at end of file -- 2.25.1 From e8379ecea1317cdd1b006a36422c57f62d0bd025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Mon, 27 Nov 2023 22:50:10 +0400 Subject: [PATCH 11/13] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=B0=D0=B1=D1=81=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D0=BA=D1=82=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B0=20AbstractStrategy.=20=D0=94=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=B5=D0=B3=D0=B8=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC?= =?UTF-8?q?=D0=B5=D1=89=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BE=D0=B1=D1=8A=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=B0=20=D0=B2=20=D1=86=D0=B5=D0=BD=D1=82=D1=80=20?= =?UTF-8?q?=D1=8D=D0=BA=D1=80=D0=B0=D0=BD=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/MoveToCenter.cs | 59 +++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Sailboat/Sailboat/MoveToCenter.cs diff --git a/Sailboat/Sailboat/MoveToCenter.cs b/Sailboat/Sailboat/MoveToCenter.cs new file mode 100644 index 0000000..02fc9e7 --- /dev/null +++ b/Sailboat/Sailboat/MoveToCenter.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.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(); + } + } + } + } +} \ No newline at end of file -- 2.25.1 From 054f03921cdc49990bc5784c5a2c8cfc2233f3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Mon, 27 Nov 2023 22:52:59 +0400 Subject: [PATCH 12/13] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=B0=D0=B1=D1=81=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D0=BA=D1=82=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B0=20AbstractStrategy.=20=D0=94=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=B5=D0=B3=D0=B8=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC?= =?UTF-8?q?=D0=B5=D1=89=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BE=D0=B1=D1=8A=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=B0=20=D0=BA=20=D0=BA=D1=80=D0=B0=D1=8E=20=D1=8D?= =?UTF-8?q?=D0=BA=D1=80=D0=B0=D0=BD=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/MoveToBorder.cs | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Sailboat/Sailboat/MoveToBorder.cs diff --git a/Sailboat/Sailboat/MoveToBorder.cs b/Sailboat/Sailboat/MoveToBorder.cs new file mode 100644 index 0000000..40c22ca --- /dev/null +++ b/Sailboat/Sailboat/MoveToBorder.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sailboat.MovementStrategy +{ + /// + /// Стратегия перемещения объекта к краю экрана + /// + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var 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() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.RightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.DownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} \ No newline at end of file -- 2.25.1 From b7e22a4fd50b044db5b26e8561f6f246bb318bfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BE=D1=84=D1=8C=D1=8F=20=D0=AF=D0=BA=D0=BE=D0=B1?= =?UTF-8?q?=D1=87=D1=83=D0=BA?= Date: Tue, 28 Nov 2023 00:23:37 +0400 Subject: [PATCH 13/13] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BD=D0=BE=D0=BF=D0=BE=D0=BA?= =?UTF-8?q?.=20=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B8=20=D1=84=D0=BE?= =?UTF-8?q?=D1=80=D0=BC=D1=8B.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sailboat/Sailboat/FormSailboat.Designer.cs | 64 +++++++++--- Sailboat/Sailboat/FormSailboat.cs | 116 +++++++++++++++++---- 2 files changed, 147 insertions(+), 33 deletions(-) diff --git a/Sailboat/Sailboat/FormSailboat.Designer.cs b/Sailboat/Sailboat/FormSailboat.Designer.cs index 53dab1c..4b435b1 100644 --- a/Sailboat/Sailboat/FormSailboat.Designer.cs +++ b/Sailboat/Sailboat/FormSailboat.Designer.cs @@ -33,7 +33,10 @@ buttonRight = new Button(); buttonDown = new Button(); buttonLeft = new Button(); - buttonCreate = new Button(); + buttonCreateBoat = new Button(); + buttonCreateSailboat = new Button(); + comboBoxStrategy = new ComboBox(); + buttonStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxSailboat).BeginInit(); SuspendLayout(); // @@ -95,23 +98,57 @@ buttonLeft.UseVisualStyleBackColor = true; buttonLeft.Click += buttonMove_Click; // - // buttonCreate + // buttonCreateBoat // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(12, 412); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(94, 29); - buttonCreate.TabIndex = 5; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; + buttonCreateBoat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateBoat.Location = new Point(149, 391); + buttonCreateBoat.Name = "buttonCreateBoat"; + buttonCreateBoat.Size = new Size(131, 50); + buttonCreateBoat.TabIndex = 5; + buttonCreateBoat.Text = "Создать лодку"; + buttonCreateBoat.UseVisualStyleBackColor = true; + buttonCreateBoat.Click += buttonCreateBoat_Click; + // + // buttonCreateSailboat + // + buttonCreateSailboat.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreateSailboat.Location = new Point(12, 391); + buttonCreateSailboat.Name = "buttonCreateSailboat"; + buttonCreateSailboat.Size = new Size(131, 50); + buttonCreateSailboat.TabIndex = 6; + buttonCreateSailboat.Text = "Создать парусную лодку"; + buttonCreateSailboat.UseVisualStyleBackColor = true; + buttonCreateSailboat.Click += buttonCreateSailboat_Click; + // + // comboBoxStrategy + // + comboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxStrategy.FormattingEnabled = true; + comboBoxStrategy.Items.AddRange(new object[] { "До центра", "До края" }); + comboBoxStrategy.Location = new Point(719, 12); + comboBoxStrategy.Name = "comboBoxStrategy"; + comboBoxStrategy.Size = new Size(151, 28); + comboBoxStrategy.TabIndex = 7; + // + // buttonStep + // + buttonStep.Location = new Point(776, 46); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(94, 29); + buttonStep.TabIndex = 8; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += buttonStep_Click; // // FormSailboat // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(882, 453); - Controls.Add(buttonCreate); + Controls.Add(buttonStep); + Controls.Add(comboBoxStrategy); + Controls.Add(buttonCreateSailboat); + Controls.Add(buttonCreateBoat); Controls.Add(buttonLeft); Controls.Add(buttonDown); Controls.Add(buttonRight); @@ -132,6 +169,9 @@ private Button buttonRight; private Button buttonDown; private Button buttonLeft; - private Button buttonCreate; + private Button buttonCreateBoat; + private Button buttonCreateSailboat; + private ComboBox comboBoxStrategy; + private Button buttonStep; } } \ No newline at end of file diff --git a/Sailboat/Sailboat/FormSailboat.cs b/Sailboat/Sailboat/FormSailboat.cs index d74960e..597a952 100644 --- a/Sailboat/Sailboat/FormSailboat.cs +++ b/Sailboat/Sailboat/FormSailboat.cs @@ -1,4 +1,6 @@ -using System; +using Sailboat.DrawingObjects; +using Sailboat.MovementStrategy; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -10,47 +12,78 @@ using System.Windows.Forms; namespace Sailboat { + /// + /// Форма работы с объектом "Парусная лодка" + /// public partial class FormSailboat : Form { - private DrawingSailboat? _drawingSailboat; - private EntitySailboat? _entitySailboat; + /// + /// Поле-объект для прорисовки объекта + /// + private DrawingBoat? _drawingBoat; + private AbstractStrategy? _abstractStrategy; + /// + /// Инициализация формы + /// public FormSailboat() { InitializeComponent(); } + /// + /// Метод прорисовки лодки + /// private void Draw() { - if (_drawingSailboat == null) + if (_drawingBoat == null) { return; } Bitmap bmp = new(pictureBoxSailboat.Width, pictureBoxSailboat.Height); Graphics gr = Graphics.FromImage(bmp); - _drawingSailboat.DrawTransport(gr); + _drawingBoat.DrawTransport(gr); pictureBoxSailboat.Image = bmp; } - private void buttonCreate_Click(object sender, EventArgs e) + /// + /// Обработка нажатия кнопки "Создать лодку" + /// + /// + /// + private void buttonCreateBoat_Click(object sender, EventArgs e) { Random random = new(); - _drawingSailboat = new DrawingSailboat(); - - _drawingSailboat.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)), - pictureBoxSailboat.Width, pictureBoxSailboat.Height); - - _drawingSailboat.SetPosition(random.Next(10, 100), random.Next(10, 100)); + _drawingBoat = new DrawingBoat(random.Next(100, 300), random.Next(1000, 3000), Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxSailboat.Width, pictureBoxSailboat.Height); + _drawingBoat.SetPosition(random.Next(10, 100), random.Next(10, + 100)); Draw(); } + /// + /// Обработка нажатия кнопки "Создать улучшеную лодку" + /// + /// + /// + private void buttonCreateSailboat_Click(object sender, EventArgs e) + { + Random random = new(); + _drawingBoat = new DrawingSailboat(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)), pictureBoxSailboat.Width, pictureBoxSailboat.Height); + _drawingBoat.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + /// + /// Изменение размеров формы + /// + /// + /// private void buttonMove_Click(object sender, EventArgs e) { - if (_drawingSailboat == null) + if (_drawingBoat == null) { return; } @@ -58,19 +91,60 @@ namespace Sailboat switch (name) { case "buttonUp": - _drawingSailboat.MoveTransport(DirectionType.Up); + _drawingBoat.MoveTransport(DirectionType.Up); break; case "buttonDown": - _drawingSailboat.MoveTransport(DirectionType.Down); + _drawingBoat.MoveTransport(DirectionType.Down); break; case "buttonLeft": - _drawingSailboat.MoveTransport(DirectionType.Left); + _drawingBoat.MoveTransport(DirectionType.Left); break; case "buttonRight": - _drawingSailboat.MoveTransport(DirectionType.Right); + _drawingBoat.MoveTransport(DirectionType.Right); break; } Draw(); } + + /// + /// Обработка нажатия кнопки "Шаг" + /// + /// + /// + private void buttonStep_Click(object sender, EventArgs e) + { + if (_drawingBoat == null) + { + return; + } + if (comboBoxStrategy.Enabled) + { + _abstractStrategy = comboBoxStrategy.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new DrawingObjectBoat(_drawingBoat), pictureBoxSailboat.Width, + pictureBoxSailboat.Height); + comboBoxStrategy.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBoxStrategy.Enabled = true; + _abstractStrategy = null; + } + } } -} +} \ No newline at end of file -- 2.25.1