230 lines
8.1 KiB
C#
230 lines
8.1 KiB
C#
using System.Reflection.Metadata.Ecma335;
|
||
|
||
namespace ProjectCar;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public class DrawningGasMachine
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityMachine? EntityMachine { 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 _drawningMachineWidth = 150;
|
||
/// <summary>
|
||
/// Высота прорисовки газ автомобиля
|
||
/// </summary>
|
||
private readonly int _drawningMachineHeight = 70;
|
||
|
||
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool gas, bool wheels1, bool wheels2, bool wheels3, bool beacon)
|
||
{
|
||
EntityMachine = new EntityMachine();
|
||
EntityMachine.Init(speed, weight, bodyColor, additionalColor, gas, wheels1, wheels2, wheels3, beacon);
|
||
_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)
|
||
{
|
||
if (EntityMachine == null)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if (width >= _drawningMachineWidth && height >= _drawningMachineHeight)
|
||
{
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
|
||
if (_startPosX.HasValue && _startPosY.HasValue)
|
||
{
|
||
if (_startPosX + _drawningMachineWidth > _pictureWidth)
|
||
{
|
||
_startPosX = _pictureWidth - _drawningMachineWidth;
|
||
}
|
||
if (_startPosY + _drawningMachineHeight > _pictureHeight)
|
||
{
|
||
_startPosY = _pictureHeight - _drawningMachineHeight;
|
||
}
|
||
}
|
||
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)
|
||
{
|
||
_startPosX = 0;
|
||
}
|
||
else if (x + _drawningMachineWidth > _pictureWidth)
|
||
{
|
||
_startPosX = _pictureWidth.Value - _drawningMachineWidth;
|
||
}
|
||
|
||
if (y < 0)
|
||
{
|
||
_startPosY = 0;
|
||
}
|
||
else if (y + _drawningMachineHeight > _pictureHeight)
|
||
{
|
||
_startPosY = _pictureHeight.Value - _drawningMachineHeight;
|
||
}
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
}
|
||
public bool MoveTransport(DirectionType direction)
|
||
{
|
||
if (EntityMachine == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return false;
|
||
}
|
||
switch (direction)
|
||
{
|
||
case DirectionType.Left:
|
||
if (_startPosX.Value - EntityMachine.Step > 0)
|
||
{
|
||
_startPosX -= (int)EntityMachine.Step;
|
||
}
|
||
return true;
|
||
case DirectionType.Up:
|
||
if (_startPosY.Value - EntityMachine.Step > 0)
|
||
{
|
||
_startPosY -= (int)EntityMachine.Step;
|
||
}
|
||
return true;
|
||
case DirectionType.Right:
|
||
if (_startPosX.Value + EntityMachine.Step < _pictureWidth - _drawningMachineWidth)
|
||
{
|
||
_startPosX += (int)EntityMachine.Step;
|
||
}
|
||
return true;
|
||
case DirectionType.Down:
|
||
if (_startPosY.Value + EntityMachine.Step < _pictureHeight - _drawningMachineHeight)
|
||
{
|
||
_startPosY += (int)EntityMachine.Step;
|
||
}
|
||
return true;
|
||
default:
|
||
return false;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityMachine == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
|
||
Pen pen = new(Color.Black);
|
||
Brush additionalBrush = new SolidBrush(EntityMachine.AdditionalColor);
|
||
|
||
//бак с газом
|
||
if (EntityMachine.Gas)
|
||
{
|
||
g.DrawEllipse(pen, _startPosX.Value + 35, _startPosY.Value + 10, 80, 30);
|
||
|
||
g.FillEllipse(additionalBrush, _startPosX.Value + 35, _startPosY.Value + 10, 80, 30);
|
||
}
|
||
//границы автомобиля
|
||
g.DrawRectangle(pen, _startPosX.Value + 40, _startPosY.Value + 40, 110, 10);
|
||
g.DrawRectangle(pen, _startPosX.Value + 116, _startPosY.Value + 10, 30, 30);
|
||
|
||
|
||
|
||
//кузов
|
||
Brush br = new SolidBrush(EntityMachine.BodyColor);
|
||
g.FillRectangle(br, _startPosX.Value + 40, _startPosY.Value + 40, 110, 10);
|
||
|
||
|
||
//кабина
|
||
g.FillRectangle(br, _startPosX.Value + 116, _startPosY.Value + 10, 30, 30);
|
||
|
||
|
||
//маяк сигнальный
|
||
if (EntityMachine.Beacon)
|
||
{
|
||
g.DrawRectangle(pen, _startPosX.Value + 130, _startPosY.Value, 10, 10);
|
||
g.FillRectangle(additionalBrush, _startPosX.Value + 130, _startPosY.Value, 10, 10);
|
||
}
|
||
|
||
|
||
//колеса(2)
|
||
//if (EntityMachine.Wheels1)
|
||
//{
|
||
// g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 50, 20, 20);
|
||
// g.FillEllipse(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 50, 20, 20);
|
||
// g.DrawEllipse(pen, _startPosX.Value + 125, _startPosY.Value + 50, 20, 20);
|
||
// g.FillEllipse(additionalBrush, _startPosX.Value + 125, _startPosY.Value + 50, 20, 20);
|
||
//}
|
||
|
||
|
||
//колеса(3)
|
||
if (EntityMachine.Wheels2)
|
||
{
|
||
g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 50, 20, 20);
|
||
g.FillEllipse(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 50, 20, 20);
|
||
g.DrawEllipse(pen, _startPosX.Value + 75, _startPosY.Value + 50, 20, 20);
|
||
g.FillEllipse(additionalBrush, _startPosX.Value + 75, _startPosY.Value + 50, 20, 20);
|
||
g.DrawEllipse(pen, _startPosX.Value + 125, _startPosY.Value + 50, 20, 20);
|
||
g.FillEllipse(additionalBrush, _startPosX.Value + 125, _startPosY.Value + 50, 20, 20);
|
||
}
|
||
|
||
//колеса(4)
|
||
//if (EntityMachine.Wheels3)
|
||
//{
|
||
// g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 50, 20, 20);
|
||
// g.FillEllipse(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 50, 20, 20);
|
||
// g.DrawEllipse(pen, _startPosX.Value + 75, _startPosY.Value + 50, 20, 20);
|
||
// g.FillEllipse(additionalBrush, _startPosX.Value + 75, _startPosY.Value + 50, 20, 20);
|
||
// g.DrawEllipse(pen, _startPosX.Value + 125, _startPosY.Value + 50, 20, 20);
|
||
// g.FillEllipse(additionalBrush, _startPosX.Value + 125, _startPosY.Value + 50, 20, 20);
|
||
// g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 50, 20, 20);
|
||
// g.FillEllipse(additionalBrush, _startPosX.Value + 100, _startPosY.Value + 50, 20, 20);
|
||
//}
|
||
|
||
}
|
||
}
|