using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ElectricLocomotive { internal class DrawningElectricLocomotive { /// /// Класс-сущность /// public EntityElectricLocomotive? EntityElectricLocomotive { get; private set; } /// /// Ширина окна /// private int _pictureWidth; /// /// Высота окна /// private int _pictureHeight; /// /// Левая координата прорисовки автомобиля /// private int _startPosX; /// /// Верхняя кооридната прорисовки автомобиля /// private int _startPosY; /// /// Ширина прорисовки автомобиля /// private readonly int _locomotiveWidth = 120; /// /// Высота прорисовки автомобиля /// private readonly int _locomotiveHeight = 70; /// /// Инициализация свойств /// /// Скорость /// Вес /// Цвет кузова /// Дополнительный цвет /// Признак наличия рогов /// Признак наличия отсека для батарей /// Ширина картинки /// Высота картинки /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool horns, bool battery, int width, int height) { if (width < _locomotiveWidth || height < _locomotiveHeight) { return false; } _pictureWidth = width; _pictureHeight = height; EntityElectricLocomotive = new EntityElectricLocomotive(); EntityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, horns, battery); return true; } /// /// Установка позиции /// /// Координата X /// Координата Y public void SetPosition(int x, int y) { x = Math.Min(Math.Max(0, x), _pictureWidth - _locomotiveWidth); y = Math.Min(Math.Max(0, y), _pictureHeight - _locomotiveHeight); _startPosX = x; _startPosY = y; } /// /// Изменение направления перемещения /// /// Направление public void MoveTransport(DirectionType direction) { if (EntityElectricLocomotive == null) { return; } switch (direction) { //влево case DirectionType.Left: if (_startPosX - EntityElectricLocomotive.Step > 0) { _startPosX -= (int)EntityElectricLocomotive.Step; } break; //вверх case DirectionType.Up: if (_startPosY - EntityElectricLocomotive.Step > 0) { _startPosY -= (int)EntityElectricLocomotive.Step; } break; // вправо case DirectionType.Right: if (_startPosX + _locomotiveWidth + EntityElectricLocomotive.Step < _pictureWidth) { _startPosX += (int)EntityElectricLocomotive.Step; } break; //вниз case DirectionType.Down: if (_startPosY + _locomotiveHeight + EntityElectricLocomotive.Step < _pictureHeight) { _startPosY += (int)EntityElectricLocomotive.Step; } break; } } /// /// Прорисовка объекта /// /// public void DrawTransport(Graphics g) { if (EntityElectricLocomotive == null) { return; } // корпус электровоза Pen pen = new(Color.Black, 5); Brush brush = new SolidBrush(EntityElectricLocomotive.BodyColor); Point[] points = new Point[5]; points[0] = new Point(_startPosX, _startPosY + 40); points[1] = new Point(_startPosX + 10, _startPosY + 20); points[2] = new Point(_startPosX + 120, _startPosY + 20); points[3] = new Point(_startPosX + 120, _startPosY + 60); points[4] = new Point(_startPosX, _startPosY + 60); g.FillPolygon(brush, points); g.DrawLine(pen, new Point(_startPosX, _startPosY + 40), new Point(_startPosX + 120, _startPosY + 40)); // окна brush = new SolidBrush(Color.FromArgb(0, 162, 232)); g.FillRectangle(brush, _startPosX + 12, _startPosY + 24, 30, 11); g.FillEllipse(brush, _startPosX + 55, _startPosY + 24, 11, 11); g.FillEllipse(brush, _startPosX + 75, _startPosY + 24, 11, 11); g.FillEllipse(brush, _startPosX + 95, _startPosY + 24, 11, 11); // колёса brush = new SolidBrush(Color.Black); g.FillEllipse(brush, _startPosX + 10, _startPosY + 55, 15, 15); g.FillEllipse(brush, _startPosX + 25, _startPosY + 55, 15, 15); g.FillEllipse(brush, _startPosX + 80, _startPosY + 55, 15, 15); g.FillEllipse(brush, _startPosX + 95, _startPosY + 55, 15, 15); // рога if (EntityElectricLocomotive.Horns) { pen = new(Color.Black, 2); points = new Point[4]; points[0] = new Point(_startPosX + 50, _startPosY + 20); points[1] = new Point(_startPosX + 40, _startPosY + 10); points[2] = new Point(_startPosX + 50, _startPosY); points[3] = new Point(_startPosX + 60, _startPosY + 10); g.DrawPolygon(pen, points); } // отсек для батарей if (EntityElectricLocomotive.Battery) { brush = new SolidBrush(EntityElectricLocomotive.AdditionalColor); g.FillRectangle(brush, _startPosX + 80, _startPosY + 45, 35, 9); } } /// /// Изменение размера поля /// /// Новый размер public void SetPictureSize(Size newSize) { _pictureHeight = newSize.Height; _pictureWidth = newSize.Width; } } }