ISEbd-21_Agliullov.D.A._Air.../AirBomber/AirBomber/DrawningAirplane.cs
2022-09-30 21:16:33 +04:00

230 lines
9.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace AirBomber
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningAirplane
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityAirplane Airplane { get; protected set; }
public IAirplaneEngines? DrawningEngines { get; private set; }
/// <summary>
/// Левая координата отрисовки самолета
/// </summary>
protected float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки самолета
/// </summary>
protected float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
protected int? _pictureWidth = null;
/// <summary>
/// Высота окна отрисовки
/// </summary>
protected int? _pictureHeight = null;
/// <summary>
/// Ширина отрисовки самолета
/// </summary>
protected readonly int _airplaneWidth = 90;
/// <summary>
/// Высота отрисовки самолета
/// </summary>
protected readonly int _airplaneHeight = 105;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес самолета</param>
/// <param name="bodyColor">Цвет обшивки</param>
/// <param name="typeAirplaneEngines">Какой будет двигатель самолета. null - двигатели отсутствуют</param>
public DrawningAirplane(int speed, float weight, Color bodyColor, IAirplaneEngines? typeAirplaneEngines = null)
{
Airplane = new EntityAirplane(speed, weight, bodyColor);
DrawningEngines = typeAirplaneEngines;
}
public DrawningAirplane(EntityAirplane entityAirplane, IAirplaneEngines? typeAirplaneEngines = null)
{
Airplane = entityAirplane;
DrawningEngines = typeAirplaneEngines;
}
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес самолета</param>
/// <param name="bodyColor">Цвет обшивки</param>
/// <param name="airplaneWidth">Ширина отрисовки самолета</param>
/// <param name="airplaneHeight">Высота отрисовки самолета</param>
/// <param name="typeAirplaneEngines">Какой будет двигатель самолета. null - двигатели отсутствуют</param>
protected DrawningAirplane(int speed, float weight, Color bodyColor, int airplaneWidth, int airplaneHeight, IAirplaneEngines? typeAirplaneEngines) :
this(speed, weight, bodyColor, typeAirplaneEngines)
{
_airplaneWidth = airplaneWidth;
_airplaneHeight = airplaneHeight;
}
/// <summary>
/// Установка позиции самолета
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void SetPosition(int x, int y, int width, int height)
{
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
// Проверка на нахождение предмета полностью в границах окна
if (width <= _airplaneWidth || height <= _airplaneHeight
|| _startPosX < 0 || _startPosX + _airplaneWidth > width ||
_startPosY < 0 || _startPosY + _airplaneHeight > height)
{
_pictureWidth = null;
_pictureHeight = null;
}
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
switch (direction)
{
// вправо
case Direction.Right:
if (_startPosX + _airplaneWidth + Airplane.Step < _pictureWidth)
{
_startPosX += Airplane.Step;
}
break;
//влево
case Direction.Left:
if (_startPosX - Airplane.Step > 0)
{
_startPosX -= Airplane.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - Airplane.Step > 0)
{
_startPosY -= Airplane.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _airplaneHeight + Airplane.Step < _pictureHeight)
{
_startPosY += Airplane.Step;
}
break;
}
}
/// <summary>
/// Отрисовка самолета
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
var x = _startPosX;
var y = _startPosY;
var w = _airplaneWidth;
var h = _airplaneHeight;
SolidBrush BodyColorBrush = new(Airplane.BodyColor);
// Треугольник самолета
g.FillPolygon(BodyColorBrush, new PointF[] {
new PointF(x , y + h / 2),
new PointF(x + 15, y + h / 2 - 7),
new PointF(x + 15, y + h / 2 + 8),
});
// Тело самолета
g.FillRectangle(BodyColorBrush, x + 15, y + h / 2 - 7, w - 35, 15);
DrawWing(g, BodyColorBrush, new PointF(x + w / 2 - 20, y + h / 2 - 7), new PointF(x + w / 2, y + h / 2 - 7), true , h / 2 - 7);
DrawWing(g, BodyColorBrush, new PointF(x + w / 2 - 20, y + h / 2 + 7), new PointF(x + w / 2, y + h / 2 + 7), false, h / 2 - 7);
// Линия примыкающая к хвосту слева
g.FillPolygon(BodyColorBrush, new PointF[] {
new PointF(x + w - 20, y + h / 2 - 7),
new PointF(x + w - 30, y + h / 2 - 7),
new PointF(x + w - 5, y + 15 ),
new PointF(x + w - 5, y + 25 ),
});
// Линия примыкающая к хвосту справа
g.FillPolygon(BodyColorBrush, new PointF[] {
new PointF(x + w - 20, y + h / 2 + 7),
new PointF(x + w - 30, y + h / 2 + 7),
new PointF(x + w - 5, y + h - 15 ),
new PointF(x + w - 5, y + h - 25 ),
});
// Конец хвоста
g.FillRectangle(BodyColorBrush, x + w - 5, y + 15, 5, h - 30);
// Двигатели
DrawningEngines?.DrawEngines(g, Airplane.BodyColor, x + w / 2 - 20, y + h, y, 15);
}
private void DrawWing(Graphics g, Brush brush, PointF startBase, PointF endBase, bool isUp, int heightWing)
{
var yDirection = (isUp ? -1 : 1);
var endWingY = startBase.Y + yDirection * heightWing;
g.FillPolygon(brush, new PointF[]
{
startBase,
endBase,
new PointF(startBase.X + (endBase.X - startBase.X) / 2, endWingY),
new PointF(startBase.X, endWingY),
});
}
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _airplaneWidth || _pictureHeight <= _airplaneHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _airplaneWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _airplaneWidth;
}
if (_startPosY + _airplaneHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _airplaneHeight;
}
}
/// <summary>
/// Получение текущей позиции объекта
/// </summary>
/// <returns></returns>
public RectangleF GetCurrentPosition()
{
return new(_startPosX, _startPosY, _airplaneWidth, _airplaneHeight);
}
}
}