202 lines
7.9 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>
internal class DrawningAirplane
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityAirplane Airplane { get; private set; }
public DrawningAirplaneEngines DrawningEngines { get; private set; }
/// <summary>
/// Левая координата отрисовки самолета
/// </summary>
private float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки самолета
/// </summary>
private float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private int? _pictureWidth = null;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private int? _pictureHeight = null;
/// <summary>
/// Ширина отрисовки самолета
/// </summary>
private readonly int _airplaneWidth = 110;
/// <summary>
/// Высота отрисовки самолета
/// </summary>
private readonly int _airplaneHeight = 140;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес самолета</param>
/// <param name="bodyColor">Цвет обшивки</param>
public void Init(int speed, float weight, Color bodyColor)
{
Airplane = new EntityAirplane();
Airplane.Init(speed, weight, bodyColor);
DrawningEngines = new();
DrawningEngines.CountEngines = 6;
}
/// <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 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;
}
}
}
}