diff --git a/WarmlyLocomotive/AbstractStrategy.cs b/WarmlyLocomotive/AbstractStrategy.cs
new file mode 100644
index 0000000..be33106
--- /dev/null
+++ b/WarmlyLocomotive/AbstractStrategy.cs
@@ -0,0 +1,121 @@
+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/Direction.cs b/WarmlyLocomotive/Direction.cs
index 3fd3c4a..ad068cc 100644
--- a/WarmlyLocomotive/Direction.cs
+++ b/WarmlyLocomotive/Direction.cs
@@ -1,10 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace WarmlyLocomotive
+namespace WarmlyLocomotive
{
///
/// Направление перемещения
@@ -27,6 +21,5 @@ namespace WarmlyLocomotive
/// Вправо
///
Right = 4
-
}
}
\ No newline at end of file
diff --git a/WarmlyLocomotive/DrawningObjectCar.cs b/WarmlyLocomotive/DrawningObjectCar.cs
new file mode 100644
index 0000000..b8b339e
--- /dev/null
+++ b/WarmlyLocomotive/DrawningObjectCar.cs
@@ -0,0 +1,31 @@
+using WarmlyLocomotive.DrawningObjects;
+
+namespace WarmlyLocomotive.MovementStrategy
+{
+ internal class DrawningObjectCar : IMoveableObject
+ {
+ private readonly DrawningWarmlyLocomotive? _drawningWarmlyLocomotive = null;
+ public DrawningObjectCar(DrawningWarmlyLocomotive drawningCar)
+ {
+ _drawningWarmlyLocomotive = drawningCar;
+ }
+ public ObjectParameters? GetObjectPosition
+ {
+ get
+ {
+ if (_drawningWarmlyLocomotive == null || _drawningWarmlyLocomotive.EntityWarmlyLocomotive ==
+ null)
+ {
+ return null;
+ }
+ return new ObjectParameters(_drawningWarmlyLocomotive.GetPosX,
+ _drawningWarmlyLocomotive.GetPosY, _drawningWarmlyLocomotive.GetWidth, _drawningWarmlyLocomotive.GetHeight);
+ }
+ }
+ public int GetStep => (int)(_drawningWarmlyLocomotive?.EntityWarmlyLocomotive?.Step ?? 0);
+ public bool CheckCanMove(Direction direction) =>
+ _drawningWarmlyLocomotive?.CanMove(direction) ?? false;
+ public void MoveObject(Direction direction) =>
+ _drawningWarmlyLocomotive?.MoveTransport(direction);
+ }
+}
diff --git a/WarmlyLocomotive/DrawningPro.cs b/WarmlyLocomotive/DrawningPro.cs
new file mode 100644
index 0000000..a1d028f
--- /dev/null
+++ b/WarmlyLocomotive/DrawningPro.cs
@@ -0,0 +1,41 @@
+using WarmlyLocomotive.Entities;
+
+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;
+ }
+ 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..88911f2
--- /dev/null
+++ b/WarmlyLocomotive/DrawningWarmlyLocomotive.cs
@@ -0,0 +1,186 @@
+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..f5ce921
--- /dev/null
+++ b/WarmlyLocomotive/EntityWarmlyLocomotive.cs
@@ -0,0 +1,37 @@
+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..4bb671f
--- /dev/null
+++ b/WarmlyLocomotive/IMoveableObject.cs
@@ -0,0 +1,29 @@
+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..24052ee
--- /dev/null
+++ b/WarmlyLocomotive/MoveToBorder.cs
@@ -0,0 +1,36 @@
+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..c5ec254
--- /dev/null
+++ b/WarmlyLocomotive/MoveToCenter.cs
@@ -0,0 +1,50 @@
+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..b4b23ff
--- /dev/null
+++ b/WarmlyLocomotive/ObjectParameters.cs
@@ -0,0 +1,51 @@
+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..a3f3d79
--- /dev/null
+++ b/WarmlyLocomotive/Pro.cs
@@ -0,0 +1,27 @@
+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..3d68e6b
--- /dev/null
+++ b/WarmlyLocomotive/Status.cs
@@ -0,0 +1,13 @@
+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..a9b7087 100644
--- a/WarmlyLocomotive/WarmlyLocomotiveForm.cs
+++ b/WarmlyLocomotive/WarmlyLocomotiveForm.cs
@@ -1,6 +1,8 @@
+using System;
using System.Windows.Forms;
-using WarmlyLocomotive;
-using static WarmlyLocomotive.EntityWarmlyLocomotive;
+using WarmlyLocomotive.DrawningObjects;
+
+using WarmlyLocomotive.MovementStrategy;
namespace WarmlyLocomotive
{
@@ -9,10 +11,11 @@ namespace WarmlyLocomotive
///
public partial class WarmlyLocomotiveForm : Form
{
- ///
- /// -
- ///
private DrawningWarmlyLocomotive? _drawningWarmlyLocomotive;
+
+ private AbstractStrategy? _abstractStrategy;
+
+ public DrawningWarmlyLocomotive? SelectedCar { get; private set; }
public WarmlyLocomotiveForm()
{
InitializeComponent();
@@ -26,27 +29,27 @@ namespace WarmlyLocomotive
{
return;
}
- Bitmap bmp = new(pictureBox1.Width,
- pictureBox1.Height);
+ Bitmap bmp = new(pictureBoxWarmlyLocomotive.Width,
+ pictureBoxWarmlyLocomotive.Height);
Graphics gr = Graphics.FromImage(bmp);
_drawningWarmlyLocomotive.DrawTransport(gr);
- pictureBox1.Image = bmp;
+ pictureBoxWarmlyLocomotive.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));
+ pictureBoxWarmlyLocomotive.Width, pictureBoxWarmlyLocomotive.Height);
+ _drawningWarmlyLocomotive.SetPosition(random.Next(10, 100), random.Next(10,
+ 100));
Draw();
}
private void buttonMove_Click(object sender, EventArgs e)
@@ -74,6 +77,67 @@ namespace WarmlyLocomotive
}
Draw();
}
+ ///
+ /// ""
+ ///
+ ///
+ ///
+ private void buttonStep_Click(object sender, EventArgs e)
+ {
+ if (_drawningWarmlyLocomotive == null)
+ {
+ return;
+ }
+ if (comboBoxWarmlyLocomotive.Enabled)
+ {
+ _abstractStrategy = comboBoxWarmlyLocomotive.SelectedIndex
+ switch
+ {
+ 0 => new MoveToCenter(),
+ 1 => new MoveToBorder(),
+ _ => null,
+ };
+ if (_abstractStrategy == null)
+ {
+ return;
+ }
+ _abstractStrategy.SetData(new
+ DrawningObjectCar(_drawningWarmlyLocomotive), pictureBoxWarmlyLocomotive.Width,
+ pictureBoxWarmlyLocomotive.Height);
+ comboBoxWarmlyLocomotive.Enabled = false;
+ }
+ if (_abstractStrategy == null)
+ {
+ return;
+ }
+ _abstractStrategy.MakeStep();
+ Draw();
+ if (_abstractStrategy.GetStatus() == Status.Finish)
+ {
+ comboBoxWarmlyLocomotive.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)),
+ pictureBoxWarmlyLocomotive.Width, pictureBoxWarmlyLocomotive.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..bcb4188 100644
--- a/WarmlyLocomotive/WarmlylocomotiveForm.Designer.cs
+++ b/WarmlyLocomotive/WarmlylocomotiveForm.Designer.cs
@@ -29,33 +29,36 @@
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WarmlyLocomotiveForm));
- pictureBox1 = new PictureBox();
+ pictureBoxWarmlyLocomotive = new PictureBox();
buttonCreate = new Button();
buttonLeft = new Button();
buttonUp = new Button();
buttonDown = new Button();
buttonRight = new Button();
- ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
+ comboBoxWarmlyLocomotive = new ComboBox();
+ buttonStep = new Button();
+ buttonCreate_Pro = new Button();
+ ((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyLocomotive).BeginInit();
SuspendLayout();
//
- // pictureBox1
+ // pictureBoxWarmlyLocomotive
//
- pictureBox1.Dock = DockStyle.Fill;
- pictureBox1.Location = new Point(0, 0);
- pictureBox1.Name = "pictureBox1";
- pictureBox1.Size = new Size(884, 461);
- pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
- pictureBox1.TabIndex = 0;
- pictureBox1.TabStop = false;
+ pictureBoxWarmlyLocomotive.Dock = DockStyle.Fill;
+ pictureBoxWarmlyLocomotive.Location = new Point(0, 0);
+ pictureBoxWarmlyLocomotive.Name = "pictureBoxWarmlyLocomotive";
+ pictureBoxWarmlyLocomotive.Size = new Size(884, 461);
+ pictureBoxWarmlyLocomotive.SizeMode = PictureBoxSizeMode.AutoSize;
+ pictureBoxWarmlyLocomotive.TabIndex = 0;
+ pictureBoxWarmlyLocomotive.TabStop = false;
//
// 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 +67,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 +79,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 +91,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,25 +103,59 @@
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;
//
+ // comboBoxWarmlyLocomotive
+ //
+ comboBoxWarmlyLocomotive.DropDownStyle = ComboBoxStyle.DropDownList;
+ comboBoxWarmlyLocomotive.FormattingEnabled = true;
+ comboBoxWarmlyLocomotive.Items.AddRange(new object[] { "Центр", "Угол" });
+ comboBoxWarmlyLocomotive.Location = new Point(717, 21);
+ comboBoxWarmlyLocomotive.Name = "comboBoxWarmlyLocomotive";
+ comboBoxWarmlyLocomotive.Size = new Size(121, 23);
+ comboBoxWarmlyLocomotive.TabIndex = 7;
+ //
+ // 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(comboBoxWarmlyLocomotive);
Controls.Add(buttonRight);
Controls.Add(buttonDown);
Controls.Add(buttonUp);
Controls.Add(buttonLeft);
Controls.Add(buttonCreate);
- Controls.Add(pictureBox1);
+ Controls.Add(pictureBoxWarmlyLocomotive);
Name = "WarmlyLocomotiveForm";
StartPosition = FormStartPosition.CenterScreen;
- ((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();
+ Text = "WarmlyLocomotiveForm";
+ ((System.ComponentModel.ISupportInitialize)pictureBoxWarmlyLocomotive).EndInit();
ResumeLayout(false);
PerformLayout();
}
@@ -127,11 +164,14 @@
- private PictureBox pictureBox1;
+ private PictureBox pictureBoxWarmlyLocomotive;
private Button buttonCreate;
private Button buttonLeft;
private Button buttonUp;
private Button buttonDown;
private Button buttonRight;
+ private ComboBox comboBoxWarmlyLocomotive;
+ private Button buttonStep;
+ private Button buttonCreate_Pro;
}
}
\ No newline at end of file