using ProjectElectricLocomotive.Entities; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ProjectElectricLocomotive.Drawnings; public class DrawningLocomotive { /// /// Класс-сущность /// public EntityLocomotive? EntityLocomotive { get; protected set; } /// /// Ширина окна /// private int? _pictureWidth; /// /// Высота окна /// private int? _pictureHeight; /// /// Левая координата прорисовки Электровоза /// protected int? _startPosX; /// /// Верхняя кооридната прорисовки Электровоза /// protected int? _startPosY; /// /// Ширина прорисовки Локомотива /// public readonly int _drawningLocomotiveWidth = 155; /// /// Высота прорисовки локомотива /// public readonly int _drawningLocomotiveHeight = 115; /// /// Координата X объекта /// public int? GetPosX => _startPosX; /// /// Координата Y объекта /// public int? GetPosY => _startPosY; /// /// Ширина объекта /// public int GetWidth => _drawningLocomotiveWidth; /// /// Высота объекта /// public int GetHeight => _drawningLocomotiveHeight; /// /// Пустой конструктор /// private DrawningLocomotive() { _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } /// /// Конструктор /// /// Скорость /// Вес /// Основной цвет public DrawningLocomotive(int speed, double weight, Color bodyColor) : this() { EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor); } /// /// Конструктор для наследников /// /// Ширина прорисовки локомотива /// Высота прорисовки локомотива protected DrawningLocomotive(int drawningLocomotiveWidtе, int drawningLocomotiveHeight) : this() { _drawningLocomotiveHeight = drawningLocomotiveHeight; _drawningLocomotiveWidth = drawningLocomotiveWidtе; } /// /// Установка границ поля /// /// Ширина поля /// Высота поля /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах public bool SetPictureSize(int width, int height) { // TODO проверка, что объект "влезает" в размеры поля if (_drawningLocomotiveWidth > width || _drawningLocomotiveHeight > height) { return false; } if (_startPosX.HasValue && _startPosY.HasValue) { if (_startPosX + _drawningLocomotiveWidth > width) { _startPosX = width - _drawningLocomotiveWidth; } if (_startPosY + _drawningLocomotiveHeight > height) { _startPosY = height - _drawningLocomotiveHeight; } } //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена _pictureWidth = width; _pictureHeight = height; return true; } public void SetPosition(int x, int y) { if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) { return; } // TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы if (x < 0 || x + _drawningLocomotiveWidth > _pictureWidth.Value) { x = _pictureWidth.Value - _drawningLocomotiveWidth; } if (y < 0 || y + _drawningLocomotiveHeight > _pictureHeight.Value) { y = _pictureHeight.Value - _drawningLocomotiveHeight; } // то надо изменить координаты, чтобы он оставался в этих границах _startPosX = x; _startPosY = y; } public bool MoveTransport(DirectionType direction) { if (EntityLocomotive == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } switch (direction) { //влево case DirectionType.Left: if (_startPosX.Value - EntityLocomotive.Step > 0) { _startPosX -= (int)EntityLocomotive.Step; } return true; //вверх case DirectionType.Up: if (_startPosY.Value - EntityLocomotive.Step > 0) { _startPosY -= (int)EntityLocomotive.Step; } return true; // вправо case DirectionType.Right: if (_startPosX.Value + EntityLocomotive.Step < _pictureWidth - _drawningLocomotiveWidth) { _startPosX += (int)EntityLocomotive.Step; } return true; //вниз case DirectionType.Down: if (_startPosY.Value + EntityLocomotive.Step < _pictureHeight - _drawningLocomotiveHeight) { _startPosY += (int)EntityLocomotive.Step; } return true; default: return false; } } public virtual void DrawTransport(Graphics g) { if (EntityLocomotive == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); Brush bodyBrush = new SolidBrush(EntityLocomotive.BodyColor); //границы Электровоза //колеса g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 65, 20, 20); g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 65, 20, 20); g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 65, 20, 20); g.DrawEllipse(pen, _startPosX.Value + 120, _startPosY.Value + 65, 20, 20); //кузов g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 50, 155, 40); g.DrawLine(pen, _startPosX.Value, _startPosY.Value + 50, _startPosX.Value + 20, _startPosY.Value + 30); g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 30, 135, 20); //покраска кузова g.FillEllipse(Brushes.Black, _startPosX.Value + 10, _startPosY.Value + 85, 20, 20); g.FillEllipse(Brushes.Black, _startPosX.Value + 30, _startPosY.Value + 85, 20, 20); g.FillEllipse(Brushes.Black, _startPosX.Value + 100, _startPosY.Value + 85, 20, 20); g.FillEllipse(Brushes.Black, _startPosX.Value + 120, _startPosY.Value + 85, 20, 20); g.FillRectangle(bodyBrush, _startPosX.Value + 1, _startPosY.Value + 51, 154, 39); g.FillRectangle(bodyBrush, _startPosX.Value + 21, _startPosY.Value + 31, 134, 19); g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 50, 155, 10); g.FillRectangle(bodyBrush, _startPosX.Value + 1, _startPosY.Value + 51, 154, 9); } }