PIbd-22_Shabunov_O.A._AirBo.../AirBomber/BomberRenderer.cs

177 lines
6.2 KiB
C#
Raw Normal View History

2023-11-18 22:48:03 +04:00
namespace AirBomber
{
public class BomberRenderer
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public BomberEntity? EntityBomber { get; private set; }
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private readonly int _bomberWidth = 200;
private readonly int _bomberHeight = 200;
public bool Init(int Speed, double Weight, Color BodyColor, Color AdditionalColor, bool FuelTanks, bool Bombs, int Width, int Height)
{
if (Width < _bomberWidth || Height < _bomberHeight)
return false;
_pictureWidth = Width;
_pictureHeight = Height;
EntityBomber = new BomberEntity();
EntityBomber.Init(Speed, Weight, BodyColor, AdditionalColor, FuelTanks, Bombs);
return true;
}
public void SetPosition(int x, int y)
{
if (EntityBomber is null)
return;
if (x < 0)
x = 0;
else if (x + _bomberWidth > _pictureWidth)
x = _pictureWidth - _bomberWidth;
_startPosX = x;
if (y < 0)
y = 0;
else if (y + _bomberHeight > _pictureHeight)
y = _pictureHeight - _bomberHeight;
_startPosY = y;
}
public void MoveEntity(DirectionType Direction)
{
if (EntityBomber == null)
return;
switch (Direction)
{
case DirectionType.Up:
if (_startPosY - EntityBomber.Step > 0)
_startPosY -= (int)EntityBomber.Step;
break;
case DirectionType.Down:
if (_startPosY + _bomberHeight + EntityBomber.Step <= _pictureHeight)
_startPosY += (int)EntityBomber.Step;
break;
case DirectionType.Left:
if (_startPosX - EntityBomber.Step > 0)
_startPosX -= (int)EntityBomber.Step;
break;
case DirectionType.Right:
if (_startPosX + _bomberWidth + EntityBomber.Step <= _pictureWidth)
_startPosX += (int)EntityBomber.Step;
break;
}
}
public void DrawEntity(Graphics g)
{
if (EntityBomber == null)
return;
Pen pen = new Pen(EntityBomber.BodyColor);
Brush Brush = new SolidBrush(EntityBomber.BodyColor);
Brush AdditionalBrush = new SolidBrush(EntityBomber.AdditionalColor);
/** Отрисовка основной части */
Point[] LeftWing = {
new Point(_startPosX + 90, _startPosY),
new Point(_startPosX + 100, _startPosY),
new Point(_startPosX + 108, _startPosY + 85),
new Point(_startPosX + 90, _startPosY + 85),
};
g.DrawPolygon(pen, LeftWing);
Point[] RightWing = {
new Point(_startPosX + 90, _startPosY + 200),
new Point(_startPosX + 100, _startPosY + 200),
new Point(_startPosX + 108, _startPosY + 115),
new Point(_startPosX + 90, _startPosY + 115),
};
g.DrawPolygon(pen, RightWing);
Point[] Body = {
new Point(_startPosX + 35, _startPosY + 85),
new Point(_startPosX + 200, _startPosY + 85),
new Point(_startPosX + 200, _startPosY + 115),
new Point(_startPosX + 35, _startPosY + 115),
};
g.DrawPolygon(pen, Body);
Point[] Nose = {
new Point(_startPosX, _startPosY + 100),
new Point(_startPosX + 35, _startPosY + 85),
new Point(_startPosX + 35, _startPosY + 115),
};
g.FillPolygon(Brush, Nose);
Point[] BackLeftWing = {
new Point(_startPosX + 170, _startPosY + 70),
new Point(_startPosX + 200, _startPosY + 40),
new Point(_startPosX + 200, _startPosY + 85),
new Point(_startPosX + 170, _startPosY + 85),
};
g.DrawPolygon(pen, BackLeftWing);
Point[] BackRightWing = {
new Point(_startPosX + 170, _startPosY + 130),
new Point(_startPosX + 200, _startPosY + 160),
new Point(_startPosX + 200, _startPosY + 115),
new Point(_startPosX + 170, _startPosY + 115),
};
g.DrawPolygon(pen, BackRightWing);
/** Отрисовка дополнительных элементов */
if (EntityBomber.FuelTanks)
{
Point[] LeftGasTank = {
new Point(_startPosX + 50, _startPosY + 85),
new Point(_startPosX + 75, _startPosY + 85),
new Point(_startPosX + 75, _startPosY + 70),
new Point(_startPosX + 50, _startPosY + 70),
};
g.FillPolygon(AdditionalBrush, LeftGasTank);
Point[] RightGasTank = {
new Point(_startPosX + 50, _startPosY + 115),
new Point(_startPosX + 75, _startPosY + 115),
new Point(_startPosX + 75, _startPosY + 130),
new Point(_startPosX + 50, _startPosY + 130),
};
g.FillPolygon(AdditionalBrush, RightGasTank);
}
if (EntityBomber.Bombs)
{
Point LeftBombStartXY = new Point(_startPosX + 110, _startPosY + 115);
Size LeftBombSize = new Size(50, 25);
g.FillEllipse(AdditionalBrush, new Rectangle(LeftBombStartXY, LeftBombSize));
Point RightBombStartXY = new Point(_startPosX + 110, _startPosY + 60);
Size RightBombSize = new Size(50, 25);
g.FillEllipse(AdditionalBrush, new Rectangle(RightBombStartXY, RightBombSize));
}
}
}
}