240 lines
8.6 KiB
C#
240 lines
8.6 KiB
C#
using ProjectStormtrooper.Entities;
|
||
|
||
namespace ProjectStormtrooper.Drawnings;
|
||
|
||
public class DrawingStormtrooperBase
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityStormtrooperBase? EntityStormtrooperBase { get; protected set; }
|
||
/// <summary>
|
||
/// Ширина окна
|
||
/// </summary>
|
||
private int? _pictureWidth;
|
||
/// <summary>
|
||
/// Высота окна
|
||
/// </summary>
|
||
|
||
private int? _pictureHeight;
|
||
/// <summary>
|
||
/// Левая координата начала прорисовки
|
||
/// </summary>
|
||
|
||
protected int? _startPosX;
|
||
/// <summary>
|
||
/// Верхняя координата начала прорисовки
|
||
/// </summary>
|
||
|
||
protected int? _startPosY;
|
||
/// <summary>
|
||
/// Ширина прорисовки
|
||
/// </summary>
|
||
|
||
private readonly int _drawningStormtrooperWidth = 140;
|
||
/// <summary>
|
||
/// Высота прорисовки
|
||
/// </summary>
|
||
|
||
private readonly int _drawningStormtooperHeight = 140;
|
||
/// <summary>
|
||
/// Пустой конструктор
|
||
/// </summary>
|
||
private DrawingStormtrooperBase()
|
||
{
|
||
_pictureHeight = null;
|
||
_pictureWidth = null;
|
||
_startPosX = null;
|
||
_startPosY = null;
|
||
}
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="speed">скорость</param>
|
||
/// <param name="weight">вес</param>
|
||
/// <param name="bodyColor">основной цвет</param>
|
||
|
||
|
||
public DrawingStormtrooperBase(int speed, double weight, Color bodyColor):this()
|
||
{
|
||
EntityStormtrooperBase = new EntityStormtrooperBase(speed, weight, bodyColor);
|
||
|
||
}
|
||
/// <summary>
|
||
/// Конструктор для наследников
|
||
/// </summary>
|
||
/// <param name="drawningStormtrooperWidth"> Ширина прорисовки</param>
|
||
/// <param name="drawningStormtooperHeight">Высота прорисовки</param>
|
||
|
||
protected DrawingStormtrooperBase(int drawningStormtrooperWidth, int drawningStormtooperHeight) : this()
|
||
{
|
||
_drawningStormtrooperWidth = drawningStormtrooperWidth;
|
||
_drawningStormtooperHeight = drawningStormtooperHeight;
|
||
|
||
}
|
||
/// <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 (EntityStormtrooperBase == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return false;
|
||
}
|
||
switch (direction)
|
||
{
|
||
//влево
|
||
case DirectionType.Left:
|
||
if (_startPosX.Value - EntityStormtrooperBase.Step > 0)
|
||
{
|
||
_startPosX -= (int)EntityStormtrooperBase.Step;
|
||
}
|
||
return true;
|
||
//Вверх
|
||
case DirectionType.Up:
|
||
if (_startPosY.Value - EntityStormtrooperBase.Step > 0)
|
||
{
|
||
_startPosY -= (int)EntityStormtrooperBase.Step;
|
||
}
|
||
return true;
|
||
|
||
//Вправо
|
||
case DirectionType.Right:
|
||
if (_startPosX.Value + EntityStormtrooperBase.Step + _drawningStormtrooperWidth < _pictureWidth)
|
||
{
|
||
_startPosX += (int)EntityStormtrooperBase.Step;
|
||
}
|
||
return true;
|
||
//Вниз
|
||
case DirectionType.Down:
|
||
if (_startPosY.Value + EntityStormtrooperBase.Step + _drawningStormtooperHeight < _pictureHeight)
|
||
{
|
||
_startPosY += (int)EntityStormtrooperBase.Step;
|
||
}
|
||
return true;
|
||
default:
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="q"></param>
|
||
public virtual void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityStormtrooperBase == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
|
||
Pen pen = new(Color.Black);
|
||
Brush bodyColorBrush = new SolidBrush(EntityStormtrooperBase.BodyColor);
|
||
|
||
|
||
//нос штурмовика
|
||
Brush brBlack = new SolidBrush(Color.Black);
|
||
|
||
Point[] Nose = new Point[3];
|
||
Nose[0].X = _startPosX.Value + 20; Nose[0].Y = _startPosY.Value + 85;
|
||
Nose[1].X = _startPosX.Value + 20; Nose[1].Y = _startPosY.Value + 65;
|
||
Nose[2].X = _startPosX.Value; Nose[2].Y = _startPosY.Value + 75;
|
||
g.FillPolygon(brBlack, Nose);
|
||
g.DrawPolygon(pen, Nose);
|
||
//Заднии крылья штурмовика
|
||
|
||
Point[] pflybtwings = new Point[6];
|
||
pflybtwings[0].X = _startPosX.Value + 120; pflybtwings[0].Y = _startPosY.Value + 65;
|
||
pflybtwings[1].X = _startPosX.Value + 120; pflybtwings[1].Y = _startPosY.Value + 55;
|
||
pflybtwings[2].X = _startPosX.Value + 140; pflybtwings[2].Y = _startPosY.Value + 35;
|
||
pflybtwings[3].X = _startPosX.Value + 140; pflybtwings[3].Y = _startPosY.Value + 115;
|
||
pflybtwings[4].X = _startPosX.Value + 120; pflybtwings[4].Y = _startPosY.Value + 95;
|
||
pflybtwings[5].X = _startPosX.Value + 120; pflybtwings[5].Y = _startPosY.Value + 85;
|
||
g.FillPolygon(bodyColorBrush, pflybtwings);
|
||
g.DrawPolygon(pen, pflybtwings);
|
||
//Тело штурмовика
|
||
|
||
g.FillRectangle(bodyColorBrush, _startPosX.Value + 20, _startPosY.Value + 65, 120, 20);
|
||
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 65, 120, 20);
|
||
//Крылья штурмовика
|
||
|
||
Point[] frontwings = new Point[4];
|
||
frontwings[0].X = _startPosX.Value + 60; frontwings[0].Y = _startPosY.Value + 65;
|
||
frontwings[1].X = _startPosX.Value + 60; frontwings[1].Y = _startPosY.Value + 5;
|
||
frontwings[2].X = _startPosX.Value + 70; frontwings[2].Y = _startPosY.Value + 5;
|
||
frontwings[3].X = _startPosX.Value + 80; frontwings[3].Y = _startPosY.Value + 65;
|
||
g.FillPolygon(bodyColorBrush, frontwings);
|
||
g.DrawPolygon(pen, frontwings);
|
||
|
||
Point[] frontwings2 = new Point[4];
|
||
frontwings2[0].X = _startPosX.Value + 60; frontwings2[0].Y = _startPosY.Value + 85;
|
||
frontwings2[1].X = _startPosX.Value + 60; frontwings2[1].Y = _startPosY.Value + 145;
|
||
frontwings2[2].X = _startPosX.Value + 70; frontwings2[2].Y = _startPosY.Value + 145;
|
||
frontwings2[3].X = _startPosX.Value + 80; frontwings2[3].Y = _startPosY.Value + 85;
|
||
g.FillPolygon(bodyColorBrush, frontwings2);
|
||
g.DrawPolygon(pen, frontwings2);
|
||
|
||
|
||
|
||
}
|
||
}
|