From e88714da832800ed4c838e6b64f3df5b83a5fd88 Mon Sep 17 00:00:00 2001 From: ALINA_KURBANOVA Date: Tue, 31 Oct 2023 15:28:54 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B5=D0=BA=D1=82=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarmlyLocomotive/AbstractStrategy.cs | 128 ++++++++++ WarmlyLocomotive/DrawningObjectCar.cs | 37 +++ WarmlyLocomotive/DrawningPro.cs | 50 ++++ WarmlyLocomotive/DrawningWarmlyLocomotive.cs | 196 ++++++++++++++ WarmlyLocomotive/EntityWarmlyLocomotive.cs | 43 ++++ WarmlyLocomotive/IMoveableObject.cs | 35 +++ WarmlyLocomotive/MoveToBorder.cs | 50 ++++ WarmlyLocomotive/MoveToCenter.cs | 58 +++++ WarmlyLocomotive/ObjectParameters.cs | 57 +++++ WarmlyLocomotive/Pro.cs | 34 +++ WarmlyLocomotive/Status.cs | 19 ++ WarmlyLocomotive/WarmlyLocomotive.cs | 240 ------------------ WarmlyLocomotive/WarmlyLocomotiveForm.cs | 97 ++++++- WarmlyLocomotive/WarmlyLocomotiveForm.resx | 6 +- .../WarmlylocomotiveForm.Designer.cs | 55 +++- 15 files changed, 844 insertions(+), 261 deletions(-) create mode 100644 WarmlyLocomotive/AbstractStrategy.cs create mode 100644 WarmlyLocomotive/DrawningObjectCar.cs create mode 100644 WarmlyLocomotive/DrawningPro.cs create mode 100644 WarmlyLocomotive/DrawningWarmlyLocomotive.cs create mode 100644 WarmlyLocomotive/EntityWarmlyLocomotive.cs create mode 100644 WarmlyLocomotive/IMoveableObject.cs create mode 100644 WarmlyLocomotive/MoveToBorder.cs create mode 100644 WarmlyLocomotive/MoveToCenter.cs create mode 100644 WarmlyLocomotive/ObjectParameters.cs create mode 100644 WarmlyLocomotive/Pro.cs create mode 100644 WarmlyLocomotive/Status.cs delete mode 100644 WarmlyLocomotive/WarmlyLocomotive.cs diff --git a/WarmlyLocomotive/AbstractStrategy.cs b/WarmlyLocomotive/AbstractStrategy.cs new file mode 100644 index 0000000..f77c150 --- /dev/null +++ b/WarmlyLocomotive/AbstractStrategy.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyLocomotive.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; + } + } +} \ No newline at end of file diff --git a/WarmlyLocomotive/DrawningObjectCar.cs b/WarmlyLocomotive/DrawningObjectCar.cs new file mode 100644 index 0000000..d5035c7 --- /dev/null +++ b/WarmlyLocomotive/DrawningObjectCar.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WarmlyLocomotive.DrawningObjects; +using WarmlyLocomotive.MovementStrategy; + +namespace WarmlyLocomotive.MovementStrategy +{ + internal class DrawningObjectCar : IMoveableObject + { + private readonly DrawningWarmlyLocomotive? _drawningCar = null; + public DrawningObjectCar(DrawningWarmlyLocomotive drawningCar) + { + _drawningCar = drawningCar; + } + public ObjectParameters? GetObjectPosition + { + get + { + if (_drawningCar == null || _drawningCar.EntityWarmlyLocomotive == + null) + { + return null; + } + return new ObjectParameters(_drawningCar.GetPosX, + _drawningCar.GetPosY, _drawningCar.GetWidth, _drawningCar.GetHeight); + } + } + public int GetStep => (int)(_drawningCar?.EntityWarmlyLocomotive?.Step ?? 0); + public bool CheckCanMove(Direction direction) => + _drawningCar?.CanMove(direction) ?? false; + public void MoveObject(Direction direction) => + _drawningCar?.MoveTransport(direction); + } +} diff --git a/WarmlyLocomotive/DrawningPro.cs b/WarmlyLocomotive/DrawningPro.cs new file mode 100644 index 0000000..393e97a --- /dev/null +++ b/WarmlyLocomotive/DrawningPro.cs @@ -0,0 +1,50 @@ +using WarmlyLocomotive.DrawningObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WarmlyLocomotive.Entities; +using WarmlyLocomotive; + +namespace WarmlyLocomotive.DrawningObjects +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawningPro : DrawningWarmlyLocomotive + { + public DrawningPro(int speed, double weight, Color bodyColor, Color + additionalColor, bool trumpet, bool luggage, int width, int height) :base(speed, weight, bodyColor, width, height, 200, 75) + { + if (EntityWarmlyLocomotive != null) + { + EntityWarmlyLocomotive = new Pro(speed, weight, bodyColor, additionalColor, trumpet, luggage); + } + } + public override void DrawTransport(Graphics g) + { + if (EntityWarmlyLocomotive is not Pro warmlylocomotive) + { + return; + } + Pen pen = new(Color.Black, 2); + Brush bodyBrush = new SolidBrush(warmlylocomotive.BodyColor); + Brush addBrush = new SolidBrush(warmlylocomotive.AdditionalColor); + Brush wheelBrush = new SolidBrush(Color.Black); + base.DrawTransport(g); + //труба + if (warmlylocomotive.Trumpet) + { + g.FillRectangle(addBrush, _startPosX + 165, _startPosY - 25, 25, 25); + } + //багаж + if (warmlylocomotive.Luggage) + { + g.FillRectangle(addBrush, _startPosX, _startPosY, 50, 50); + g.FillEllipse(wheelBrush, _startPosX + 10, _startPosY + 50, 20, 20); + g.FillEllipse(wheelBrush, _startPosX + 35, _startPosY + 50, 20, 20); + } + } + } +} \ No newline at end of file diff --git a/WarmlyLocomotive/DrawningWarmlyLocomotive.cs b/WarmlyLocomotive/DrawningWarmlyLocomotive.cs new file mode 100644 index 0000000..b20bf5e --- /dev/null +++ b/WarmlyLocomotive/DrawningWarmlyLocomotive.cs @@ -0,0 +1,196 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WarmlyLocomotive.Entities; + +namespace WarmlyLocomotive.DrawningObjects +{ + /// + /// Класс, отвечающий за прорисовку и перемещение объекта-сущности + /// + public class DrawningWarmlyLocomotive + { + /// + /// Класс-сущность + /// + public EntityWarmlyLocomotive? EntityWarmlyLocomotive { get; protected set; } + /// + /// Ширина окна + /// + private int _pictureWidth; + /// + /// Высота окна + /// + private int _pictureHeight; + /// + /// Левая координата прорисовки локомотива + /// + protected int _startPosX; + /// + /// Верхняя кооридната прорисовки локомотива + /// + protected int _startPosY; + /// + /// Ширина прорисовки локомотива + /// + protected readonly int _WarmlyLocomotiveWidth = 200; + /// + /// Высота прорисовки локомотива + /// + protected readonly int _WarmlyLocomotiveHeight = 75; + private readonly int _trumpetHeight = 25; + public int GetPosX => _startPosX; + /// + /// Координата Y объекта + /// + public int GetPosY => _startPosY; + /// + /// Ширина объекта + /// + public int GetWidth => _WarmlyLocomotiveWidth; + /// + /// Высота объекта + /// + public int GetHeight => _WarmlyLocomotiveHeight; + public DrawningWarmlyLocomotive(int speed, double weight, Color bodyColor, int width, int height) + { + if (width < _WarmlyLocomotiveWidth || height < _WarmlyLocomotiveHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + + EntityWarmlyLocomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor); + } + + protected DrawningWarmlyLocomotive(int speed, double weight, Color bodyColor, int + width, int height, int carWidth, int carHeight) + { + if (width <= _WarmlyLocomotiveWidth || height <= _WarmlyLocomotiveHeight) + { + return; + } + _pictureWidth = width; + _pictureHeight = height; + _WarmlyLocomotiveWidth = carWidth; + _WarmlyLocomotiveHeight = carHeight; + EntityWarmlyLocomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor); + } + /// + /// Установка позиции + /// + /// Координата X + /// Координата Y + public void SetPosition(int x, int y) + { + if (x < 0 || x >= _pictureWidth || y < 0 || y >= _pictureHeight) + { + _startPosX = 0; + _startPosY = 0; + } + _startPosX = x; + _startPosY = y; + + } + + public bool CanMove(Direction direction) + { + if (EntityWarmlyLocomotive == null) + { + return false; + } + return direction switch + { + //влево + Direction.Left => _startPosX - EntityWarmlyLocomotive.Step > 0, + //вверх + Direction.Up => _startPosY - EntityWarmlyLocomotive.Step > 0, + // вправо + Direction.Right => _startPosX + EntityWarmlyLocomotive.Step + _WarmlyLocomotiveWidth < _pictureWidth, + //вниз + Direction.Down => _startPosY + EntityWarmlyLocomotive.Step + _WarmlyLocomotiveHeight < _pictureHeight, + _ => false, + }; + } + + /// Направление + public void MoveTransport(Direction direction) + { + if (!CanMove(direction) || EntityWarmlyLocomotive == null) + { + return; + } + switch (direction) + { + //влево + case Direction.Left: + if (_startPosX - EntityWarmlyLocomotive.Step > 0) + { + _startPosX -= (int)EntityWarmlyLocomotive.Step; + } + break; + //вверх + case Direction.Up: + if (_startPosY - _trumpetHeight - EntityWarmlyLocomotive.Step > 0) + { + _startPosY -= (int)EntityWarmlyLocomotive.Step; + } + break; + // вправо + case Direction.Right: + + if (_startPosX + _WarmlyLocomotiveWidth + EntityWarmlyLocomotive.Step < _pictureWidth) + { + _startPosX += (int)EntityWarmlyLocomotive.Step; + } + break; + //вниз + case Direction.Down: + if (_startPosY + _WarmlyLocomotiveHeight + EntityWarmlyLocomotive.Step < _pictureHeight) + { + _startPosY += (int)EntityWarmlyLocomotive.Step; + } + break; + } + } + /// + /// Прорисовка объекта + /// + /// + public virtual void DrawTransport(Graphics g) + { + if (EntityWarmlyLocomotive == null) + { + return; + } + Pen pen = new(Color.Black, 2); + Brush bodyBrush = new SolidBrush(EntityWarmlyLocomotive.BodyColor); + Brush wheelBrush = new SolidBrush(Color.Black); + + //корпус + g.FillRectangle(bodyBrush, _startPosX + 50, _startPosY, 150, 50); + Point[] Points = { new Point(_startPosX + 50, _startPosY + 25), new Point(_startPosX + 125, _startPosY + 25) }; + g.DrawPolygon(pen, Points); + Point[] Points2 = { new Point(_startPosX + 150, _startPosY + 25), new Point(_startPosX + 200, _startPosY + 25) }; + g.DrawPolygon(pen, Points2); + //окна + g.DrawRectangle(pen, _startPosX + 125, _startPosY + 10, 25, 30); + g.DrawRectangle(pen, _startPosX + 60, _startPosY + 7, 10, 13); + g.DrawRectangle(pen, _startPosX + 160, _startPosY + 7, 10, 13); + g.DrawRectangle(pen, _startPosX + 175, _startPosY + 7, 10, 13); + //колеса + g.FillEllipse(wheelBrush, _startPosX + 60, _startPosY + 50, 20, 20); + g.FillEllipse(wheelBrush, _startPosX + 85, _startPosY + 50, 20, 20); + g.FillEllipse(wheelBrush, _startPosX + 145, _startPosY + 50, 20, 20); + g.FillEllipse(wheelBrush, _startPosX + 170, _startPosY + 50, 20, 20); + + } + } +} + + + + diff --git a/WarmlyLocomotive/EntityWarmlyLocomotive.cs b/WarmlyLocomotive/EntityWarmlyLocomotive.cs new file mode 100644 index 0000000..4c83bcc --- /dev/null +++ b/WarmlyLocomotive/EntityWarmlyLocomotive.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyLocomotive.Entities +{ + /// + /// Класс-сущность "Автомобиль" + /// + public class EntityWarmlyLocomotive + { + /// + /// Скорость + /// + 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 EntityWarmlyLocomotive(int speed, double weight, Color bodyColor) + { + Speed = speed; + Weight = weight; + BodyColor = bodyColor; + } + } +} diff --git a/WarmlyLocomotive/IMoveableObject.cs b/WarmlyLocomotive/IMoveableObject.cs new file mode 100644 index 0000000..a038a63 --- /dev/null +++ b/WarmlyLocomotive/IMoveableObject.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyLocomotive.MovementStrategy +{ + /// + /// Интерфейс для работы с перемещаемым объектом + /// + public interface IMoveableObject + { + /// + /// Получение координаты X объекта + /// + ObjectParameters? GetObjectPosition { get; } + /// + /// Шаг объекта + /// + int GetStep { get; } + /// + /// Проверка, можно ли переместиться по нужному направлению + /// + /// + /// + bool CheckCanMove(Direction direction); + /// + /// Изменение направления пермещения объекта + /// + /// Направление + void MoveObject(Direction direction); + } +} + diff --git a/WarmlyLocomotive/MoveToBorder.cs b/WarmlyLocomotive/MoveToBorder.cs new file mode 100644 index 0000000..01162f8 --- /dev/null +++ b/WarmlyLocomotive/MoveToBorder.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WarmlyLocomotive.MovementStrategy; + +namespace WarmlyLocomotive.MovementStrategy +{ + internal 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 = FieldWidth - objParams.ObjectMiddleHorizontal; + if (Math.Abs(diffX) > GetStep()) + { + + MoveRight(); + + } + var diffY = FieldHeight - objParams.ObjectMiddleVertical; + if (Math.Abs(diffY) > GetStep()) + { + + MoveDown(); + + } + } + + + } +} \ No newline at end of file diff --git a/WarmlyLocomotive/MoveToCenter.cs b/WarmlyLocomotive/MoveToCenter.cs new file mode 100644 index 0000000..a6b9a85 --- /dev/null +++ b/WarmlyLocomotive/MoveToCenter.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WarmlyLocomotive.MovementStrategy; +namespace WarmlyLocomotive.MovementStrategy + +{ + internal 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 diff --git a/WarmlyLocomotive/ObjectParameters.cs b/WarmlyLocomotive/ObjectParameters.cs new file mode 100644 index 0000000..30bdeb7 --- /dev/null +++ b/WarmlyLocomotive/ObjectParameters.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyLocomotive.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/WarmlyLocomotive/Pro.cs b/WarmlyLocomotive/Pro.cs new file mode 100644 index 0000000..1c2d294 --- /dev/null +++ b/WarmlyLocomotive/Pro.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Net.Mime.MediaTypeNames; + +namespace WarmlyLocomotive.Entities +{ + public class Pro : EntityWarmlyLocomotive + { + /// + /// Дополнительный цвет (для опциональных элементов) + /// + public Color AdditionalColor { get; private set; } + /// + /// Признак (опция) наличия трубы + /// + public bool Trumpet { get; private set; } + /// + /// Признак (опция) наличия прицепа + /// + public bool Luggage { get; private set; } + public Pro(int speed, double weight, Color bodyColor, Color + additionalColor, bool trumpet,bool luggage) : base(speed, weight, bodyColor) + { + + AdditionalColor = additionalColor; + Trumpet = trumpet; + Luggage = luggage; + } + } +} + diff --git a/WarmlyLocomotive/Status.cs b/WarmlyLocomotive/Status.cs new file mode 100644 index 0000000..9184bdd --- /dev/null +++ b/WarmlyLocomotive/Status.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WarmlyLocomotive.MovementStrategy +{ + /// + /// Статус выполнения операции перемещения + /// + public enum Status + { + NotInit, + InProgress, + Finish + } +} + diff --git a/WarmlyLocomotive/WarmlyLocomotive.cs b/WarmlyLocomotive/WarmlyLocomotive.cs deleted file mode 100644 index c9df860..0000000 --- a/WarmlyLocomotive/WarmlyLocomotive.cs +++ /dev/null @@ -1,240 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace WarmlyLocomotive -{ - public class EntityWarmlyLocomotive - { - /// - /// Скорость - /// - public int Speed { get; set; } - /// - /// Вес - /// - public double Weight { get; private set; } - /// - /// Основной цвет - /// - public Color BodyColor { get; private set; } - /// - /// Дополнительный цвет (для опциональных элементов) - /// - public Color AdditionalColor { get; private set; } - /// - /// Признак (опция) наличия трубы - /// - public bool Trumpet { get; private set; } - /// - /// Признак (опция) наличия прицепа - /// - public bool Luggage { get; private set; } - /// - /// Шаг перемещения - /// - public double Step => (double)Speed * 100 / Weight; - /// - /// Инициализация полей объекта-класса тепловоза - /// - /// Скорость - /// Вес тепловоза - /// Основной цвет - /// Дополнительный цвет - /// Признак наличия трубы - /// Признак наличия отсека под топливо - - public void Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool trumpet, bool luggage, bool v, int width, int height) - { - Speed = speed; - Weight = weight; - BodyColor = bodyColor; - AdditionalColor = additionalColor; - Trumpet = trumpet; - Luggage = luggage; - - } - - /// - /// Класс, отвечающий за прорисовку и перемещение объекта-сущности - /// - public class DrawningWarmlyLocomotive - { - /// - /// Класс-сущность - /// - public EntityWarmlyLocomotive? EntityWarmlyLocomotive { get; private set; } - /// - /// Ширина окна - /// - private int _pictureWidth; - /// - /// Высота окна - /// - private int _pictureHeight; - /// - /// /// Левая координата прорисовки тепловоза - /// - private int _startPosX; - /// - /// Верхняя кооридната прорисовки тепловоза - /// - private int _startPosY; - /// - /// Ширина прорисовки тепловоза - /// - private readonly int _carWidth = 200; - /// - /// Высота прорисовки тепловоза - /// - private readonly int _carHeight = 75; - //высота трубы - private readonly int _trumpetHeight = 25; - /// - /// Инициализация свойств - /// - /// Скорость - /// Вес - /// Цвет кузова - /// Дополнительный цвет - /// Признак наличия трубы - /// Признак наличия отсека под топливо - /// Ширина картинки - /// Высота картинки - /// true - объект создан, false - проверка не пройдена,нельзя создать объект в этих размерах - public bool Init(int speed, double weight, Color bodyColor, Color - additionalColor, bool trumpet, bool luggage, bool v, int width, int height) - { - _pictureWidth = width; - _pictureHeight = height; - if (_carWidth < _pictureWidth || _carHeight < _pictureHeight) - { - EntityWarmlyLocomotive = new EntityWarmlyLocomotive(); - EntityWarmlyLocomotive.Init(speed, weight, bodyColor, additionalColor, trumpet, luggage, v, width, height); - return true; - } - return false; - } - - /// - /// Установка позиции - /// - /// Координата X - /// Координата Y - public void SetPosition(int x, int y) - { - - if (EntityWarmlyLocomotive == null) return; - while (x + _carWidth > _pictureWidth) - { - x -= (int)EntityWarmlyLocomotive.Step; - } - while (x < 0) - { - x += (int)EntityWarmlyLocomotive.Step; - } - while (y + _carHeight > _pictureHeight) - { - y -= (int)EntityWarmlyLocomotive.Step; - } - while (y < 0) - { - y += (int)EntityWarmlyLocomotive.Step; - } - _startPosX = x; - _startPosY = y; - } - /// - /// Изменение направления перемещения - /// - /// Направление - public void MoveTransport(Direction direction) - { - if (EntityWarmlyLocomotive == null) - { - return; - } - switch (direction) - { - //влево - case Direction.Left: - if (_startPosX - EntityWarmlyLocomotive.Step > 0) - { - _startPosX -= (int)EntityWarmlyLocomotive.Step; - } - break; - //вверх - case Direction.Up: - if (_startPosY - _trumpetHeight - EntityWarmlyLocomotive.Step > 0) - { - _startPosY -= (int)EntityWarmlyLocomotive.Step; - } - break; - // вправо - case Direction.Right: - - if (_startPosX + _carWidth + EntityWarmlyLocomotive.Step < _pictureWidth) - { - _startPosX += (int)EntityWarmlyLocomotive.Step; - } - break; - //вниз - case Direction.Down: - if (_startPosY + _carHeight + EntityWarmlyLocomotive.Step < _pictureHeight) - { - _startPosY += (int)EntityWarmlyLocomotive.Step; - } - break; - } - } - /// - /// Прорисовка объекта - /// - /// - public void DrawTransport(Graphics g) - { - if (EntityWarmlyLocomotive == null) - { - return; - } - Pen pen = new(Color.Black, 2); - Brush bodyBrush = new SolidBrush(EntityWarmlyLocomotive.BodyColor); - Brush addBrush = new SolidBrush(EntityWarmlyLocomotive.AdditionalColor); - Brush wheelBrush = new SolidBrush(Color.Black); - - //корпус - g.FillRectangle(bodyBrush, _startPosX + 50, _startPosY, 150, 50); - Point[] Points = { new Point(_startPosX + 50, _startPosY + 25), new Point(_startPosX + 125, _startPosY + 25) }; - g.DrawPolygon(pen, Points); - Point[] Points2 = { new Point(_startPosX + 150, _startPosY + 25), new Point(_startPosX + 200, _startPosY + 25) }; - g.DrawPolygon(pen, Points2); - //окна - g.DrawRectangle(pen, _startPosX + 125, _startPosY + 10, 25, 30); - g.DrawRectangle(pen, _startPosX + 60, _startPosY + 7, 10, 13); - g.DrawRectangle(pen, _startPosX + 160, _startPosY + 7, 10, 13); - g.DrawRectangle(pen, _startPosX + 175, _startPosY + 7, 10, 13); - //колеса - g.FillEllipse(wheelBrush, _startPosX + 60, _startPosY + 50, 20, 20); - g.FillEllipse(wheelBrush, _startPosX + 85, _startPosY + 50, 20, 20); - g.FillEllipse(wheelBrush, _startPosX + 145, _startPosY + 50, 20, 20); - g.FillEllipse(wheelBrush, _startPosX + 170, _startPosY + 50, 20, 20); - //труба - if (EntityWarmlyLocomotive.Trumpet) - { - g.FillRectangle(addBrush, _startPosX + 165, _startPosY - 25, 25, 25); - } - //багаж - if (EntityWarmlyLocomotive.Luggage) - { - g.FillRectangle(addBrush, _startPosX, _startPosY, 50, 50); - g.FillEllipse(wheelBrush, _startPosX + 10, _startPosY + 50, 20, 20); - g.FillEllipse(wheelBrush, _startPosX + 35, _startPosY + 50, 20, 20); - } - } - } - } -} - diff --git a/WarmlyLocomotive/WarmlyLocomotiveForm.cs b/WarmlyLocomotive/WarmlyLocomotiveForm.cs index 9e5b0c1..7cf6e64 100644 --- a/WarmlyLocomotive/WarmlyLocomotiveForm.cs +++ b/WarmlyLocomotive/WarmlyLocomotiveForm.cs @@ -1,6 +1,7 @@ using System.Windows.Forms; -using WarmlyLocomotive; -using static WarmlyLocomotive.EntityWarmlyLocomotive; +using WarmlyLocomotive.DrawningObjects; + +using WarmlyLocomotive.MovementStrategy; namespace WarmlyLocomotive { @@ -13,10 +14,12 @@ namespace WarmlyLocomotive /// - /// private DrawningWarmlyLocomotive? _drawningWarmlyLocomotive; + private AbstractStrategy? _abstractStrategy; public WarmlyLocomotiveForm() { InitializeComponent(); } + /// /// /// @@ -32,23 +35,24 @@ namespace WarmlyLocomotive _drawningWarmlyLocomotive.DrawTransport(gr); pictureBox1.Image = bmp; } + /// + /// " " + /// + /// + /// private void buttonCreate_Click(object sender, EventArgs e) { Random random = new(); - _drawningWarmlyLocomotive = new DrawningWarmlyLocomotive(); - _drawningWarmlyLocomotive.Init(random.Next(100, 300), + _drawningWarmlyLocomotive = new DrawningWarmlyLocomotive(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)), pictureBox1.Width, pictureBox1.Height); - _drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), - random.Next(10, 100)); + _drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, + 100)); Draw(); } + private void buttonMove_Click(object sender, EventArgs e) { @@ -74,6 +78,77 @@ namespace WarmlyLocomotive } Draw(); } - } + private void pictureBox1_Click(object sender, EventArgs e) + { + + } + + private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) + { + + } + /// + /// "" + /// + /// + /// + private void buttonStep_Click(object sender, EventArgs e) + { + if (_drawningWarmlyLocomotive == null) + { + return; + } + if (comboBox1.Enabled) + { + _abstractStrategy = comboBox1.SelectedIndex + switch + { + 0 => new MoveToCenter(), + 1 => new MoveToBorder(), + _ => null, + }; + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.SetData(new + DrawningObjectCar(_drawningWarmlyLocomotive), pictureBox1.Width, + pictureBox1.Height); + comboBox1.Enabled = false; + } + if (_abstractStrategy == null) + { + return; + } + _abstractStrategy.MakeStep(); + Draw(); + if (_abstractStrategy.GetStatus() == Status.Finish) + { + comboBox1.Enabled = true; + _abstractStrategy = null; + } + } + /// + /// " " + /// + /// + /// + private void buttonCreate_Pro_Click(object sender, EventArgs e) + { + Random random = new(); + _drawningWarmlyLocomotive = new DrawningPro(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)), + pictureBox1.Width, pictureBox1.Height); + _drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10, + 100)); + Draw(); + } + } } diff --git a/WarmlyLocomotive/WarmlyLocomotiveForm.resx b/WarmlyLocomotive/WarmlyLocomotiveForm.resx index e8199df..f25f7f2 100644 --- a/WarmlyLocomotive/WarmlyLocomotiveForm.resx +++ b/WarmlyLocomotive/WarmlyLocomotiveForm.resx @@ -123,7 +123,7 @@ iVBORw0KGgoAAAANSUhEUgAAAT4AAACeCAMAAACcjZZYAAAABGdBTUEAALGPC/xhBQAAAJZQTFRF//// Zs3/adH/AGKVAGWYOpnLH22dNpLEKYa4K3ilaM//AFiRAF2RAF+VatP/AGGUAFyU8/j6AF6VAFWQXcP1 Vbnr2eXtq8TWQqLUEnCjcZ285O3y6/L2Wr/xT7LkMIy+R4Srbdf/Wo+zvtHfz97olLTLo77SI3+yUoqw - uM3cZJa3x9jjgKfCHHirOpfJeKG+nLrPN3ynjFhM0AAAAAlwSFlzAAAOwQAADsEBuJFr7QAABM1JREFU + uM3cZJa3x9jjgKfCHHirOpfJeKG+nLrPN3ynjFhM0AAAAAlwSFlzAAAOvAAADrwBlbxySQAABM1JREFU eF7tndtS2zAURZ3Y3ELACRSSlLRAAwkp1/7/z/XoYqfEuh2Jh4611wt5YJhhzY7ssc+WCgAAAOB/Z6l/ ghjWC/0B8FnenZ7oj4DNbFJX0BcJRa+qoC+S2aQke9AXxfJJRA/64pidnVVVvdlAXwRTEb1yPL8/LqGP zfZCRO/123AIfWymv8SqV8+HowH0sdnWFL0xRW8wgD42v0X0xiJ60MdmJS64OnrQx2Qqo1cfqOhBH49V @@ -152,7 +152,7 @@ iVBORw0KGgoAAAANSUhEUgAAAJ4AAAE+CAMAAABLMFkTAAAABGdBTUEAALGPC/xhBQAAAJZQTFRF//// Zs3/adH/AGKVAGWYOpnLH22dNpLEKYa4K3ilaM//AFiRAF2RAF+VatP/AGGUAFyU8/j6AF6VAFWQXcP1 Vbnr2eXtq8TWQqLUEnCjcZ285O3y6/L2Wr/xT7LkMIy+R4Srbdf/Wo+zvtHfz97olLTLo77SI3+yUoqw - uM3cZJa3x9jjgKfCHHirOpfJeKG+nLrPN3ynjFhM0AAAAAlwSFlzAAAOwQAADsEBuJFr7QAABYVJREFU + uM3cZJa3x9jjgKfCHHirOpfJeKG+nLrPN3ynjFhM0AAAAAlwSFlzAAAOvAAADrwBlbxySQAABYVJREFU eF7tnQtz0lwURUty+yC8sbbUvlDb4qNY/f9/7jvncvmkbQhJ9rlJxtlrdKZaiGtYXoqyZ3pECGmA58fw QSe5GAyuw4ddpJ+m/fBhB3mcOjftbN6LD04YXIRfdo1+6lL50dG8z3OX3t+nbv4cfqNTXAykbK/X1bwn zg3vkuRu6NxJ+K0O4dOOe71xJ/P6U9vLevJDPvjQtbybtD2hi3mfR26iaZXx/aRjeR/01EpZj887eAif @@ -184,7 +184,7 @@ iVBORw0KGgoAAAANSUhEUgAAAJ4AAAE+CAMAAABLMFkTAAAABGdBTUEAALGPC/xhBQAAAJZQTFRF//// Zs3/adH/AGKVAGWYOpnLH22dNpLEKYa4K3ilaM//AFiRAF2RAF+VatP/AGGUAFyU8/j6AF6VAFWQXcP1 Vbnr2eXtq8TWQqLUEnCjcZ285O3y6/L2Wr/xT7LkMIy+R4Srbdf/Wo+zvtHfz97olLTLo77SI3+yUoqw - uM3cZJa3x9jjgKfCHHirOpfJeKG+nLrPN3ynjFhM0AAAAAlwSFlzAAAOwQAADsEBuJFr7QAABXNJREFU + uM3cZJa3x9jjgKfCHHirOpfJeKG+nLrPN3ynjFhM0AAAAAlwSFlzAAAOvAAADrwBlbxySQAABXNJREFU eF7tndty2kgURY0kXxAgAXZsHOOYJLbJxcTJ//9czmkaJ3YkdNmnJSq1V81THlqrWNVMZrSrOCKE/Mec GOOPtSKJE0PixB9rhTvUCD3LH2uFnLg5NWITQi8+ziITsmP5/PyxVqheNDAhoh4A9RCoh0A9BOohUA+B egjUQ6AeAvUQqIdAPQTqIVAPgXoI1EOgHgL1EKiHQD0E6iFQD4F6CNRDoB4C9RCoh0A9BOohUA+BegjU diff --git a/WarmlyLocomotive/WarmlylocomotiveForm.Designer.cs b/WarmlyLocomotive/WarmlylocomotiveForm.Designer.cs index eabdd11..b5f7600 100644 --- a/WarmlyLocomotive/WarmlylocomotiveForm.Designer.cs +++ b/WarmlyLocomotive/WarmlylocomotiveForm.Designer.cs @@ -35,6 +35,9 @@ buttonUp = new Button(); buttonDown = new Button(); buttonRight = new Button(); + comboBox1 = new ComboBox(); + buttonStep = new Button(); + buttonCreate_Pro = new Button(); ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); SuspendLayout(); // @@ -47,15 +50,16 @@ pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; pictureBox1.TabIndex = 0; pictureBox1.TabStop = false; + pictureBox1.Click += pictureBox1_Click; // // buttonCreate // buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - buttonCreate.Location = new Point(30, 379); + buttonCreate.Location = new Point(210, 404); buttonCreate.Name = "buttonCreate"; - buttonCreate.Size = new Size(75, 23); + buttonCreate.Size = new Size(119, 23); buttonCreate.TabIndex = 1; - buttonCreate.Text = "Создать"; + buttonCreate.Text = "Создать тепловоз"; buttonCreate.UseVisualStyleBackColor = true; buttonCreate.Click += buttonCreate_Click; // @@ -64,7 +68,7 @@ buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonLeft.BackgroundImage = (Image)resources.GetObject("buttonLeft.BackgroundImage"); buttonLeft.BackgroundImageLayout = ImageLayout.Zoom; - buttonLeft.Location = new Point(664, 395); + buttonLeft.Location = new Point(751, 400); buttonLeft.Name = "buttonLeft"; buttonLeft.Size = new Size(30, 30); buttonLeft.TabIndex = 2; @@ -76,7 +80,7 @@ buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonUp.BackgroundImage = (Image)resources.GetObject("buttonUp.BackgroundImage"); buttonUp.BackgroundImageLayout = ImageLayout.Zoom; - buttonUp.Location = new Point(690, 372); + buttonUp.Location = new Point(777, 372); buttonUp.Name = "buttonUp"; buttonUp.Size = new Size(30, 30); buttonUp.TabIndex = 3; @@ -88,7 +92,7 @@ buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonDown.BackgroundImage = (Image)resources.GetObject("buttonDown.BackgroundImage"); buttonDown.BackgroundImageLayout = ImageLayout.Zoom; - buttonDown.Location = new Point(690, 395); + buttonDown.Location = new Point(777, 400); buttonDown.Name = "buttonDown"; buttonDown.Size = new Size(30, 30); buttonDown.TabIndex = 4; @@ -100,16 +104,50 @@ buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; buttonRight.BackgroundImage = (Image)resources.GetObject("buttonRight.BackgroundImage"); buttonRight.BackgroundImageLayout = ImageLayout.Zoom; - buttonRight.Location = new Point(717, 395); + buttonRight.Location = new Point(808, 400); buttonRight.Name = "buttonRight"; buttonRight.Size = new Size(30, 30); buttonRight.TabIndex = 5; buttonRight.UseVisualStyleBackColor = true; buttonRight.Click += buttonMove_Click; // + // comboBox1 + // + comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; + comboBox1.FormattingEnabled = true; + comboBox1.Items.AddRange(new object[] { "Центр", "Угол" }); + comboBox1.Location = new Point(717, 21); + comboBox1.Name = "comboBox1"; + comboBox1.Size = new Size(121, 23); + comboBox1.TabIndex = 7; + comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged; + // + // buttonStep + // + buttonStep.Location = new Point(732, 60); + buttonStep.Name = "buttonStep"; + buttonStep.Size = new Size(75, 23); + buttonStep.TabIndex = 9; + buttonStep.Text = "Шаг"; + buttonStep.UseVisualStyleBackColor = true; + buttonStep.Click += buttonStep_Click; + // + // buttonCreate_Pro + // + buttonCreate_Pro.Location = new Point(54, 395); + buttonCreate_Pro.Name = "buttonCreate_Pro"; + buttonCreate_Pro.Size = new Size(136, 41); + buttonCreate_Pro.TabIndex = 10; + buttonCreate_Pro.Text = "Создать продвинутый тепловоз"; + buttonCreate_Pro.UseVisualStyleBackColor = true; + buttonCreate_Pro.Click += buttonCreate_Pro_Click; + // // WarmlyLocomotiveForm // ClientSize = new Size(884, 461); + Controls.Add(buttonCreate_Pro); + Controls.Add(buttonStep); + Controls.Add(comboBox1); Controls.Add(buttonRight); Controls.Add(buttonDown); Controls.Add(buttonUp); @@ -133,5 +171,8 @@ private Button buttonUp; private Button buttonDown; private Button buttonRight; + private ComboBox comboBox1; + private Button buttonStep; + private Button buttonCreate_Pro; } } \ No newline at end of file