ISEbd-12_Pravednikova_D._O._ LabWork02.Simple #2
31
Excavator/Excavator/Drawnings/DirectionType.cs
Normal file
31
Excavator/Excavator/Drawnings/DirectionType.cs
Normal file
@ -0,0 +1,31 @@
|
||||
namespace Excavator.Drawnings;
|
||||
|
||||
/// <summary>
|
||||
/// Направление перемещения
|
||||
/// </summary>
|
||||
public enum DirectionType
|
||||
{
|
||||
/// <summary>
|
||||
/// Неизвестное направление
|
||||
/// </summary>
|
||||
Unknow = -1,
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
||||
Up = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Вниз
|
||||
/// </summary>
|
||||
Down = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Влево
|
||||
/// </summary>
|
||||
Left = 3,
|
||||
|
||||
/// <summary>
|
||||
/// Вправо
|
||||
/// </summary>
|
||||
Right = 4
|
||||
}
|
90
Excavator/Excavator/Drawnings/DrawningExcavator.cs
Normal file
90
Excavator/Excavator/Drawnings/DrawningExcavator.cs
Normal file
@ -0,0 +1,90 @@
|
||||
using Excavator.Entities;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Excavator.Drawnings;
|
||||
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawningExcavator : DrawningTrackedVehicle
|
||||
{
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="bucket">Признак наличия ковша</param>
|
||||
/// <param name="supports">Признак наличия опор</param>
|
||||
|
||||
public DrawningExcavator(int speed, double weight, Color bodyColor, Color additionalColor, bool bucket, bool supports) : base(150,126)
|
||||
{
|
||||
EntityTrackedVehicle = new EntityExcavator(speed, weight, bodyColor, additionalColor, bucket, supports);
|
||||
|
||||
}
|
||||
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityTrackedVehicle == null || EntityTrackedVehicle is not EntityExcavator excavator || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(excavator.AdditionalColor);
|
||||
Brush brBlue = new SolidBrush(Color.LightBlue);
|
||||
Brush brGray = new SolidBrush(Color.Gray);
|
||||
Brush brRed = new SolidBrush(Color.Red);
|
||||
Brush brYellow = new SolidBrush(Color.Yellow);
|
||||
Brush brBlack = new SolidBrush(Color.Black);
|
||||
|
||||
|
||||
|
||||
int bodyHeight = 90;
|
||||
int cabineHeight = 40;
|
||||
int bucketWidth = 30;
|
||||
int wheelsHeight = 40;
|
||||
|
||||
Pen bPen = new(Color.Black);
|
||||
bPen.Width = 3;
|
||||
|
||||
_startPosX += 20;
|
||||
base.DrawTransport(g);
|
||||
_startPosX -= 20;
|
||||
|
||||
|
||||
// ковш
|
||||
if (excavator.Bucket)
|
||||
{
|
||||
|
||||
PointF p1 = new Point(_startPosX.Value + bucketWidth, _startPosY.Value + cabineHeight);
|
||||
PointF p2 = new Point(_startPosX.Value, _startPosY.Value + cabineHeight);
|
||||
PointF p3 = new Point(_startPosX.Value + bucketWidth , _startPosY.Value + cabineHeight + bodyHeight / 2);
|
||||
|
||||
g.DrawLine(pen, p1, p2);
|
||||
g.DrawLine(pen, p2, p3);
|
||||
g.DrawLine(pen, p3, p1);
|
||||
|
||||
g.FillPolygon(additionalBrush, [p1, p2, p3]);
|
||||
|
||||
}
|
||||
|
||||
// опоры
|
||||
if (excavator.Supports)
|
||||
{
|
||||
|
||||
g.DrawLine(bPen,
|
||||
_startPosX.Value + bucketWidth + bodyHeight, _startPosY.Value + cabineHeight + 10,
|
||||
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + cabineHeight + 10);
|
||||
g.DrawLine(bPen,
|
||||
_startPosX.Value + bucketWidth + bodyHeight +15, _startPosY.Value + cabineHeight + 10,
|
||||
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + bodyHeight + wheelsHeight);
|
||||
g.DrawLine(bPen,
|
||||
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + bodyHeight + wheelsHeight,
|
||||
_startPosX.Value + bucketWidth + bodyHeight + 30, _startPosY.Value + bodyHeight + wheelsHeight);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,18 @@
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Net.Sockets;
|
||||
using Excavator.Entities;
|
||||
|
||||
namespace Excavator;
|
||||
|
||||
namespace Excavator.Drawnings;
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// Класс, отвечающий за прорисовку и перемещение базового объекта сущности
|
||||
/// </summary>
|
||||
public class DrawningExcavator
|
||||
public class DrawningTrackedVehicle
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityExcavator? EntityExcavator { get; private set; }
|
||||
public EntityTrackedVehicle? EntityTrackedVehicle { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
@ -26,50 +27,94 @@ public class DrawningExcavator
|
||||
/// <summary>
|
||||
/// Левая координата прорисовки эскаватора
|
||||
/// </summary>
|
||||
private int? _startPosX;
|
||||
protected int? _startPosX;
|
||||
|
||||
/// <summary>
|
||||
/// Верхняя кооридната прорисовки экскаватора
|
||||
/// </summary>
|
||||
private int? _startPosY;
|
||||
protected int? _startPosY;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина прорисовки экскаватора
|
||||
/// </summary>
|
||||
private readonly int _drawningExcavatorWidth = 145;
|
||||
private readonly int _drawningExcavatorWidth = 110;
|
||||
|
||||
/// <summary>
|
||||
/// Высота прорисовки экскаватора
|
||||
/// </summary>
|
||||
private readonly int _drawningExcavatorHeight = 125;
|
||||
private readonly int _drawningExcavatorHeight = 126;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Инициализация свойств
|
||||
/// Координата X объекта
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="bucket">Признак наличия ковша</param>
|
||||
/// <param name="supports">Признак наличия опор</param>
|
||||
public int? GetPosX => _startPosX;
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bucket, bool supports)
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int? GetPosY => _startPosY;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _drawningExcavatorWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _drawningExcavatorHeight;
|
||||
/// <summary>
|
||||
/// Пустой конструктор
|
||||
/// </summary>
|
||||
private DrawningTrackedVehicle()
|
||||
{
|
||||
EntityExcavator = new EntityExcavator();
|
||||
EntityExcavator.Init(speed, weight, bodyColor, additionalColor, bucket, supports);
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
|
||||
|
||||
|
||||
public DrawningTrackedVehicle(int speed, double weight, Color bodyColor) : this()
|
||||
{
|
||||
EntityTrackedVehicle = new EntityTrackedVehicle(speed, weight, bodyColor);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор для наследников
|
||||
/// </summary>
|
||||
/// <param name="drawningExcavatorWidth">Ширина прорисовки гусеничной машины</param>
|
||||
/// <param name="drawningExcavatorHeight">Высота прорисовки гусеничной машины</param>
|
||||
|
||||
|
||||
|
||||
|
||||
protected DrawningTrackedVehicle(int drawningExcavatorWidth, int drawningExcavatorHeight) : this()
|
||||
{
|
||||
_drawningExcavatorWidth = drawningExcavatorWidth;
|
||||
_pictureHeight = drawningExcavatorHeight;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Установка границ поля
|
||||
/// </summary>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
|
||||
|
||||
|
||||
|
||||
public bool SetPictureSize(int width, int height)
|
||||
{
|
||||
@ -102,11 +147,6 @@ public class DrawningExcavator
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
@ -149,7 +189,7 @@ public class DrawningExcavator
|
||||
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
|
||||
public bool MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityExcavator == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
if (EntityTrackedVehicle == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -159,35 +199,33 @@ public class DrawningExcavator
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
|
||||
if (_startPosX.Value - EntityExcavator.Step > 0)
|
||||
if (_startPosX.Value - EntityTrackedVehicle.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityExcavator.Step;
|
||||
_startPosX -= (int)EntityTrackedVehicle.Step;
|
||||
}
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY.Value - EntityExcavator.Step > 0)
|
||||
if (_startPosY.Value - EntityTrackedVehicle.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityExcavator.Step;
|
||||
_startPosY -= (int)EntityTrackedVehicle.Step;
|
||||
}
|
||||
return true;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
//TODO прописать логику сдвига в право
|
||||
|
||||
if (_startPosX.Value + _drawningExcavatorWidth + EntityExcavator.Step < _pictureWidth)
|
||||
if (_startPosX.Value + _drawningExcavatorWidth + EntityTrackedVehicle.Step < _pictureWidth)
|
||||
{
|
||||
|
||||
_startPosX += (int)EntityExcavator.Step;
|
||||
_startPosX += (int)EntityTrackedVehicle.Step;
|
||||
|
||||
}
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
//TODO прописать логику сдвига в вниз
|
||||
if (_startPosY.Value + _drawningExcavatorHeight + EntityExcavator.Step < _pictureHeight)
|
||||
if (_startPosY.Value + _drawningExcavatorHeight + EntityTrackedVehicle.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityExcavator.Step;
|
||||
_startPosY += (int)EntityTrackedVehicle.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
@ -211,18 +249,11 @@ public class DrawningExcavator
|
||||
return path;
|
||||
}
|
||||
|
||||
// top left arc
|
||||
path.AddArc(arc, 180, 90);
|
||||
|
||||
// top right arc
|
||||
path.AddArc(arc, 180, 90);
|
||||
arc.X = bounds.Right - diameter;
|
||||
path.AddArc(arc, 270, 90);
|
||||
|
||||
// bottom right arc
|
||||
arc.Y = bounds.Bottom - diameter;
|
||||
path.AddArc(arc, 0, 90);
|
||||
|
||||
// bottom left arc
|
||||
arc.X = bounds.Left;
|
||||
path.AddArc(arc, 90, 90);
|
||||
|
||||
@ -239,27 +270,27 @@ public class DrawningExcavator
|
||||
/// <param name="g"></param>
|
||||
///
|
||||
|
||||
public void DrawTransport(Graphics g)
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityExcavator == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
if (EntityTrackedVehicle == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(EntityExcavator.AdditionalColor);
|
||||
|
||||
Brush brBlue = new SolidBrush(Color.LightBlue);
|
||||
Brush brGray = new SolidBrush(Color.Gray);
|
||||
Brush brRed = new SolidBrush(Color.Red);
|
||||
Brush brYellow = new SolidBrush(Color.Yellow);
|
||||
Brush brBlack = new SolidBrush(Color.Black);
|
||||
Brush bodycolor = new SolidBrush(EntityTrackedVehicle.BodyColor);
|
||||
|
||||
|
||||
|
||||
int bodyHeight = 90;
|
||||
int cabineHeight = 40;
|
||||
int pipeHeight = 35;
|
||||
int bucketWidth = 30;
|
||||
int bucketWidth = 10;
|
||||
int pipeWidth = 7;
|
||||
|
||||
int pipeOffsetX = 25;
|
||||
@ -270,7 +301,7 @@ public class DrawningExcavator
|
||||
|
||||
//кузов
|
||||
g.DrawRectangle(pen, _startPosX.Value + bucketWidth, _startPosY.Value + cabineHeight, bodyHeight, bodyHeight - cabineHeight);
|
||||
g.FillRectangle(brGray, _startPosX.Value + bucketWidth, _startPosY.Value + cabineHeight, bodyHeight, bodyHeight - cabineHeight);
|
||||
g.FillRectangle(bodycolor, _startPosX.Value + bucketWidth, _startPosY.Value + cabineHeight, bodyHeight, bodyHeight - cabineHeight);
|
||||
|
||||
//труба
|
||||
g.DrawRectangle(pen, _startPosX.Value + bucketWidth + pipeOffsetX, _startPosY.Value + cabineHeight - pipeHeight, pipeWidth, pipeHeight);
|
||||
@ -281,7 +312,7 @@ public class DrawningExcavator
|
||||
g.FillRectangle(brBlue, _startPosX.Value + bucketWidth + cabineOffsetX, _startPosY.Value, cabineHeight, cabineHeight);
|
||||
|
||||
//трак
|
||||
g.DrawPath(pen, RoundedRect(g, new Rectangle(_startPosX.Value + wheelsOffsetX, _startPosY.Value + bodyHeight, wheelsOffsetX + bodyHeight, wheelsHeight), 15));
|
||||
g.DrawPath(pen, RoundedRect(g, new Rectangle(_startPosX.Value , _startPosY.Value + bodyHeight, wheelsOffsetX + bodyHeight, wheelsHeight), 15));
|
||||
|
||||
Pen bPen = new(Color.Black);
|
||||
bPen.Width = 3;
|
||||
@ -313,35 +344,9 @@ public class DrawningExcavator
|
||||
g.DrawEllipse(bPen, _startPosX.Value + bucketWidth + wheelsOffsetX + 13, _startPosY.Value + bodyHeight + wheelsHeight / 6, 9, 9);
|
||||
g.DrawEllipse(bPen, _startPosX.Value + bucketWidth + wheelsOffsetX + 28, _startPosY.Value + bodyHeight + wheelsHeight / 6, 9, 9);
|
||||
g.FillEllipse(brYellow, _startPosX.Value + bucketWidth + wheelsOffsetX + 13, _startPosY.Value + bodyHeight + wheelsHeight / 6, 9, 9);
|
||||
g.FillEllipse(brYellow, _startPosX.Value + bucketWidth + wheelsOffsetX + 28, _startPosY.Value + bodyHeight + wheelsHeight / 6, 9, 9);
|
||||
|
||||
// ковш
|
||||
if (EntityExcavator.Bucket)
|
||||
{
|
||||
PointF p1 = new Point(_startPosX.Value + bucketWidth, _startPosY.Value + cabineHeight);
|
||||
PointF p2 = new Point(_startPosX.Value, _startPosY.Value + cabineHeight);
|
||||
PointF p3 = new Point(_startPosX.Value + bucketWidth, _startPosY.Value + cabineHeight + bodyHeight / 2);
|
||||
|
||||
g.DrawLine(pen, p1, p2);
|
||||
g.DrawLine(pen, p2, p3);
|
||||
g.DrawLine(pen, p3, p1);
|
||||
|
||||
g.FillPolygon(additionalBrush, [p1, p2, p3]);
|
||||
}
|
||||
|
||||
// опоры
|
||||
if (EntityExcavator.Supports)
|
||||
{
|
||||
g.DrawLine(bPen,
|
||||
_startPosX.Value + bucketWidth + bodyHeight, _startPosY.Value + cabineHeight + 10,
|
||||
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + cabineHeight + 10);
|
||||
g.DrawLine(bPen,
|
||||
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + cabineHeight + 10,
|
||||
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + bodyHeight + wheelsHeight);
|
||||
g.DrawLine(bPen,
|
||||
_startPosX.Value + bucketWidth + bodyHeight + 15, _startPosY.Value + bodyHeight + wheelsHeight,
|
||||
_startPosX.Value + bucketWidth + bodyHeight + 30, _startPosY.Value + bodyHeight + wheelsHeight);
|
||||
}
|
||||
g.FillEllipse(brYellow, _startPosX.Value + bucketWidth + wheelsOffsetX + 28, _startPosY.Value + bodyHeight + wheelsHeight / 6, 9, 9);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,26 +1,10 @@
|
||||
namespace Excavator;
|
||||
namespace Excavator.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Класс-сущность "Экскаватор"
|
||||
/// </summary>
|
||||
public class EntityExcavator
|
||||
public class EntityExcavator : EntityTrackedVehicle
|
||||
{
|
||||
|
||||
public int m;
|
||||
/// <summary>
|
||||
/// Скорость
|
||||
/// </summary>
|
||||
public int Speed { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Вес
|
||||
/// </summary>
|
||||
public double Weight { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Основной цвет
|
||||
/// </summary>
|
||||
public Color BodyColor { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Дополнительный цвет (для опциональных элементов)
|
||||
@ -45,20 +29,17 @@ public class EntityExcavator
|
||||
public double Step => Speed * 100 / Weight;
|
||||
|
||||
/// <summary>
|
||||
/// Инициализация полей объекта-класса спортивного автомобиля
|
||||
/// Инициализация полей объекта-класса экскаватора
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес автомобиля</param>
|
||||
/// <param name="weight">Вес экскаватора</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="bucket">Признак наличия ковша</param>
|
||||
/// <param name="supports">Признак наличия опор</param>
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bucket, bool supports)
|
||||
public EntityExcavator(int speed, double weight, Color bodyColor, Color additionalColor, bool bucket, bool supports) : base(speed, weight, bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
AdditionalColor = additionalColor;
|
||||
Bucket = bucket;
|
||||
Supports = supports;
|
50
Excavator/Excavator/Entities/EntityTrackedVehicle.cs
Normal file
50
Excavator/Excavator/Entities/EntityTrackedVehicle.cs
Normal file
@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Excavator.Entities;
|
||||
/// <summary>
|
||||
/// Класс-сущность "Гусеничная машина"
|
||||
/// </summary>
|
||||
public class EntityTrackedVehicle
|
||||
{
|
||||
public int m;
|
||||
/// <summary>
|
||||
/// Скорость
|
||||
/// </summary>
|
||||
public int Speed { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Вес
|
||||
/// </summary>
|
||||
public double Weight { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Основной цвет
|
||||
/// </summary>
|
||||
public Color BodyColor { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Шаг перемещения экскаватора
|
||||
/// </summary>
|
||||
public double Step => Speed * 100 / Weight;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор сущности
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес автомобиля</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
|
||||
|
||||
public EntityTrackedVehicle(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
|
||||
}
|
||||
}
|
65
Excavator/Excavator/FormExcavator.Designer.cs
generated
65
Excavator/Excavator/FormExcavator.Designer.cs
generated
@ -29,11 +29,14 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
pictureBoxExcavator = new PictureBox();
|
||||
buttonCreate = new Button();
|
||||
buttonCreateExcavator = new Button();
|
||||
buttonLeft = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonUp = new Button();
|
||||
buttonDown = new Button();
|
||||
buttonCreateTrackedVehicle = new Button();
|
||||
ComboBoxStrategy = new ComboBox();
|
||||
buttonStrategyStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxExcavator).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -47,16 +50,16 @@
|
||||
pictureBoxExcavator.TabStop = false;
|
||||
|
||||
//
|
||||
// buttonCreate
|
||||
// buttonCreateExcavator
|
||||
//
|
||||
buttonCreate.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreate.Location = new Point(12, 366);
|
||||
buttonCreate.Name = "buttonCreate";
|
||||
buttonCreate.Size = new Size(94, 29);
|
||||
buttonCreate.TabIndex = 1;
|
||||
buttonCreate.Text = "Создать";
|
||||
buttonCreate.UseVisualStyleBackColor = true;
|
||||
buttonCreate.Click += buttonCreate_Click;
|
||||
buttonCreateExcavator.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateExcavator.Location = new Point(12, 366);
|
||||
buttonCreateExcavator.Name = "buttonCreateExcavator";
|
||||
buttonCreateExcavator.Size = new Size(154, 29);
|
||||
buttonCreateExcavator.TabIndex = 1;
|
||||
buttonCreateExcavator.Text = "Создать экскаватор";
|
||||
buttonCreateExcavator.UseVisualStyleBackColor = true;
|
||||
buttonCreateExcavator.Click += buttonCreateExcavator_Click;
|
||||
//
|
||||
// buttonLeft
|
||||
//
|
||||
@ -106,20 +109,53 @@
|
||||
buttonDown.UseVisualStyleBackColor = true;
|
||||
buttonDown.Click += ButtonMove_Click;
|
||||
//
|
||||
// buttonCreateTrackedVehicle
|
||||
//
|
||||
buttonCreateTrackedVehicle.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateTrackedVehicle.Location = new Point(183, 366);
|
||||
buttonCreateTrackedVehicle.Name = "buttonCreateTrackedVehicle";
|
||||
buttonCreateTrackedVehicle.Size = new Size(238, 29);
|
||||
buttonCreateTrackedVehicle.TabIndex = 6;
|
||||
buttonCreateTrackedVehicle.Text = "Создать гусеничную машину";
|
||||
buttonCreateTrackedVehicle.UseVisualStyleBackColor = true;
|
||||
buttonCreateTrackedVehicle.Click += buttonCreateTrackedVehicle_Click;
|
||||
//
|
||||
// ComboBoxStrategy
|
||||
//
|
||||
ComboBoxStrategy.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
ComboBoxStrategy.FormattingEnabled = true;
|
||||
ComboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю" });
|
||||
ComboBoxStrategy.Location = new Point(661, 12);
|
||||
ComboBoxStrategy.Name = "ComboBoxStrategy";
|
||||
ComboBoxStrategy.Size = new Size(151, 28);
|
||||
ComboBoxStrategy.TabIndex = 7;
|
||||
//
|
||||
// buttonStrategyStep
|
||||
//
|
||||
buttonStrategyStep.Location = new Point(710, 46);
|
||||
buttonStrategyStep.Name = "buttonStrategyStep";
|
||||
buttonStrategyStep.Size = new Size(102, 27);
|
||||
buttonStrategyStep.TabIndex = 8;
|
||||
buttonStrategyStep.Text = "Шаг";
|
||||
buttonStrategyStep.UseVisualStyleBackColor = true;
|
||||
buttonStrategyStep.Click += buttonStrategyStep_Click;
|
||||
//
|
||||
// FormExcavator
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(824, 407);
|
||||
Controls.Add(buttonStrategyStep);
|
||||
Controls.Add(ComboBoxStrategy);
|
||||
Controls.Add(buttonCreateTrackedVehicle);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonUp);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonLeft);
|
||||
Controls.Add(buttonCreate);
|
||||
Controls.Add(buttonCreateExcavator);
|
||||
Controls.Add(pictureBoxExcavator);
|
||||
Name = "FormExcavator";
|
||||
Text = "Экскаватор";
|
||||
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxExcavator).EndInit();
|
||||
ResumeLayout(false);
|
||||
}
|
||||
@ -127,10 +163,13 @@
|
||||
#endregion
|
||||
|
||||
private PictureBox pictureBoxExcavator;
|
||||
private Button buttonCreate;
|
||||
private Button buttonCreateExcavator;
|
||||
private Button buttonLeft;
|
||||
private Button buttonRight;
|
||||
private Button buttonUp;
|
||||
private Button buttonDown;
|
||||
private Button buttonCreateTrackedVehicle;
|
||||
private ComboBox ComboBoxStrategy;
|
||||
private Button buttonStrategyStep;
|
||||
}
|
||||
}
|
@ -1,4 +1,7 @@
|
||||
namespace Excavator;
|
||||
using Excavator.Drawnings;
|
||||
using Excavator.MovementStrategy;
|
||||
|
||||
namespace Excavator;
|
||||
|
||||
/// <summary>
|
||||
/// Форма работы с объектом "Экскаватор"
|
||||
@ -8,8 +11,11 @@ public partial class FormExcavator : Form
|
||||
/// <summary>
|
||||
/// Поле-объект для прорисовки объекта
|
||||
/// </summary>
|
||||
private DrawningExcavator? _drawningExcavator;
|
||||
|
||||
private DrawningTrackedVehicle? _drawningTrackedVehicle;
|
||||
/// <summary>
|
||||
///Стратегия перемещения
|
||||
/// </summary>
|
||||
private AbstractStrategy? _strategy;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор формы
|
||||
@ -17,6 +23,7 @@ public partial class FormExcavator : Form
|
||||
public FormExcavator()
|
||||
{
|
||||
InitializeComponent();
|
||||
_strategy = null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Метод прорисовки экскаватора
|
||||
@ -24,76 +31,147 @@ public partial class FormExcavator : Form
|
||||
|
||||
private void Draw()
|
||||
{
|
||||
if(_drawningExcavator == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Bitmap bmp = new(pictureBoxExcavator.Width, pictureBoxExcavator.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningExcavator.DrawTransport(gr);
|
||||
pictureBoxExcavator.Image = bmp;
|
||||
if (_drawningTrackedVehicle == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Bitmap bmp = new(pictureBoxExcavator.Width, pictureBoxExcavator.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningTrackedVehicle.DrawTransport(gr);
|
||||
pictureBoxExcavator.Image = bmp;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Создание объекта класса-перемещения
|
||||
/// </summary>
|
||||
/// <param name="type">Тип создаваемого объекта</param>
|
||||
private void CreateObject(string type)
|
||||
{
|
||||
Random random = new();
|
||||
switch (type)
|
||||
{
|
||||
case nameof(DrawningTrackedVehicle):
|
||||
_drawningTrackedVehicle = new DrawningTrackedVehicle(random.Next(100, 300), random.Next(1000, 3000),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)));
|
||||
break;
|
||||
case nameof(DrawningExcavator):
|
||||
_drawningTrackedVehicle = new DrawningExcavator(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)));
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
_drawningTrackedVehicle.SetPictureSize(pictureBoxExcavator.Width, pictureBoxExcavator.Height);
|
||||
_drawningTrackedVehicle.SetPosition(random.Next(0, 100), random.Next(0, 100));
|
||||
_strategy = null;
|
||||
ComboBoxStrategy.Enabled = true;
|
||||
Draw();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать"
|
||||
/// Обработка нажатия кнопки "Создать экскаватор"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void buttonCreate_Click(object sender, EventArgs e)
|
||||
private void buttonCreateExcavator_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningExcavator));
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать гусеничную машину"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void buttonCreateTrackedVehicle_Click(object sender, EventArgs e) => CreateObject(nameof(DrawningTrackedVehicle));
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение объекта по форме (нажатие кнопок навигации)
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
Random random = new();
|
||||
_drawningExcavator = new DrawningExcavator();
|
||||
_drawningExcavator.Init(random.Next(100, 300), random.Next(1000, 3000),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
||||
Color.FromArgb(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256)),
|
||||
Convert.ToBoolean(random.Next(0, 2)), Convert.ToBoolean(random.Next(0, 2)));
|
||||
_drawningExcavator.SetPictureSize(pictureBoxExcavator.Width, pictureBoxExcavator.Height);
|
||||
_drawningExcavator.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
if (_drawningTrackedVehicle == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||||
bool result = false;
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
result = _drawningTrackedVehicle.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result = _drawningTrackedVehicle.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result = _drawningTrackedVehicle.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result = _drawningTrackedVehicle.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
|
||||
if (result)
|
||||
{
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение объекта по форме (нажатие кнопок навигации)
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void buttonStrategyStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningTrackedVehicle == null)
|
||||
{
|
||||
if (_drawningExcavator == null)
|
||||
return;
|
||||
}
|
||||
|
||||
if (ComboBoxStrategy.Enabled)
|
||||
{
|
||||
_strategy = ComboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string name = ((Button)sender)?.Name ?? string.Empty;
|
||||
bool result = false;
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
result = _drawningExcavator.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result = _drawningExcavator.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result = _drawningExcavator.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result = _drawningExcavator.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
|
||||
if (result)
|
||||
{
|
||||
Draw();
|
||||
}
|
||||
_strategy.SetData(new MoveableTrackedVehicle(_drawningTrackedVehicle), pictureBoxExcavator.Width, pictureBoxExcavator.Height);
|
||||
}
|
||||
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ComboBoxStrategy.Enabled = false;
|
||||
_strategy.MakeStep();
|
||||
Draw();
|
||||
|
||||
if (_strategy.GetStatus() == StrategyStatus.Finish)
|
||||
{
|
||||
ComboBoxStrategy.Enabled = true;
|
||||
_strategy = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
139
Excavator/Excavator/MovementStrategy/AbstractStrategy.cs
Normal file
139
Excavator/Excavator/MovementStrategy/AbstractStrategy.cs
Normal file
@ -0,0 +1,139 @@
|
||||
namespace Excavator.MovementStrategy;
|
||||
|
||||
/// <summary>
|
||||
/// Класс-стратегия перемещения объекта
|
||||
/// </summary>
|
||||
public abstract class AbstractStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Перемещаемый объект
|
||||
/// </summary>
|
||||
private IMoveableObject? _moveableObject;
|
||||
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
private StrategyStatus _state = StrategyStatus.NotInit;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина поля
|
||||
/// </summary>
|
||||
protected int FieldWidth { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Высота поля
|
||||
/// </summary>
|
||||
protected int FieldHeight { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Статус перемещения
|
||||
/// </summary>
|
||||
public StrategyStatus GetStatus() { return _state; }
|
||||
|
||||
/// <summary>
|
||||
/// Установка данных
|
||||
/// </summary>
|
||||
/// <param name="moveableObject">Перемещаемый объект</param>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
public void SetData(IMoveableObject moveableObject, int width, int height)
|
||||
{
|
||||
if (moveableObject == null)
|
||||
{
|
||||
_state = StrategyStatus.NotInit;
|
||||
return;
|
||||
}
|
||||
|
||||
_state = StrategyStatus.InProgress;
|
||||
_moveableObject = moveableObject;
|
||||
FieldWidth = width;
|
||||
FieldHeight = height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Шаг перемещения
|
||||
/// </summary>
|
||||
public void MakeStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsTargetDestinaion())
|
||||
{
|
||||
_state = StrategyStatus.Finish;
|
||||
return;
|
||||
}
|
||||
|
||||
MoveToTarget();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение влево
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveLeft() => MoveTo(MovementDirection.Left);
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение вправо
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveRight() => MoveTo(MovementDirection.Right);
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение вверх
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveUp() => MoveTo(MovementDirection.Up);
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение вниз
|
||||
/// </summary>
|
||||
/// <returns>Результат перемещения (true - удалось переместиться, false - неудача)</returns>
|
||||
protected bool MoveDown() => MoveTo(MovementDirection.Down);
|
||||
|
||||
/// <summary>
|
||||
/// Параметры объекта
|
||||
/// </summary>
|
||||
protected ObjectParameters? GetObjectParameters => _moveableObject?.GetObjectPosition;
|
||||
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected int? GetStep()
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _moveableObject?.GetStep;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Перемещение к цели
|
||||
/// </summary>
|
||||
protected abstract void MoveToTarget();
|
||||
|
||||
/// <summary>
|
||||
/// Достигнута ли цель
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected abstract bool IsTargetDestinaion();
|
||||
|
||||
/// <summary>
|
||||
/// Попытка перемещения в требуемом направлении
|
||||
/// </summary>
|
||||
/// <param name="movementDirection">Направление</param>
|
||||
/// <returns>Результат попытки (true - удалось переместиться, false - неудача)</returns>
|
||||
private bool MoveTo(MovementDirection movementDirection)
|
||||
{
|
||||
if (_state != StrategyStatus.InProgress)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _moveableObject?.TryMoveObject(movementDirection) ?? false;
|
||||
}
|
||||
}
|
24
Excavator/Excavator/MovementStrategy/IMoveableObject.cs
Normal file
24
Excavator/Excavator/MovementStrategy/IMoveableObject.cs
Normal file
@ -0,0 +1,24 @@
|
||||
namespace Excavator.MovementStrategy;
|
||||
|
||||
/// <summary>
|
||||
/// Интерфейс для работы с перемещаемым объектом
|
||||
/// </summary>
|
||||
public interface IMoveableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение координаты объекта
|
||||
/// </summary>
|
||||
ObjectParameters? GetObjectPosition { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Шаг объекта
|
||||
/// </summary>
|
||||
int GetStep { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Попытка переместить объект в указанном направлении
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - объект перемещен, false - перемещение невозможно</returns>
|
||||
bool TryMoveObject(MovementDirection direction);
|
||||
}
|
51
Excavator/Excavator/MovementStrategy/MoveToBorder.cs
Normal file
51
Excavator/Excavator/MovementStrategy/MoveToBorder.cs
Normal file
@ -0,0 +1,51 @@
|
||||
namespace Excavator.MovementStrategy;
|
||||
/// <summary>
|
||||
/// Стратегия перемещения объекта к границе
|
||||
/// </summary>
|
||||
public 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 = objParams.ObjectMiddleHorizontal - FieldWidth;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
var diffY = objParams.ObjectMiddleVertical - FieldHeight;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
53
Excavator/Excavator/MovementStrategy/MoveToCenter.cs
Normal file
53
Excavator/Excavator/MovementStrategy/MoveToCenter.cs
Normal file
@ -0,0 +1,53 @@
|
||||
namespace Excavator.MovementStrategy;
|
||||
/// <summary>
|
||||
/// Стратегия перемещения объекта в центр экрана
|
||||
/// </summary>
|
||||
public class MoveToCenter : AbstractStrategy
|
||||
{
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return objParams.ObjectMiddleHorizontal - GetStep() <= FieldWidth / 2 && objParams.ObjectMiddleHorizontal + GetStep() >= FieldWidth / 2 &&
|
||||
objParams.ObjectMiddleVertical - GetStep() <= FieldHeight / 2 && objParams.ObjectMiddleVertical + GetStep() >= FieldHeight / 2;
|
||||
}
|
||||
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int diffX = objParams.ObjectMiddleHorizontal - FieldWidth / 2;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
if (diffX > 0)
|
||||
{
|
||||
MoveLeft();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
}
|
||||
|
||||
int diffY = objParams.ObjectMiddleVertical - FieldHeight / 2;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
if (diffY > 0)
|
||||
{
|
||||
MoveUp();
|
||||
}
|
||||
else
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
using Excavator.Drawnings;
|
||||
|
||||
|
||||
namespace Excavator.MovementStrategy;
|
||||
/// <summary>
|
||||
/// Класс-реализация IMoveableObject с использованием DrawningTrackedVehicle
|
||||
/// </summary>
|
||||
public class MoveableTrackedVehicle : IMoveableObject
|
||||
|
||||
{
|
||||
/// <summary>
|
||||
/// Поле-объект класса DrawningTrackedVehicle или его наследника
|
||||
/// </summary>
|
||||
private readonly DrawningTrackedVehicle? _trackedvehicle = null;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="car">Объект класса DrawningCar</param>
|
||||
public MoveableTrackedVehicle(DrawningTrackedVehicle trackedvehicle)
|
||||
{
|
||||
_trackedvehicle = trackedvehicle;
|
||||
}
|
||||
|
||||
public ObjectParameters? GetObjectPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_trackedvehicle == null || _trackedvehicle.EntityTrackedVehicle == null || !_trackedvehicle.GetPosX.HasValue || !_trackedvehicle.GetPosY.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_trackedvehicle.GetPosX.Value, _trackedvehicle.GetPosY.Value, _trackedvehicle.GetWidth, _trackedvehicle.GetHeight);
|
||||
}
|
||||
}
|
||||
|
||||
public int GetStep => (int)(_trackedvehicle?.EntityTrackedVehicle?.Step ?? 0);
|
||||
|
||||
public bool TryMoveObject(MovementDirection direction)
|
||||
{
|
||||
if (_trackedvehicle == null || _trackedvehicle.EntityTrackedVehicle == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _trackedvehicle.MoveTransport(GetDirectionType(direction));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конвертация из MovementDirection в DirectionType
|
||||
/// </summary>
|
||||
/// <param name="direction">MovementDirection</param>
|
||||
/// <returns>DirectionType</returns>
|
||||
private static DirectionType GetDirectionType(MovementDirection direction)
|
||||
{
|
||||
return direction switch
|
||||
{
|
||||
MovementDirection.Left => DirectionType.Left,
|
||||
MovementDirection.Right => DirectionType.Right,
|
||||
MovementDirection.Up => DirectionType.Up,
|
||||
MovementDirection.Down => DirectionType.Down,
|
||||
_ => DirectionType.Unknow,
|
||||
};
|
||||
}
|
||||
}
|
@ -1,27 +1,23 @@
|
||||
namespace Excavator;
|
||||
|
||||
namespace Excavator.MovementStrategy;
|
||||
/// <summary>
|
||||
/// Направление перемещения
|
||||
/// </summary>
|
||||
public enum DirectionType
|
||||
public enum MovementDirection
|
||||
{
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
||||
Up = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Вниз
|
||||
/// </summary>
|
||||
Down = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Влево
|
||||
/// </summary>
|
||||
Left = 3,
|
||||
|
||||
/// <summary>
|
||||
/// Вправо
|
||||
/// </summary>
|
||||
Right = 4
|
||||
}
|
||||
}
|
72
Excavator/Excavator/MovementStrategy/ObjectParameters.cs
Normal file
72
Excavator/Excavator/MovementStrategy/ObjectParameters.cs
Normal file
@ -0,0 +1,72 @@
|
||||
namespace Excavator.MovementStrategy;
|
||||
|
||||
/// <summary>
|
||||
/// Параметры-координаты объекта
|
||||
/// </summary>
|
||||
public class ObjectParameters
|
||||
{
|
||||
/// <summary>
|
||||
/// Координата X
|
||||
/// </summary>
|
||||
private readonly int _x;
|
||||
|
||||
/// <summary>
|
||||
/// Координата Y
|
||||
/// </summary>
|
||||
private readonly int _y;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
private readonly int _width;
|
||||
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
private readonly int _height;
|
||||
|
||||
/// <summary>
|
||||
/// Левая граница
|
||||
/// </summary>
|
||||
public int LeftBorder => _x;
|
||||
|
||||
/// <summary>
|
||||
/// Верхняя граница
|
||||
/// </summary>
|
||||
public int TopBorder => _y;
|
||||
|
||||
/// <summary>
|
||||
/// Правая граница
|
||||
/// </summary>
|
||||
public int RightBorder => _x + _width;
|
||||
|
||||
/// <summary>
|
||||
/// Нижняя граница
|
||||
/// </summary>
|
||||
public int DownBorder => _y + _height;
|
||||
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleHorizontal => _x + _width / 2;
|
||||
|
||||
/// <summary>
|
||||
/// Середина объекта
|
||||
/// </summary>
|
||||
public int ObjectMiddleVertical => _y + _height / 2;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
/// <param name="width">Ширина объекта</param>
|
||||
/// <param name="height">Высота объекта</param>
|
||||
public ObjectParameters(int x, int y, int width, int height)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
}
|
19
Excavator/Excavator/MovementStrategy/StrategyStatus.cs
Normal file
19
Excavator/Excavator/MovementStrategy/StrategyStatus.cs
Normal file
@ -0,0 +1,19 @@
|
||||
namespace Excavator.MovementStrategy;
|
||||
/// <summary>
|
||||
/// Статус выполнения операции перемещения
|
||||
/// </summary>
|
||||
public enum StrategyStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// Всё готово к началу
|
||||
/// </summary>
|
||||
NotInit,
|
||||
/// <summary>
|
||||
/// Выполняется
|
||||
/// </summary>
|
||||
InProgress,
|
||||
/// <summary>
|
||||
/// Завершено
|
||||
/// </summary>
|
||||
Finish
|
||||
}
|
Loading…
Reference in New Issue
Block a user