268 lines
8.7 KiB
C#
268 lines
8.7 KiB
C#
using ProjectAirplaneWithRadar.Entities;
|
||
|
||
|
||
namespace ProjectAirplaneWithRadar.Drawnings
|
||
{
|
||
public class DrawningAirplane
|
||
{
|
||
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityAirplane? EntityAirplane { 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 PlaneWidth = 150;
|
||
|
||
/// <summary>
|
||
/// Высота прорисовки самолета
|
||
/// </summary>
|
||
private readonly int PlaneHeight = 85;
|
||
|
||
/// <summary>
|
||
/// Координата X объекта
|
||
/// </summary>
|
||
public int? GetPosX => _startPosX;
|
||
|
||
/// <summary>
|
||
/// Координата Y объекта
|
||
/// </summary>
|
||
public int? GetPosY => _startPosY;
|
||
|
||
/// <summary>
|
||
/// Ширина объекта
|
||
/// </summary>
|
||
public int GetWidth => PlaneWidth;
|
||
|
||
/// <summary>
|
||
/// Высота объекта
|
||
/// </summary>
|
||
public int GetHeight => PlaneHeight;
|
||
|
||
/// <summary>
|
||
/// Пустой конструктор
|
||
/// </summary>
|
||
private DrawningAirplane()
|
||
{
|
||
_pictureWidth = null;
|
||
_pictureHeight = null;
|
||
_startPosX = null;
|
||
_startPosY = null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес</param>
|
||
/// <param name="bodyColor">Основной цвет</param>
|
||
public DrawningAirplane(int speed, double weight, Color bodyColor) : this()
|
||
{
|
||
EntityAirplane = new EntityAirplane(speed, weight, bodyColor);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Конструктор для наследников
|
||
/// </summary>
|
||
/// <param name="planeWidth">Ширина прорисовки самолета</param>
|
||
/// <param name="planeHeight">Высота прорисовки самолета</param>
|
||
protected DrawningAirplane(int planeWidth, int planeHeight) : this()
|
||
{
|
||
PlaneWidth = planeWidth;
|
||
PlaneHeight = planeHeight;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Установка границ поля
|
||
/// </summary>
|
||
/// <param name="width">Ширина поля</param>
|
||
/// <param name="height">Высота поля</param>
|
||
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
|
||
public bool SetPictureSize(int width, int height)
|
||
{
|
||
if (PlaneWidth < width && PlaneHeight < height)
|
||
{
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
if (_startPosX != null && _startPosY != null)
|
||
{
|
||
if (_startPosX.Value < 0)
|
||
{
|
||
_startPosX = 0;
|
||
}
|
||
if (_startPosY.Value < 0)
|
||
{
|
||
_startPosY = 0;
|
||
}
|
||
|
||
if (_startPosX.Value + PlaneWidth > _pictureWidth)
|
||
{
|
||
_startPosX = _pictureWidth - PlaneWidth;
|
||
}
|
||
if (_startPosY.Value + PlaneHeight > _pictureHeight)
|
||
{
|
||
_startPosY = _pictureHeight - PlaneHeight;
|
||
}
|
||
}
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
|
||
if (_startPosX.Value < 0)
|
||
{
|
||
_startPosX = 0;
|
||
}
|
||
if (_startPosY.Value < 0)
|
||
{
|
||
_startPosY = 0;
|
||
}
|
||
|
||
if (_startPosX.Value + PlaneWidth > _pictureWidth)
|
||
{
|
||
_startPosX = _pictureWidth - PlaneWidth;
|
||
}
|
||
if (_startPosY.Value + PlaneHeight > _pictureHeight)
|
||
{
|
||
_startPosY = _pictureHeight - PlaneHeight;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
|
||
public bool MoveTransport(DirectionType direction)
|
||
{
|
||
if (EntityAirplane == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
int step = (int)EntityAirplane.Step;
|
||
switch (direction)
|
||
{
|
||
//влево
|
||
case DirectionType.Left:
|
||
if (_startPosX.Value - step > 0)
|
||
{
|
||
_startPosX -= step;
|
||
}
|
||
break;
|
||
//вверх
|
||
case DirectionType.Up:
|
||
if (_startPosY.Value - step > 0)
|
||
{
|
||
_startPosY -= step;
|
||
}
|
||
break;
|
||
// вправо
|
||
case DirectionType.Right:
|
||
if (_startPosX.Value + step < _pictureWidth - PlaneWidth)
|
||
{
|
||
_startPosX += step;
|
||
}
|
||
break;
|
||
//вниз
|
||
case DirectionType.Down:
|
||
if (_startPosY.Value + step < _pictureHeight - PlaneHeight)
|
||
{
|
||
_startPosY += step;
|
||
}
|
||
break;
|
||
default:
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public virtual void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityAirplane == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
|
||
|
||
Pen pen = new(Color.Black);
|
||
|
||
|
||
//Корпус
|
||
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 40, 100, 30);
|
||
|
||
//Хвост
|
||
Point[] points = {
|
||
new Point(_startPosX.Value + 10, _startPosY.Value),
|
||
new Point(_startPosX.Value + 10, _startPosY.Value + 40),
|
||
new Point(_startPosX.Value + 50, _startPosY.Value + 40)
|
||
};
|
||
g.DrawPolygon(pen, points);
|
||
|
||
//Кабина
|
||
Point[] points2 = {
|
||
new Point(_startPosX.Value + 110, _startPosY.Value + 35),
|
||
new Point(_startPosX.Value + 110, _startPosY.Value + 55),
|
||
new Point(_startPosX.Value + 150, _startPosY.Value + 55)
|
||
};
|
||
g.DrawPolygon(pen, points2);
|
||
Point[] points3 = {
|
||
new Point(_startPosX.Value + 110, _startPosY.Value + 55),
|
||
new Point(_startPosX.Value + 110, _startPosY.Value + 75),
|
||
new Point(_startPosX.Value + 150, _startPosY.Value + 55)
|
||
};
|
||
g.DrawPolygon(pen, points3);
|
||
|
||
//Крыло
|
||
Brush brBlack = new SolidBrush(EntityAirplane.BodyColor);
|
||
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 50, 70, 10);
|
||
g.FillEllipse(brBlack, _startPosX.Value + 30, _startPosY.Value + 50, 70, 10);
|
||
|
||
//Хвостовой элерон
|
||
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 35, 30, 10);
|
||
g.FillEllipse(brBlack, _startPosX.Value, _startPosY.Value + 35, 30, 10);
|
||
|
||
}
|
||
}
|
||
} |