246 lines
9.2 KiB
C#
246 lines
9.2 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);
|
||
|
||
//нос штурмовика
|
||
Brush brBlack = new SolidBrush(Color.Black);
|
||
|
||
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(brBlack, Nose);
|
||
g.DrawPolygon(pen, Nose);
|
||
//Заднии крылья штурмовика
|
||
|
||
Point[] pflybtwings = new Point[6];
|
||
pflybtwings[0].X = _startPosX.Value + 120; pflybtwings[0].Y = _startPosY.Value + 60;
|
||
pflybtwings[1].X = _startPosX.Value + 120; pflybtwings[1].Y = _startPosY.Value + 50;
|
||
pflybtwings[2].X = _startPosX.Value + 140; pflybtwings[2].Y = _startPosY.Value + 30;
|
||
pflybtwings[3].X = _startPosX.Value + 140; pflybtwings[3].Y = _startPosY.Value + 110;
|
||
pflybtwings[4].X = _startPosX.Value + 120; pflybtwings[4].Y = _startPosY.Value + 90;
|
||
pflybtwings[5].X = _startPosX.Value + 120; pflybtwings[5].Y = _startPosY.Value + 80;
|
||
g.FillPolygon(bodyColorBrush, pflybtwings);
|
||
g.DrawPolygon(pen, pflybtwings);
|
||
//Тело штурмовика
|
||
g.FillRectangle(bodyColorBrush, _startPosX.Value + 20, _startPosY.Value + 60, 120, 20);
|
||
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 60, 120, 20);
|
||
|
||
|
||
//Крылья штурмовика
|
||
|
||
|
||
Point[] frontwings = new Point[4];
|
||
frontwings[0].X = _startPosX.Value + 60; frontwings[0].Y = _startPosY.Value + 60;
|
||
frontwings[1].X = _startPosX.Value + 60; frontwings[1].Y = _startPosY.Value ;
|
||
frontwings[2].X = _startPosX.Value + 70; frontwings[2].Y = _startPosY.Value ;
|
||
frontwings[3].X = _startPosX.Value + 80; frontwings[3].Y = _startPosY.Value + 60;
|
||
g.FillPolygon(bodyColorBrush, frontwings);
|
||
g.DrawPolygon(pen, frontwings);
|
||
|
||
Point[] frontwings2 = new Point[4];
|
||
frontwings2[0].X = _startPosX.Value + 60; frontwings2[0].Y = _startPosY.Value + 80;
|
||
frontwings2[1].X = _startPosX.Value + 60; frontwings2[1].Y = _startPosY.Value+140;
|
||
frontwings2[2].X = _startPosX.Value + 70; frontwings2[2].Y = _startPosY.Value+140;
|
||
frontwings2[3].X = _startPosX.Value + 80; frontwings2[3].Y = _startPosY.Value + 80;
|
||
g.FillPolygon(bodyColorBrush, frontwings2);
|
||
g.DrawPolygon(pen, frontwings2);
|
||
|
||
|
||
//Ракеты штурмовика
|
||
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);
|
||
g.DrawRectangle(pen, _startPosX.Value + 45, _startPosY.Value + 20, 15, 5);
|
||
g.DrawRectangle(pen, _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);
|
||
g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 40, 10, 10);
|
||
g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 90, 10, 10);
|
||
|
||
}
|
||
}
|
||
|
||
|
||
}
|