diff --git a/AircraftCarrier/AircraftCarrier/AbstractStrategy.cs b/AircraftCarrier/AircraftCarrier/AbstractStrategy.cs new file mode 100644 index 0000000..6e0ee1a --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/AbstractStrategy.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AircraftCarrier.DrawningObjects; +namespace AircraftCarrier.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(Direction.Left); + /// Перемещение вправо + /// Результат перемещения (true - удалось переместиться,false - неудача) + protected bool MoveRight() => MoveTo(Direction.Right); + /// Перемещение вверх + /// Результат перемещения (true - удалось переместиться,false - неудача) + protected bool MoveUp() => MoveTo(Direction.Up); + /// Перемещение вниз + /// Результат перемещения (true - удалось переместиться,false - неудача) + protected bool MoveDown() => MoveTo(Direction.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(Direction directionType) + { + if (_state != Status.InProgress) + { + return false; + } + if (_moveableObject?.CheckCanMove(directionType) ?? false) + { + _moveableObject.MoveObject(directionType); + return true; + } + return false; + } + } +} + diff --git a/AircraftCarrier/AircraftCarrier/DrawningAircraft.cs b/AircraftCarrier/AircraftCarrier/DrawningAircraft.cs new file mode 100644 index 0000000..b269968 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/DrawningAircraft.cs @@ -0,0 +1,153 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AircraftCarrier.Entities; +namespace AircraftCarrier.DrawningObjects +{ + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + public class DrawningAircraft + { + /// Класс-сущность + public EntityAircraft? EntityAircraft { get; protected set; } + /// Ширина окна + private int _pictureWidth; + /// Высота окна + private int _pictureHeight; + protected int _startPosX; + protected int _startPosY; + protected readonly int _AircraftWidth = 164; + protected readonly int _AircraftHeight = 40; + /// Координата X объекта + public int GetPosX => _startPosX; + /// Координата Y объекта + public int GetPosY => _startPosY; + /// Ширина объекта + public int GetWidth => _AircraftWidth; + /// Высота объекта + public int GetHeight => _AircraftHeight; + /// Проверка, что объект может переместится по указанному направлению + /// Направление + /// true - можно переместится по указанному направлению + public bool CanMove(Direction direction) + { + if (EntityAircraft == null) + { + return false; + } + return direction switch + { + //влево + Direction.Left => _startPosX - EntityAircraft.Step > 0, + //вверх + Direction.Up => _startPosY - EntityAircraft.Step > 0, + // вправо + Direction.Right => _startPosX + EntityAircraft.Step + _AircraftWidth < _pictureWidth, + //вниз + Direction.Down => _startPosY + EntityAircraft.Step + _AircraftHeight < _pictureHeight, + _ => false + }; + } + /// Изменение направления перемещения + /// Направление + public void MoveTransport(Direction direction) + { + if (!CanMove(direction) || EntityAircraft == null) + { + return; + } + switch (direction) + { + //влево + case Direction.Left: + _startPosX -= (int)EntityAircraft.Step; + break; + //вверх + case Direction.Up: + _startPosY -= (int)EntityAircraft.Step; + break; + // вправо + case Direction.Right: + _startPosX += (int)EntityAircraft.Step; + break; + //вниз + case Direction.Down: + _startPosY += (int)EntityAircraft.Step; + break; + } + } + /// Конструктор + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + public DrawningAircraft(int speed, double weight, Color bodyColor, int + width, int height) + { + if (width < _AircraftWidth || height < _AircraftHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + EntityAircraft = new EntityAircraft(speed, weight, bodyColor); + } + /// Конструктор + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки автомобиля + /// Высота прорисовки автомобиля + protected DrawningAircraft(int speed, double weight, Color bodyColor,int + width, int height, int AircraftWidth, int AircraftHeight) + { + if (width < _AircraftWidth || height < _AircraftHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _AircraftWidth = AircraftWidth; + _AircraftHeight = AircraftHeight; + EntityAircraft = new EntityAircraft(speed, weight, bodyColor); + + } + /// Установка позиции + public void SetPosition(int x, int y) + { + _startPosX = x; + _startPosY = y; + if (_startPosX < 0 || _startPosY < 0 || _startPosX > (_pictureWidth - _AircraftWidth) || _startPosY > (_pictureHeight - _AircraftHeight)) + { + _startPosX = 50; + _startPosY = 50; + } + } + /// Изменение направления перемещения + /// Прорисовка объекта + public virtual void DrawTransport(Graphics g) + { + if (EntityAircraft == null) + { + return; + } + Pen pen = new(Color.Black); + Brush budyBrush = new SolidBrush(EntityAircraft.BodyColor); + g.DrawLine(pen, _startPosX + 4, _startPosY, _startPosX + 124, _startPosY); + g.DrawLine(pen, _startPosX + 124, _startPosY, _startPosX + 164, _startPosY + 20); + g.DrawLine(pen, _startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40); + g.DrawLine(pen, _startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40); + g.DrawLine(pen, _startPosX + 124, _startPosY + 40, _startPosX + 4, _startPosY + 40); + g.DrawLine(pen, _startPosX + 4, _startPosY + 40, _startPosX + 4, _startPosY); + g.FillRectangle(budyBrush, _startPosX, _startPosY + 6, 4, 12); + g.FillRectangle(budyBrush, _startPosX, _startPosY + 24, 4, 12); + g.DrawEllipse(pen, _startPosX + 115, _startPosY + 13, 14, 14); + g.DrawRectangle(pen, _startPosX + 63, _startPosY + 13, 27, 14); + g.DrawRectangle(pen, _startPosX + 90, _startPosY + 9, 14, 22); + } + } +} diff --git a/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs index 6230099..7c5d711 100644 --- a/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/DrawningAircraftCarrier.cs @@ -3,108 +3,37 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AircraftCarrier +using AircraftCarrier.Entities; +namespace AircraftCarrier.DrawningObjects { - internal class DrawningAircraftCarrier + public class DrawningAircraftCarrier : DrawningAircraft { - public EntityAircraftCarrier? EntityAircraftCarrier { get; private set; } - private int _pictureWidth; - private int _pictureHeight; - private int _startPosX; - private int _startPosY; - private readonly int _AircraftCarrierWidth = 164; - private readonly int _AircraftCarrierHeight = 40; - public bool Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool bodyKit, bool wing, bool sportLine, int width, int height) + public DrawningAircraftCarrier(int speed, double weight, Color bodyColor, Color additionalColor, bool cabin, bool runway, int width, int height) : + base(speed, weight, bodyColor, width, height, 164, 40) { - _pictureWidth = width; - _pictureHeight = height; - if(_AircraftCarrierWidth <_pictureWidth || _AircraftCarrierHeight <_pictureHeight) + if (EntityAircraft != null) { - EntityAircraftCarrier = new EntityAircraftCarrier(); - EntityAircraftCarrier.Init(speed, weight, bodyColor, additionalColor, - bodyKit, wing, sportLine); - return true; - } - return false; - } - public void SetPosition(int x, int y) - { - _startPosX = x; - _startPosY = y; - if (_startPosX < 0 || _startPosY < 0 || _startPosX > (_pictureWidth - _AircraftCarrierWidth) || _startPosY>(_pictureHeight - _AircraftCarrierHeight)) - { - _startPosX = 50; - _startPosY = 50; + EntityAircraft = new EntityAircraftCarrier(speed, weight, bodyColor, + additionalColor, cabin, runway); } } - public void MoveTransport(Direction direction) + public override void DrawTransport(Graphics g) { - if (EntityAircraftCarrier == null) + if (EntityAircraft is not EntityAircraftCarrier aircraftCarrie) { return; } - switch (direction) - { - //влево - case Direction.Left: - if (_startPosX - EntityAircraftCarrier.Step > 0) - { - _startPosX -= (int)EntityAircraftCarrier.Step; - } - break; - //вверх - case Direction.Up: - if (_startPosY - EntityAircraftCarrier.Step > 0) - { - _startPosY -= (int)EntityAircraftCarrier.Step; - } - break; - // вправо - case Direction.Right: - if (_startPosX + EntityAircraftCarrier.Step + _AircraftCarrierWidth < _pictureWidth) - { - _startPosX += (int)EntityAircraftCarrier.Step; - } - break; - //вниз - case Direction.Down: - if(_startPosY + EntityAircraftCarrier.Step + _AircraftCarrierHeight < _pictureHeight) - { - _startPosY += (int)EntityAircraftCarrier.Step; - } - break; - } - } - public void DrawTransport(Graphics g) - { - if (EntityAircraftCarrier == null) - { - return; - } - Pen pen = new(Color.Black); - Brush additionalBrush = new SolidBrush(EntityAircraftCarrier.AdditionalColor); - g.DrawLine(pen, _startPosX+4, _startPosY, _startPosX + 124, _startPosY); - g.DrawLine(pen, _startPosX+124, _startPosY, _startPosX + 164, _startPosY+20); - g.DrawLine(pen, _startPosX + 164, _startPosY+20, _startPosX + 124, _startPosY + 40); - g.DrawLine(pen, _startPosX + 164, _startPosY + 20, _startPosX + 124, _startPosY + 40); - g.DrawLine(pen, _startPosX + 124, _startPosY + 40, _startPosX + 4, _startPosY + 40); - g.DrawLine(pen, _startPosX+4, _startPosY + 40, _startPosX + 4, _startPosY); - g.FillRectangle(additionalBrush, _startPosX, _startPosY + 6, 4, 12); - g.FillRectangle(additionalBrush, _startPosX, _startPosY + 24, 4, 12); - g.DrawEllipse(pen, _startPosX + 115, _startPosY + 13, 14, 14); - g.DrawRectangle(pen, _startPosX + 63, _startPosY + 13, 27, 14); - g.DrawRectangle(pen, _startPosX + 90, _startPosY + 9, 14, 22); - Pen penWhite = new(Color.White); - if (EntityAircraftCarrier.BodyKit) + Brush additionalBrush = new SolidBrush(aircraftCarrie.AdditionalColor); + Pen pen = new(Color.White); + if (aircraftCarrie.Cabin) { g.FillRectangle(additionalBrush, _startPosX + 4, _startPosY + 13, 59, 14); - g.DrawLine(penWhite, _startPosX + 62, _startPosY + 19, _startPosX + 52, _startPosY + 19); - g.DrawLine(penWhite, _startPosX + 47, _startPosY + 19, _startPosX + 37, _startPosY + 19); - g.DrawLine(penWhite, _startPosX + 32, _startPosY + 19, _startPosX + 22, _startPosY + 19); - g.DrawLine(penWhite, _startPosX + 17, _startPosY + 19, _startPosX + 7, _startPosY + 19); + g.DrawLine(pen, _startPosX + 62, _startPosY + 19, _startPosX + 52, _startPosY + 19); + g.DrawLine(pen, _startPosX + 47, _startPosY + 19, _startPosX + 37, _startPosY + 19); + g.DrawLine(pen, _startPosX + 32, _startPosY + 19, _startPosX + 22, _startPosY + 19); + g.DrawLine(pen, _startPosX + 17, _startPosY + 19, _startPosX + 7, _startPosY + 19); } - if (EntityAircraftCarrier.Wing) + if (aircraftCarrie.Runway) { g.FillRectangle(additionalBrush, _startPosX + 90, _startPosY, 15, 9); } diff --git a/AircraftCarrier/AircraftCarrier/DrawningObjectAircraft.cs b/AircraftCarrier/AircraftCarrier/DrawningObjectAircraft.cs new file mode 100644 index 0000000..9b6a5c0 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/DrawningObjectAircraft.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AircraftCarrier.DrawningObjects; +namespace AircraftCarrier.MovementStrategy +{ + public class DrawningObjectAircraft : IMoveableObject + { + private readonly DrawningAircraft? _drawningAircraft = null; + public DrawningObjectAircraft(DrawningAircraft drawningAircraft) + { + _drawningAircraft = drawningAircraft; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningAircraft == null || _drawningAircraft.EntityAircraft == + null) + { + return null; + } + return new ObjectParameters(_drawningAircraft.GetPosX, + _drawningAircraft.GetPosY, _drawningAircraft.GetWidth, _drawningAircraft.GetHeight); + } + } + public int GetStep => (int)(_drawningAircraft?.EntityAircraft?.Step ?? 0); + public bool CheckCanMove(Direction direction) => + _drawningAircraft?.CanMove(direction) ?? false; + public void MoveObject(Direction direction) => + _drawningAircraft?.MoveTransport(direction); + + } +} diff --git a/AircraftCarrier/AircraftCarrier/EntityAircraft.cs b/AircraftCarrier/AircraftCarrier/EntityAircraft.cs new file mode 100644 index 0000000..c6223e7 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/EntityAircraft.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +namespace AircraftCarrier.Entities +{ + /// Класс-сущность "Автомобиль" + public class EntityAircraft + { + /// Скорость + 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 EntityAircraft(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} diff --git a/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs b/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs index eac481b..51ef381 100644 --- a/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs +++ b/AircraftCarrier/AircraftCarrier/EntityAircraftCarrier.cs @@ -3,53 +3,23 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; - -namespace AircraftCarrier +namespace AircraftCarrier.Entities { - public class EntityAircraftCarrier + /// Класс-сущность "Спортивный автомобиль" + public class EntityAircraftCarrier : EntityAircraft { - /// - /// Скорость - /// - 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 BodyKit { get; private set; } - /// - /// Признак (опция) наличия антикрыла - /// - public bool Wing { get; private set; } - /// - /// Признак (опция) наличия гоночной полосы - /// - public bool SportLine { get; private set; } - /// - /// Шаг перемещения автомобиля - /// + /// Признак (опция) наличия кабины + public bool Cabin { get; private set; } + /// Признак (опция) наличия взлетной полосы + public bool Runway { get; private set; } public double Step => (double)Speed * 100 / Weight; - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool bodyKit, bool wing, bool sportLine) + public EntityAircraftCarrier(int speed, double weight, Color bodyColor, Color additionalColor, bool cabin, bool runway) : base(speed, weight, bodyColor) { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; AdditionalColor = additionalColor; - BodyKit = bodyKit; - Wing = wing; - SportLine = sportLine; + Cabin = cabin; + Runway = runway; } } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/IMoveableObject.cs b/AircraftCarrier/AircraftCarrier/IMoveableObject.cs new file mode 100644 index 0000000..fe93978 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/IMoveableObject.cs @@ -0,0 +1,22 @@ +using AircraftCarrier.MovementStrategy; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AircraftCarrier.DrawningObjects; +namespace AircraftCarrier.MovementStrategy +{ + public interface IMoveableObject + { + /// Получение координаты X объекта + ObjectParameters? GetObjectPosition { get; } + /// Шаг объекта + int GetStep { get; } + /// Проверка, можно ли переместиться по нужному направлению + bool CheckCanMove(Direction direction); + /// Изменение направления пермещения объекта + /// Направление + void MoveObject(Direction direction); + } +} diff --git a/AircraftCarrier/AircraftCarrier/MoveToCenter.cs b/AircraftCarrier/AircraftCarrier/MoveToCenter.cs new file mode 100644 index 0000000..fe1000d --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/MoveToCenter.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +namespace AircraftCarrier.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/AircraftCarrier/AircraftCarrier/ObjectParameters.cs b/AircraftCarrier/AircraftCarrier/ObjectParameters.cs new file mode 100644 index 0000000..1b13650 --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/ObjectParameters.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +namespace AircraftCarrier.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/AircraftCarrier/AircraftCarrier/Program.cs b/AircraftCarrier/AircraftCarrier/Program.cs index f373679..e2ac503 100644 --- a/AircraftCarrier/AircraftCarrier/Program.cs +++ b/AircraftCarrier/AircraftCarrier/Program.cs @@ -2,9 +2,7 @@ namespace AircraftCarrier { internal static class Program { - /// /// The main entry point for the application. - /// [STAThread] static void Main() { diff --git a/AircraftCarrier/AircraftCarrier/Status.cs b/AircraftCarrier/AircraftCarrier/Status.cs new file mode 100644 index 0000000..d10fffd --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/Status.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +namespace AircraftCarrier.MovementStrategy +{ + public enum Status + { + NotInit, + InProgress, + Finish + } +}