227 lines
7.2 KiB
C#
227 lines
7.2 KiB
C#
using ProjectElectroTrans.Entities;
|
||
|
||
namespace ProjectElectroTrans.Drawnings;
|
||
|
||
public class DrawingTrans
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityTrans? EntityTrans { 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 _drawningTransWidth = 80;
|
||
|
||
/// <summary>
|
||
/// Высота прорисовки автомобиля
|
||
/// </summary>
|
||
private readonly int _drawningTransHeight = 60;
|
||
/// <summary>
|
||
/// Координата X объекта
|
||
/// </summary>
|
||
public int? GetPosX => _startPosX;
|
||
/// <summary>
|
||
/// Координата Y объекта
|
||
/// </summary>
|
||
public int? GetPosY => _startPosY;
|
||
/// <summary>
|
||
/// Ширина объекта
|
||
/// </summary>
|
||
public int GetWidth => _drawningTransWidth;
|
||
/// <summary>
|
||
/// Высота объекта
|
||
/// </summary>
|
||
public int GetHeight => _drawningTransHeight;
|
||
|
||
/// <summary>
|
||
/// Пустой конструктор
|
||
/// </summary>
|
||
private DrawingTrans()
|
||
{
|
||
_pictureWidth = null;
|
||
_pictureHeight = null;
|
||
_startPosX = null;
|
||
_startPosY = null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес</param>
|
||
/// <param name="bodyColor">Основной цвет</param>
|
||
public DrawingTrans(int speed, double weight, Color bodyColor) : this()
|
||
{
|
||
EntityTrans = new EntityTrans(speed, weight, bodyColor);
|
||
}
|
||
/// <summary>
|
||
/// Конструктор для наследников
|
||
/// </summary>
|
||
/// <param name="drawning
|
||
/// Width">Ширина прорисовки автомобиля</param>
|
||
/// <param name="drawningTransHeight">Высота прорисовки автомобиля</param>
|
||
protected DrawingTrans(int drawningTransWidth, int drawningTransHeight) : this()
|
||
{
|
||
_drawningTransWidth = drawningTransWidth;
|
||
_pictureHeight = drawningTransHeight;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Установка границ поля
|
||
/// </summary>
|
||
/// <param name="width">Ширина поля</param>
|
||
/// <param name="height">Высота поля</param>
|
||
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
|
||
public bool SetPictureSize(int width, int height)
|
||
{
|
||
if (width >= _drawningTransWidth || height >= _drawningTransWidth)
|
||
{
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
if (_startPosX.HasValue && _startPosY.HasValue)
|
||
{
|
||
SetPosition(_startPosX.Value, _startPosY.Value);
|
||
}
|
||
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;
|
||
}
|
||
|
||
if (x < 0)
|
||
{
|
||
x = 0;
|
||
}
|
||
else if (x > _pictureWidth - _drawningTransWidth)
|
||
{
|
||
x = _pictureWidth.Value - _drawningTransWidth;
|
||
}
|
||
|
||
if (y < 0)
|
||
{
|
||
y = 0;
|
||
}
|
||
else if (y > _pictureHeight - _drawningTransHeight)
|
||
{
|
||
y = _pictureHeight.Value - _drawningTransHeight;
|
||
}
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
}
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
/// <returns>true - перемещене выполнено, false - перемещениеневозможно</returns>
|
||
public bool MoveTransport(DirectionType direction)
|
||
{
|
||
if (EntityTrans == null || !_startPosX.HasValue ||
|
||
!_startPosY.HasValue)
|
||
{
|
||
return false;
|
||
}
|
||
switch (direction)
|
||
{
|
||
//влево
|
||
case DirectionType.Left:
|
||
if (_startPosX.Value - EntityTrans.Step > 0)
|
||
{
|
||
_startPosX -= (int)EntityTrans.Step;
|
||
}
|
||
return true;
|
||
//вверх
|
||
case DirectionType.Up:
|
||
if (_startPosY.Value - EntityTrans.Step > 0)
|
||
{
|
||
_startPosY -= (int)EntityTrans.Step;
|
||
}
|
||
return true;
|
||
// вправо
|
||
case DirectionType.Right:
|
||
if (_startPosX.Value + EntityTrans.Step < _pictureWidth - _drawningTransWidth)
|
||
{
|
||
_startPosX += (int)EntityTrans.Step;
|
||
}
|
||
return true;
|
||
//вниз
|
||
case DirectionType.Down:
|
||
if (_startPosY.Value + EntityTrans.Step < _pictureHeight - _drawningTransHeight)
|
||
{
|
||
_startPosY += (int)EntityTrans.Step;
|
||
}
|
||
return true;
|
||
default:
|
||
return false;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public virtual void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityTrans == null || !_startPosX.HasValue ||
|
||
!_startPosY.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
Pen pen = new(EntityTrans.BodyColor);
|
||
|
||
|
||
// Тело
|
||
|
||
g.DrawPolygon(pen, new Point[] {
|
||
new Point(_startPosX.Value, _startPosY.Value + 30),
|
||
new Point(_startPosX.Value + 10, _startPosY.Value + 10),
|
||
new Point(_startPosX.Value + 70, _startPosY.Value + 10),
|
||
new Point(_startPosX.Value + 80, _startPosY.Value + 30),
|
||
new Point(_startPosX.Value + 80, _startPosY.Value + 50),
|
||
new Point(_startPosX.Value, _startPosY.Value + 50),
|
||
});
|
||
// Колеса
|
||
for (int i = 0; i < 4; i++)
|
||
{
|
||
g.DrawEllipse(pen, new Rectangle((_startPosX.Value) + (70 / 4) * (i + 1) + -4, _startPosY.Value + 46, 8, 8));
|
||
}
|
||
// Стекла
|
||
g.DrawPolygon(pen, new Point[] {
|
||
new Point(_startPosX.Value + 2, _startPosY.Value + 30),
|
||
new Point(_startPosX.Value + 10, _startPosY.Value + 13),
|
||
new Point(_startPosX.Value + 70, _startPosY.Value + 13),
|
||
new Point(_startPosX.Value + 78, _startPosY.Value + 30),
|
||
});
|
||
}
|
||
}
|