307 lines
10 KiB
C#
307 lines
10 KiB
C#
|
namespace ProjectBus;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
|||
|
/// </summary>
|
|||
|
|
|||
|
public class DrawingBus
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Класс-сущность
|
|||
|
/// </summary>
|
|||
|
public EntityBus? EntityBus { 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 _drawningBusWidth = 310;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Высота прорисовки автобуса
|
|||
|
/// </summary>
|
|||
|
private readonly int _drawningBusHeight = 52;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Инициализация свойств
|
|||
|
/// </summary>
|
|||
|
/// <param name="speed">Скорость</param>
|
|||
|
/// <param name="weight">Вес</param>
|
|||
|
/// <param name="bodyColor">Основной цвет</param>
|
|||
|
/// <param name="additionalColor">Дополнительный цвет</param>
|
|||
|
/// <param name="additionalCompartment">Признак наличия дополнительного отсека</param>
|
|||
|
/// <param name="accordion"">Признак наличия гармошки</param>
|
|||
|
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool additionalCompartment, bool accordion, bool v)
|
|||
|
{
|
|||
|
EntityBus = new EntityBus();
|
|||
|
EntityBus.Init(speed, weight, bodyColor, additionalColor, additionalCompartment, accordion);
|
|||
|
_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)
|
|||
|
//{
|
|||
|
// // TODO проверка, что объект "влезает" в размеры поля
|
|||
|
// // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена
|
|||
|
|
|||
|
// _pictureWidth = width;
|
|||
|
// _pictureHeight = height;
|
|||
|
|
|||
|
// return true;
|
|||
|
//}
|
|||
|
public bool SetPictureSize(int width, int height)
|
|||
|
{
|
|||
|
if (width >= _drawningBusWidth && height >= _drawningBusWidth)
|
|||
|
{
|
|||
|
_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;
|
|||
|
// }
|
|||
|
|
|||
|
|
|||
|
|
|||
|
// _startPosX = x;
|
|||
|
// _startPosY = y;
|
|||
|
//}
|
|||
|
|
|||
|
public void SetPosition(int x, int y)
|
|||
|
{
|
|||
|
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (x < 0)
|
|||
|
{
|
|||
|
x = 0;
|
|||
|
}
|
|||
|
else if (x > _pictureWidth.Value - _drawningBusWidth)
|
|||
|
{
|
|||
|
x = _pictureWidth.Value - _drawningBusWidth;
|
|||
|
}
|
|||
|
|
|||
|
if (y < 0)
|
|||
|
{
|
|||
|
y = 0;
|
|||
|
}
|
|||
|
else if (y > _pictureHeight.Value - _drawningBusHeight)
|
|||
|
{
|
|||
|
y = _pictureHeight.Value - _drawningBusHeight;
|
|||
|
}
|
|||
|
|
|||
|
_startPosX = x;
|
|||
|
_startPosY = y;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Изменение направления перемещения
|
|||
|
/// </summary>
|
|||
|
/// <param name="direction">Направление</param>
|
|||
|
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
|
|||
|
public bool MoveTransport(DirectionType direction)
|
|||
|
{
|
|||
|
if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
switch (direction)
|
|||
|
{
|
|||
|
//влево
|
|||
|
case DirectionType.Left:
|
|||
|
if (_startPosX.Value - EntityBus.Step > 0)
|
|||
|
{
|
|||
|
_startPosX -= (int)EntityBus.Step;
|
|||
|
}
|
|||
|
return true;
|
|||
|
//вверх
|
|||
|
case DirectionType.Up:
|
|||
|
if (_startPosY.Value - EntityBus.Step > 0)
|
|||
|
{
|
|||
|
_startPosY -= (int)EntityBus.Step;
|
|||
|
}
|
|||
|
return true;
|
|||
|
// вправо
|
|||
|
case DirectionType.Right:
|
|||
|
if (_startPosX.Value + EntityBus.Step <_pictureWidth - _drawningBusWidth)
|
|||
|
{
|
|||
|
_startPosX += (int)EntityBus.Step;
|
|||
|
}
|
|||
|
return true;
|
|||
|
//вниз
|
|||
|
case DirectionType.Down:
|
|||
|
if (_startPosY.Value + EntityBus.Step < _pictureHeight - _drawningBusHeight)
|
|||
|
{
|
|||
|
_startPosY += (int)EntityBus.Step;
|
|||
|
}
|
|||
|
return true;
|
|||
|
default:
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Прорисовка объекта
|
|||
|
/// </summary>
|
|||
|
/// <param name="g"></param>
|
|||
|
public void DrawTransport(Graphics g)
|
|||
|
{
|
|||
|
if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
Pen pen = new(Color.Black);
|
|||
|
Brush additionalBrush = new SolidBrush(EntityBus.AdditionalColor);
|
|||
|
|
|||
|
//корпус автобуса
|
|||
|
|
|||
|
|
|||
|
|
|||
|
g.DrawRectangle(pen, _startPosX.Value + 0, _startPosY.Value, 175, 50);
|
|||
|
g.DrawRectangle(pen, _startPosX.Value + 0, _startPosY.Value - 1, 175, 50);
|
|||
|
|
|||
|
g.FillRectangle(additionalBrush, _startPosX.Value + 0, _startPosY.Value, 175, 50);
|
|||
|
|
|||
|
//окна
|
|||
|
|
|||
|
Brush brBlue = new SolidBrush(Color.LightBlue);
|
|||
|
g.FillEllipse(brBlue, _startPosX.Value + 80, _startPosY.Value + 6, 16, 25);
|
|||
|
g.FillEllipse(brBlue, _startPosX.Value + 105, _startPosY.Value + 6, 16, 25);
|
|||
|
g.FillEllipse(brBlue, _startPosX.Value + 130, _startPosY.Value + 6, 16, 25);
|
|||
|
g.FillEllipse(brBlue, _startPosX.Value + 155, _startPosY.Value + 6, 16, 25);
|
|||
|
g.FillEllipse(brBlue, _startPosX.Value + 25, _startPosY.Value + 6, 16, 25);
|
|||
|
g.FillEllipse(brBlue, _startPosX.Value + 5, _startPosY.Value + 6, 16, 25);
|
|||
|
|
|||
|
g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 6, 16, 25);
|
|||
|
g.DrawEllipse(pen, _startPosX.Value + 105, _startPosY.Value + 6, 16, 25);
|
|||
|
g.DrawEllipse(pen, _startPosX.Value + 130, _startPosY.Value + 6, 16, 25);
|
|||
|
g.DrawEllipse(pen, _startPosX.Value + 155, _startPosY.Value + 6, 16, 25);
|
|||
|
g.DrawEllipse(pen, _startPosX.Value + 25, _startPosY.Value + 6, 16, 25);
|
|||
|
g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 6, 16, 25);
|
|||
|
|
|||
|
|
|||
|
//дверь
|
|||
|
|
|||
|
Brush br = new SolidBrush(EntityBus.BodyColor);
|
|||
|
g.DrawRectangle(pen, _startPosX.Value + 49, _startPosY.Value + 9, 20, 40);
|
|||
|
g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 10, 20, 40);
|
|||
|
g.FillRectangle(br, _startPosX.Value + 50, _startPosY.Value + 10, 20, 40);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
//колеса
|
|||
|
|
|||
|
Brush brGrey = new SolidBrush(Color.Gray);
|
|||
|
g.FillEllipse(brGrey, _startPosX.Value + 15, _startPosY.Value + 40, 20, 20);
|
|||
|
g.FillEllipse(brGrey, _startPosX.Value + 115, _startPosY.Value + 40, 20, 20);
|
|||
|
g.DrawEllipse(pen, _startPosX.Value + 15, _startPosY.Value + 40, 20, 20);
|
|||
|
g.DrawEllipse(pen, _startPosX.Value + 115, _startPosY.Value + 40, 20, 20);
|
|||
|
|
|||
|
|
|||
|
//доп отсек
|
|||
|
if (EntityBus.AdditionalCompartment)
|
|||
|
{
|
|||
|
//корпус
|
|||
|
g.DrawRectangle(pen, _startPosX.Value + 204, _startPosY.Value - 1, 105, 50);
|
|||
|
g.DrawRectangle(pen, _startPosX.Value + 205, _startPosY.Value, 105, 50);
|
|||
|
g.FillRectangle(additionalBrush, _startPosX.Value + 205, _startPosY.Value, 105, 50);
|
|||
|
|
|||
|
|
|||
|
//окна
|
|||
|
g.FillEllipse(brBlue, _startPosX.Value + 247, _startPosY.Value + 6, 16, 25);
|
|||
|
g.FillEllipse(brBlue, _startPosX.Value + 275, _startPosY.Value + 6, 16, 25);
|
|||
|
g.DrawEllipse(pen, _startPosX.Value + 247, _startPosY.Value + 6, 16, 25);
|
|||
|
g.DrawEllipse(pen, _startPosX.Value + 275, _startPosY.Value + 6, 16, 25);
|
|||
|
|
|||
|
|
|||
|
|
|||
|
//дверь
|
|||
|
g.FillRectangle(br, _startPosX.Value + 215, _startPosY.Value + 10, 20, 40);
|
|||
|
g.DrawRectangle(pen, _startPosX.Value + 215, _startPosY.Value + 10, 20, 40);
|
|||
|
//колеса
|
|||
|
|
|||
|
|
|||
|
g.FillEllipse(brGrey, _startPosX.Value + 250, _startPosY.Value + 40, 20, 20);
|
|||
|
g.DrawEllipse(pen, _startPosX.Value + 250, _startPosY.Value + 40, 20, 20);
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
//гармошка
|
|||
|
if (EntityBus.Accordion)
|
|||
|
{
|
|||
|
Brush brGray = new SolidBrush(Color.LightGray);
|
|||
|
|
|||
|
g.FillRectangle(brGray, _startPosX.Value + 175, _startPosY.Value + 4, 30, 42);
|
|||
|
g.FillRectangle(brGray, _startPosX.Value + 180, _startPosY.Value + 4, 5, 42);
|
|||
|
g.FillRectangle(brGray, _startPosX.Value + 185, _startPosY.Value + 4, 5, 42);
|
|||
|
g.FillRectangle(brGray, _startPosX.Value + 190, _startPosY.Value + 4, 5, 42);
|
|||
|
g.FillRectangle(brGray, _startPosX.Value + 195, _startPosY.Value + 4, 5, 42);
|
|||
|
|
|||
|
g.DrawRectangle(pen, _startPosX.Value + 175, _startPosY.Value + 4, 30, 42);
|
|||
|
g.DrawRectangle(pen, _startPosX.Value + 180, _startPosY.Value + 4, 5, 42);
|
|||
|
g.DrawRectangle(pen, _startPosX.Value + 185, _startPosY.Value + 4, 5, 42);
|
|||
|
g.DrawRectangle(pen, _startPosX.Value + 190, _startPosY.Value + 4, 5, 42);
|
|||
|
g.DrawRectangle(pen, _startPosX.Value + 195, _startPosY.Value + 4, 5, 42);
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|