using System.Drawing; namespace Lab1; public class DrawingElectroTrans { /// /// Класс-сущность /// public EntityElectroTrans? EntityElectroTrans { get; private set; } /// /// Ширина окна /// private int? _pictureWidth; /// /// Высота окна /// private int? _pictureHeight; /// /// Левая координата прорисовки автомобиля /// private int? _startPosX; /// /// Верхняя кооридната прорисовки автомобиля /// private int? _startPosY; /// /// Ширина прорисовки автомобиля /// private readonly int _drawningTransWidth = 110; /// /// Высота прорисовки автомобиля /// private readonly int _drawningTransHeight = 60; /// /// Инициализация свойств /// /// Скорость /// Вес автомобиля /// Основной цвет /// Дополнительный цвет /// Признак активности усов /// Признак колличества колес /// Признак наличия бптпрей public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool horns, int wheels, bool battery) { EntityElectroTrans = new EntityElectroTrans(); EntityElectroTrans.Init(speed, weight, bodyColor, additionalColor, horns, wheels, battery); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } /// /// Установка границ поля /// /// Ширина поля /// Высота поля /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах public bool SetPictureSize(int width, int height) { _pictureWidth = width; _pictureHeight = height; return true; } /// /// Установка позиции /// /// Координата X /// Координата Y public void SetPosition(int x, int y) { if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) { return; } // TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы // то надо изменить координаты, чтобы он оставался в этих границах _startPosX = x; _startPosY = y; } /// /// Изменение направления перемещения /// /// Направление /// true - перемещене выполнено, false - перемещение невозможно public bool MoveTransport(DirectionType direction) { if (EntityElectroTrans == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } if (_startPosX.Value < 40 || _startPosY.Value < 40 || _startPosX > Config.screenSize.Width - 40 || _startPosY > Config.screenSize.Height - 40) { return false; } switch (direction) { //влево case DirectionType.Left: if (_startPosX.Value - EntityElectroTrans.Step > 40) { _startPosX -= (int)EntityElectroTrans.Step; } return true; //вверх case DirectionType.Up: if (_startPosY.Value - EntityElectroTrans.Step > 40) { _startPosY -= (int)EntityElectroTrans.Step; } return true; // вправо case DirectionType.Right: if (_startPosX.Value + EntityElectroTrans.Step < Config.screenSize.Width - 40) { _startPosX += (int)EntityElectroTrans.Step; } return true; //вниз case DirectionType.Down: if (_startPosY.Value + EntityElectroTrans.Step < Config.screenSize.Height - 40) { _startPosY += (int)EntityElectroTrans.Step; } return true; default: return false; } } /// /// Прорисовка объекта /// /// public void DrawTransport(Graphics g) { if (EntityElectroTrans == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(EntityElectroTrans.BodyColor); Pen additionalPen = new(EntityElectroTrans.AdditionalColor); Brush brush = new SolidBrush(EntityElectroTrans.BodyColor); Brush additionalBrush = new SolidBrush(EntityElectroTrans.AdditionalColor); g.DrawPolygon(pen, new Point[] { new Point(_startPosX.Value - 40, _startPosY.Value), new Point(_startPosX.Value - 30, _startPosY.Value - 20), new Point(_startPosX.Value + 30, _startPosY.Value - 20), new Point(_startPosX.Value + 40, _startPosY.Value), new Point(_startPosX.Value + 40, _startPosY.Value + 20), new Point(_startPosX.Value - 40, _startPosY.Value + 20), }); for (int i = 0; i < EntityElectroTrans.Wheels; i++) { g.FillEllipse(additionalBrush, new Rectangle((_startPosX.Value - 40) + (70 / EntityElectroTrans.Wheels) * (i + 1) + -4, _startPosY.Value + 16, 8, 8)); } g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX.Value - 38, _startPosY.Value), new Point(_startPosX.Value - 30, _startPosY.Value - 17), new Point(_startPosX.Value + 30, _startPosY.Value - 17), new Point(_startPosX.Value + 38, _startPosY.Value), }); if (EntityElectroTrans.Horns) { g.DrawPolygon(additionalPen, new Point[] { new Point(_startPosX.Value, _startPosY.Value - 20), new Point(_startPosX.Value - 20, _startPosY.Value - 30), new Point(_startPosX.Value + 20, _startPosY.Value - 30), }); } else { g.DrawPolygon(additionalPen, new Point[] { new Point(_startPosX.Value, _startPosY.Value - 20), new Point(_startPosX.Value - 20, _startPosY.Value - 23), new Point(_startPosX.Value + 20, _startPosY.Value - 23), }); } if (EntityElectroTrans.Battery){ g.FillPolygon(additionalBrush, new Point[] { new Point(_startPosX.Value - 15, _startPosY.Value + 2), new Point(_startPosX.Value - 15, _startPosY.Value + 6), new Point(_startPosX.Value - 18, _startPosY.Value + 6), new Point(_startPosX.Value - 18, _startPosY.Value + 10), new Point(_startPosX.Value - 15, _startPosY.Value + 10), new Point(_startPosX.Value - 15, _startPosY.Value + 16), new Point(_startPosX.Value + 18, _startPosY.Value + 16), new Point(_startPosX.Value + 18, _startPosY.Value + 2), }); } } }