188 lines
7.1 KiB
C#
188 lines
7.1 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace AirFighter
|
||
{
|
||
/// <summary>
|
||
/// Класс отвечающий за прорисовку и перемещение объекта-сущности
|
||
/// </summary>
|
||
internal class DrawningAirFighter
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityAirFighter AirFighter { 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 _airFighterWidth = 80;
|
||
/// <summary>
|
||
/// Высота отрисовки самолета
|
||
/// </summary>
|
||
private readonly int _airFighterHeight = 70;
|
||
/// <summary>
|
||
/// Инициализация свойств
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес самолета</param>
|
||
/// <param name="bodyColor">Цвет кузова</param>
|
||
public void Init(int speed, float weight, Color bodyColor)
|
||
{
|
||
AirFighter = new EntityAirFighter();
|
||
AirFighter.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)
|
||
{
|
||
if(width < _airFighterWidth || height < _airFighterHeight)
|
||
{
|
||
_pictureWidth = null;
|
||
_pictureHeight = null;
|
||
return;
|
||
}
|
||
if(x < 0 || x + _airFighterWidth > width)
|
||
{
|
||
x = width - _airFighterWidth;
|
||
}
|
||
if(y < 0 || y + _airFighterHeight > height)
|
||
{
|
||
y = height - _airFighterHeight;
|
||
}
|
||
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
}
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
public void MoveTransport(Direction direction)
|
||
{
|
||
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
switch (direction)
|
||
{
|
||
// вправо
|
||
case Direction.Right:
|
||
if (_startPosX + _airFighterWidth + AirFighter.Step < _pictureWidth)
|
||
{
|
||
_startPosX += AirFighter.Step;
|
||
}
|
||
break;
|
||
//влево
|
||
case Direction.Left:
|
||
if (_startPosX - AirFighter.Step >= 0)
|
||
{
|
||
_startPosX -= AirFighter.Step;
|
||
}
|
||
break;
|
||
//вверх
|
||
case Direction.Up:
|
||
if (_startPosY - AirFighter.Step >= 0)
|
||
{
|
||
_startPosY -= AirFighter.Step;
|
||
}
|
||
break;
|
||
//вниз
|
||
case Direction.Down:
|
||
if (_startPosY + _airFighterHeight + AirFighter.Step < _pictureHeight)
|
||
{
|
||
_startPosY += AirFighter.Step;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Отрисовка самолета
|
||
/// </summary>
|
||
public void DrawAirFighter(Graphics g)
|
||
{
|
||
if (_startPosX < 0 || _startPosY < 0 || !_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
Pen pen = new(Color.Black);
|
||
PointF[] pointAirFighter =
|
||
{
|
||
new PointF(_startPosX + 80, _startPosY + 35),
|
||
new PointF(_startPosX + 70, _startPosY + 30),
|
||
new PointF(_startPosX + 30, _startPosY + 30),
|
||
new PointF(_startPosX + 35, _startPosY),
|
||
new PointF(_startPosX + 40, _startPosY),
|
||
new PointF(_startPosX + 40, _startPosY + 30),
|
||
new PointF(_startPosX, _startPosY + 30),
|
||
new PointF(_startPosX, _startPosY + 15),
|
||
new PointF(_startPosX + 10, _startPosY + 25),
|
||
new PointF(_startPosX + 10, _startPosY + 30),
|
||
new PointF(_startPosX, _startPosY + 30),
|
||
new PointF(_startPosX, _startPosY + 55),
|
||
new PointF(_startPosX + 10, _startPosY + 45),
|
||
new PointF(_startPosX + 10, _startPosY + 40),
|
||
new PointF(_startPosX, _startPosY + 40),
|
||
new PointF(_startPosX + 40, _startPosY + 40),
|
||
new PointF(_startPosX + 40, _startPosY + 70),
|
||
new PointF(_startPosX + 35, _startPosY + 70),
|
||
new PointF(_startPosX + 30, _startPosY + 40),
|
||
new PointF(_startPosX + 70, _startPosY + 40)
|
||
};
|
||
SolidBrush brushAirFighter = new SolidBrush(AirFighter.BodyColor);
|
||
g.FillPolygon(brushAirFighter, pointAirFighter);
|
||
g.DrawPolygon(pen, pointAirFighter);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Смена границ формы отрисовки
|
||
/// </summary>
|
||
/// <param name="width">Ширина картинки</param>
|
||
/// <param name="height">Высота картинки</param>
|
||
public void ChangeBorders(int width, int height)
|
||
{
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
if (_pictureWidth <= _airFighterWidth || _pictureHeight <= _airFighterHeight)
|
||
{
|
||
_pictureWidth = null;
|
||
_pictureHeight = null;
|
||
return;
|
||
}
|
||
if (_startPosX + _airFighterWidth > _pictureWidth.Value)
|
||
{
|
||
_startPosX = _pictureWidth.Value - _airFighterWidth;
|
||
}
|
||
if (_startPosY + _airFighterHeight > _pictureHeight.Value)
|
||
{
|
||
_startPosY = _pictureHeight.Value - _airFighterHeight;
|
||
}
|
||
}
|
||
}
|
||
}
|