PIBD14-BOYKO-M.S.SuperEasyS.../ProjectElectroTrans/Drawnings/DrawingTrans.cs
2024-03-13 19:49:07 +04:00

226 lines
7.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 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="drawningCarWidth">Ширина прорисовки автомобиля</param>
/// <param name="drawningCarHeight">Высота прорисовки автомобиля</param>
protected DrawingTrans(int drawningCarWidth, int drawningCarHeight) : this()
{
_drawningTransWidth = drawningCarWidth;
_pictureHeight = drawningCarHeight;
}
/// <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),
});
}
}