2024-03-17 18:32:46 +04:00

263 lines
9.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ProjectTrolleybus.Entities;
namespace ProjectTrolleybus.Drawnings;
public class DrawningTrolleyB
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityTrolleyB? EntityTrolleyB { 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 _drawningVehicleWidth = 89;
/// <summary>
/// Высота прорисовки транспорта
/// </summary>
private readonly int _drawningVehicleHeight = 31;
/// <summary>
/// Координата X объекта
/// </summary>
public int? GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int? GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _drawningVehicleWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHight => _drawningVehicleHeight;
/// <summary>
/// Пустой конструктор
/// </summary>
private DrawningTrolleyB()
{
_pictureHeight = null;
_pictureWidth = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public DrawningTrolleyB(int speed, double weight, Color bodyColor) : this()
{
EntityTrolleyB = new EntityTrolleyB(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningVehicleWidth">Ширина прорисовки транспорта</param>
/// <param name="drawningVehicleHeight">Высота прорисовки транспорта</param>
protected DrawningTrolleyB(int drawningVehicleWidth, int drawningVehicleHeight) : this()
{
_drawningVehicleWidth = drawningVehicleWidth;
_drawningVehicleHeight = drawningVehicleHeight;
}
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height)
{
//если влезает, сохраняем границы и корректируем позицию объекта, если она уже была установлена
if (_drawningVehicleWidth > width || _drawningVehicleHeight > height)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX.HasValue || _startPosY.HasValue)
{
if (_startPosX + _drawningVehicleWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawningVehicleWidth;
}
else if (_startPosX < 0) _startPosX = 0;
if (_startPosY + _drawningVehicleHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawningVehicleHeight;
}
else if (_startPosY < 0) _startPosY = 0;
}
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y, int width, int height)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
//то надо изменить координаты, чтобы он оставался в этих границах
if (x + _drawningVehicleWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawningVehicleWidth;
}
else if (x < 0) _startPosX = 0;
else _startPosX = x;
if (y + _drawningVehicleHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawningVehicleHeight;
}
else if (y < 0) _startPosY = 0;
else _startPosY = y;
}
/// <summary>
/// Изменение направления движения
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
public bool MoveVehicle(DirectionType direction)
{
if (EntityTrolleyB == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
switch (direction)
{
// влево
case DirectionType.Left:
if (_startPosX.Value - EntityTrolleyB.Step > 0)
{
_startPosX -= (int)EntityTrolleyB.Step;
}
return true;
// вверх
case DirectionType.Up:
if (_startPosY.Value - EntityTrolleyB.Step > 0)
{
_startPosY -= (int)EntityTrolleyB.Step;
}
return true;
// вправо
case DirectionType.Right:
{
if (_startPosX.Value + _drawningVehicleWidth + EntityTrolleyB.Step < _pictureWidth)
{
_startPosX += (int)EntityTrolleyB.Step;
}
}
return true;
// вниз
case DirectionType.Down:
{
if (_startPosY.Value + _drawningVehicleHeight + EntityTrolleyB.Step < _pictureHeight)
{
_startPosY += (int)EntityTrolleyB.Step;
}
}
return true;
default:
return false;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (EntityTrolleyB == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
//Колёса
pen.Width = 2;
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 23, 12, 12);
g.DrawEllipse(pen, _startPosX.Value + 70, _startPosY.Value + 23, 12, 12);
pen.Width = 1;
Brush brBlackWheel = new SolidBrush(Color.Black);
g.FillEllipse(brBlackWheel, _startPosX.Value + 10, _startPosY.Value + 23, 12, 12);
g.FillEllipse(brBlackWheel, _startPosX.Value + 70, _startPosY.Value + 23, 12, 12);
Brush brYelWheel = new SolidBrush(Color.Goldenrod);
g.FillEllipse(brYelWheel, _startPosX.Value + 12, _startPosY.Value + 25, 8, 8);
g.FillEllipse(brYelWheel, _startPosX.Value + 72, _startPosY.Value + 25, 8, 8);
//Кузов
Brush br = new SolidBrush(EntityTrolleyB.BodyColor);
g.FillRectangle(br, _startPosX.Value + 1, _startPosY.Value + 3, 89, 24);
//Границы Автомобиля
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 2, 90, 25);
g.DrawRectangle(pen, _startPosX.Value + 26, _startPosY.Value + 12, 10, 15);
g.DrawRectangle(pen, _startPosX.Value + 78, _startPosY.Value + 6, 12, 12);
pen.Width = 2;
g.DrawEllipse(pen, _startPosX.Value + 2, _startPosY.Value + 7, 8, 10);
g.DrawEllipse(pen, _startPosX.Value + 14, _startPosY.Value + 7, 8, 10);
g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 7, 8, 10);
g.DrawEllipse(pen, _startPosX.Value + 52, _startPosY.Value + 7, 8, 10);
g.DrawEllipse(pen, _startPosX.Value + 64, _startPosY.Value + 7, 8, 10);
pen.Width = 1;
//Стекла и кабина
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillRectangle(brBlue, _startPosX.Value + 79, _startPosY.Value + 7, 11, 11);
g.FillEllipse(brBlue, _startPosX.Value + 2, _startPosY.Value + 7, 8, 10);
g.FillEllipse(brBlue, _startPosX.Value + 14, _startPosY.Value + 7, 8, 10);
g.FillEllipse(brBlue, _startPosX.Value + 40, _startPosY.Value + 7, 8, 10);
g.FillEllipse(brBlue, _startPosX.Value + 52, _startPosY.Value + 7, 8, 10);
g.FillEllipse(brBlue, _startPosX.Value + 64, _startPosY.Value + 7, 8, 10);
//Дверь
Brush brBlack = new SolidBrush(Color.LightGray);
g.FillRectangle(brBlack, _startPosX.Value + 27, _startPosY.Value + 13, 9, 14);
}
}