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