PI-14_Lopatin_D.A._Simple/WarmlyLocomotive/DrawningWarmlyLocomotive.cs

225 lines
7.6 KiB
C#
Raw Permalink Normal View History

namespace WarmlyLocomotive;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningWarmlyLocomotive
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityWarmlyLocomotive? EntityWarmlyLocomotive { 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 _drawningLocomotiveWidth = 150;
/// <summary>
/// Высота прорисовки автомобиля
/// </summary>
private readonly int _drawningLocomotiveHeight = 100;
/// <summary>
/// Инициализация свойств
/// </summary>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool tube, bool fuelTank)
{
EntityWarmlyLocomotive = new EntityWarmlyLocomotive();
EntityWarmlyLocomotive.Init(speed, weight, bodyColor, additionalColor,
tube, fuelTank);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Установка границ поля
/// </summary>
2024-04-01 14:05:54 +03:00
public bool SetPictureSize(int width, int height)
{
2024-04-14 16:25:24 +03:00
if (width > _drawningLocomotiveWidth && height > _drawningLocomotiveHeight)
{
2024-02-17 14:35:37 +03:00
_pictureWidth = width;
_pictureHeight = height;
2024-04-14 16:25:24 +03:00
//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;
}
}
2024-02-17 14:35:37 +03:00
return true;
}
return false;
2024-04-14 16:25:24 +03:00
}
/// <summary>
/// Установка позиции
/// </summary>
2024-04-01 14:05:54 +03:00
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
2024-02-19 10:19:47 +03:00
2024-04-01 14:09:38 +03:00
//проверка у
2024-02-19 10:19:47 +03:00
if ((y + _drawningLocomotiveHeight) > _pictureHeight)
2024-02-17 14:35:37 +03:00
{
2024-04-01 14:05:54 +03:00
_startPosY = _pictureHeight - _drawningLocomotiveHeight;
2024-02-17 14:35:37 +03:00
}
2024-04-14 16:25:24 +03:00
else if (y < 0)
2024-04-01 14:09:38 +03:00
{
_startPosY = 0;
}
2024-02-19 10:19:47 +03:00
else
2024-02-17 14:35:37 +03:00
{
2024-02-19 10:19:47 +03:00
_startPosY = y;
2024-02-17 14:35:37 +03:00
}
2024-04-14 16:25:24 +03:00
2024-04-01 14:09:38 +03:00
//проверка х
2024-02-19 10:19:47 +03:00
if ((x + _drawningLocomotiveWidth) > _pictureWidth)
2024-02-17 14:35:37 +03:00
{
2024-04-01 14:05:54 +03:00
_startPosX = _pictureWidth - _drawningLocomotiveWidth;
2024-02-17 14:35:37 +03:00
}
2024-04-14 16:25:24 +03:00
else if (x < 0)
2024-04-01 14:09:38 +03:00
{
_startPosX = 0;
}
2024-02-17 14:35:37 +03:00
else
{
_startPosX = x;
}
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
2024-04-01 14:05:54 +03:00
public bool MoveTransport(DirectionType direction)
{
2024-04-01 14:05:54 +03:00
if (EntityWarmlyLocomotive == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityWarmlyLocomotive.Step > 0)
{
_startPosX -= (int)EntityWarmlyLocomotive.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityWarmlyLocomotive.Step > 0)
{
_startPosY -= (int)EntityWarmlyLocomotive.Step;
}
return true;
// вправо
case DirectionType.Right:
2024-02-17 14:35:37 +03:00
if (_startPosX.Value + EntityWarmlyLocomotive.Step + _drawningLocomotiveWidth < _pictureWidth)
{
_startPosX += (int)EntityWarmlyLocomotive.Step;
}
return true;
//вниз
case DirectionType.Down:
2024-02-17 14:35:37 +03:00
if (_startPosY.Value + EntityWarmlyLocomotive.Step + _drawningLocomotiveHeight < _pictureHeight)
{
_startPosY += (int)EntityWarmlyLocomotive.Step;
}
return true;
default:
return false;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
2024-04-01 14:05:54 +03:00
public void DrawTransport(Graphics g)
{
2024-04-01 14:05:54 +03:00
if (EntityWarmlyLocomotive == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityWarmlyLocomotive.AdditionalColor);
//труба
if (EntityWarmlyLocomotive.Tube)
{
g.DrawRectangle(pen, _startPosX.Value + 40, _startPosY.Value, 20, 30);
g.FillRectangle(additionalBrush, _startPosX.Value + 40, _startPosY.Value, 20, 40);
}
//локомотив
Brush br = new SolidBrush(EntityWarmlyLocomotive.BodyColor);
Brush blBr = new SolidBrush(Color.Black);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 60, 140, 20);
g.FillRectangle(br, _startPosX.Value, _startPosY.Value + 60, 140, 20);
Point point1 = new Point(_startPosX.Value, _startPosY.Value + 60);
Point point2 = new Point(_startPosX.Value + 20, _startPosY.Value + 30);
Point point3 = new Point(_startPosX.Value + 140, _startPosY.Value + 30);
Point point4 = new Point(_startPosX.Value + 140, _startPosY.Value + 60);
Point[] body ={point1, point2, point3, point4};
g.DrawPolygon(pen, body);
g.FillPolygon(br, body);
g.DrawRectangle(pen, _startPosX.Value + 140, _startPosY.Value + 40, 10, 40);
g.FillRectangle(br, _startPosX.Value + 140, _startPosY.Value + 40, 10, 40);
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 80, 20, 20);
g.FillEllipse(blBr, _startPosX.Value, _startPosY.Value + 80, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 80, 20, 20);
g.FillEllipse(blBr, _startPosX.Value + 30, _startPosY.Value + 80, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value + 80, 20, 20);
g.FillEllipse(blBr, _startPosX.Value + 90, _startPosY.Value + 80, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 120, _startPosY.Value + 80, 20, 20);
g.FillEllipse(blBr, _startPosX.Value + 120, _startPosY.Value + 80, 20, 20);
// отсек для топлива
if (EntityWarmlyLocomotive.FuelTank)
{
g.DrawRectangle(pen, _startPosX.Value + 80, _startPosY.Value + 40, 50, 40);
g.FillRectangle(additionalBrush, _startPosX.Value + 80, _startPosY.Value + 40, 50, 40);
}
}
}