diff --git a/ProjectSeaplane/ProjectSeaplane/AbstractStrategy.cs b/ProjectSeaplane/ProjectSeaplane/AbstractStrategy.cs new file mode 100644 index 0000000..8901197 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/AbstractStrategy.cs @@ -0,0 +1,137 @@ +using ProjectSeaplane.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static ProjectSeaplane.Direction; + + +namespace ProjectSeaplane.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/ProjectSeaplane/ProjectSeaplane/Directions.cs b/ProjectSeaplane/ProjectSeaplane/Directions.cs index b58fc9f..14978d8 100644 --- a/ProjectSeaplane/ProjectSeaplane/Directions.cs +++ b/ProjectSeaplane/ProjectSeaplane/Directions.cs @@ -6,26 +6,29 @@ using System.Threading.Tasks; namespace ProjectSeaplane { - /// - /// Направление перемещения - /// - public enum DirectionType + public class Direction { /// - /// Вверх + /// Направление перемещения /// - Up = 1, - /// - /// Вниз - /// - Down = 2, - /// - /// Влево - /// - Left = 3, - /// - /// Вправо - /// - Right = 4 + public enum DirectionType + { + /// + /// Вверх + /// + Up = 1, + /// + /// Вниз + /// + Down = 2, + /// + /// Влево + /// + Left = 3, + /// + /// Вправо + /// + Right = 4 + } } } diff --git a/ProjectSeaplane/ProjectSeaplane/DrawingPlane.cs b/ProjectSeaplane/ProjectSeaplane/DrawingPlane.cs new file mode 100644 index 0000000..92a916e --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/DrawingPlane.cs @@ -0,0 +1,243 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectSeaplane.Entities; +using static ProjectSeaplane.Direction; +namespace ProjectSeaplane.DrawningObjects +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawningPlane + { + /// + /// Класс-сущность + /// + public EntityPlane? EntityPlane { get; protected set; } + /// + /// Ширина окна + /// + private int _pictureWidth; + /// + /// Высота окна + /// + private int _pictureHeight; + /// + /// Левая координата прорисовки автомобиля + /// + protected int _startPosX; + /// + /// Верхняя кооридната прорисовки автомобиля + /// + protected int _startPosY; + /// + /// Ширина прорисовки автомобиля + /// + private int _planeWidth = 200; + /// + /// Высота прорисовки автомобиля + /// + private int _planeHeight = 90; + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + public DrawningPlane(int speed, double weight, Color bodyColor, int width, int height) + { + // TODO: Продумать проверки + + if (width > _planeWidth && height > _planeHeight) + { + _pictureWidth = width; + _pictureHeight = height; + EntityPlane = new EntityPlane(speed, weight, bodyColor); + } + } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки автомобиля + /// Высота прорисовки автомобиля + protected DrawningPlane(int speed, double weight, Color bodyColor, int width, int height, int planeWidth, int planeHeight) + { + // TODO: Продумать проверки + + if (width > _planeWidth && height > _planeHeight) + { + _pictureWidth = width; + _pictureHeight = height; + _planeWidth = planeWidth; + _planeHeight = planeHeight; + EntityPlane = new EntityPlane(speed, weight, bodyColor); + } + + + } + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + if ((x > 0) && (x < _pictureWidth - _planeWidth)) + _startPosX = x; + else _startPosX = 0; + if ((y > 0) && (y < _pictureHeight - _planeHeight)) + _startPosY = y; + + else _startPosY = 0; + _startPosX = x; + _startPosY = y; + } + + /// + /// Координата X объекта + /// + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _planeWidth; + /// + /// Высота объекта + /// + public int GetHeight => _planeHeight; + + /// + /// Проверка, что объект может переместится по указанному направлению + /// + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(DirectionType direction) + { + if (EntityPlane == null) + { + return false; + } + return direction switch + { + //влево + DirectionType.Left => _startPosX - EntityPlane.Step > 0, + //вверх + DirectionType.Up => _startPosY - EntityPlane.Step > 0, + // вправо + DirectionType.Right => _startPosX + _planeWidth + EntityPlane.Step < _pictureWidth, + //вниз + DirectionType.Down => _startPosY + _planeHeight + EntityPlane.Step < _pictureHeight, + }; + + } + /// + /// Изменение направления перемещения + /// + /// Направление + public void MoveTransport(DirectionType direction) + { + if (!CanMove(direction) || EntityPlane == null) + { + return; + } + switch (direction) + { + //влево + case DirectionType.Left: + _startPosX -= (int)EntityPlane.Step; + break; + //вверх + case DirectionType.Up: + _startPosY -= (int)EntityPlane.Step; + break; + // вправо + case DirectionType.Right: + _startPosX += (int)EntityPlane.Step; + break; + //вниз + case DirectionType.Down: + _startPosY += (int)EntityPlane.Step; + break; + + } + } + + /// + /// Прорисовка объекта + /// + /// + public virtual void DrawTransport(Graphics g) + { + if (EntityPlane == null) + { + return; + } + + Brush BodyBrush = new SolidBrush(EntityPlane.BodyColor); + Pen pen = new(Color.Black); + + g.DrawLine(pen, _startPosX + 10, _startPosY, _startPosX + 10, _startPosY + 30); + g.DrawLine(pen, _startPosX + 10, _startPosY, _startPosX + 40, _startPosY + 30); + g.DrawLine(pen, _startPosX + 10, _startPosY + 30, _startPosX + 150, _startPosY + 30); + g.DrawLine(pen, _startPosX + 150, _startPosY + 30, _startPosX + 150, _startPosY + 80); + g.DrawLine(pen, _startPosX + 10, _startPosY + 80, _startPosX + 150, _startPosY + 80); + g.DrawLine(pen, _startPosX + 150, _startPosY + 30, _startPosX + 200, _startPosY + 55); + g.DrawLine(pen, _startPosX + 200, _startPosY + 55, _startPosX + 150, _startPosY + 55); + g.DrawLine(pen, _startPosX + 200, _startPosY + 55, _startPosX + 150, _startPosY + 80); + g.DrawPie(pen, _startPosX, _startPosY + 30, 20, 50, 90, 180); + g.DrawEllipse(pen, _startPosX, _startPosY + 25, 35, 10); + g.DrawEllipse(pen, _startPosX + 40, _startPosY + 50, 80, 10); + + //вертикальная стабилизация + Brush mainBrush = new SolidBrush(EntityPlane.BodyColor); + Point point7 = new Point(_startPosX + 11, _startPosY + 1); + Point point8 = new Point(_startPosX + 11, _startPosY + 31); + Point point9 = new Point(_startPosX + 39, _startPosY + 31); + Point[] vert_stab = { point7, point8, point9, point7 }; + g.FillPolygon(mainBrush, vert_stab); + + //хвостовая часть + g.FillPie(mainBrush, _startPosX, _startPosY + 30, 20, 50, 90, 180); + //нос + Brush brGray = new SolidBrush(Color.Gray); + Point point1 = new Point(_startPosX + 150, _startPosY + 30); + Point point2 = new Point(_startPosX + 200, _startPosY + 55); + Point point3 = new Point(_startPosX + 150, _startPosY + 55); + Point[] nos1 = { point1, point2, point3, point1 }; + g.FillPolygon(brGray, nos1); + Point point4 = new Point(_startPosX + 150, _startPosY + 55); + Point point5 = new Point(_startPosX + 200, _startPosY + 55); + Point point6 = new Point(_startPosX + 150, _startPosY + 80); + Point[] nos2 = { point4, point5, point6, point4 }; + g.FillPolygon(brGray, nos2); + g.DrawLine(pen, _startPosX + 200, _startPosY + 55, _startPosX + 150, _startPosY + 55); + + //корпус + Point point10 = new Point(_startPosX + 10, _startPosY + 31); + Point point11 = new Point(_startPosX + 149, _startPosY + 31); + Point point12 = new Point(_startPosX + 149, _startPosY + 79); + Point point13 = new Point(_startPosX + 10, _startPosY + 79); + Point[] body = { point10, point11, point12, point13, point10 }; + g.FillPolygon(mainBrush, body); + + //крылья + Brush brBlack = new SolidBrush(Color.Black); + g.FillEllipse(brBlack, _startPosX + 40, _startPosY + 50, 80, 10); + g.FillEllipse(brBlack, _startPosX, _startPosY + 25, 35, 10); + } + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/DrawingSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/DrawingSeaplane.cs index 68a1ff1..3536d6b 100644 --- a/ProjectSeaplane/ProjectSeaplane/DrawingSeaplane.cs +++ b/ProjectSeaplane/ProjectSeaplane/DrawingSeaplane.cs @@ -3,43 +3,15 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ProjectSeaplane.Entities; -namespace ProjectSeaplane +namespace ProjectSeaplane.DrawningObjects { /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// - public class DrawningSeaplane + public class DrawningSeaplane : DrawningPlane { - /// - /// Класс-сущность - /// - public EntitySeaplane? EntitySeaplane { get; private set; } - /// - /// Ширина окна - /// - private int _pictureWidth; - /// - /// Высота окна - /// - private int _pictureHeight; - /// - - /// Левая координата прорисовки автомобиля - /// - private int _startPosX; - /// - /// Верхняя кооридната прорисовки автомобиля - /// - private int _startPosY; - /// - /// Ширина прорисовки автомобиля - /// - private int _seaplaneWidth = 200; - /// - /// Высота прорисовки автомобиля - /// - private int _seaplaneHeight = 110; /// /// Инициализация свойств /// @@ -52,115 +24,32 @@ namespace ProjectSeaplane /// Ширина картинки /// Высота картинки /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах - public bool Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool boat, bool floater, int width, int height) + public DrawningSeaplane(int speed, double weight, Color bodyColor, Color additionalColor, bool boat, bool floater, int width, int height) : + base(speed, weight, bodyColor, width, height, 200, 110) { - if (width < _pictureWidth || height < _pictureHeight) + if (EntityPlane != null) { - return false; - } - if (!floater) - { - _seaplaneHeight = 90; - } - else - { - _seaplaneHeight = 110; - } - _pictureWidth = width; - _pictureHeight = height; - EntitySeaplane = new EntitySeaplane(); - EntitySeaplane.Init(speed, weight, bodyColor, additionalColor, boat, floater); - return true; - } - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) - { - if (x <= _pictureWidth - _seaplaneWidth && y <= _pictureHeight - _seaplaneHeight) - { - _startPosX = x; - _startPosY = y; - } - } - /// - /// Изменение направления перемещения - /// - /// Направление - public void MoveTransport(DirectionType direction) - { - if (EntitySeaplane == null) - { - return; - } - switch (direction) - { - //влево - case DirectionType.Left: - if (_startPosX - EntitySeaplane.Step > 0) - { - _startPosX -= (int)EntitySeaplane.Step; - } - else - { - _startPosX = 0; - } - break; - //вверх - case DirectionType.Up: - if (_startPosY - EntitySeaplane.Step > 0) - { - _startPosY -= (int)EntitySeaplane.Step; - } - else - { - _startPosY = 0; - } - break; - // вправо - case DirectionType.Right: - if (_startPosX + _seaplaneWidth + EntitySeaplane.Step <= _pictureWidth) - { - _startPosX += (int)EntitySeaplane.Step; - } - else - { - _startPosX = _pictureWidth - _seaplaneWidth; - } - break; - //вниз - case DirectionType.Down: - if (_startPosY + _seaplaneHeight + EntitySeaplane.Step <= _pictureHeight) - { - _startPosY += (int)EntitySeaplane.Step; - } - else - { - _startPosY = _pictureHeight - _seaplaneHeight; - } - break; + EntityPlane = new EntitySeaplane(speed, weight, bodyColor, additionalColor, boat, floater); } } + /// /// Прорисовка объекта /// /// - public void DrawTransport(Graphics g) + public override void DrawTransport(Graphics g) { - if (EntitySeaplane == null) + if (EntityPlane is not EntitySeaplane seaPlane) { return; } Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntitySeaplane.AdditionalColor); + Brush additionalBrush = new SolidBrush(seaPlane.AdditionalColor); // лодка - if (EntitySeaplane.Boat) + if (seaPlane.Boat) { g.DrawEllipse(pen, _startPosX + 40, _startPosY, 90, 20); g.DrawLine(pen, _startPosX + 60, _startPosY + 20, _startPosX + 50, _startPosY + 30); @@ -169,58 +58,8 @@ namespace ProjectSeaplane g.DrawLine(pen, _startPosX + 110, _startPosY + 20, _startPosX + 120, _startPosY + 30); g.FillEllipse(additionalBrush, _startPosX + 40, _startPosY, 90, 20); } - // границы гидролета - g.DrawLine(pen, _startPosX + 10, _startPosY, _startPosX + 10, _startPosY + 30); - g.DrawLine(pen, _startPosX + 10, _startPosY, _startPosX + 40, _startPosY + 30); - g.DrawLine(pen, _startPosX + 10, _startPosY + 30, _startPosX + 150, _startPosY + 30); - g.DrawLine(pen, _startPosX + 150, _startPosY + 30, _startPosX + 150, _startPosY + 80); - g.DrawLine(pen, _startPosX + 10, _startPosY + 80, _startPosX + 150, _startPosY + 80); - g.DrawLine(pen, _startPosX + 150, _startPosY + 30, _startPosX + 200, _startPosY + 55); - g.DrawLine(pen, _startPosX + 200, _startPosY + 55, _startPosX + 150, _startPosY + 55); - g.DrawLine(pen, _startPosX + 200, _startPosY + 55, _startPosX + 150, _startPosY + 80); - g.DrawPie(pen, _startPosX, _startPosY + 30, 20, 50, 90, 180); - g.DrawEllipse(pen, _startPosX, _startPosY + 25, 35, 10); - g.DrawEllipse(pen, _startPosX + 40, _startPosY + 50, 80, 10); - - //вертикальная стабилизация - Brush mainBrush = new SolidBrush(EntitySeaplane.BodyColor); - Point point7 = new Point(_startPosX + 11, _startPosY + 1); - Point point8 = new Point(_startPosX + 11, _startPosY + 31); - Point point9 = new Point(_startPosX + 39, _startPosY + 31); - Point[] vert_stab = {point7, point8, point9, point7 }; - g.FillPolygon(mainBrush, vert_stab); - - //хвостовая часть - g.FillPie(mainBrush, _startPosX, _startPosY + 30, 20, 50, 90, 180); - //нос - Brush brGray = new SolidBrush(Color.Gray); - Point point1 = new Point(_startPosX + 150, _startPosY + 30); - Point point2 = new Point(_startPosX + 200, _startPosY + 55); - Point point3 = new Point(_startPosX + 150, _startPosY + 55); - Point[] nos1 = { point1, point2, point3, point1 }; - g.FillPolygon(brGray, nos1); - Point point4 = new Point(_startPosX + 150, _startPosY + 55); - Point point5 = new Point(_startPosX + 200, _startPosY + 55); - Point point6 = new Point(_startPosX + 150, _startPosY + 80); - Point[] nos2 = { point4, point5, point6, point4 }; - g.FillPolygon(brGray, nos2); - g.DrawLine(pen, _startPosX + 200, _startPosY + 55, _startPosX + 150, _startPosY + 55); - - //корпус - Point point10 = new Point(_startPosX + 10, _startPosY + 31); - Point point11 = new Point(_startPosX + 149, _startPosY + 31); - Point point12 = new Point(_startPosX + 149, _startPosY + 79); - Point point13 = new Point(_startPosX + 10, _startPosY + 79); - Point[] body = {point10, point11, point12, point13, point10 }; - g.FillPolygon(mainBrush, body); - - //крылья - Brush brBlack = new SolidBrush(Color.Black); - g.FillEllipse(brBlack, _startPosX + 40, _startPosY + 50, 80, 10); - g.FillEllipse(brBlack, _startPosX, _startPosY + 25, 35, 10); - //поплавок - if(EntitySeaplane.Floater) + if(seaPlane.Floater) { g.DrawLine(pen, _startPosX + 70, _startPosY + 80, _startPosX + 50, _startPosY + 100); g.DrawLine(pen, _startPosX + 70, _startPosY + 80, _startPosX + 70, _startPosY + 100); @@ -230,6 +69,7 @@ namespace ProjectSeaplane g.DrawEllipse(pen, _startPosX + 30, _startPosY + 100, 140, 10); g.FillEllipse(additionalBrush, _startPosX + 30, _startPosY + 100, 140, 10); } + base.DrawTransport(g); } diff --git a/ProjectSeaplane/ProjectSeaplane/DrawningObjectPlane.cs b/ProjectSeaplane/ProjectSeaplane/DrawningObjectPlane.cs new file mode 100644 index 0000000..9d302c1 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/DrawningObjectPlane.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ProjectSeaplane.MovementStrategy; +using ProjectSeaplane.DrawningObjects; +using static ProjectSeaplane.Direction; + + +namespace ProjectSeaplane.MovementStrategy +{ + /// + /// Реализация интерфейса IDrawningObject для работы с объектом DrawningPlane (паттерн Adapter) + /// + public class DrawningObjectPlane : IMoveableObject + { + private readonly DrawningPlane? _drawningPlane = null; + public DrawningObjectPlane(DrawningPlane drawningPlane) + { + _drawningPlane = drawningPlane; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningPlane == null || _drawningPlane.EntityPlane == null) + { + return null; + } + return new ObjectParameters(_drawningPlane.GetPosX, _drawningPlane.GetPosY, _drawningPlane.GetWidth, _drawningPlane.GetHeight); + } + } + public int GetStep => (int)(_drawningPlane?.EntityPlane?.Step ?? 0); + public bool CheckCanMove(DirectionType direction) => _drawningPlane?.CanMove(direction) ?? false; + public void MoveObject(DirectionType direction) => _drawningPlane?.MoveTransport(direction); + } +} + diff --git a/ProjectSeaplane/ProjectSeaplane/EntityPlane.cs b/ProjectSeaplane/ProjectSeaplane/EntityPlane.cs new file mode 100644 index 0000000..d0e43a0 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/EntityPlane.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.Entities +{ + /// + /// Класс-сущность "Корабль" + /// + public class EntityPlane + { + /// + /// Скорость + /// + 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 EntityPlane(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } + +} diff --git a/ProjectSeaplane/ProjectSeaplane/EntitySeaplane.cs b/ProjectSeaplane/ProjectSeaplane/EntitySeaplane.cs index 45b4cb8..6687a65 100644 --- a/ProjectSeaplane/ProjectSeaplane/EntitySeaplane.cs +++ b/ProjectSeaplane/ProjectSeaplane/EntitySeaplane.cs @@ -4,22 +4,10 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProjectSeaplane +namespace ProjectSeaplane.Entities { - public class EntitySeaplane + public class EntitySeaplane : EntityPlane { - /// - /// Скорость - /// - public int Speed { get; private set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } /// /// Лодка /// @@ -33,22 +21,16 @@ namespace ProjectSeaplane /// public Color AdditionalColor { get; private set; } /// - /// Шаг перемещения автомобиля - /// - public double Step => (double)Speed * 100 / Weight; - /// /// Инициализация полей объекта-класса спортивного автомобиля /// /// Скорость /// Вес автомобиля /// Основной цвет /// Дополнительный цвет - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool boat, bool floater) + /// /// Признак наличия лодки + /// Признак наличия поплавка + public EntitySeaplane(int speed, double weight, Color bodyColor, Color additionalColor, bool boat, bool floater) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; Boat = boat; Floater = floater; diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs index 85f59a3..dbddc87 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.Designer.cs @@ -29,11 +29,14 @@ private void InitializeComponent() { pictureBoxSeaplane = new PictureBox(); - buttonCreate = new Button(); + buttonCreatePlane = new Button(); buttonRight = new Button(); buttonDown = new Button(); buttonLeft = new Button(); buttonUp = new Button(); + ComboBox = new ComboBox(); + buttonCreateSeaplane = new Button(); + buttonStep = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBoxSeaplane).BeginInit(); SuspendLayout(); // @@ -47,16 +50,16 @@ pictureBoxSeaplane.TabIndex = 0; pictureBoxSeaplane.TabStop = false; // - // buttonCreate + // buttonCreatePlane // - buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(0, 438); - buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(75, 23); - buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; - buttonCreate.UseVisualStyleBackColor = true; - buttonCreate.Click += buttonCreate_Click; + buttonCreatePlane.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonCreatePlane.Location = new Point(174, 431); + buttonCreatePlane.Name = "buttonCreatePlane"; + buttonCreatePlane.Size = new Size(147, 23); + buttonCreatePlane.TabIndex = 1; + buttonCreatePlane.Text = "Создать самолет"; + buttonCreatePlane.UseVisualStyleBackColor = true; + buttonCreatePlane.Click += buttonCreatePlane_Click; // // buttonRight // @@ -106,18 +109,50 @@ buttonUp.UseVisualStyleBackColor = true; buttonUp.Click += buttonMove_Click; // - // Seaplane + // ComboBox + // + ComboBox.DropDownStyle = ComboBoxStyle.DropDownList; + ComboBox.FormattingEnabled = true; + ComboBox.Location = new Point(751, 12); + ComboBox.Name = "ComboBox"; + ComboBox.Size = new Size(121, 23); + ComboBox.TabIndex = 6; + // + // buttonCreateSeaplane + // + buttonCreateSeaplane.Location = new Point(9, 431); + buttonCreateSeaplane.Name = "buttonCreateSeaplane"; + buttonCreateSeaplane.Size = new Size(159, 23); + buttonCreateSeaplane.TabIndex = 7; + buttonCreateSeaplane.Text = "Создать гидросамолет"; + buttonCreateSeaplane.UseVisualStyleBackColor = true; + buttonCreateSeaplane.Click += buttonCreateSeaplane_Click; + // + // buttonStep + // + buttonStep.Location = new Point(797, 41); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(75, 23); + buttonStep.TabIndex = 8; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += buttonStep_Click; + // + // FormSeaplane // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(884, 461); + Controls.Add(buttonStep); + Controls.Add(buttonCreateSeaplane); + Controls.Add(ComboBox); Controls.Add(buttonUp); Controls.Add(buttonLeft); Controls.Add(buttonDown); Controls.Add(buttonRight); - Controls.Add(buttonCreate); + Controls.Add(buttonCreatePlane); Controls.Add(pictureBoxSeaplane); - Name = "Seaplane"; + Name = "FormSeaplane"; StartPosition = FormStartPosition.CenterScreen; Text = "Гидросамолет"; ((System.ComponentModel.ISupportInitialize)pictureBoxSeaplane).EndInit(); @@ -128,10 +163,13 @@ #endregion private PictureBox pictureBoxSeaplane; - private Button buttonCreate; + private Button buttonCreatePlane; private Button buttonRight; private Button buttonDown; private Button buttonLeft; private Button buttonUp; + private ComboBox ComboBox; + private Button buttonCreateSeaplane; + private Button buttonStep; } } \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs index ebe5f6c..ff6f6c3 100644 --- a/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs +++ b/ProjectSeaplane/ProjectSeaplane/FormSeaplane.cs @@ -1,12 +1,23 @@ +using ProjectSeaplane.DrawningObjects; +using ProjectSeaplane.MovementStrategy; +using static ProjectSeaplane.Direction; +using System; + namespace ProjectSeaplane { public partial class FormSeaplane : Form { - private DrawningSeaplane? _drawningSeaplane; + private DrawningPlane? _drawningSeaplane; + /// + /// + /// + private AbstractStrategy? _abstractStrategy; public FormSeaplane() { InitializeComponent(); + ComboBox.Items.Add(0); + ComboBox.Items.Add(1); } private void Draw() @@ -21,16 +32,11 @@ namespace ProjectSeaplane pictureBoxSeaplane.Image = bmp; } - private void buttonCreate_Click(object sender, EventArgs e) + private void buttonCreatePlane_Click(object sender, EventArgs e) { Random random = new(); - _drawningSeaplane = new DrawningSeaplane(); - _drawningSeaplane.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)), - pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); + _drawningSeaplane = new DrawningPlane(random.Next(100, 300), random.Next(1000, 3000), + Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)), pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); _drawningSeaplane.SetPosition(random.Next(10, 100), random.Next(10, 100)); Draw(); } @@ -55,10 +61,58 @@ namespace ProjectSeaplane break; case "buttonRight": _drawningSeaplane.MoveTransport(DirectionType.Right); - break; + break; } Draw(); } + + private void buttonCreateSeaplane_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningSeaplane = new DrawningSeaplane(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)), + pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); + _drawningSeaplane.SetPosition(random.Next(10, 100), random.Next(10, 100)); + Draw(); + } + + private void buttonStep_Click(object sender, EventArgs e) + { + if (_drawningSeaplane == null) + { + return; + } + if (ComboBox.Enabled) + { + _abstractStrategy = ComboBox.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new DrawningObjectPlane(_drawningSeaplane), pictureBoxSeaplane.Width, pictureBoxSeaplane.Height); + ComboBox.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + ComboBox.Enabled = true; + _abstractStrategy = null; + } + } } } \ No newline at end of file diff --git a/ProjectSeaplane/ProjectSeaplane/IMoveableObject.cs b/ProjectSeaplane/ProjectSeaplane/IMoveableObject.cs new file mode 100644 index 0000000..70bb7f6 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/IMoveableObject.cs @@ -0,0 +1,37 @@ +using ProjectSeaplane.MovementStrategy; +using ProjectSeaplane; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static ProjectSeaplane.Direction; + +namespace ProjectSeaplane.MovementStrategy +{ + /// + /// Интерфейс для работы с перемещаемым объектом + /// + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/MoveToBorder.cs b/ProjectSeaplane/ProjectSeaplane/MoveToBorder.cs new file mode 100644 index 0000000..ce7c5e6 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MoveToBorder.cs @@ -0,0 +1,57 @@ +using ProjectSeaplane.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.MovementStrategy +{ + public class MoveToBorder : AbstractStrategy + { + protected override bool IsTargetDestinaion() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return false; + } + return objParams.ObjectRightBorder <= FieldWidth && + objParams.ObjectRightBorder + GetStep() >= FieldWidth && + objParams.ObjectDownBorder <= FieldHeight && + objParams.ObjectDownBorder + GetStep() >= FieldHeight; + } + protected override void MoveToTarget() + { + var objParams = GetObjectParameters; + if (objParams == null) + { + return; + } + var diffX = objParams.ObjectRightBorder - FieldWidth; + if (Math.Abs(diffX) > GetStep()) + { + if (diffX > 0) + { + MoveLeft(); + } + else + { + MoveRight(); + } + } + var diffY = objParams.ObjectDownBorder - FieldHeight; + if (Math.Abs(diffY) > GetStep()) + { + if (diffY > 0) + { + MoveUp(); + } + else + { + MoveDown(); + } + } + } + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/MoveToCenter.cs b/ProjectSeaplane/ProjectSeaplane/MoveToCenter.cs new file mode 100644 index 0000000..dad0880 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/MoveToCenter.cs @@ -0,0 +1,60 @@ +using ProjectSeaplane.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.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/ProjectSeaplane/ProjectSeaplane/ObjectParameters.cs b/ProjectSeaplane/ProjectSeaplane/ObjectParameters.cs new file mode 100644 index 0000000..2b6f3db --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/ObjectParameters.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.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; + /// + /// Правая граница + /// + public int ObjectRightBorder => _x + _width; + /// + /// Нижняя граница + /// + public int ObjectDownBorder => _y + _height; + + + /// + /// Конструктор + /// + /// Координата X + /// Координата Y + /// Ширина + /// Высота + public ObjectParameters(int x, int y, int width, int height) + { + _x = x; + _y = y; + _width = width; + _height = height; + } + } +} diff --git a/ProjectSeaplane/ProjectSeaplane/Status.cs b/ProjectSeaplane/ProjectSeaplane/Status.cs new file mode 100644 index 0000000..201eb60 --- /dev/null +++ b/ProjectSeaplane/ProjectSeaplane/Status.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectSeaplane.MovementStrategy +{ + public enum Status + { + NotInit, InProgress, Finish + } +}