195 lines
7.7 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; }
/// <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 DrawningAirplane(int speed, float weight, Color bodyColor)
{
Airplane = new EntityAirplane(speed, weight, bodyColor);
}
/// <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);
}
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;
}
}
}
}