278 lines
9.6 KiB
C#
278 lines
9.6 KiB
C#
using Battleship.Entities;
|
||
|
||
namespace Battleship.Drawings;
|
||
|
||
public class DrawingWarship
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityWarship? EntityWarship { 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 _drawingWarshipWidth = 129;
|
||
|
||
/// <summary>
|
||
/// Высота прорисовки военного корабля
|
||
/// </summary>
|
||
private readonly int _drawingWarshipHeight = 80;
|
||
|
||
/// <summary>
|
||
/// Координата Х объекта
|
||
/// </summary>
|
||
public int? GetPosX => _startPosX;
|
||
|
||
/// <summary>
|
||
/// Координата Y объекта
|
||
/// </summary>
|
||
public int? GetPosY => _startPosY;
|
||
|
||
/// <summary>
|
||
/// Ширина объекта
|
||
/// </summary>
|
||
public int GetWidth => _drawingWarshipWidth;
|
||
|
||
/// <summary>
|
||
/// Высота объекта
|
||
/// </summary>
|
||
public int GetHeight => _drawingWarshipHeight;
|
||
|
||
/// <summary>
|
||
/// Пустой конструктор
|
||
/// </summary>
|
||
private DrawingWarship()
|
||
{
|
||
_pictureWidth = null;
|
||
_pictureHeight = null;
|
||
_startPosX = null;
|
||
_startPosY = null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес</param>
|
||
/// <param name="bodyColor">Основной цвет</param>
|
||
public DrawingWarship(int speed, double weight, Color bodyColor) : this()
|
||
{
|
||
EntityWarship = new EntityWarship(speed, weight, bodyColor);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Конструктор для наследников
|
||
/// </summary>
|
||
/// <param name="drawingWarshipWidth">Ширина прорисовки военного корабля</param>
|
||
/// <param name="drawingWarshipHeight">Высота прорисовки военного корабля</param>
|
||
protected DrawingWarship(int drawingWarshipWidth, int drawingWarshipHeight) : this()
|
||
{
|
||
this._drawingWarshipWidth = drawingWarshipWidth;
|
||
this._drawingWarshipHeight = drawingWarshipHeight;
|
||
}
|
||
|
||
public DrawingWarship(EntityWarship ship) : this()
|
||
{
|
||
EntityWarship = new EntityWarship(ship.Speed, ship.Weight, ship.BodyColor);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Установка границ поля
|
||
/// </summary>
|
||
/// <param name="width">Ширина поля</param>
|
||
/// <param name="height">Высота поля</param>
|
||
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
|
||
|
||
public bool SetPictureSize(int width, int height)
|
||
{
|
||
//TODO проверка, что объект "влезает" в размеры поля
|
||
//если влезает, сохраняем границы и корректируем позицию объекта, если она уже была установлена
|
||
|
||
if (_drawingWarshipWidth > width || _drawingWarshipHeight > height)
|
||
{
|
||
return false;
|
||
}
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
|
||
if (_startPosX.HasValue || _startPosY.HasValue)
|
||
{
|
||
|
||
if (_startPosX + _drawingWarshipWidth > _pictureWidth)
|
||
{
|
||
_startPosX = _pictureWidth - _drawingWarshipWidth;
|
||
}
|
||
else if (_startPosX < 0) _startPosX = 0;
|
||
if (_startPosY + _drawingWarshipHeight > _pictureHeight)
|
||
{
|
||
_startPosY = _pictureHeight - _drawingWarshipHeight;
|
||
}
|
||
else if (_startPosY < 0) _startPosY = 0;
|
||
}
|
||
return true;
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// Установка позиции
|
||
/// </summary>
|
||
/// <param name="x">Координата X</param>
|
||
/// <param name="y">Координата Y</param>
|
||
|
||
public void SetPosition(int x, int y)
|
||
{
|
||
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
|
||
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
|
||
// то надо изменить координаты, чтобы он оставался в этих границах
|
||
|
||
if (x + _drawingWarshipWidth > _pictureWidth)
|
||
{
|
||
_startPosX = _pictureWidth - _drawingWarshipWidth;
|
||
}
|
||
else if (x < 0) _startPosX = 0;
|
||
else _startPosX = x;
|
||
|
||
if (y + _drawingWarshipHeight > _pictureHeight)
|
||
{
|
||
_startPosY = _pictureHeight - _drawingWarshipHeight;
|
||
}
|
||
else if (y < 0) _startPosY = 0;
|
||
else _startPosY = y;
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
|
||
|
||
public bool MoveTransport(DirectionType direction)
|
||
{
|
||
if (EntityWarship == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
switch (direction)
|
||
{
|
||
//влево
|
||
case DirectionType.Left:
|
||
if (_startPosX.Value - EntityWarship.Step > 0)
|
||
{
|
||
_startPosX -= (int)EntityWarship.Step;
|
||
}
|
||
return true;
|
||
|
||
//вправо
|
||
case DirectionType.Right:
|
||
if (_startPosX.Value + EntityWarship.Step + _drawingWarshipWidth < _pictureWidth)
|
||
{
|
||
_startPosX += (int)EntityWarship.Step;
|
||
}
|
||
return true;
|
||
|
||
//вверх
|
||
case DirectionType.Up:
|
||
if (_startPosY.Value - EntityWarship.Step > 0)
|
||
{
|
||
_startPosY -= (int)EntityWarship.Step;
|
||
}
|
||
return true;
|
||
|
||
//вниз
|
||
case DirectionType.Down:
|
||
if (_startPosY.Value + EntityWarship.Step + _drawingWarshipHeight < _pictureHeight) //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||
{
|
||
_startPosY += (int)EntityWarship.Step;
|
||
}
|
||
return true;
|
||
|
||
default:
|
||
return false;
|
||
}
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
|
||
public virtual void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityWarship == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
|
||
Pen pen = new(Color.Black);
|
||
|
||
//заливка линкора
|
||
Brush br = new SolidBrush(EntityWarship.BodyColor);
|
||
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value, 80, 40);
|
||
//границы линкора
|
||
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value, 80, 40); //ширина высота (сама палуба)
|
||
|
||
//заливка двигателей (2 фигни сзади)
|
||
Brush brBlack = new SolidBrush(Color.Black);
|
||
g.FillRectangle(brBlack, _startPosX.Value + 5, _startPosY.Value + 7, 5, 10); //верхний
|
||
g.FillRectangle(brBlack, _startPosX.Value + 5, _startPosY.Value + 22, 5, 10); //нижний
|
||
//границы двигателей
|
||
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 7, 5, 10); //двигатель верхний
|
||
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 22, 5, 10); //двигатель нижний
|
||
|
||
//прямоугольник левый на палубе (горизонтальный)
|
||
Brush brLightSteelBlue = new SolidBrush(Color.LightSteelBlue);
|
||
g.FillRectangle(brLightSteelBlue, _startPosX.Value + 32, _startPosY.Value + 13, 21, 12);
|
||
g.DrawRectangle(pen, _startPosX.Value + 32, _startPosY.Value + 13, 21, 12);
|
||
|
||
//прямоугольник правый на палубе (вертикальный)
|
||
Brush brGray = new SolidBrush(Color.Gray);
|
||
g.FillRectangle(brGray, _startPosX.Value + 53, _startPosY.Value + 7, 16, 24);
|
||
g.DrawRectangle(pen, _startPosX.Value + 53, _startPosY.Value + 7, 16, 24);
|
||
|
||
//линии для треугольника
|
||
Point[] pointsNOS =
|
||
{
|
||
new Point(_startPosX.Value + 90, _startPosY.Value),
|
||
new Point(_startPosX.Value + 120, _startPosY.Value + 20),
|
||
new Point(_startPosX.Value + 90, _startPosY.Value + 40),
|
||
|
||
};
|
||
g.DrawPolygon(pen, pointsNOS);
|
||
g.FillPolygon(br, pointsNOS);
|
||
|
||
//заливка круга
|
||
Brush brLightSlateGray = new SolidBrush(Color.LightSlateGray);
|
||
g.FillEllipse(brLightSlateGray, _startPosX.Value + 78, _startPosY.Value + 12, 15, 15);
|
||
//круг на палубе
|
||
g.DrawEllipse(pen, _startPosX.Value + 78, _startPosY.Value + 12, 15, 15);
|
||
}
|
||
}
|