183 lines
7.3 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 AirplaneWithRadar
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
internal class DrawningAirplane
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityAirplane Airplane { private set; get; }
/// <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 = 100;
/// <summary>
/// Высота отрисовки самолёта
/// </summary>
private readonly int _airplaneHeight = 10;
/// <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);
}
/// <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 (x < 0 || y < 0 || width <= _airplaneWidth || height <= _airplaneHeight || x + _airplaneWidth > width || y + _airplaneHeight > height)
{
_pictureHeight = null;
_pictureWidth = 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;
}
Pen pen = new(Color.Black);
Brush brush = new SolidBrush(Airplane.BodyColor);
//корпус
g.DrawRectangle(pen, _startPosX, _startPosY + _airplaneHeight-7,
_airplaneWidth-5, _airplaneHeight);
//нос
PointF[] point = new PointF[3];
point[0] = new PointF(_startPosX + _airplaneWidth-5, _startPosY + _airplaneHeight / 3);
point[1] = new PointF(_startPosX + _airplaneWidth+10 , _startPosY + _airplaneHeight-3);
point[2] = new PointF(_startPosX + _airplaneWidth-5, _startPosY + _airplaneHeight + (_airplaneHeight / 3));
g.FillPolygon(brush, point);
//хвост
point = new PointF[3];
point[0] = new PointF(_startPosX, _startPosY-10);
point[1] = new PointF(_startPosX, _startPosY + _airplaneHeight / 3);
point[2] = new PointF(_startPosX + 15, _startPosY + _airplaneHeight / 3);
g.FillPolygon(brush, point);
//крылья
g.DrawLine(pen, _startPosX + 20, _startPosY + _airplaneHeight - 3, _startPosX + 60, _startPosY + _airplaneHeight - 3);
g.DrawLine(pen, _startPosX + 20, _startPosY + _airplaneHeight - 2, _startPosX + 60, _startPosY + _airplaneHeight - 2);
g.DrawLine(pen, _startPosX-1, _startPosY + _airplaneHeight / 3, _startPosX + 15, _startPosY + _airplaneHeight / 3);
g.DrawLine(pen, _startPosX-1, _startPosY + _airplaneHeight / 3 + 1, _startPosX + 15, _startPosY + _airplaneHeight / 3 + 1);
g.DrawLine(pen, _startPosX-1, _startPosY + _airplaneHeight / 3 + 2, _startPosX + 15, _startPosY + _airplaneHeight / 3 + 2);
//колёса
g.DrawRectangle(pen, _startPosX + 20, _startPosY + _airplaneHeight + (_airplaneHeight / 3), 2, 2);
g.DrawRectangle(pen, _startPosX + 24, _startPosY + _airplaneHeight + (_airplaneHeight / 3), 2, 2);
g.DrawRectangle(pen, _startPosX + _airplaneWidth-20, _startPosY + _airplaneHeight + (_airplaneHeight / 3), 2, 2);
}
/// <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;
}
}
}
}