From eedea313509092331c2ef143d4b03494f98ca157 Mon Sep 17 00:00:00 2001 From: gavrilov Date: Fri, 20 Oct 2023 01:15:02 +0400 Subject: [PATCH] Laba_2_60 % --- DumpTruck/DumpTruck/AbstractStrategy.cs | 132 +++++++++++++++ .../{Direction.cs => DirectionType.cs} | 2 +- DumpTruck/DumpTruck/DrawningDumpTruck.cs | 8 +- DumpTruck/DumpTruck/DrawningTruck.cs | 152 ++++++++++++++++++ DumpTruck/DumpTruck/DumpTruck.cs | 2 +- DumpTruck/DumpTruck/IMoveableObject.cs | 37 +++++ DumpTruck/DumpTruck/ObjectParameters.cs | 58 +++++++ DumpTruck/DumpTruck/Status.cs | 19 +++ DumpTruck/DumpTruck/Truck.cs | 48 ++++++ 9 files changed, 452 insertions(+), 6 deletions(-) create mode 100644 DumpTruck/DumpTruck/AbstractStrategy.cs rename DumpTruck/DumpTruck/{Direction.cs => DirectionType.cs} (93%) create mode 100644 DumpTruck/DumpTruck/DrawningTruck.cs create mode 100644 DumpTruck/DumpTruck/IMoveableObject.cs create mode 100644 DumpTruck/DumpTruck/ObjectParameters.cs create mode 100644 DumpTruck/DumpTruck/Status.cs create mode 100644 DumpTruck/DumpTruck/Truck.cs diff --git a/DumpTruck/DumpTruck/AbstractStrategy.cs b/DumpTruck/DumpTruck/AbstractStrategy.cs new file mode 100644 index 0000000..ae39402 --- /dev/null +++ b/DumpTruck/DumpTruck/AbstractStrategy.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static DumpTruck.DrawningDumpTruck; + +namespace DumpTruck.MovementStrategy +{ + internal 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/DumpTruck/DumpTruck/Direction.cs b/DumpTruck/DumpTruck/DirectionType.cs similarity index 93% rename from DumpTruck/DumpTruck/Direction.cs rename to DumpTruck/DumpTruck/DirectionType.cs index a6cc088..19e672e 100644 --- a/DumpTruck/DumpTruck/Direction.cs +++ b/DumpTruck/DumpTruck/DirectionType.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace DumpTruck { - internal enum DirectionType + public enum DirectionType { /// /// Вверх diff --git a/DumpTruck/DumpTruck/DrawningDumpTruck.cs b/DumpTruck/DumpTruck/DrawningDumpTruck.cs index eeb611f..a51c445 100644 --- a/DumpTruck/DumpTruck/DrawningDumpTruck.cs +++ b/DumpTruck/DumpTruck/DrawningDumpTruck.cs @@ -48,13 +48,13 @@ namespace DumpTruck /// true - объект создан, false - проверка не пройдена, public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool body, bool trailer, int width, int height) - { - _pictureWidth = width; - _pictureHeight = height; - if ((_pictureHeight < _DumpTruckHeight) || (_pictureWidth < _DumpTruckWidth)) + { + if ((height < _DumpTruckHeight) || (width < _DumpTruckWidth)) { return false; } + _pictureWidth = width; + _pictureHeight = height; DumpTruck = new DumpTruck(); DumpTruck.Init(speed, weight, bodyColor, additionalColor, body, trailer); diff --git a/DumpTruck/DumpTruck/DrawningTruck.cs b/DumpTruck/DumpTruck/DrawningTruck.cs new file mode 100644 index 0000000..9c05918 --- /dev/null +++ b/DumpTruck/DumpTruck/DrawningTruck.cs @@ -0,0 +1,152 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DumpTruck.Entities; + +namespace DumpTruck.DrawningObjects +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawningTruck + { + /// + /// Класс-сущность + /// + public Truck? Truck { get; protected set; } + /// + /// Ширина окна + /// + private int _pictureWidth; + /// + /// Высота окна + /// + private int _pictureHeight; + /// + /// Левая координата прорисовки автомобиля + /// + protected int _startPosX; + /// + /// Верхняя кооридната прорисовки автомобиля + /// + protected int _startPosY; + /// + /// Ширина прорисовки автомобиля + /// + protected readonly int _truckWidth = 100; + /// + /// Высота прорисовки автомобиля + /// + protected readonly int _truckHeight = 55; + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + public DrawningTruck(int speed, double weight, Color bodyColor,Color additionalColor, int width, int height) + { + // TODO: Продумать проверки + _pictureWidth = width; + _pictureHeight = height; + Truck = new Truck(speed, weight, bodyColor, additionalColor); + } + /// + /// Конструктор + /// + /// Скорость + /// Вес + /// Основной цвет + /// Ширина картинки + /// Высота картинки + /// Ширина прорисовки автомобиля + /// Высота прорисовки автомобиля + protected DrawningTruck(int speed, double weight, Color bodyColor, Color additionalColor, int width, int height, int truckWidth, int truckHeight) + { + // TODO: Продумать проверки + _pictureWidth = width; + _pictureHeight = height; + _truckWidth = truckWidth; + _truckHeight = truckHeight; + Truck = new Truck(speed, weight, bodyColor, additionalColor); + } + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + // TODO: Изменение x, y, если при установке объект выходит за границы + _startPosX = x; + _startPosY = y; + } + /// + /// Изменение направления перемещения + /// + /// Направление + public void MoveTransport(DirectionType direction) + { + if (Truck == null) + { + return; + } + switch (direction) + { + //влево + case DirectionType.Left: + if (_startPosX - Truck.Step > 0) + { + _startPosX -= (int)Truck.Step; + } + break; + //вверх + case DirectionType.Up: + if (_startPosY - Truck.Step > 0) + { + _startPosY -= (int)Truck.Step; + } + break; + // вправо + case DirectionType.Right: + if (_startPosX + _truckWidth + Truck.Step < _pictureWidth) + { + _startPosX += (int)Truck.Step; + } + break; + //вниз + case DirectionType.Down: + if (_startPosY + _truckHeight + Truck.Step < _pictureHeight) + { + _startPosY += (int)Truck.Step; + } + break; + } + } + public virtual void DrawTransport(Graphics g) + { + if (Truck == null) + { + return; + } + Pen pen = new(Color.Black); + //грани + g.DrawRectangle(pen, _startPosX + 80, _startPosY, 30, 40); + g.DrawRectangle(pen, _startPosX, _startPosY + 40, 110, 20); + //кузов + Brush br = new SolidBrush(Truck.BodyColor); + g.FillRectangle(br, _startPosX + 81, _startPosY + 1, 29, 40); + Brush br1 = new SolidBrush(Truck.AdditionalColor); + g.FillRectangle(br1, _startPosX + 1, _startPosY + 41, 109, 19); + //колеса + Brush wheels = new SolidBrush(Color.Black); + g.FillEllipse(wheels, _startPosX, _startPosY + 63, 25, 25); + g.FillEllipse(wheels, _startPosX + 25, _startPosY + 63, 25, 25); + g.FillEllipse(wheels, _startPosX + 85, _startPosY + 63, 25, 25); + } + } +} \ No newline at end of file diff --git a/DumpTruck/DumpTruck/DumpTruck.cs b/DumpTruck/DumpTruck/DumpTruck.cs index 81b0446..3c6cfa1 100644 --- a/DumpTruck/DumpTruck/DumpTruck.cs +++ b/DumpTruck/DumpTruck/DumpTruck.cs @@ -37,7 +37,7 @@ namespace DumpTruck /// public double Step => (double)Speed * 100 / Weight; /// - /// Инициализация полей объекта-класса спортивного автомобиля + /// Инициализация полей объекта-класса /// /// Скорость /// Вес автомобиля diff --git a/DumpTruck/DumpTruck/IMoveableObject.cs b/DumpTruck/DumpTruck/IMoveableObject.cs new file mode 100644 index 0000000..87d8028 --- /dev/null +++ b/DumpTruck/DumpTruck/IMoveableObject.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static DumpTruck.DrawningDumpTruck; + + +namespace DumpTruck.MovementStrategy +{ + /// + /// Интерфейс для работы с перемещаемым объектом + /// + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(DirectionType direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(DirectionType direction); + } +} + diff --git a/DumpTruck/DumpTruck/ObjectParameters.cs b/DumpTruck/DumpTruck/ObjectParameters.cs new file mode 100644 index 0000000..d084727 --- /dev/null +++ b/DumpTruck/DumpTruck/ObjectParameters.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.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/DumpTruck/DumpTruck/Status.cs b/DumpTruck/DumpTruck/Status.cs new file mode 100644 index 0000000..99b69a0 --- /dev/null +++ b/DumpTruck/DumpTruck/Status.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.MovementStrategy +{ + /// + /// Статус выполнения операции перемещения + /// + public enum Status + { + NotInit, + InProgress, + Finish + } +} + diff --git a/DumpTruck/DumpTruck/Truck.cs b/DumpTruck/DumpTruck/Truck.cs new file mode 100644 index 0000000..fe99a67 --- /dev/null +++ b/DumpTruck/DumpTruck/Truck.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DumpTruck.Entities +{ + /// + /// Класс-сущность "Автомобиль" + /// + public class Truck + { + /// + /// Скорость + /// + 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 double Step => (double)Speed * 100 / Weight; + /// + /// Конструктор с параметрами + /// + /// Скорость + /// Вес автомобиля + /// Основной цвет + public Truck(int speed, double weight, Color bodyColor, Color additionalColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + AdditionalColor = additionalColor; + } + } +} +