using ProjectMonorail.Entities; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ProjectMonorail.Drawings; /// /// Класс, отвечающий за прорисовку и перемещение базового объекта-сущности /// public class DrawingTrain { /// /// Класс-сущность "монорельс" /// public EntityTrain? EntityTrain { get; protected set; } /// /// Высота окна /// private int? _pictureHeight; /// /// Ширина окна /// private int? _pictureWidth; /// /// Левая координата прорисовки монорельса /// protected int? _startPosX; /// /// Верхняя координата прорисовки монорельса /// protected int? _startPosY; /// /// Высота прорисовки монорельса /// private readonly int _drawingMonorailHeight = 40; /// /// Ширина прорисовки монорельса /// private readonly int _drawingMonorailWidth = 95; /// /// Координата X объекта /// public int? GetPosX => _startPosX; /// /// Координата Y объекта /// public int? GetPosY => _startPosY; /// /// Ширина объекта /// public int GetWidth => _drawingMonorailWidth; /// /// Высота объекта /// public int GetHeight => _drawingMonorailHeight; /// /// Пустой конструктор /// public DrawingTrain() { _pictureHeight = null; _pictureWidth = null; _startPosX = null; _startPosY = null; } /// /// Конструктор /// /// Скорость /// Вес /// Основной цвет public DrawingTrain(int speed, double weight, Color mainColor) : this() { EntityTrain = new EntityTrain(speed, weight, mainColor); } public DrawingTrain(EntityTrain? entityTrain) { if (entityTrain != null) { EntityTrain = entityTrain; } } /// /// Конструктор для наследников /// /// Высота /// Ширина protected DrawingTrain(int drawingMonorailHeight, int drawingMonorailWidth) : this() { _drawingMonorailHeight = drawingMonorailHeight; _drawingMonorailWidth = drawingMonorailWidth; } /// /// Установка границ поля /// /// Ширина поля /// Высота поля /// public bool SetPictureSize(int width, int height) { if (_drawingMonorailWidth <= width && _drawingMonorailHeight <= height) { _pictureWidth = width; _pictureHeight = height; if (_startPosX.HasValue && _startPosY.HasValue) { if (_startPosX + _drawingMonorailWidth > _pictureWidth) { _startPosX = _pictureWidth - _drawingMonorailWidth; } if (_startPosY + _drawingMonorailHeight > _pictureHeight) { _startPosY = _pictureHeight - _drawingMonorailHeight; } } return true; } return false; } /// /// Установка позиции /// /// Координата x /// Координата y public void SetPosition(int x, int y) { if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) { return; } if (x < 0) { x = 0; } if (x + _drawingMonorailWidth > _pictureWidth.Value) { x = _pictureWidth.Value - _drawingMonorailWidth; } if (y < 0) { y = 0; } if (y + _drawingMonorailHeight > _pictureHeight.Value) { y = _pictureHeight.Value - _drawingMonorailHeight; } _startPosX = x; _startPosY = y; } /// /// Изменение направления перемещения /// /// Направление /// true - перемещение выполнено; false - перемещение невозможно public bool MoveTransport(DirectionType direction) { if (EntityTrain == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } switch (direction) { //Перемещение влево case DirectionType.Left: if (_startPosX.Value - EntityTrain.Step > 0) { _startPosX -= (int)EntityTrain.Step; } return true; //Перемещение вправо case DirectionType.Right: if (_startPosX.Value + _drawingMonorailWidth + EntityTrain.Step < _pictureWidth) { _startPosX += (int)EntityTrain.Step; } return true; //Перемещение вверх case DirectionType.Up: if (_startPosY.Value - EntityTrain.Step > 0) { _startPosY -= (int)EntityTrain.Step; } return true; //Перемещение вниз case DirectionType.Down: if (_startPosY.Value + _drawingMonorailHeight + EntityTrain.Step < _pictureHeight) { _startPosY += (int)EntityTrain.Step; } return true; default: return false; } } /// /// Прорисовка объекта /// /// public virtual void DrawTransport(Graphics g) { if (EntityTrain == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); Brush mainBrush = new SolidBrush(EntityTrain.MainColor); Brush brBlue = new SolidBrush(Color.LightBlue); Brush brWhite = new SolidBrush(Color.White); Brush brBlack = new SolidBrush(Color.Black); //Границы монорельса g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 15, 80, 15); Point[] points1 = { new Point(_startPosX.Value + 10, _startPosY.Value), new Point(_startPosX.Value + 85, _startPosY.Value), new Point(_startPosX.Value + 85, _startPosY.Value + 15), new Point(_startPosX.Value + 5, _startPosY.Value + 15) }; g.DrawPolygon(pen, points1); g.FillRectangle(mainBrush, _startPosX.Value + 6, _startPosY.Value + 16, 79, 14); Point[] points2 = { new Point(_startPosX.Value + 10, _startPosY.Value + 1), new Point(_startPosX.Value + 85, _startPosY.Value + 1), new Point(_startPosX.Value + 85, _startPosY.Value + 15), new Point(_startPosX.Value + 5, _startPosY.Value + 15) }; g.FillPolygon(mainBrush, points2); //Держатель 1 Point[] points_cart1 = { new Point(_startPosX.Value, _startPosY.Value + 35), new Point(_startPosX.Value + 15, _startPosY.Value + 35), new Point(_startPosX.Value + 15, _startPosY.Value + 30), new Point(_startPosX.Value + 5, _startPosY.Value + 30) }; g.DrawPolygon(pen, points_cart1); g.FillPolygon(brBlack, points_cart1); //Держатель 2 Point[] points_cart2 = { new Point(_startPosX.Value + 20, _startPosY.Value + 30), new Point(_startPosX.Value + 25, _startPosY.Value + 35), new Point(_startPosX.Value + 30, _startPosY.Value + 35), new Point(_startPosX.Value + 35, _startPosY.Value + 30) }; g.DrawPolygon(pen, points_cart2); g.FillPolygon(brBlack, points_cart2); //Держатель 3 Point[] points_cart3 = { new Point(_startPosX.Value + 55, _startPosY.Value + 30), new Point(_startPosX.Value + 60, _startPosY.Value + 35), new Point(_startPosX.Value + 65, _startPosY.Value + 35), new Point(_startPosX.Value + 70, _startPosY.Value + 30) }; g.DrawPolygon(pen, points_cart3); g.FillPolygon(brBlack, points_cart3); //Держатель 4 Point[] points_cart4 = { new Point(_startPosX.Value + 70, _startPosY.Value + 30), new Point(_startPosX.Value + 75, _startPosY.Value + 35), new Point(_startPosX.Value + 90, _startPosY.Value + 35), new Point(_startPosX.Value + 85, _startPosY.Value + 30) }; g.DrawPolygon(pen, points_cart4); g.FillPolygon(brBlack, points_cart4); //Окна монорельса g.DrawRectangle(pen, _startPosX.Value + 17, _startPosY.Value + 5, 17, 7); g.FillRectangle(brBlue, _startPosX.Value + 18, _startPosY.Value + 6, 16, 6); g.DrawRectangle(pen, _startPosX.Value + 55, _startPosY.Value + 5, 27, 7); g.FillRectangle(brBlue, _startPosX.Value + 56, _startPosY.Value + 6, 26, 6); //Дверь монорельса g.DrawRectangle(pen, _startPosX.Value + 40, _startPosY.Value + 7, 10, 21); g.FillRectangle(brWhite, _startPosX.Value + 41, _startPosY.Value + 8, 9, 20); //колёса //1, 4 колесо g.DrawEllipse(pen, _startPosX.Value + 15, _startPosY.Value + 30, 10, 10); g.FillEllipse(brWhite, _startPosX.Value + 15, _startPosY.Value + 30, 10, 10); g.DrawEllipse(pen, _startPosX.Value + 65, _startPosY.Value + 30, 10, 10); g.FillEllipse(brWhite, _startPosX.Value + 65, _startPosY.Value + 30, 10, 10); } }