PIbd-22_Shabunov_O.A._AirBo.../AirBomber/Rendering/BomberRendererBase.cs

172 lines
5.6 KiB
C#
Raw Normal View History

using AirBomber.Entities;
using AirBomber.MovementStrategy;
namespace AirBomber.Rendering
2023-11-18 22:48:03 +04:00
{
public class BomberRendererBase
2023-11-18 22:48:03 +04:00
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public BomberEntityBase? EntityBomber { get; protected set; }
2023-11-18 22:48:03 +04:00
private int _pictureWidth;
private int _pictureHeight;
protected int _startPosX;
protected int _startPosY;
protected readonly int _bomberWidth = 200;
protected readonly int _bomberHeight = 200;
2023-11-18 22:48:03 +04:00
public int PosX => _startPosX;
public int PosY => _startPosY;
2023-11-18 22:48:03 +04:00
public int EntityWidth => _bomberWidth;
public int EntityHeight => _bomberHeight;
public BomberRendererBase(int Speed, double Weight, Color BodyColor, int Width, int Height)
2023-11-18 22:48:03 +04:00
{
if (Width < _bomberWidth || Height < _bomberHeight)
return;
2023-11-18 22:48:03 +04:00
_pictureWidth = Width;
_pictureHeight = Height;
EntityBomber = new BomberEntityBase(Speed, Weight, BodyColor);
}
public BomberRendererBase(int Speed, double Weight, Color BodyColor, int Width, int Height, int EntityWidth, int EntityHeight)
{
if (Width < _bomberWidth || Height < _bomberHeight)
return;
_pictureWidth = Width;
_pictureHeight = Height;
2023-11-18 22:48:03 +04:00
_bomberWidth = EntityWidth;
_bomberHeight = EntityHeight;
EntityBomber = new BomberEntityBase(Speed, Weight, BodyColor);
2023-11-18 22:48:03 +04:00
}
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;
2023-11-18 22:48:03 +04:00
_startPosX = x;
if (y < 0)
y = 0;
else if (y + _bomberHeight > _pictureHeight)
y = _pictureHeight - _bomberHeight;
_startPosY = y;
}
public bool CanMove(DirectionType direction)
2023-11-18 22:48:03 +04:00
{
if (EntityBomber is null)
return false;
2023-11-18 22:48:03 +04:00
return direction switch
2023-11-18 22:48:03 +04:00
{
DirectionType.Left => _startPosX - EntityBomber.Step > 0,
DirectionType.Up => _startPosY - EntityBomber.Step > 0,
DirectionType.Right => _startPosX + _bomberWidth + EntityBomber.Step <= _pictureWidth,
DirectionType.Down => _startPosY + _bomberHeight + EntityBomber.Step <= _pictureHeight,
2023-11-18 22:48:03 +04:00
_ => false,
};
}
2023-11-18 22:48:03 +04:00
public void MoveEntity(DirectionType Direction)
{
if (EntityBomber == null || !CanMove(Direction))
return;
2023-11-18 22:48:03 +04:00
switch (Direction)
{
2023-11-18 22:48:03 +04:00
case DirectionType.Left:
_startPosX -= (int)EntityBomber.Step;
break;
2023-11-18 22:48:03 +04:00
case DirectionType.Up:
_startPosY -= (int)EntityBomber.Step;
2023-11-18 22:48:03 +04:00
break;
case DirectionType.Right:
_startPosX += (int)EntityBomber.Step;
break;
2023-11-18 22:48:03 +04:00
case DirectionType.Down:
_startPosY += (int)EntityBomber.Step;
2023-11-18 22:48:03 +04:00
break;
}
}
public virtual void DrawEntity(Graphics g)
2023-11-18 22:48:03 +04:00
{
if (EntityBomber == null)
return;
Pen Pen = new Pen(EntityBomber.BodyColor);
2023-11-18 22:48:03 +04:00
Brush Brush = new SolidBrush(EntityBomber.BodyColor);
/** Отрисовка основной части */
Point[] LeftWing = {
new Point(_startPosX + 90, _startPosY),
new Point(_startPosX + 100, _startPosY),
new Point(_startPosX + 108, _startPosY + 85),
new Point(_startPosX + 90, _startPosY + 85),
2023-11-18 22:48:03 +04:00
};
g.DrawPolygon(Pen, LeftWing);
2023-11-18 22:48:03 +04:00
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);
2023-11-18 22:48:03 +04:00
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);
2023-11-18 22:48:03 +04:00
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);
2023-11-18 22:48:03 +04:00
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);
2023-11-18 22:48:03 +04:00
}
}
}