255 lines
8.1 KiB
C#
255 lines
8.1 KiB
C#
using WarmlyLocomotive.Entities;
|
||
|
||
namespace WarmlyLocomotive.Drawnings;
|
||
/// <summary>
|
||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||
/// </summary>
|
||
public class DrawningLocomotive
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityLocomotive? EntityLocomotive { 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 _drawningLocomotiveWidth = 150;
|
||
/// <summary>
|
||
/// Высота прорисовки паровоза
|
||
/// </summary>
|
||
private readonly int _drawningLocomotiveHeight = 70;
|
||
|
||
/// <summary>
|
||
/// Координата X объекта
|
||
/// </summary>
|
||
public int? GetPosX => _startPosX;
|
||
|
||
/// <summary>
|
||
/// Координата Y объекта
|
||
/// </summary>
|
||
public int? GetPosY => _startPosY;
|
||
|
||
/// <summary>
|
||
/// Ширина объекта
|
||
/// </summary>
|
||
public int GetWidth => _drawningLocomotiveWidth;
|
||
|
||
/// <summary>
|
||
/// Высота объекта
|
||
/// </summary>
|
||
public int GetHeight => _drawningLocomotiveHeight;
|
||
|
||
|
||
/// <summary>
|
||
/// Пустой конструктор
|
||
/// </summary>
|
||
private DrawningLocomotive()
|
||
{
|
||
_pictureWidth = null;
|
||
_pictureHeight = null;
|
||
_startPosX = null;
|
||
_startPosY = null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес</param>
|
||
/// <param name="bodyColor">Основной цвет</param>
|
||
public DrawningLocomotive(int speed, double weight, Color bodyColor) : this()
|
||
{
|
||
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Конструктор для наследников
|
||
/// </summary>
|
||
/// <param name="drawningLocomotiveWidth">Ширина прорисовки паровоза</param>
|
||
/// <param name="drawningLocomotiveHeight">Высота прорисовки паровоза</param>
|
||
protected DrawningLocomotive(int drawningLocomotiveWidth, int drawningLocomotiveHeight) : this()
|
||
{
|
||
_drawningLocomotiveWidth = drawningLocomotiveWidth;
|
||
_pictureHeight = drawningLocomotiveHeight;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Установка границ поля
|
||
/// </summary>
|
||
|
||
public bool SetPictureSize(int width, int height)
|
||
{
|
||
if (width > _drawningLocomotiveWidth && height > _drawningLocomotiveHeight)
|
||
{
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
|
||
//todo
|
||
if (_startPosX != null && _startPosY != null)
|
||
{
|
||
//проверка х
|
||
if (_startPosX.Value + _drawningLocomotiveWidth > _pictureWidth)
|
||
{
|
||
_startPosX = _pictureWidth - _drawningLocomotiveWidth;
|
||
}
|
||
else if (_startPosX.Value < 0)
|
||
{
|
||
_startPosX = 0;
|
||
}
|
||
|
||
//проверка у
|
||
if (_startPosY.Value + _drawningLocomotiveHeight > _pictureHeight)
|
||
{
|
||
_startPosY = _pictureHeight - _drawningLocomotiveHeight;
|
||
}
|
||
else if (_startPosY.Value < 0)
|
||
{
|
||
_startPosY = 0;
|
||
}
|
||
|
||
}
|
||
return true;
|
||
}
|
||
return false;
|
||
|
||
}
|
||
/// <summary>
|
||
/// Установка позиции
|
||
/// </summary>
|
||
|
||
public void SetPosition(int x, int y)
|
||
{
|
||
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
|
||
//проверка у
|
||
if (y + _drawningLocomotiveHeight > _pictureHeight)
|
||
{
|
||
_startPosY = _pictureHeight - _drawningLocomotiveHeight;
|
||
}
|
||
else if (y < 0)
|
||
{
|
||
_startPosY = 0;
|
||
}
|
||
else
|
||
{
|
||
_startPosY = y;
|
||
}
|
||
|
||
//проверка х
|
||
if (x + _drawningLocomotiveWidth > _pictureWidth)
|
||
{
|
||
_startPosX = _pictureWidth - _drawningLocomotiveWidth;
|
||
}
|
||
else if (x < 0)
|
||
{
|
||
_startPosX = 0;
|
||
}
|
||
else
|
||
{
|
||
_startPosX = x;
|
||
}
|
||
|
||
}
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
|
||
public bool MoveTransport(DirectionType direction)
|
||
{
|
||
if (EntityLocomotive == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return false;
|
||
}
|
||
switch (direction)
|
||
{
|
||
//влево
|
||
case DirectionType.Left:
|
||
if (_startPosX.Value - EntityLocomotive.Step > 0)
|
||
{
|
||
_startPosX -= (int)EntityLocomotive.Step;
|
||
}
|
||
return true;
|
||
//вверх
|
||
case DirectionType.Up:
|
||
if (_startPosY.Value - EntityLocomotive.Step > 0)
|
||
{
|
||
_startPosY -= (int)EntityLocomotive.Step;
|
||
}
|
||
return true;
|
||
// вправо
|
||
case DirectionType.Right:
|
||
if (_startPosX.Value + EntityLocomotive.Step + _drawningLocomotiveWidth < _pictureWidth)
|
||
{
|
||
_startPosX += (int)EntityLocomotive.Step;
|
||
}
|
||
return true;
|
||
//вниз
|
||
case DirectionType.Down:
|
||
if (_startPosY.Value + EntityLocomotive.Step + _drawningLocomotiveHeight < _pictureHeight)
|
||
{
|
||
_startPosY += (int)EntityLocomotive.Step;
|
||
}
|
||
return true;
|
||
default:
|
||
return false;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
|
||
public virtual void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityLocomotive == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
Pen pen = new(Color.Black);
|
||
|
||
//локомотив
|
||
Brush br = new SolidBrush(EntityLocomotive.BodyColor);
|
||
Brush blBr = new SolidBrush(Color.Black);
|
||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 30, 140, 20);
|
||
g.FillRectangle(br, _startPosX.Value, _startPosY.Value + 30, 140, 20);
|
||
Point point1 = new Point(_startPosX.Value, _startPosY.Value + 30);
|
||
Point point2 = new Point(_startPosX.Value + 20, _startPosY.Value);
|
||
Point point3 = new Point(_startPosX.Value + 140, _startPosY.Value);
|
||
Point point4 = new Point(_startPosX.Value + 140, _startPosY.Value + 30);
|
||
Point[] body = { point1, point2, point3, point4 };
|
||
g.DrawPolygon(pen, body);
|
||
g.FillPolygon(br, body);
|
||
g.DrawRectangle(pen, _startPosX.Value + 140, _startPosY.Value + 10, 10, 40);
|
||
g.FillRectangle(br, _startPosX.Value + 140, _startPosY.Value + 10, 10, 40);
|
||
|
||
//колеса
|
||
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 50, 20, 20);
|
||
g.FillEllipse(blBr, _startPosX.Value, _startPosY.Value + 50, 20, 20);
|
||
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 50, 20, 20);
|
||
g.FillEllipse(blBr, _startPosX.Value + 30, _startPosY.Value + 50, 20, 20);
|
||
g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value + 50, 20, 20);
|
||
g.FillEllipse(blBr, _startPosX.Value + 90, _startPosY.Value + 50, 20, 20);
|
||
g.DrawEllipse(pen, _startPosX.Value + 120, _startPosY.Value + 50, 20, 20);
|
||
g.FillEllipse(blBr, _startPosX.Value + 120, _startPosY.Value + 50, 20, 20);
|
||
|
||
|
||
}
|
||
} |