230 lines
8.0 KiB
C#
230 lines
8.0 KiB
C#
using ProjectCruiser.Entities;
|
||
|
||
namespace ProjectCruiser.Drawnings;
|
||
/// <summary>
|
||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||
/// </summary>
|
||
public class DrawningCruiser
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityCruiser? EntityCruiser { 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 _drawningCruiserWidth = 150;
|
||
/// <summary>
|
||
/// Высота прорисовки крейсера
|
||
/// </summary>
|
||
private readonly int _drawningCruiserHeight = 50;
|
||
private readonly int _drawningEnginesWidth = 3;
|
||
|
||
/// <summary>
|
||
/// Координата X объекта
|
||
/// </summary>
|
||
public int? GetPosX => _startPosX;
|
||
/// <summary>
|
||
/// Координата Y объекта
|
||
/// </summary>
|
||
public int? GetPosY => _startPosY;
|
||
/// <summary>
|
||
/// Ширина объекта
|
||
/// </summary>
|
||
public int GetWidth => _drawningCruiserWidth;
|
||
/// <summary>
|
||
/// Высота объекта
|
||
/// </summary>
|
||
public int GetHeight => _drawningCruiserHeight;
|
||
|
||
/// <summary>
|
||
/// Пустой онструктор
|
||
/// </summary>
|
||
public DrawningCruiser()
|
||
{
|
||
_pictureWidth = null;
|
||
_pictureHeight = null;
|
||
_startPosX = null;
|
||
_startPosY = null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес</param>
|
||
/// <param name="bodyColor">Основной цвет</param>
|
||
public DrawningCruiser(int speed, double weight, Color bodyColor) : this()
|
||
{
|
||
EntityCruiser = new EntityCruiser(speed, weight, bodyColor);
|
||
}
|
||
/// <summary>
|
||
/// Конструктор для наследников
|
||
/// </summary>
|
||
/// <param name="drawningCarWidth">Ширина прорисовки автомобиля</param>
|
||
/// <param name="drawningCarHeight">Высота прорисовки автомобиля</param>
|
||
protected DrawningCruiser(int drawningCarWidth, int drawningCarHeight) : this()
|
||
{
|
||
_drawningCruiserWidth = drawningCarWidth;
|
||
_pictureHeight = drawningCarHeight;
|
||
}
|
||
|
||
/// <summary>
|
||
/// конструктор
|
||
/// </summary>
|
||
/// <param name="entityCruiser"></param>
|
||
public DrawningCruiser(EntityCruiser entityCruiser)
|
||
{
|
||
EntityCruiser = entityCruiser;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Установка границ поля
|
||
/// </summary>
|
||
/// <param name="width">Ширина поля</param>
|
||
/// <param name="height">Высота поля</param>
|
||
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
|
||
public bool SetPictureSize(int width, int height)
|
||
{
|
||
// TODO проверка, что объект "влезает" в размеры поля
|
||
// если влезает, сохраняем границы и корректируем позицию объекта,если она была уже установлена
|
||
|
||
if (_drawningCruiserHeight > height || _drawningCruiserWidth > width)
|
||
{
|
||
return false;
|
||
|
||
}
|
||
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
|
||
if (_startPosX.HasValue && _startPosY.HasValue)
|
||
{
|
||
SetPosition(_startPosX.Value, _startPosY.Value);
|
||
}
|
||
|
||
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 < 0 || x + _drawningCruiserWidth > _pictureWidth || y < 0 || y + _drawningCruiserHeight > _pictureHeight)
|
||
{
|
||
_startPosX = _pictureWidth - _drawningCruiserWidth;
|
||
_startPosY = _pictureHeight - _drawningCruiserHeight;
|
||
}
|
||
else
|
||
{
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
}
|
||
|
||
}
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
|
||
public bool MoveTransport(DirectionType direction)
|
||
{
|
||
if (EntityCruiser == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return false;
|
||
}
|
||
switch (direction)
|
||
{
|
||
//влево
|
||
case DirectionType.Left:
|
||
if (_startPosX.Value - EntityCruiser.Step - _drawningEnginesWidth > 0)
|
||
{
|
||
_startPosX -= (int)EntityCruiser.Step;
|
||
}
|
||
return true;
|
||
//вверх
|
||
case DirectionType.Up:
|
||
if (_startPosY.Value - EntityCruiser.Step > 0)
|
||
{
|
||
_startPosY -= (int)EntityCruiser.Step;
|
||
}
|
||
return true;
|
||
// вправо
|
||
case DirectionType.Right:
|
||
//TODO прописать логику сдвига в право
|
||
if (_startPosX.Value + EntityCruiser.Step + _drawningCruiserWidth < _pictureWidth)
|
||
{
|
||
_startPosX += (int)EntityCruiser.Step;
|
||
}
|
||
return true;
|
||
//вниз
|
||
case DirectionType.Down:
|
||
if (_startPosY.Value + EntityCruiser.Step + _drawningCruiserHeight < _pictureHeight)
|
||
{
|
||
_startPosY += (int)EntityCruiser.Step;
|
||
}
|
||
return true;
|
||
default:
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public virtual void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityCruiser == null || !_startPosX.HasValue ||
|
||
!_startPosY.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
Pen pen = new(EntityCruiser.BodyColor, 2);
|
||
Brush additionalBrush = new SolidBrush(Color.Black);
|
||
|
||
//границы круисера
|
||
g.DrawLine(pen, _startPosX.Value, _startPosY.Value, _startPosX.Value + 105, _startPosY.Value);
|
||
g.DrawLine(pen, _startPosX.Value + 105, _startPosY.Value, _startPosX.Value + 147, _startPosY.Value + 24);
|
||
|
||
g.DrawLine(pen, _startPosX.Value, _startPosY.Value + 49, _startPosX.Value + 105, _startPosY.Value + 49);
|
||
g.DrawLine(pen, _startPosX.Value + 105, _startPosY.Value + 49, _startPosX.Value + 147, _startPosY.Value + 24);
|
||
|
||
g.DrawLine(pen, _startPosX.Value, _startPosY.Value, _startPosX.Value, _startPosY.Value + 49);
|
||
|
||
//внутренности круисера
|
||
g.DrawEllipse(pen, _startPosX.Value + 94, _startPosY.Value + 14, 19, 19);
|
||
|
||
g.DrawRectangle(pen, _startPosX.Value + 63, _startPosY.Value + 11, 21, 28);
|
||
g.DrawRectangle(pen, _startPosX.Value + 35, _startPosY.Value + 17, 28, 14);
|
||
|
||
//зад
|
||
g.FillRectangle(additionalBrush, _startPosX.Value - 3, _startPosY.Value + 7, 3, 14);
|
||
g.FillRectangle(additionalBrush, _startPosX.Value - 3, _startPosY.Value + 26, 3, 14);
|
||
|
||
}
|
||
} |