222 lines
8.3 KiB
C#
222 lines
8.3 KiB
C#
|
namespace ProjectStormtrooper;
|
|||
|
/// <summary>
|
|||
|
/// Класс отвечающий за прорисовку и перемещение объекта-сущности
|
|||
|
/// </summary>
|
|||
|
public class DrawingStormtrooper
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Класс-сущность
|
|||
|
/// </summary>
|
|||
|
public EntityStormtrooper? EntityStormtrooper { get; private set; }
|
|||
|
/// <summary>
|
|||
|
/// Ширина окна
|
|||
|
/// </summary>
|
|||
|
private int? _pictureWidth;
|
|||
|
/// <summary>
|
|||
|
/// Высота окна
|
|||
|
/// </summary>
|
|||
|
|
|||
|
private int? _pictureHeight;
|
|||
|
/// <summary>
|
|||
|
/// Левая координата начала прорисовки
|
|||
|
/// </summary>
|
|||
|
|
|||
|
private int? _startPosX;
|
|||
|
/// <summary>
|
|||
|
/// Верхняя координата начала прорисовки
|
|||
|
/// </summary>
|
|||
|
|
|||
|
private int? _startPosY;
|
|||
|
/// <summary>
|
|||
|
/// Ширина прорисовки
|
|||
|
/// </summary>
|
|||
|
|
|||
|
private readonly int _drawningStormtrooperWidth = 140;
|
|||
|
/// <summary>
|
|||
|
/// Высота прорисовки
|
|||
|
/// </summary>
|
|||
|
|
|||
|
private readonly int _drawningStormtooperHeight = 140;
|
|||
|
/// <summary>
|
|||
|
/// Инициализация свойств
|
|||
|
/// </summary>
|
|||
|
/// <param name="speed"></param>
|
|||
|
/// <param name="weight"></param>
|
|||
|
/// <param name="bodyColor"></param>
|
|||
|
/// <param name="additionalColor"></param>
|
|||
|
/// <param name="rockets"></param>
|
|||
|
/// <param name="bombs"></param>
|
|||
|
|
|||
|
public void Init(int speed,double weight, Color bodyColor,Color additionalColor, bool rockets, bool bombs)
|
|||
|
{
|
|||
|
EntityStormtrooper = new EntityStormtrooper();
|
|||
|
EntityStormtrooper.Init(speed, weight, bodyColor, additionalColor, rockets, bombs);
|
|||
|
_pictureHeight = null;
|
|||
|
_pictureWidth = null;
|
|||
|
_startPosX = null;
|
|||
|
_startPosY = null;
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// Установка границ поля
|
|||
|
/// </summary>
|
|||
|
/// <param name="width">Ширина поля</param>
|
|||
|
/// <param name="height">Высота поля</param>
|
|||
|
/// <returns>true - граница задана, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
|
|||
|
public bool SetPictureSize(int width,int height)
|
|||
|
{
|
|||
|
if (width >= _drawningStormtrooperWidth || height >= _drawningStormtooperHeight)
|
|||
|
{
|
|||
|
_pictureWidth = width;
|
|||
|
_pictureHeight = height;
|
|||
|
if(_startPosX!=null && _startPosY != null)
|
|||
|
{
|
|||
|
SetPosition(_startPosX.Value, _startPosY.Value);
|
|||
|
}
|
|||
|
return true;
|
|||
|
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Установка позиции
|
|||
|
/// </summary>
|
|||
|
/// <param name="x">Координата Х</param>
|
|||
|
/// <param name="y">Координата У</param>
|
|||
|
public void SetPosition(int x, int y)
|
|||
|
{
|
|||
|
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
|
|||
|
// то надо изменить координаты, чтобы он оставался в этих границах
|
|||
|
|
|||
|
|
|||
|
if (x < 0)
|
|||
|
{
|
|||
|
x = 0;
|
|||
|
}
|
|||
|
else if (x > _pictureWidth - _drawningStormtrooperWidth)
|
|||
|
{
|
|||
|
x = _pictureWidth.Value - _drawningStormtrooperWidth;
|
|||
|
}
|
|||
|
if (y < 0)
|
|||
|
{
|
|||
|
y = 0;
|
|||
|
}
|
|||
|
else if (y > _pictureHeight - _drawningStormtooperHeight)
|
|||
|
{
|
|||
|
y = _pictureHeight.Value - _drawningStormtooperHeight;
|
|||
|
}
|
|||
|
|
|||
|
_startPosX = x;
|
|||
|
_startPosY = y;
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// Изменение направления перемещения
|
|||
|
/// </summary>
|
|||
|
/// <param name="direction">направление</param>
|
|||
|
/// <returns>true - перемещение выполнено, false - перемещение невозможно</returns>
|
|||
|
public bool MoveTransport(DirectionType direction)
|
|||
|
{
|
|||
|
if (EntityStormtrooper == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
switch (direction)
|
|||
|
{
|
|||
|
//влево
|
|||
|
case DirectionType.Left:
|
|||
|
if (_startPosX.Value - EntityStormtrooper.Step > 0)
|
|||
|
{
|
|||
|
_startPosX -= (int)EntityStormtrooper.Step;
|
|||
|
}
|
|||
|
return true;
|
|||
|
//Вверх
|
|||
|
case DirectionType.Up:
|
|||
|
if (_startPosY.Value - EntityStormtrooper.Step > 0)
|
|||
|
{
|
|||
|
_startPosY -= (int)EntityStormtrooper.Step;
|
|||
|
}
|
|||
|
return true;
|
|||
|
|
|||
|
//Вправо
|
|||
|
case DirectionType.Right:
|
|||
|
if (_startPosX.Value + EntityStormtrooper.Step+_drawningStormtrooperWidth < _pictureWidth)
|
|||
|
{
|
|||
|
_startPosX += (int)EntityStormtrooper.Step;
|
|||
|
}
|
|||
|
return true;
|
|||
|
//Вниз
|
|||
|
case DirectionType.Down:
|
|||
|
if (_startPosY.Value + EntityStormtrooper.Step + _drawningStormtooperHeight < _pictureHeight)
|
|||
|
{
|
|||
|
_startPosY += (int)EntityStormtrooper.Step;
|
|||
|
}
|
|||
|
return true;
|
|||
|
default:
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Прорисовка объекта
|
|||
|
/// </summary>
|
|||
|
/// <param name="q"></param>
|
|||
|
public void DrawTransport(Graphics g)
|
|||
|
{
|
|||
|
if (EntityStormtrooper == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
Pen pen = new(Color.Black);
|
|||
|
Brush bodyColorBrush = new SolidBrush(EntityStormtrooper.BodyColor);
|
|||
|
Brush additionalBrush = new SolidBrush(EntityStormtrooper.AdditionalColor);
|
|||
|
|
|||
|
//нос штурмовика
|
|||
|
|
|||
|
Point[] Nose = new Point[3];
|
|||
|
Nose[0].X = _startPosX.Value + 20; Nose[0].Y = _startPosY.Value + 80;
|
|||
|
Nose[1].X = _startPosX.Value + 20; Nose[1].Y = _startPosY.Value + 60;
|
|||
|
Nose[2].X = _startPosX.Value; Nose[2].Y = _startPosY.Value + 70;
|
|||
|
g.FillPolygon(bodyColorBrush, Nose);
|
|||
|
|
|||
|
//Тело штурмовика
|
|||
|
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 60, 120, 20);
|
|||
|
|
|||
|
//Крылья штурмовика
|
|||
|
|
|||
|
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 60, _startPosX.Value + 60, _startPosY.Value);
|
|||
|
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value, _startPosX.Value + 70, _startPosY.Value);
|
|||
|
g.DrawLine(pen, _startPosX.Value + 70, _startPosY.Value, _startPosX.Value + 80, _startPosY.Value + 60);
|
|||
|
|
|||
|
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 80, _startPosX.Value + 60, _startPosY.Value+140);
|
|||
|
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value+140, _startPosX.Value + 70, _startPosY.Value+140);
|
|||
|
g.DrawLine(pen, _startPosX.Value + 70, _startPosY.Value+140, _startPosX.Value + 80, _startPosY.Value + 80);
|
|||
|
|
|||
|
//Заднии крылья штурмовика
|
|||
|
g.DrawLine(pen, _startPosX.Value + 120, _startPosY.Value + 60, _startPosX.Value + 120, _startPosY.Value + 50);
|
|||
|
g.DrawLine(pen, _startPosX.Value + 120, _startPosY.Value + 50, _startPosX.Value + 140, _startPosY.Value + 30);
|
|||
|
g.DrawLine(pen, _startPosX.Value + 140, _startPosY.Value + 30, _startPosX.Value + 140, _startPosY.Value + 110);
|
|||
|
g.DrawLine(pen, _startPosX.Value + 140, _startPosY.Value + 110, _startPosX.Value + 120, _startPosY.Value + 90);
|
|||
|
g.DrawLine(pen, _startPosX.Value + 120, _startPosY.Value + 90, _startPosX.Value + 120, _startPosY.Value + 80);
|
|||
|
//Ракеты штурмовика
|
|||
|
if (EntityStormtrooper.Rockets)
|
|||
|
{
|
|||
|
g.FillRectangle(additionalBrush, _startPosX.Value + 45, _startPosY.Value + 20, 15, 5);
|
|||
|
g.FillRectangle(additionalBrush, _startPosX.Value + 45, _startPosY.Value + 110, 15, 5);
|
|||
|
|
|||
|
}
|
|||
|
//Бомбы бомбардировщика
|
|||
|
if (EntityStormtrooper.Bombs)
|
|||
|
{
|
|||
|
g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 40, 10, 10);
|
|||
|
g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 90, 10, 10);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|