namespace ProjectStormtrooper; /// /// Класс отвечающий за прорисовку и перемещение объекта-сущности /// public class DrawingStormtrooper { /// /// Класс-сущность /// public EntityStormtrooper? EntityStormtrooper { get; private set; } /// /// Ширина окна /// private int? _pictureWidth; /// /// Высота окна /// private int? _pictureHeight; /// /// Левая координата начала прорисовки /// private int? _startPosX; /// /// Верхняя координата начала прорисовки /// private int? _startPosY; /// /// Ширина прорисовки /// private readonly int _drawningStormtrooperWidth = 140; /// /// Высота прорисовки /// private readonly int _drawningStormtooperHeight = 140; /// /// Инициализация свойств /// /// /// /// /// /// /// public void Init(int speed,double weight, Color bodyColor,Color additionalColor, bool rockets, bool bombs) { EntityStormtrooper = new EntityStormtrooper(); EntityStormtrooper.Init(speed, weight, bodyColor, additionalColor, rockets, bombs); _pictureHeight = null; _pictureWidth = null; _startPosX = null; _startPosY = null; } /// /// Установка границ поля /// /// Ширина поля /// Высота поля /// true - граница задана, false - проверка не пройдена, нельзя разместить объект в этих размерах 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; } /// /// Установка позиции /// /// Координата Х /// Координата У 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; } /// /// Изменение направления перемещения /// /// направление /// true - перемещение выполнено, false - перемещение невозможно public bool MoveTransport(DirectionType direction) { if (EntityStormtrooper == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } switch (direction) { //влево case DirectionType.Left: if (_startPosX.Value - EntityStormtrooper.Step > 0) { _startPosX -= (int)EntityStormtrooper.Step; } return true; //Вверх case DirectionType.Up: if (_startPosY.Value - EntityStormtrooper.Step > 0) { _startPosY -= (int)EntityStormtrooper.Step; } return true; //Вправо case DirectionType.Right: if (_startPosX.Value + EntityStormtrooper.Step+_drawningStormtrooperWidth < _pictureWidth) { _startPosX += (int)EntityStormtrooper.Step; } return true; //Вниз case DirectionType.Down: if (_startPosY.Value + EntityStormtrooper.Step + _drawningStormtooperHeight < _pictureHeight) { _startPosY += (int)EntityStormtrooper.Step; } return true; default: return false; } } /// /// Прорисовка объекта /// /// public void DrawTransport(Graphics g) { if (EntityStormtrooper == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new (Color.Black); Brush bodyColorBrush = new SolidBrush(EntityStormtrooper.BodyColor); Brush additionalBrush = new SolidBrush(EntityStormtrooper.AdditionalColor); //нос штурмовика Brush brBlack = new SolidBrush(Color.Black); Point[] Nose = new Point[3]; Nose[0].X = _startPosX.Value + 20; Nose[0].Y = _startPosY.Value + 80; Nose[1].X = _startPosX.Value + 20; Nose[1].Y = _startPosY.Value + 60; Nose[2].X = _startPosX.Value; Nose[2].Y = _startPosY.Value + 70; g.FillPolygon(brBlack, Nose); g.DrawPolygon(pen, Nose); //Заднии крылья штурмовика Point[] pflybtwings = new Point[6]; pflybtwings[0].X = _startPosX.Value + 120; pflybtwings[0].Y = _startPosY.Value + 60; pflybtwings[1].X = _startPosX.Value + 120; pflybtwings[1].Y = _startPosY.Value + 50; pflybtwings[2].X = _startPosX.Value + 140; pflybtwings[2].Y = _startPosY.Value + 30; pflybtwings[3].X = _startPosX.Value + 140; pflybtwings[3].Y = _startPosY.Value + 110; pflybtwings[4].X = _startPosX.Value + 120; pflybtwings[4].Y = _startPosY.Value + 90; pflybtwings[5].X = _startPosX.Value + 120; pflybtwings[5].Y = _startPosY.Value + 80; g.FillPolygon(bodyColorBrush, pflybtwings); g.DrawPolygon(pen, pflybtwings); //Тело штурмовика g.FillRectangle(bodyColorBrush, _startPosX.Value + 20, _startPosY.Value + 60, 120, 20); g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 60, 120, 20); //Крылья штурмовика Point[] frontwings = new Point[4]; frontwings[0].X = _startPosX.Value + 60; frontwings[0].Y = _startPosY.Value + 60; frontwings[1].X = _startPosX.Value + 60; frontwings[1].Y = _startPosY.Value ; frontwings[2].X = _startPosX.Value + 70; frontwings[2].Y = _startPosY.Value ; frontwings[3].X = _startPosX.Value + 80; frontwings[3].Y = _startPosY.Value + 60; g.FillPolygon(bodyColorBrush, frontwings); g.DrawPolygon(pen, frontwings); Point[] frontwings2 = new Point[4]; frontwings2[0].X = _startPosX.Value + 60; frontwings2[0].Y = _startPosY.Value + 80; frontwings2[1].X = _startPosX.Value + 60; frontwings2[1].Y = _startPosY.Value+140; frontwings2[2].X = _startPosX.Value + 70; frontwings2[2].Y = _startPosY.Value+140; frontwings2[3].X = _startPosX.Value + 80; frontwings2[3].Y = _startPosY.Value + 80; g.FillPolygon(bodyColorBrush, frontwings2); g.DrawPolygon(pen, frontwings2); //Ракеты штурмовика if (EntityStormtrooper.Rockets) { g.FillRectangle(additionalBrush, _startPosX.Value + 45, _startPosY.Value + 20, 15, 5); g.FillRectangle(additionalBrush, _startPosX.Value + 45, _startPosY.Value + 110, 15, 5); g.DrawRectangle(pen, _startPosX.Value + 45, _startPosY.Value + 20, 15, 5); g.DrawRectangle(pen, _startPosX.Value + 45, _startPosY.Value + 110, 15, 5); } //Бомбы бомбардировщика if (EntityStormtrooper.Bombs) { g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 40, 10, 10); g.FillRectangle(additionalBrush, _startPosX.Value + 50, _startPosY.Value + 90, 10, 10); g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 40, 10, 10); g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value + 90, 10, 10); } } }