218 lines
8.5 KiB
C#
218 lines
8.5 KiB
C#
using System.Drawing.Drawing2D;
|
||
|
||
namespace ProjectCruiser;
|
||
/// <summary>
|
||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||
/// </summary>
|
||
public class DrawningCruiser
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityCruiser? EntityCruiser { 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 _drawningCruiserWidth = 150;
|
||
/// <summary>
|
||
/// Высота прорисовки крейсера
|
||
/// </summary>
|
||
private readonly int _drawningCruiserHeight = 50;
|
||
private readonly int _drawningEnginesWidth = 3;
|
||
/// <summary>
|
||
/// Инициализация свойств
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес</param>
|
||
/// <param name="bodyColor">Основной цвет</param>
|
||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||
/// <param name="helicopterArea">Признак наличия вертолетной площадки</param>
|
||
/// <param name="boat">Признак наличия шлюпок</param>
|
||
/// <param name="wepon">Признак наличия пушки</param>
|
||
public void Init(int speed, double weight, Color bodyColor, Color
|
||
additionalColor, bool helicopterArea, bool boat, bool wepon)
|
||
{
|
||
EntityCruiser = new EntityCruiser();
|
||
EntityCruiser.Init(speed, weight, bodyColor, additionalColor,
|
||
helicopterArea, boat, wepon);
|
||
_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 проверка, что объект "влезает" в размеры поля
|
||
// если влезает, сохраняем границы и корректируем позицию объекта,если она была уже установлена
|
||
|
||
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 void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityCruiser == null || !_startPosX.HasValue ||
|
||
!_startPosY.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
Pen pen = new(EntityCruiser.BodyColor, 2);
|
||
Brush additionalBrush = new SolidBrush(Color.Black);
|
||
Brush weaponBrush = new SolidBrush(Color.Black);
|
||
Brush weaponBrush2 = new SolidBrush(EntityCruiser.AdditionalColor);
|
||
Brush helicopterAreaBrush = new HatchBrush(HatchStyle.ZigZag, EntityCruiser.AdditionalColor, Color.FromArgb(163, 163, 163));
|
||
Brush boatBrush = new SolidBrush(EntityCruiser.AdditionalColor);
|
||
|
||
//границы круисера
|
||
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);
|
||
|
||
if (EntityCruiser.HelicopterArea)
|
||
{
|
||
g.FillEllipse(helicopterAreaBrush, _startPosX.Value + 5, _startPosY.Value + 9, 25, 30);
|
||
g.DrawEllipse(pen, _startPosX.Value + 5, _startPosY.Value + 9, 25, 30);
|
||
}
|
||
|
||
if (EntityCruiser.Boat)
|
||
{
|
||
g.DrawEllipse(pen, _startPosX.Value + 34, _startPosY.Value + 2, 30, 7);
|
||
g.FillEllipse(boatBrush, _startPosX.Value + 34, _startPosY.Value + 2, 30, 7);
|
||
|
||
g.DrawEllipse(pen, _startPosX.Value + 34, _startPosY.Value + 39, 30, 7);
|
||
g.FillEllipse(boatBrush, _startPosX.Value + 34, _startPosY.Value + 39, 30, 7);
|
||
}
|
||
|
||
if (EntityCruiser.Weapon)
|
||
{
|
||
g.DrawEllipse(pen, _startPosX.Value + 97, _startPosY.Value + 36, 10, 10);
|
||
g.FillEllipse(weaponBrush2, _startPosX.Value + 97, _startPosY.Value + 36, 10, 10);
|
||
|
||
g.FillRectangle(weaponBrush, _startPosX.Value + 107, _startPosY.Value + 40, 15, 5);
|
||
}
|
||
|
||
}
|
||
} |