Обновлен класс прорисовки продвинутого объекта

This commit is contained in:
Никита Потапов 2023-09-25 23:20:48 +04:00
parent 31a85c3479
commit 78ba3286f0
3 changed files with 59 additions and 202 deletions

View File

@ -26,19 +26,19 @@ namespace ProjectStormtrooper
/// <summary>
/// Левая координата начала прорисовки
/// </summary>
private int _startPosX;
protected int _startPosX;
/// <summary>
/// Верхняя координата начала прорисовки
/// </summary>
private int _startPosY;
protected int _startPosY;
/// <summary>
/// Ширина прорисовки
/// </summary>
private readonly int _planeWidth = 110;
protected readonly int _planeWidth = 110;
/// <summary>
/// Высота прорисовки
/// </summary>
private readonly int _planeHeight = 110;
protected readonly int _planeHeight = 110;
/// <summary>
/// Конструктор
/// </summary>
@ -153,7 +153,7 @@ namespace ProjectStormtrooper
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public virtual void DrawTransport(Graphics g)
{
if (EntityPlane == null)
{

View File

@ -9,38 +9,10 @@ namespace ProjectStormtrooper
/// <summary>
/// Класс отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawingStormtrooper
public class DrawingStormtrooper : DrawingPlane
{
/// <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 _stormtrooperWidth = 110;
/// <summary>
/// Высота прорисовки
/// </summary>
private readonly int _stormtrooperHeight = 110;
/// <summary>
/// Инициализация свойств
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
@ -50,121 +22,45 @@ namespace ProjectStormtrooper
/// <param name="bombs">Признак наличия бомб</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - если объект успешно создан, false - проверка не пройдена,
/// т.к. нельзя создать объект в этих размерах</returns>
public bool Init(int speed, double weight, Color bodyColor,
public DrawingStormtrooper(int speed, double weight, Color bodyColor,
Color additionalColor, bool rockets, bool bombs,
int width, int height)
int width, int height) : base(speed, weight, bodyColor, width, height, 110, 110)
{
if (width < _stormtrooperWidth && height < _stormtrooperHeight)
if (EntityPlane != null)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
EntityStormtrooper = new EntityStormtrooper();
EntityStormtrooper.Init(speed, weight, bodyColor, additionalColor, rockets, bombs);
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (x < 0)
{
x = 0;
}
else if (x > _pictureWidth - _stormtrooperWidth)
{
x = _pictureWidth - _stormtrooperWidth;
}
_startPosX = x;
if (y < 0)
{
y = 0;
}
else if (y > _pictureHeight - _stormtrooperHeight)
{
y = _pictureHeight - _stormtrooperHeight;
}
_startPosY = y;
}
/// <summary>
/// Перемещение объекта
/// </summary>
/// <param name="direction">Направление перемещения</param>
public void MoveTransport(DirectionType direction)
{
if (EntityStormtrooper == null)
{
return;
}
switch (direction)
{
// Вверх
case DirectionType.Up:
if (_startPosY - EntityStormtrooper.Step >= 0)
{
_startPosY -= (int)EntityStormtrooper.Step;
}
break;
// Вниз
case DirectionType.Down:
if (_startPosY + _stormtrooperHeight + EntityStormtrooper.Step <= _pictureHeight)
{
_startPosY += (int)EntityStormtrooper.Step;
}
break;
// Влево
case DirectionType.Left:
if (_startPosX - EntityStormtrooper.Step >= 0)
{
_startPosX -= (int)EntityStormtrooper.Step;
}
break;
// Вправо
case DirectionType.Right:
if (_startPosX + _stormtrooperWidth + EntityStormtrooper.Step <= _pictureWidth)
{
_startPosX += (int)EntityStormtrooper.Step;
}
break;
EntityPlane = new EntityStormtrooper(speed, weight, bodyColor, additionalColor, rockets, bombs);
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
public override void DrawTransport(Graphics g)
{
if (EntityStormtrooper == null)
if (EntityPlane is not EntityStormtrooper stormtrooper)
{
return;
}
Pen penBlack = new Pen(Color.Black);
Brush brushBlack = new SolidBrush(Color.Black);
Brush brushBodyColor = new SolidBrush(EntityStormtrooper.BodyColor);
Brush brushAdditionalColor = new SolidBrush(EntityStormtrooper.AdditionalColor);
Brush brushBodyColor = new SolidBrush(stormtrooper.BodyColor);
Brush brushAdditionalColor = new SolidBrush(stormtrooper.AdditionalColor);
// Высота фюзеляжа
int bodyHeight = _stormtrooperHeight / 9;
int bodyHeight = _planeHeight / 9;
// Рисуем бомбы
if (EntityStormtrooper.Bombs)
if (stormtrooper.Bombs)
{
Point[] pointsBombTail = {
new Point(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8 + bodyHeight * 3 - 5,
_startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 - _stormtrooperHeight / 3 + bodyHeight / 2),
new Point(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8 + bodyHeight * 3 + 5,
_startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 - _stormtrooperHeight / 3 + bodyHeight / 2 - 5),
new Point(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8 + bodyHeight * 3 + 5,
_startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 - _stormtrooperHeight / 3 + bodyHeight / 2 + 5),
new Point(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8 + bodyHeight * 3 - 5,
_startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 - _stormtrooperHeight / 3 + bodyHeight / 2)
new Point(_startPosX + _planeWidth / 2 - _planeWidth / 8 + bodyHeight * 3 - 5,
_startPosY + _planeHeight / 2 - bodyHeight / 2 - _planeHeight / 3 + bodyHeight / 2),
new Point(_startPosX + _planeWidth / 2 - _planeWidth / 8 + bodyHeight * 3 + 5,
_startPosY + _planeHeight / 2 - bodyHeight / 2 - _planeHeight / 3 + bodyHeight / 2 - 5),
new Point(_startPosX + _planeWidth / 2 - _planeWidth / 8 + bodyHeight * 3 + 5,
_startPosY + _planeHeight / 2 - bodyHeight / 2 - _planeHeight / 3 + bodyHeight / 2 + 5),
new Point(_startPosX + _planeWidth / 2 - _planeWidth / 8 + bodyHeight * 3 - 5,
_startPosY + _planeHeight / 2 - bodyHeight / 2 - _planeHeight / 3 + bodyHeight / 2)
};
g.FillPolygon(brushAdditionalColor, pointsBombTail);
@ -173,7 +69,7 @@ namespace ProjectStormtrooper
for (int i = 0; i < pointsBombTail.Length; i++)
{
Point p = pointsBombTail[i];
p.Y = _startPosY + _stormtrooperHeight - (p.Y - _startPosY);
p.Y = _startPosY + _planeHeight - (p.Y - _startPosY);
pointsBombTail[i] = p;
}
@ -181,30 +77,30 @@ namespace ProjectStormtrooper
g.DrawPolygon(penBlack, pointsBombTail);
g.FillEllipse(brushAdditionalColor,
_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8,
_startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 - _stormtrooperHeight / 3,
_startPosX + _planeWidth / 2 - _planeWidth / 8,
_startPosY + _planeHeight / 2 - bodyHeight / 2 - _planeHeight / 3,
bodyHeight * 3,
bodyHeight);
g.DrawEllipse(penBlack,
_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8,
_startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 - _stormtrooperHeight / 3,
_startPosX + _planeWidth / 2 - _planeWidth / 8,
_startPosY + _planeHeight / 2 - bodyHeight / 2 - _planeHeight / 3,
bodyHeight * 3,
bodyHeight);
g.FillEllipse(brushAdditionalColor,
_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8,
_startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 + _stormtrooperHeight / 3,
_startPosX + _planeWidth / 2 - _planeWidth / 8,
_startPosY + _planeHeight / 2 - bodyHeight / 2 + _planeHeight / 3,
bodyHeight * 3,
bodyHeight);
g.DrawEllipse(penBlack,
_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 8,
_startPosY + _stormtrooperHeight / 2 - bodyHeight / 2 + _stormtrooperHeight / 3,
_startPosX + _planeWidth / 2 - _planeWidth / 8,
_startPosY + _planeHeight / 2 - bodyHeight / 2 + _planeHeight / 3,
bodyHeight * 3,
bodyHeight);
}
// Рисуем ракеты
if (EntityStormtrooper.Rockets)
if (stormtrooper.Rockets)
{
int rocketWidth = bodyHeight * 4;
int rocketHeight = bodyHeight / 2;
@ -212,21 +108,21 @@ namespace ProjectStormtrooper
Brush brushRed = new SolidBrush(Color.Red);
Point[] pointsRocketCockPit = {
new Point(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5 - rocketHeight,
_startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2 + rocketHeight / 2),
new Point(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5,
_startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2),
new Point(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5,
_startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2 + rocketHeight)
new Point(_startPosX + _planeWidth / 2 - _planeWidth / 5 - rocketHeight,
_startPosY + _planeHeight / 2 - bodyHeight - bodyHeight / 2 + rocketHeight / 2),
new Point(_startPosX + _planeWidth / 2 - _planeWidth / 5,
_startPosY + _planeHeight / 2 - bodyHeight - bodyHeight / 2),
new Point(_startPosX + _planeWidth / 2 - _planeWidth / 5,
_startPosY + _planeHeight / 2 - bodyHeight - bodyHeight / 2 + rocketHeight)
};
Point[] pointsRocketTail = {
new Point(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5 - rocketHeight + rocketWidth - 10,
_startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2 + rocketHeight / 2),
new Point(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5 + rocketWidth,
_startPosY + _stormtrooperHeight / 2 - bodyHeight * 2 + rocketHeight / 2),
new Point(_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5 + rocketWidth,
_startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2 + rocketHeight + bodyHeight / 2 - rocketHeight / 2)
new Point(_startPosX + _planeWidth / 2 - _planeWidth / 5 - rocketHeight + rocketWidth - 10,
_startPosY + _planeHeight / 2 - bodyHeight - bodyHeight / 2 + rocketHeight / 2),
new Point(_startPosX + _planeWidth / 2 - _planeWidth / 5 + rocketWidth,
_startPosY + _planeHeight / 2 - bodyHeight * 2 + rocketHeight / 2),
new Point(_startPosX + _planeWidth / 2 - _planeWidth / 5 + rocketWidth,
_startPosY + _planeHeight / 2 - bodyHeight - bodyHeight / 2 + rocketHeight + bodyHeight / 2 - rocketHeight / 2)
};
g.FillPolygon(brushRed, pointsRocketCockPit);
@ -238,14 +134,14 @@ namespace ProjectStormtrooper
for (int i = 0; i < pointsRocketCockPit.Length; i++)
{
Point p = pointsRocketCockPit[i];
p.Y = _startPosY + _stormtrooperHeight - (p.Y - _startPosY);
p.Y = _startPosY + _planeHeight - (p.Y - _startPosY);
pointsRocketCockPit[i] = p;
}
for (int i = 0; i < pointsRocketTail.Length; i++)
{
Point p = pointsRocketTail[i];
p.Y = _startPosY + _stormtrooperHeight - (p.Y - _startPosY);
p.Y = _startPosY + _planeHeight - (p.Y - _startPosY);
pointsRocketTail[i] = p;
}
@ -256,68 +152,29 @@ namespace ProjectStormtrooper
g.DrawPolygon(penBlack, pointsRocketTail);
g.FillRectangle(brushAdditionalColor,
_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5,
_startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2,
_startPosX + _planeWidth / 2 - _planeWidth / 5,
_startPosY + _planeHeight / 2 - bodyHeight - bodyHeight / 2,
rocketWidth,
rocketHeight);
g.DrawRectangle(penBlack,
_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5,
_startPosY + _stormtrooperHeight / 2 - bodyHeight - bodyHeight / 2,
_startPosX + _planeWidth / 2 - _planeWidth / 5,
_startPosY + _planeHeight / 2 - bodyHeight - bodyHeight / 2,
rocketWidth,
rocketHeight);
g.FillRectangle(brushAdditionalColor,
_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5,
_startPosY + _stormtrooperHeight / 2 + bodyHeight / 2 + bodyHeight / 2,
_startPosX + _planeWidth / 2 - _planeWidth / 5,
_startPosY + _planeHeight / 2 + bodyHeight / 2 + bodyHeight / 2,
rocketWidth,
rocketHeight);
g.DrawRectangle(penBlack,
_startPosX + _stormtrooperWidth / 2 - _stormtrooperWidth / 5,
_startPosY + _stormtrooperHeight / 2 + bodyHeight / 2 + bodyHeight / 2,
_startPosX + _planeWidth / 2 - _planeWidth / 5,
_startPosY + _planeHeight / 2 + bodyHeight / 2 + bodyHeight / 2,
rocketWidth,
rocketHeight);
}
// Рисуем нос
Point[] pointsCockPit = {
new Point(_startPosX, _startPosY + _stormtrooperHeight / 2),
new Point(_startPosX + _stormtrooperWidth / 8, _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2),
new Point(_startPosX + _stormtrooperWidth / 8, _startPosY + _stormtrooperHeight / 2 + bodyHeight / 2)
};
g.FillPolygon(brushBlack, pointsCockPit);
// Рисуем крылья
Point[] pointsWings = {
new Point(_startPosX + _stormtrooperWidth / 2, _startPosY),
new Point(_startPosX + _stormtrooperWidth / 2 + _stormtrooperWidth / 15, _startPosY),
new Point(_startPosX + _stormtrooperWidth / 2 + _stormtrooperWidth / 6, _startPosY + _stormtrooperHeight / 2),
new Point(_startPosX + _stormtrooperWidth / 2 + _stormtrooperWidth / 15, _startPosY + _stormtrooperHeight),
new Point(_startPosX + _stormtrooperWidth / 2 , _startPosY + _stormtrooperHeight)
};
g.FillPolygon(brushBodyColor, pointsWings);
g.DrawPolygon(penBlack, pointsWings);
// Рисуем хвостовое оперение
Point[] pointsTail = {
new Point(_startPosX + _stormtrooperWidth, _startPosY + _stormtrooperHeight / 2 - _stormtrooperHeight / 3),
new Point(_startPosX + _stormtrooperWidth - _stormtrooperWidth / 8, _startPosY + _stormtrooperHeight / 2 - _stormtrooperHeight / 8),
new Point(_startPosX + _stormtrooperWidth - _stormtrooperWidth / 8, _startPosY + _stormtrooperHeight / 2 + _stormtrooperHeight / 8),
new Point(_startPosX + _stormtrooperWidth, _startPosY + _stormtrooperHeight / 2 + _stormtrooperHeight / 3)
};
g.FillPolygon(brushBodyColor, pointsTail);
g.DrawPolygon(penBlack, pointsTail);
// Рисуем фюзеляж
g.FillRectangle(brushBodyColor, _startPosX + _stormtrooperWidth / 8, _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2, _stormtrooperWidth - _stormtrooperWidth / 8, bodyHeight);
g.DrawRectangle(penBlack, _startPosX + _stormtrooperWidth / 8, _startPosY + _stormtrooperHeight / 2 - bodyHeight / 2, _stormtrooperWidth - _stormtrooperWidth / 8, bodyHeight);
base.DrawTransport(g);
}
}
}

View File

@ -22,7 +22,7 @@ namespace ProjectStormtrooper
/// </summary>
public bool Bombs { get; private set; }
public EntityStormtrooper(int speed, double weight, Color bodyColor,
Color additionalColor, bool rockets, bool bombs)
Color additionalColor, bool rockets, bool bombs) : base(speed, weight, bodyColor)
{
AdditionalColor = additionalColor;
Rockets = rockets;