PIbd_13 Klykov_N.V.Lab02 Simple #2
@ -1,302 +0,0 @@
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Excavator;
|
||||
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawningExcavator
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntityExcavator? EntityExcavator { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
/// </summary>
|
||||
private int? _pictureWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Высота окна
|
||||
/// </summary>
|
||||
private int? _pictureHeight;
|
||||
|
||||
/// <summary>
|
||||
/// Левая координата прорисовки экскаваторя
|
||||
/// </summary>
|
||||
private int? _startPosX;
|
||||
|
||||
/// <summary>
|
||||
/// Верхняя кооридната прорисовки экскаваторя
|
||||
/// </summary>
|
||||
private int? _startPosY;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина прорисовки экскаватора
|
||||
/// </summary>
|
||||
private readonly int _drawningExcavatorWidth = 145;
|
||||
|
||||
/// <summary>
|
||||
/// Высота прорисовки экскаватора
|
||||
/// </summary>
|
||||
private readonly int _drawningExcavatorHeight = 100;
|
||||
|
||||
/// <summary>
|
||||
/// Инициализация свойств
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="supports">Признак наличия обвеса</param>
|
||||
/// <param name="bucket">Признак наличия антикрыла</param>
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor,bool supports, bool bucket)
|
||||
{
|
||||
EntityExcavator = new EntityExcavator();
|
||||
EntityExcavator.Init(speed, weight, bodyColor, additionalColor, supports, bucket);
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установка границ поля
|
||||
/// </summary>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
|
||||
|
||||
public bool SetPictureSize(int width, int height)
|
||||
{
|
||||
if (width > _drawningExcavatorWidth && height > _drawningExcavatorHeight)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_startPosX != null && _startPosY != null)
|
||||
{
|
||||
if (_startPosX.Value + _drawningExcavatorWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawningExcavatorWidth;
|
||||
}
|
||||
if (_startPosY.Value + _drawningExcavatorHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawningExcavatorHeight;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
|
||||
if (_drawningExcavatorWidth + _startPosX > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawningExcavatorWidth;
|
||||
}
|
||||
if (_drawningExcavatorHeight + _startPosY > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawningExcavatorHeight;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
|
||||
public bool MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntityExcavator == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX.Value - EntityExcavator.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntityExcavator.Step;
|
||||
}
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY.Value - EntityExcavator.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntityExcavator.Step;
|
||||
}
|
||||
return true;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX.Value + _drawningExcavatorWidth + EntityExcavator.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntityExcavator.Step;
|
||||
}
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY.Value + _drawningExcavatorHeight + EntityExcavator.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntityExcavator.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntityExcavator == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(EntityExcavator.AdditionalColor);
|
||||
|
||||
// Ковш
|
||||
if(EntityExcavator.Bucket)
|
||||
{
|
||||
|
||||
Point[] pointBucket1 =
|
||||
{
|
||||
new Point(_startPosX.Value + 90, _startPosY.Value + 50),
|
||||
new Point(_startPosX.Value + 90, _startPosY.Value),
|
||||
new Point(_startPosX.Value + 135, _startPosY.Value),
|
||||
new Point(_startPosX.Value + 135, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 125, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 125, _startPosY.Value + 10),
|
||||
new Point(_startPosX.Value + 105, _startPosY.Value + 10),
|
||||
new Point(_startPosX.Value + 105, _startPosY.Value + 45),
|
||||
new Point(_startPosX.Value + 100, _startPosY.Value + 50),
|
||||
new Point(_startPosX.Value + 90, _startPosY.Value + 50),
|
||||
};
|
||||
g.FillPolygon(additionalBrush, pointBucket1);
|
||||
g.DrawPolygon(pen, pointBucket1);
|
||||
|
||||
Point[] pointBucket2 =
|
||||
{
|
||||
new Point(_startPosX.Value + 125, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 135, _startPosY.Value+ 30),
|
||||
new Point(_startPosX.Value + 145, _startPosY.Value + 40),
|
||||
new Point(_startPosX.Value + 145, _startPosY.Value + 60),
|
||||
new Point(_startPosX.Value + 140, _startPosY.Value + 70),
|
||||
new Point(_startPosX.Value + 120, _startPosY.Value + 70),
|
||||
new Point(_startPosX.Value + 125, _startPosY.Value + 60),
|
||||
new Point(_startPosX.Value + 125, _startPosY.Value + 30),
|
||||
};
|
||||
Brush br1 = new SolidBrush(Color.FromArgb(100, 100, 100));
|
||||
g.FillPolygon(br1, pointBucket2);
|
||||
g.DrawPolygon(pen, pointBucket2);
|
||||
}
|
||||
|
||||
// Гусеницы
|
||||
Brush brGrey = new SolidBrush(Color.FromArgb(100, 100, 100));
|
||||
Brush br = new SolidBrush(EntityExcavator.BodyColor);
|
||||
|
||||
g.FillEllipse(brGrey, _startPosX.Value + 30, _startPosY.Value + 75, 25, 25);
|
||||
g.FillEllipse(brGrey, _startPosX.Value + 75, _startPosY.Value + 75, 25, 25);
|
||||
g.FillRectangle(brGrey, _startPosX.Value + 40, _startPosY.Value + 75, 50, 25);
|
||||
|
||||
Rectangle rect1 = new Rectangle(_startPosX.Value + 30, _startPosY.Value + 75, 25, 25);
|
||||
g.DrawArc(pen, rect1, 90, 180);
|
||||
Rectangle rect2 = new Rectangle(_startPosX.Value + 75, _startPosY.Value + 75, 25, 25);
|
||||
g.DrawArc(pen, rect2, 270, 180);
|
||||
g.DrawLine(pen, _startPosX.Value + 40, _startPosY.Value + 100, _startPosX.Value + 90, _startPosY.Value + 100);
|
||||
|
||||
|
||||
g.FillEllipse(br, _startPosX.Value + 33, _startPosY.Value + 78, 18, 18);
|
||||
g.FillEllipse(br, _startPosX.Value + 78, _startPosY.Value + 78, 18, 18);
|
||||
g.FillEllipse(br, _startPosX.Value + 51, _startPosY.Value + 89, 7, 7);
|
||||
g.FillEllipse(br, _startPosX.Value + 61, _startPosY.Value + 89, 7, 7);
|
||||
g.FillEllipse(br, _startPosX.Value + 71, _startPosY.Value + 89, 7, 7);
|
||||
g.FillEllipse(br, _startPosX.Value + 57, _startPosY.Value + 81, 4, 4);
|
||||
g.FillEllipse(br, _startPosX.Value + 69, _startPosY.Value + 81, 4, 4);
|
||||
|
||||
g.DrawEllipse(pen, _startPosX.Value + 33, _startPosY.Value + 78, 18, 18);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 78, _startPosY.Value + 78, 18, 18);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 51, _startPosY.Value + 89, 7, 7);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 61, _startPosY.Value + 89, 7, 7);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 71, _startPosY.Value + 89, 7, 7);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 57, _startPosY.Value + 81, 4, 4);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 69, _startPosY.Value + 81, 4, 4);
|
||||
|
||||
// Корпус
|
||||
Point[] pointBody1 =
|
||||
{
|
||||
new Point(_startPosX.Value + 30, _startPosY.Value + 75),
|
||||
new Point(_startPosX.Value + 30, _startPosY.Value + 50),
|
||||
new Point(_startPosX.Value + 70, _startPosY.Value + 50),
|
||||
new Point(_startPosX.Value + 70, _startPosY.Value + 20),
|
||||
new Point(_startPosX.Value + 100, _startPosY.Value + 20),
|
||||
new Point(_startPosX.Value + 100, _startPosY.Value + 75),
|
||||
new Point(_startPosX.Value + 30, _startPosY.Value + 75),
|
||||
};
|
||||
g.FillPolygon(br, pointBody1);
|
||||
g.DrawPolygon(pen, pointBody1);
|
||||
g.FillRectangle(br, _startPosX.Value + 40, _startPosY.Value + 25, 10, 25);
|
||||
g.FillRectangle(br, _startPosX.Value + 40, _startPosY.Value + 25, 10, 25);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 40, _startPosY.Value + 25, 10, 25);
|
||||
|
||||
// Окно
|
||||
Brush brLBlue = new SolidBrush(Color.LightBlue);
|
||||
|
||||
g.FillRectangle(brLBlue, _startPosX.Value + 72, _startPosY.Value + 22, 26, 26);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 72, _startPosY.Value + 22, 26, 26);
|
||||
|
||||
// Опоры
|
||||
if (EntityExcavator.Supports)
|
||||
{
|
||||
Point[] pointSupport1 =
|
||||
{
|
||||
new Point(_startPosX.Value + 10, _startPosY.Value + 100),
|
||||
new Point(_startPosX.Value + 20, _startPosY.Value + 100),
|
||||
new Point(_startPosX.Value + 50, _startPosY.Value + 70),
|
||||
new Point(_startPosX.Value + 50, _startPosY.Value + 60),
|
||||
new Point(_startPosX.Value + 30, _startPosY.Value + 60),
|
||||
new Point(_startPosX.Value + 10, _startPosY.Value + 100),
|
||||
};
|
||||
g.FillPolygon(additionalBrush, pointSupport1);
|
||||
g.DrawPolygon(pen, pointSupport1);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 95, 30, 5);
|
||||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 95, 30, 5);
|
||||
|
||||
Point[] pointSupport2 =
|
||||
{
|
||||
new Point(_startPosX.Value + 120, _startPosY.Value + 100),
|
||||
new Point(_startPosX.Value + 110, _startPosY.Value + 100),
|
||||
new Point(_startPosX.Value + 80, _startPosY.Value + 70),
|
||||
new Point(_startPosX.Value + 80, _startPosY.Value + 60),
|
||||
new Point(_startPosX.Value + 100, _startPosY.Value + 60),
|
||||
new Point(_startPosX.Value + 120, _startPosY.Value + 100),
|
||||
};
|
||||
g.FillPolygon(additionalBrush, pointSupport2);
|
||||
g.DrawPolygon(pen, pointSupport2);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 100, _startPosY.Value + 95, 30, 5);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value + 95, 30, 5);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
32
Excavator/Excavator/Drawnings/DirectionType.cs
Normal file
32
Excavator/Excavator/Drawnings/DirectionType.cs
Normal file
@ -0,0 +1,32 @@
|
||||
namespace Excavator;
|
||||
|
||||
/// <summary>
|
||||
/// Направление перемещения
|
||||
/// </summary>
|
||||
public enum DirectionType
|
||||
{
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
/// </summary>
|
||||
Up = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Вниз
|
||||
/// </summary>
|
||||
Down = 2,
|
||||
|
||||
/// <summary>
|
||||
/// Влево
|
||||
/// </summary>
|
||||
Left = 3,
|
||||
|
||||
/// <summary>
|
||||
/// Вправо
|
||||
/// </summary>
|
||||
Right = 4,
|
||||
|
||||
/// <summary>
|
||||
/// Неизвестное направление
|
||||
/// </summary>
|
||||
Unknow = -1
|
||||
}
|
110
Excavator/Excavator/Drawnings/DrawningExcavator.cs
Normal file
110
Excavator/Excavator/Drawnings/DrawningExcavator.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using System.Net.Sockets;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using Excavator.Drawnings;
|
||||
using Excavator.Entities;
|
||||
|
||||
namespace Excavator.Drawnings;
|
||||
|
||||
/// <summary>
|
||||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||||
/// </summary>
|
||||
public class DrawningExcavator : DrawningSimpleExcavator
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="supports">Признак наличия обвеса</param>
|
||||
/// <param name="bucket">Признак наличия антикрыла</param>
|
||||
public DrawningExcavator(int speed, double weight, Color bodyColor, Color additionalColor,bool supports, bool bucket) : base(145, 100)
|
||||
{
|
||||
EntitySimpleExcavator = new EntityExcavator(speed, weight, bodyColor, additionalColor, supports, bucket);
|
||||
|
||||
}
|
||||
|
||||
public override void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntitySimpleExcavator == null || EntitySimpleExcavator is not EntityExcavator excavator || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Pen pen = new(Color.Black);
|
||||
Brush additionalBrush = new SolidBrush(excavator.AdditionalColor);
|
||||
|
||||
// Ковш
|
||||
if (excavator.Bucket)
|
||||
{
|
||||
|
||||
Point[] pointBucket1 =
|
||||
{
|
||||
new Point(_startPosX.Value + 90, _startPosY.Value + 50),
|
||||
new Point(_startPosX.Value + 90, _startPosY.Value),
|
||||
new Point(_startPosX.Value + 135, _startPosY.Value),
|
||||
new Point(_startPosX.Value + 135, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 125, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 125, _startPosY.Value + 10),
|
||||
new Point(_startPosX.Value + 105, _startPosY.Value + 10),
|
||||
new Point(_startPosX.Value + 105, _startPosY.Value + 45),
|
||||
new Point(_startPosX.Value + 100, _startPosY.Value + 50),
|
||||
new Point(_startPosX.Value + 90, _startPosY.Value + 50),
|
||||
};
|
||||
g.FillPolygon(additionalBrush, pointBucket1);
|
||||
g.DrawPolygon(pen, pointBucket1);
|
||||
|
||||
Point[] pointBucket2 =
|
||||
{
|
||||
new Point(_startPosX.Value + 125, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 135, _startPosY.Value+ 30),
|
||||
new Point(_startPosX.Value + 145, _startPosY.Value + 40),
|
||||
new Point(_startPosX.Value + 145, _startPosY.Value + 60),
|
||||
new Point(_startPosX.Value + 140, _startPosY.Value + 70),
|
||||
new Point(_startPosX.Value + 120, _startPosY.Value + 70),
|
||||
new Point(_startPosX.Value + 125, _startPosY.Value + 60),
|
||||
new Point(_startPosX.Value + 125, _startPosY.Value + 30),
|
||||
};
|
||||
Brush br1 = new SolidBrush(Color.FromArgb(100, 100, 100));
|
||||
g.FillPolygon(br1, pointBucket2);
|
||||
g.DrawPolygon(pen, pointBucket2);
|
||||
}
|
||||
// Опоры
|
||||
if (excavator.Supports)
|
||||
{
|
||||
Point[] pointSupport1 =
|
||||
{
|
||||
new Point(_startPosX.Value + 10, _startPosY.Value + 100),
|
||||
new Point(_startPosX.Value + 20, _startPosY.Value + 100),
|
||||
new Point(_startPosX.Value + 50, _startPosY.Value + 70),
|
||||
new Point(_startPosX.Value + 50, _startPosY.Value + 60),
|
||||
new Point(_startPosX.Value + 30, _startPosY.Value + 60),
|
||||
new Point(_startPosX.Value + 10, _startPosY.Value + 100),
|
||||
};
|
||||
g.FillPolygon(additionalBrush, pointSupport1);
|
||||
g.DrawPolygon(pen, pointSupport1);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 95, 30, 5);
|
||||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 95, 30, 5);
|
||||
|
||||
Point[] pointSupport2 =
|
||||
{
|
||||
new Point(_startPosX.Value + 120, _startPosY.Value + 100),
|
||||
new Point(_startPosX.Value + 110, _startPosY.Value + 100),
|
||||
new Point(_startPosX.Value + 80, _startPosY.Value + 70),
|
||||
new Point(_startPosX.Value + 80, _startPosY.Value + 60),
|
||||
new Point(_startPosX.Value + 100, _startPosY.Value + 60),
|
||||
new Point(_startPosX.Value + 120, _startPosY.Value + 100),
|
||||
};
|
||||
g.FillPolygon(additionalBrush, pointSupport2);
|
||||
g.DrawPolygon(pen, pointSupport2);
|
||||
g.FillRectangle(additionalBrush, _startPosX.Value + 100, _startPosY.Value + 95, 30, 5);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value + 95, 30, 5);
|
||||
}
|
||||
_startPosY += 20;
|
||||
_startPosX += 30;
|
||||
base.DrawTransport(g);
|
||||
_startPosY -= 20;
|
||||
_startPosX -= 30;
|
||||
}
|
||||
}
|
284
Excavator/Excavator/Drawnings/DrawningSimpleExcavator.cs
Normal file
284
Excavator/Excavator/Drawnings/DrawningSimpleExcavator.cs
Normal file
@ -0,0 +1,284 @@
|
||||
using Excavator.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Excavator.Drawnings;
|
||||
|
||||
public class DrawningSimpleExcavator
|
||||
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Класс-сущность
|
||||
/// </summary>
|
||||
public EntitySimpleExcavator? EntitySimpleExcavator { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Ширина окна
|
||||
/// </summary>
|
||||
private int? _pictureWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Высота окна
|
||||
/// </summary>
|
||||
private int? _pictureHeight;
|
||||
|
||||
/// <summary>
|
||||
/// Левая координата прорисовки экскаваторя
|
||||
/// </summary>
|
||||
protected int? _startPosX;
|
||||
|
||||
/// <summary>
|
||||
/// Верхняя кооридната прорисовки экскаваторя
|
||||
/// </summary>
|
||||
protected int? _startPosY;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина прорисовки экскаватора
|
||||
/// </summary>
|
||||
private readonly int _drawningExcavatorWidth = 70;
|
||||
|
||||
/// <summary>
|
||||
/// Высота прорисовки экскаватора
|
||||
/// </summary>
|
||||
private readonly int _drawningExcavatorHeight = 80;
|
||||
|
||||
/// <summary>
|
||||
/// Координата X объекта
|
||||
/// </summary>
|
||||
public int? GetPosX => _startPosX;
|
||||
|
||||
/// <summary>
|
||||
/// Координата Y объекта
|
||||
/// </summary>
|
||||
public int? GetPosY => _startPosY;
|
||||
|
||||
/// <summary>
|
||||
/// Ширина объекта
|
||||
/// </summary>
|
||||
public int GetWidth => _drawningExcavatorWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Высота объекта
|
||||
/// </summary>
|
||||
public int GetHeight => _drawningExcavatorHeight;
|
||||
|
||||
/// <summary>
|
||||
/// Пустой конструктор
|
||||
/// </summary>
|
||||
private DrawningSimpleExcavator()
|
||||
{
|
||||
_pictureWidth = null;
|
||||
_pictureHeight = null;
|
||||
_startPosX = null;
|
||||
_startPosY = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
public DrawningSimpleExcavator(int speed, double weight, Color bodyColor) : this()
|
||||
{
|
||||
EntitySimpleExcavator = new EntitySimpleExcavator(speed, weight, bodyColor);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор для наследников
|
||||
/// </summary>
|
||||
/// <param name="drawningExcavatorWidth">Ширина прорисовки экскаватора</param>
|
||||
/// <param name="drawningExcavatorHeight">Высота прорисовки экскаватора</param>
|
||||
public DrawningSimpleExcavator(int drawningExcavatorWidth, int drawningExcavatorHeight) : this()
|
||||
{
|
||||
_drawningExcavatorWidth = drawningExcavatorWidth;
|
||||
_drawningExcavatorHeight = drawningExcavatorHeight;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установка границ поля
|
||||
/// </summary>
|
||||
/// <param name="width">Ширина поля</param>
|
||||
/// <param name="height">Высота поля</param>
|
||||
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
|
||||
|
||||
public bool SetPictureSize(int width, int height)
|
||||
{
|
||||
if (width > _drawningExcavatorWidth && height > _drawningExcavatorHeight)
|
||||
{
|
||||
_pictureWidth = width;
|
||||
_pictureHeight = height;
|
||||
if (_startPosX != null && _startPosY != null)
|
||||
{
|
||||
if (_startPosX.Value + _drawningExcavatorWidth > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawningExcavatorWidth;
|
||||
}
|
||||
if (_startPosY.Value + _drawningExcavatorHeight > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawningExcavatorHeight;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установка позиции
|
||||
/// </summary>
|
||||
/// <param name="x">Координата X</param>
|
||||
/// <param name="y">Координата Y</param>
|
||||
public void SetPosition(int x, int y)
|
||||
{
|
||||
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_startPosX = x;
|
||||
_startPosY = y;
|
||||
|
||||
if (_drawningExcavatorWidth + _startPosX > _pictureWidth)
|
||||
{
|
||||
_startPosX = _pictureWidth - _drawningExcavatorWidth;
|
||||
}
|
||||
if (_drawningExcavatorHeight + _startPosY > _pictureHeight)
|
||||
{
|
||||
_startPosY = _pictureHeight - _drawningExcavatorHeight;
|
||||
}
|
||||
if (_startPosX < 0)
|
||||
{
|
||||
_startPosX = 0;
|
||||
}
|
||||
if (_startPosY < 0)
|
||||
{
|
||||
_startPosY = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Изменение направления перемещения
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
|
||||
public bool MoveTransport(DirectionType direction)
|
||||
{
|
||||
if (EntitySimpleExcavator == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
//влево
|
||||
case DirectionType.Left:
|
||||
if (_startPosX.Value - EntitySimpleExcavator.Step > 0)
|
||||
{
|
||||
_startPosX -= (int)EntitySimpleExcavator.Step;
|
||||
}
|
||||
return true;
|
||||
//вверх
|
||||
case DirectionType.Up:
|
||||
if (_startPosY.Value - EntitySimpleExcavator.Step > 0)
|
||||
{
|
||||
_startPosY -= (int)EntitySimpleExcavator.Step;
|
||||
}
|
||||
return true;
|
||||
// вправо
|
||||
case DirectionType.Right:
|
||||
if (_startPosX.Value + _drawningExcavatorWidth + EntitySimpleExcavator.Step < _pictureWidth)
|
||||
{
|
||||
_startPosX += (int)EntitySimpleExcavator.Step;
|
||||
}
|
||||
return true;
|
||||
//вниз
|
||||
case DirectionType.Down:
|
||||
if (_startPosY.Value + _drawningExcavatorHeight + EntitySimpleExcavator.Step < _pictureHeight)
|
||||
{
|
||||
_startPosY += (int)EntitySimpleExcavator.Step;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Прорисовка объекта
|
||||
/// </summary>
|
||||
/// <param name="g"></param>
|
||||
public virtual void DrawTransport(Graphics g)
|
||||
{
|
||||
if (EntitySimpleExcavator == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Pen pen = new(Color.Black);
|
||||
|
||||
|
||||
// Гусеницы
|
||||
Brush brGrey = new SolidBrush(Color.FromArgb(100, 100, 100));
|
||||
Brush br = new SolidBrush(EntitySimpleExcavator.BodyColor);
|
||||
|
||||
g.FillEllipse(brGrey, _startPosX.Value, _startPosY.Value + 55, 25, 25);
|
||||
g.FillEllipse(brGrey, _startPosX.Value + 45, _startPosY.Value + 55, 25, 25);
|
||||
g.FillRectangle(brGrey, _startPosX.Value + 10, _startPosY.Value + 55, 50, 25);
|
||||
|
||||
Rectangle rect1 = new Rectangle(_startPosX.Value, _startPosY.Value + 55, 25, 25);
|
||||
g.DrawArc(pen, rect1, 90, 180);
|
||||
Rectangle rect2 = new Rectangle(_startPosX.Value + 45, _startPosY.Value + 55, 25, 25);
|
||||
g.DrawArc(pen, rect2, 270, 180);
|
||||
g.DrawLine(pen, _startPosX.Value + 10, _startPosY.Value + 80, _startPosX.Value + 60, _startPosY.Value + 80);
|
||||
|
||||
|
||||
g.FillEllipse(br, _startPosX.Value + 3, _startPosY.Value + 58, 18, 18);
|
||||
g.FillEllipse(br, _startPosX.Value + 48, _startPosY.Value + 58, 18, 18);
|
||||
g.FillEllipse(br, _startPosX.Value + 21, _startPosY.Value + 69, 7, 7);
|
||||
g.FillEllipse(br, _startPosX.Value + 31, _startPosY.Value + 69, 7, 7);
|
||||
g.FillEllipse(br, _startPosX.Value + 41, _startPosY.Value + 69, 7, 7);
|
||||
g.FillEllipse(br, _startPosX.Value + 27, _startPosY.Value + 61, 4, 4);
|
||||
g.FillEllipse(br, _startPosX.Value + 39, _startPosY.Value + 61, 4, 4);
|
||||
|
||||
g.DrawEllipse(pen, _startPosX.Value + 3, _startPosY.Value + 58, 18, 18);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 48, _startPosY.Value + 58, 18, 18);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 21, _startPosY.Value + 69, 7, 7);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 31, _startPosY.Value + 69, 7, 7);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 41, _startPosY.Value + 69, 7, 7);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 27, _startPosY.Value + 61, 4, 4);
|
||||
g.DrawEllipse(pen, _startPosX.Value + 39, _startPosY.Value + 61, 4, 4);
|
||||
|
||||
// Корпус
|
||||
Point[] pointBody1 =
|
||||
{
|
||||
new Point(_startPosX.Value, _startPosY.Value + 55),
|
||||
new Point(_startPosX.Value, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 40, _startPosY.Value + 30),
|
||||
new Point(_startPosX.Value + 40, _startPosY.Value),
|
||||
new Point(_startPosX.Value + 70, _startPosY.Value),
|
||||
new Point(_startPosX.Value + 70, _startPosY.Value + 55),
|
||||
new Point(_startPosX.Value, _startPosY.Value + 55),
|
||||
};
|
||||
g.FillPolygon(br, pointBody1);
|
||||
g.DrawPolygon(pen, pointBody1);
|
||||
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 5, 10, 25);
|
||||
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 5, 10, 25);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 5, 10, 25);
|
||||
|
||||
// Окно
|
||||
Brush brLBlue = new SolidBrush(Color.LightBlue);
|
||||
|
||||
g.FillRectangle(brLBlue, _startPosX.Value + 42, _startPosY.Value + 2, 26, 26);
|
||||
g.DrawRectangle(pen, _startPosX.Value + 42, _startPosY.Value + 2, 26, 26);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,25 +1,11 @@
|
||||
namespace Excavator;
|
||||
namespace Excavator.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Класс-сущность "Экскаватор"
|
||||
/// </summary>
|
||||
public class EntityExcavator
|
||||
public class EntityExcavator : EntitySimpleExcavator
|
||||
{
|
||||
/// <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>
|
||||
@ -36,16 +22,17 @@ public class EntityExcavator
|
||||
public bool Bucket { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Шаг перемещения экскаватора
|
||||
/// Конструктор сущности
|
||||
/// </summary>
|
||||
public double Step => Speed * 100 / Weight;
|
||||
|
||||
|
||||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool supports, bool bucket)
|
||||
/// <param name="speed">Скорость</param>
|
||||
/// <param name="weight">Вес</param>
|
||||
/// <param name="bodyColor">Основной цвет</param>
|
||||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||||
/// <param name="supports">Опоры</param>
|
||||
/// <param name="bucket">Ковш</param>
|
||||
public EntityExcavator(int speed, double weight, Color bodyColor, Color additionalColor, bool supports, bool bucket) : base(speed, weight, bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
|
||||
AdditionalColor = additionalColor;
|
||||
Supports = supports;
|
||||
Bucket = bucket;
|
47
Excavator/Excavator/Entities/EntitySimpleExcavator.cs
Normal file
47
Excavator/Excavator/Entities/EntitySimpleExcavator.cs
Normal file
@ -0,0 +1,47 @@
|
||||
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 EntitySimpleExcavator
|
||||
{
|
||||
/// <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 EntitySimpleExcavator(int speed, double weight, Color bodyColor)
|
||||
{
|
||||
Speed = speed;
|
||||
Weight = weight;
|
||||
BodyColor = bodyColor;
|
||||
}
|
||||
}
|
53
Excavator/Excavator/FormExcavator.Designer.cs
generated
53
Excavator/Excavator/FormExcavator.Designer.cs
generated
@ -34,6 +34,9 @@
|
||||
buttonUp = new Button();
|
||||
buttonDown = new Button();
|
||||
buttonRight = new Button();
|
||||
buttonCreateSimpleExcavator = new Button();
|
||||
comboBoxStrategy = new ComboBox();
|
||||
buttonStrategyStep = new Button();
|
||||
((System.ComponentModel.ISupportInitialize)pictureBoxExcavator).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -51,9 +54,9 @@
|
||||
buttonCreateExcavator.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateExcavator.Location = new Point(12, 562);
|
||||
buttonCreateExcavator.Name = "buttonCreateExcavator";
|
||||
buttonCreateExcavator.Size = new Size(75, 23);
|
||||
buttonCreateExcavator.Size = new Size(217, 23);
|
||||
buttonCreateExcavator.TabIndex = 1;
|
||||
buttonCreateExcavator.Text = "Создать";
|
||||
buttonCreateExcavator.Text = "Создать экскаватор с обвесами";
|
||||
buttonCreateExcavator.UseVisualStyleBackColor = true;
|
||||
buttonCreateExcavator.Click += ButtonCreateExcavator_Click;
|
||||
//
|
||||
@ -62,7 +65,7 @@
|
||||
buttonLeft.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonLeft.BackgroundImage = Properties.Resources.arrowLeft;
|
||||
buttonLeft.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonLeft.Location = new Point(740, 535);
|
||||
buttonLeft.Location = new Point(749, 535);
|
||||
buttonLeft.Name = "buttonLeft";
|
||||
buttonLeft.Size = new Size(50, 50);
|
||||
buttonLeft.TabIndex = 2;
|
||||
@ -74,7 +77,7 @@
|
||||
buttonUp.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonUp.BackgroundImage = Properties.Resources.arrowUp;
|
||||
buttonUp.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonUp.Location = new Point(796, 479);
|
||||
buttonUp.Location = new Point(805, 479);
|
||||
buttonUp.Name = "buttonUp";
|
||||
buttonUp.Size = new Size(50, 50);
|
||||
buttonUp.TabIndex = 3;
|
||||
@ -86,7 +89,7 @@
|
||||
buttonDown.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonDown.BackgroundImage = Properties.Resources.arrowDown;
|
||||
buttonDown.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonDown.Location = new Point(796, 535);
|
||||
buttonDown.Location = new Point(805, 535);
|
||||
buttonDown.Name = "buttonDown";
|
||||
buttonDown.Size = new Size(50, 50);
|
||||
buttonDown.TabIndex = 4;
|
||||
@ -98,18 +101,53 @@
|
||||
buttonRight.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
|
||||
buttonRight.BackgroundImage = Properties.Resources.arrowRight;
|
||||
buttonRight.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonRight.Location = new Point(852, 535);
|
||||
buttonRight.Location = new Point(861, 535);
|
||||
buttonRight.Name = "buttonRight";
|
||||
buttonRight.Size = new Size(50, 50);
|
||||
buttonRight.TabIndex = 5;
|
||||
buttonRight.UseVisualStyleBackColor = true;
|
||||
buttonRight.Click += ButtonMove_Click;
|
||||
//
|
||||
// buttonCreateSimpleExcavator
|
||||
//
|
||||
buttonCreateSimpleExcavator.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
|
||||
buttonCreateSimpleExcavator.Location = new Point(235, 562);
|
||||
buttonCreateSimpleExcavator.Name = "buttonCreateSimpleExcavator";
|
||||
buttonCreateSimpleExcavator.Size = new Size(217, 23);
|
||||
buttonCreateSimpleExcavator.TabIndex = 6;
|
||||
buttonCreateSimpleExcavator.Text = "Создать экскаватор ";
|
||||
buttonCreateSimpleExcavator.UseVisualStyleBackColor = true;
|
||||
buttonCreateSimpleExcavator.Click += ButtonCreateSimpleExcavator_Click;
|
||||
//
|
||||
// comboBoxStrategy
|
||||
//
|
||||
comboBoxStrategy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
comboBoxStrategy.FormattingEnabled = true;
|
||||
comboBoxStrategy.Items.AddRange(new object[] { "К центру", "К краю " });
|
||||
comboBoxStrategy.Location = new Point(790, 12);
|
||||
comboBoxStrategy.Name = "comboBoxStrategy";
|
||||
comboBoxStrategy.Size = new Size(121, 23);
|
||||
comboBoxStrategy.TabIndex = 7;
|
||||
//
|
||||
// buttonStrategyStep
|
||||
//
|
||||
buttonStrategyStep.Anchor = AnchorStyles.Top | AnchorStyles.Right;
|
||||
buttonStrategyStep.Location = new Point(790, 41);
|
||||
buttonStrategyStep.Name = "buttonStrategyStep";
|
||||
buttonStrategyStep.Size = new Size(121, 23);
|
||||
buttonStrategyStep.TabIndex = 8;
|
||||
buttonStrategyStep.Text = "Шаг";
|
||||
buttonStrategyStep.UseVisualStyleBackColor = true;
|
||||
buttonStrategyStep.Click += ButtonStrategyStep_Click;
|
||||
//
|
||||
// FormExcavator
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(923, 597);
|
||||
Controls.Add(buttonStrategyStep);
|
||||
Controls.Add(comboBoxStrategy);
|
||||
Controls.Add(buttonCreateSimpleExcavator);
|
||||
Controls.Add(buttonRight);
|
||||
Controls.Add(buttonDown);
|
||||
Controls.Add(buttonUp);
|
||||
@ -130,5 +168,8 @@
|
||||
private Button buttonUp;
|
||||
private Button buttonDown;
|
||||
private Button buttonRight;
|
||||
private Button buttonCreateSimpleExcavator;
|
||||
private ComboBox comboBoxStrategy;
|
||||
private Button buttonStrategyStep;
|
||||
}
|
||||
}
|
@ -1,4 +1,8 @@
|
||||
namespace Excavator;
|
||||
using Excavator.Drawnings;
|
||||
using Excavator.Entities;
|
||||
using Excavator.MovementSrtategy;
|
||||
|
||||
namespace Excavator;
|
||||
|
||||
/// <summary>
|
||||
/// Форма работы с объектом "Экскаватор"
|
||||
@ -8,7 +12,9 @@ public partial class FormExcavator : Form
|
||||
/// <summary>
|
||||
/// Поле-объект для прорисовки объекта
|
||||
/// </summary>
|
||||
private DrawningExcavator? _drawningExcavator;
|
||||
private DrawningSimpleExcavator? _drawningSimpleExcavator;
|
||||
|
||||
private AbstractStrategy? _strategy;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор формы
|
||||
@ -16,40 +22,74 @@ public partial class FormExcavator : Form
|
||||
public FormExcavator()
|
||||
{
|
||||
InitializeComponent();
|
||||
_strategy = null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Метод прорисовки экскаватора
|
||||
/// </summary>
|
||||
private void Draw()
|
||||
{
|
||||
if (_drawningExcavator == null)
|
||||
if (_drawningSimpleExcavator == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Bitmap bmp = new(pictureBoxExcavator.Width, pictureBoxExcavator.Height);
|
||||
Graphics gr = Graphics.FromImage(bmp);
|
||||
_drawningExcavator.DrawTransport(gr);
|
||||
_drawningSimpleExcavator.DrawTransport(gr);
|
||||
pictureBoxExcavator.Image = bmp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать"
|
||||
/// Создание объекта
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
private void CreateObject(string type)
|
||||
{
|
||||
Random random = new();
|
||||
switch (type)
|
||||
{
|
||||
case nameof(DrawningSimpleExcavator):
|
||||
_drawningSimpleExcavator = new DrawningSimpleExcavator(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):
|
||||
_drawningSimpleExcavator = 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(1, 2)), Convert.ToBoolean(random.Next(1, 2)));
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
_drawningSimpleExcavator.SetPictureSize(pictureBoxExcavator.Width, pictureBoxExcavator.Height);
|
||||
_drawningSimpleExcavator.SetPosition(random.Next(10, 100), random.Next(10, 100));
|
||||
_strategy = null;
|
||||
//comboBoxStrategy.Enabled = true;
|
||||
Draw();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать экскаватор с обвесами"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreateExcavator_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));
|
||||
Draw();
|
||||
CreateObject(nameof(DrawningExcavator));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Создать экскаватор"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonCreateSimpleExcavator_Click(object sender, EventArgs e)
|
||||
{
|
||||
CreateObject(nameof(DrawningSimpleExcavator));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -59,7 +99,7 @@ public partial class FormExcavator : Form
|
||||
/// <param name="e"></param>
|
||||
private void ButtonMove_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningExcavator == null)
|
||||
if (_drawningSimpleExcavator == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -69,16 +109,16 @@ public partial class FormExcavator : Form
|
||||
switch (name)
|
||||
{
|
||||
case "buttonUp":
|
||||
result = _drawningExcavator.MoveTransport(DirectionType.Up);
|
||||
result = _drawningSimpleExcavator.MoveTransport(DirectionType.Up);
|
||||
break;
|
||||
case "buttonDown":
|
||||
result = _drawningExcavator.MoveTransport(DirectionType.Down);
|
||||
result = _drawningSimpleExcavator.MoveTransport(DirectionType.Down);
|
||||
break;
|
||||
case "buttonLeft":
|
||||
result = _drawningExcavator.MoveTransport(DirectionType.Left);
|
||||
result = _drawningSimpleExcavator.MoveTransport(DirectionType.Left);
|
||||
break;
|
||||
case "buttonRight":
|
||||
result = _drawningExcavator.MoveTransport(DirectionType.Right);
|
||||
result = _drawningSimpleExcavator.MoveTransport(DirectionType.Right);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -87,4 +127,47 @@ public partial class FormExcavator : Form
|
||||
Draw();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Обработка нажатия кнопки "Шаг"
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ButtonStrategyStep_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_drawningSimpleExcavator == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (comboBoxStrategy.Enabled)
|
||||
{
|
||||
_strategy = comboBoxStrategy.SelectedIndex switch
|
||||
{
|
||||
0 => new MoveToCenter(),
|
||||
1 => new MoveToBorder(),
|
||||
_ => null,
|
||||
};
|
||||
if (_strategy == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_strategy.SetData(new MoveableSimpleExcavator(_drawningSimpleExcavator), 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/MovementSrtategy/AbstractStrategy.cs
Normal file
139
Excavator/Excavator/MovementSrtategy/AbstractStrategy.cs
Normal file
@ -0,0 +1,139 @@
|
||||
using Excavator.MovementStrategy;
|
||||
|
||||
namespace Excavator.MovementSrtategy;
|
||||
|
||||
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?.GetObjectParameters;
|
||||
|
||||
/// <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/MovementSrtategy/IMoveableObject.cs
Normal file
24
Excavator/Excavator/MovementSrtategy/IMoveableObject.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using Excavator.MovementStrategy;
|
||||
|
||||
namespace Excavator.MovementSrtategy;
|
||||
|
||||
public interface IMoveableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение координаты объекта
|
||||
/// </summary>
|
||||
ObjectParameters? GetObjectParameters { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Шаг
|
||||
/// </summary>
|
||||
int GetStep { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Попытка перемстить объект
|
||||
/// </summary>
|
||||
/// <param name="direction">Направление</param>
|
||||
/// <returns></returns>
|
||||
bool TryMoveObject(MovementDirection direction);
|
||||
|
||||
}
|
39
Excavator/Excavator/MovementSrtategy/MoveToBorder.cs
Normal file
39
Excavator/Excavator/MovementSrtategy/MoveToBorder.cs
Normal file
@ -0,0 +1,39 @@
|
||||
namespace Excavator.MovementSrtategy;
|
||||
|
||||
public class MoveToBorder : AbstractStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Стратегия перемещения объекта к Краю экрана
|
||||
/// </summary>
|
||||
protected override bool IsTargetDestinaion()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return objParams.RightBorder + GetStep() >= FieldWidth &&
|
||||
objParams.DownBorder + GetStep() >= FieldHeight;
|
||||
}
|
||||
protected override void MoveToTarget()
|
||||
{
|
||||
ObjectParameters? objParams = GetObjectParameters;
|
||||
if (objParams == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int diffX = objParams.RightBorder - FieldWidth;
|
||||
if (Math.Abs(diffX) > GetStep())
|
||||
{
|
||||
MoveRight();
|
||||
}
|
||||
|
||||
int diffY = objParams.DownBorder - FieldHeight;
|
||||
if (Math.Abs(diffY) > GetStep())
|
||||
{
|
||||
MoveDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
55
Excavator/Excavator/MovementSrtategy/MoveToCenter.cs
Normal file
55
Excavator/Excavator/MovementSrtategy/MoveToCenter.cs
Normal file
@ -0,0 +1,55 @@
|
||||
namespace Excavator.MovementSrtategy;
|
||||
|
||||
/// <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;
|
||||
using Excavator.MovementStrategy;
|
||||
|
||||
namespace Excavator.MovementSrtategy;
|
||||
|
||||
/// <summary>
|
||||
/// Класс-реализация IMoveableObject с использованием Drawningexcavator
|
||||
/// </summary>
|
||||
public class MoveableSimpleExcavator : IMoveableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Поле-объект класса Drawningexcavator или его наследника
|
||||
/// </summary>
|
||||
private readonly DrawningSimpleExcavator? _excavator = null;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="excavator">Объект класса Drawningexcavator</param>
|
||||
public MoveableSimpleExcavator(DrawningSimpleExcavator excavator)
|
||||
{
|
||||
_excavator = excavator;
|
||||
}
|
||||
|
||||
public ObjectParameters? GetObjectParameters
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_excavator == null || _excavator.EntitySimpleExcavator == null || !_excavator.GetPosX.HasValue || !_excavator.GetPosY.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new ObjectParameters(_excavator.GetPosX.Value, _excavator.GetPosY.Value, _excavator.GetWidth, _excavator.GetHeight);
|
||||
}
|
||||
}
|
||||
|
||||
public int GetStep => (int)(_excavator?.EntitySimpleExcavator?.Step ?? 0);
|
||||
|
||||
public bool TryMoveObject(MovementDirection direction)
|
||||
{
|
||||
if (_excavator == null || _excavator.EntitySimpleExcavator == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _excavator.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,9 +1,9 @@
|
||||
namespace Excavator;
|
||||
namespace Excavator.MovementStrategy;
|
||||
|
||||
/// <summary>
|
||||
/// Направление перемещения
|
||||
/// </summary>
|
||||
public enum DirectionType
|
||||
public enum MovementDirection
|
||||
{
|
||||
/// <summary>
|
||||
/// Вверх
|
||||
@ -24,4 +24,4 @@ public enum DirectionType
|
||||
/// Вправо
|
||||
/// </summary>
|
||||
Right = 4
|
||||
}
|
||||
}
|
70
Excavator/Excavator/MovementSrtategy/ObjectParameters.cs
Normal file
70
Excavator/Excavator/MovementSrtategy/ObjectParameters.cs
Normal file
@ -0,0 +1,70 @@
|
||||
|
||||
namespace Excavator.MovementSrtategy;
|
||||
|
||||
public class ObjectParameters
|
||||
{
|
||||
/// <summary>
|
||||
/// Координата Х
|
||||
/// </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"></param>
|
||||
/// <param name="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;
|
||||
}
|
||||
}
|
21
Excavator/Excavator/MovementSrtategy/StrategyStatus.cs
Normal file
21
Excavator/Excavator/MovementSrtategy/StrategyStatus.cs
Normal file
@ -0,0 +1,21 @@
|
||||
namespace Excavator.MovementSrtategy;
|
||||
|
||||
|
||||
public enum StrategyStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// Всё готово к началу
|
||||
/// </summary>
|
||||
NotInit,
|
||||
|
||||
/// <summary>
|
||||
/// Выполняется
|
||||
/// </summary>
|
||||
InProgress,
|
||||
|
||||
/// <summary>
|
||||
/// Завершено
|
||||
/// </summary>
|
||||
Finish
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user
Имя элемента проекта не соответствует указанному в задании