233 lines
8.1 KiB
C#
233 lines
8.1 KiB
C#
using System.Drawing;
|
||
|
||
namespace Lab1;
|
||
|
||
public class DrawingElectroTrans
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityElectroTrans? EntityElectroTrans { 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 _drawningTransWidth = 110;
|
||
|
||
/// <summary>
|
||
/// Высота прорисовки автомобиля
|
||
/// </summary>
|
||
private readonly int _drawningTransHeight = 60;
|
||
|
||
/// <summary>
|
||
/// Инициализация свойств
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес автомобиля</param>
|
||
/// <param name="bodyColor">Основной цвет</param>
|
||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||
/// <param name="horns">Признак активности усов</param>
|
||
/// <param name="wheels">Признак колличества колес</param>
|
||
/// <param name="battery">Признак наличия бптпрей</param>
|
||
public void Init(int speed, double weight, Color bodyColor, Color
|
||
additionalColor, bool horns, int wheels, bool battery)
|
||
{
|
||
EntityElectroTrans = new EntityElectroTrans();
|
||
EntityElectroTrans.Init(speed, weight, bodyColor, additionalColor, horns, wheels, battery);
|
||
_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)
|
||
{
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
return true;
|
||
}
|
||
|
||
/// <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 < 40)
|
||
{
|
||
x = 40;
|
||
}else if (x > Config.screenSize.Width - 40)
|
||
{
|
||
x = Config.screenSize.Width - 40;
|
||
}
|
||
|
||
if (y < 40)
|
||
{
|
||
y = 40;
|
||
}
|
||
else if (y > Config.screenSize.Height - 40)
|
||
{
|
||
y = Config.screenSize.Height - 40;
|
||
}
|
||
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
|
||
public bool MoveTransport(DirectionType direction)
|
||
{
|
||
if (EntityElectroTrans == null || !_startPosX.HasValue ||
|
||
!_startPosY.HasValue)
|
||
{
|
||
return false;
|
||
}
|
||
if (_startPosX.Value < 40 || _startPosY.Value < 40 || _startPosX > Config.screenSize.Width - 40 || _startPosY > Config.screenSize.Height - 40)
|
||
{
|
||
return false;
|
||
}
|
||
switch (direction)
|
||
{
|
||
//влево
|
||
case DirectionType.Left:
|
||
if (_startPosX.Value - EntityElectroTrans.Step > 40)
|
||
{
|
||
_startPosX -= (int)EntityElectroTrans.Step;
|
||
}
|
||
|
||
return true;
|
||
//вверх
|
||
case DirectionType.Up:
|
||
if (_startPosY.Value - EntityElectroTrans.Step > 40)
|
||
{
|
||
_startPosY -= (int)EntityElectroTrans.Step;
|
||
}
|
||
|
||
return true;
|
||
// вправо
|
||
case DirectionType.Right:
|
||
if (_startPosX.Value + EntityElectroTrans.Step < Config.screenSize.Width - 40)
|
||
{
|
||
_startPosX += (int)EntityElectroTrans.Step;
|
||
}
|
||
return true;
|
||
//вниз
|
||
case DirectionType.Down:
|
||
if (_startPosY.Value + EntityElectroTrans.Step < Config.screenSize.Height - 40)
|
||
{
|
||
_startPosY += (int)EntityElectroTrans.Step;
|
||
}
|
||
|
||
return true;
|
||
default:
|
||
return false;
|
||
}
|
||
|
||
}
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityElectroTrans == null || !_startPosX.HasValue ||
|
||
!_startPosY.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
Pen pen = new(EntityElectroTrans.BodyColor);
|
||
Pen additionalPen = new(EntityElectroTrans.AdditionalColor);
|
||
Brush brush = new SolidBrush(EntityElectroTrans.BodyColor);
|
||
Brush additionalBrush = new SolidBrush(EntityElectroTrans.AdditionalColor);
|
||
|
||
g.DrawPolygon(pen, new Point[] {
|
||
new Point(_startPosX.Value - 40, _startPosY.Value),
|
||
new Point(_startPosX.Value - 30, _startPosY.Value - 20),
|
||
new Point(_startPosX.Value + 30, _startPosY.Value - 20),
|
||
new Point(_startPosX.Value + 40, _startPosY.Value),
|
||
new Point(_startPosX.Value + 40, _startPosY.Value + 20),
|
||
new Point(_startPosX.Value - 40, _startPosY.Value + 20),
|
||
});
|
||
|
||
for (int i = 0; i < EntityElectroTrans.Wheels; i++)
|
||
{
|
||
g.FillEllipse(additionalBrush, new Rectangle((_startPosX.Value - 40) + (70 / EntityElectroTrans.Wheels) * (i + 1) + -4, _startPosY.Value + 16, 8, 8));
|
||
}
|
||
g.FillPolygon(additionalBrush, new Point[] {
|
||
new Point(_startPosX.Value - 38, _startPosY.Value),
|
||
new Point(_startPosX.Value - 30, _startPosY.Value - 17),
|
||
new Point(_startPosX.Value + 30, _startPosY.Value - 17),
|
||
new Point(_startPosX.Value + 38, _startPosY.Value),
|
||
});
|
||
|
||
if (EntityElectroTrans.Horns)
|
||
{
|
||
g.DrawPolygon(additionalPen, new Point[] {
|
||
new Point(_startPosX.Value, _startPosY.Value - 20),
|
||
new Point(_startPosX.Value - 20, _startPosY.Value - 30),
|
||
new Point(_startPosX.Value + 20, _startPosY.Value - 30),
|
||
|
||
});
|
||
}
|
||
else
|
||
{
|
||
g.DrawPolygon(additionalPen, new Point[] {
|
||
new Point(_startPosX.Value, _startPosY.Value - 20),
|
||
new Point(_startPosX.Value - 20, _startPosY.Value - 23),
|
||
new Point(_startPosX.Value + 20, _startPosY.Value - 23),
|
||
|
||
});
|
||
}
|
||
if (EntityElectroTrans.Battery){
|
||
g.FillPolygon(additionalBrush, new Point[] {
|
||
new Point(_startPosX.Value - 15, _startPosY.Value + 2),
|
||
new Point(_startPosX.Value - 15, _startPosY.Value + 6),
|
||
new Point(_startPosX.Value - 18, _startPosY.Value + 6),
|
||
new Point(_startPosX.Value - 18, _startPosY.Value + 10),
|
||
new Point(_startPosX.Value - 15, _startPosY.Value + 10),
|
||
new Point(_startPosX.Value - 15, _startPosY.Value + 16),
|
||
new Point(_startPosX.Value + 18, _startPosY.Value + 16),
|
||
new Point(_startPosX.Value + 18, _startPosY.Value + 2),
|
||
|
||
});
|
||
}
|
||
}
|
||
|
||
|
||
} |