using System.Drawing.Drawing2D; using System.Net.Sockets; namespace Excavator; /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// public class DrawningExcavator { /// /// Класс-сущность /// public EntityExcavator? EntityExcavator { get; private set; } /// /// Ширина окна /// private int? _pictureWidth; /// /// Высота окна /// private int? _pictureHeight; /// /// Левая координата прорисовки эскаватора /// private int? _startPosX; /// /// Верхняя кооридната прорисовки экскаватора /// private int? _startPosY; /// /// Ширина прорисовки экскаватора /// private readonly int _drawningExcavatorWidth = 130; /// /// Высота прорисовки экскаватора /// private readonly int _drawningExcavatorHeight = 140; /// /// Инициализация свойств /// /// Скорость /// Вес /// Основной цвет /// Дополнительный цвет /// Признак наличия ковша /// Признак наличия опор public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bucket, bool supports) { EntityExcavator = new EntityExcavator(); EntityExcavator.Init(speed, weight, bodyColor, additionalColor, bucket, supports); _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } /// /// Установка границ поля /// /// Ширина поля /// Высота поля /// true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах public bool SetPictureSize(int width, int height) { if (width > _drawningExcavatorWidth && height > _drawningExcavatorHeight) { _pictureWidth = width; _pictureHeight = height; if (_startPosX != null && _startPosY != null) { if (_startPosX.Value < 0) { _startPosX = 0; } if (_startPosY.Value < 0) { _startPosY = 0; } if (_startPosX.Value + _drawningExcavatorWidth > _pictureWidth) { _startPosX = _pictureWidth - _drawningExcavatorWidth; } if (_startPosY.Value + _drawningExcavatorHeight > _pictureHeight) { _startPosY = _pictureHeight - _drawningExcavatorHeight; } } return true; } return false; } /// /// Установка позиции /// /// Координата X /// Координата Y public void SetPosition(int x, int y) { if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) { return; } else { _startPosX = x; _startPosY = y; if (_startPosX.Value < 0) { _startPosX = 0; } if (_startPosY.Value < 0) { _startPosY = 0; } if (_startPosX + _drawningExcavatorWidth > _pictureWidth) { _startPosX = _pictureWidth - _drawningExcavatorWidth; } if (_startPosY + _drawningExcavatorHeight > _pictureHeight) { _startPosY = _pictureHeight - _drawningExcavatorHeight; } } } /// /// Изменение направления перемещения /// /// Направление /// true - перемещене выполнено, false - перемещение невозможно public bool MoveTransport(DirectionType direction) { if (EntityExcavator == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } switch (direction) { //влево case DirectionType.Left: if (_startPosX.Value - EntityExcavator.Step > 0) { _startPosX -= (int)EntityExcavator.Step; } return true; //вверх case DirectionType.Up: if (_startPosY.Value - EntityExcavator.Step > 0) { _startPosY -= (int)EntityExcavator.Step; } return true; // вправо case DirectionType.Right: //TODO прописать логику сдвига в право if (_startPosX.Value + _drawningExcavatorWidth + EntityExcavator.Step < _pictureWidth) { _startPosX += (int)EntityExcavator.Step; } return true; //вниз case DirectionType.Down: //TODO прописать логику сдвига в вниз if (_startPosY.Value + _drawningExcavatorHeight + EntityExcavator.Step < _pictureHeight) { _startPosY += (int)EntityExcavator.Step; } return true; default: return false; } } public static GraphicsPath RoundedRect(Graphics g, Rectangle bounds, int radius) { int diameter = radius * 2; Size size = new Size(diameter, diameter); Rectangle arc = new Rectangle(bounds.Location, size); GraphicsPath path = new GraphicsPath(); if (radius == 0) { path.AddRectangle(bounds); return path; } // top left arc path.AddArc(arc, 180, 90); // top right arc arc.X = bounds.Right - diameter; path.AddArc(arc, 270, 90); // bottom right arc arc.Y = bounds.Bottom - diameter; path.AddArc(arc, 0, 90); // bottom left arc arc.X = bounds.Left; path.AddArc(arc, 90, 90); g.FillPath(Brushes.Black, path); path.CloseFigure(); return path; } /// /// Прорисовка объекта /// /// /// public void DrawTransport(Graphics g) { if (EntityExcavator == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black); Brush additionalBrush = new SolidBrush(EntityExcavator.AdditionalColor); Brush brBlue = new SolidBrush(Color.LightBlue); Brush brGray = new SolidBrush(Color.Gray); Brush brRed = new SolidBrush(Color.Red); Brush brYellow = new SolidBrush(Color.Yellow); Brush brBlack = new SolidBrush(Color.Black); //кузов g.DrawRectangle(pen, _startPosX.Value , _startPosY.Value, 150, 150); g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, 90, 40); g.FillRectangle(brGray, _startPosX.Value, _startPosY.Value, 90, 40); //труба g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value - 35, 7, 35); g.FillRectangle(brBlack, _startPosX.Value + 10, _startPosY.Value - 35, 7, 35); //кабина g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value - 40, 40, 40); g.FillRectangle(brBlue, _startPosX.Value + 50, _startPosY.Value - 40, 40, 40); //колеса g.DrawPath(pen, RoundedRect(g, new Rectangle(_startPosX.Value - 10, _startPosY.Value + 44, 110, 40), 15)); Pen bPen = new(Color.Black); bPen.Width = 3; g.DrawEllipse(bPen, _startPosX.Value - 9, _startPosY.Value + 52, 25, 25); g.DrawEllipse(bPen, _startPosX.Value + 73, _startPosY.Value + 52, 25, 25); g.FillEllipse(brBlue, _startPosX.Value - 9, _startPosY.Value + 52, 25, 25); g.FillEllipse(brBlue, _startPosX.Value + 73, _startPosY.Value + 52, 25, 25); //колеса g.DrawEllipse(bPen, _startPosX.Value + 20, _startPosY.Value + 67, 13, 13); g.DrawEllipse(bPen, _startPosX.Value + 40, _startPosY.Value + 67, 13, 13); g.DrawEllipse(bPen, _startPosX.Value + 60, _startPosY.Value + 67, 13, 13); g.FillEllipse(brRed, _startPosX.Value + 20, _startPosY.Value + 67, 13, 13); g.FillEllipse(brRed, _startPosX.Value + 40, _startPosY.Value + 67, 13, 13); g.FillEllipse(brRed, _startPosX.Value + 60, _startPosY.Value + 67, 13, 13); g.DrawEllipse(bPen, _startPosX.Value + 30, _startPosY.Value + 48, 9, 9); g.DrawEllipse(bPen, _startPosX.Value + 55, _startPosY.Value + 48, 9, 9); g.FillEllipse(brYellow, _startPosX.Value + 30, _startPosY.Value + 48, 9, 9); g.FillEllipse(brYellow, _startPosX.Value + 55, _startPosY.Value + 48, 9, 9); // ковш if (EntityExcavator.Bucket) { g.DrawLine(pen, _startPosX.Value, _startPosY.Value + 40, _startPosX.Value - 30, _startPosY.Value - 10); g.DrawLine(pen, _startPosX.Value - 30, _startPosY.Value - 10, _startPosX.Value, _startPosY.Value - 10); g.DrawLine(pen, _startPosX.Value, _startPosY.Value - 10, _startPosX.Value, _startPosY.Value + 40); PointF[] p = [new PointF(_startPosX.Value, _startPosY.Value + 40), new PointF(_startPosX.Value - 30, _startPosY.Value - 10), new PointF(_startPosX.Value, _startPosY.Value - 10)]; g.FillPolygon(additionalBrush, p); } // опоры if (EntityExcavator.Supports) { g.DrawLine(bPen, _startPosX.Value + 90, _startPosY.Value + 20, _startPosX.Value + 112, _startPosY.Value + 20); g.DrawLine(bPen, _startPosX.Value + 112, _startPosY.Value + 20, _startPosX.Value + 112, _startPosY.Value + 80); g.DrawLine(bPen, _startPosX.Value + 112, _startPosY.Value + 80, _startPosX.Value + 120, _startPosY.Value + 80); } } }