using ProjectLainer.Entities; using ProjectLainer.MovementStrategy; using System.Drawing; namespace ProjectLainer.DrawningObjects { public class DrawingLainer { public EntityLainer? EntityLainer { get; protected set; } private int _pictureWidth; private int _pictureHeight; protected int _startPosX; protected int _startPosY; public IMoveableObject GetMoveableObject => new DrawningObjectLainer(this); public DrawingLainer(int speed, double weight, Color bodyColor, int width, int height) { if (width < _LainerWidth || height < _LainerHeight) return; _pictureWidth = width; _pictureHeight = height; EntityLainer = new EntityLainer(speed, weight, bodyColor); } protected DrawingLainer(int speed, double weight, Color bodyColor, int width, int height, int carWidth, int carHeight) { if (width < _LainerWidth || height < _LainerHeight) return; _pictureWidth = width; _pictureHeight = height; _LainerWidth = carWidth; _LainerHeight = carHeight; EntityLainer = new EntityLainer(speed, weight, bodyColor); } public void SetPosition(int x, int y) { if (x + _LainerWidth < _pictureWidth && x + _LainerWidth > 0) { _startPosX = x; } if (y + _LainerHeight < _pictureHeight && y + _LainerHeight > 0) { _startPosY = y; } } public virtual void DrawTransport(Graphics g) { if (EntityLainer == null) { return; } Brush brBlue = new SolidBrush(Color.LightBlue); //отрисовка труб Brush brush = new SolidBrush(Color.Black); Rectangle smokestack1 = new Rectangle(_startPosX + 20, _startPosY, 20, 50); Rectangle smokestack2 = new Rectangle(_startPosX + 60, _startPosY, 20, 50); g.FillRectangle(brush, smokestack1); g.FillRectangle(brush, smokestack2); //границы лайнера Pen pen = new(Color.Red); brush = new SolidBrush(Color.Red); Point point1 = new Point(_startPosX, _startPosY + 50); Point point2 = new Point(_startPosX + _LainerWidth, _startPosY + 50); Point point3 = new Point(_startPosX + 20, _startPosY + 80); Point point4 = new Point(_startPosX + 80, _startPosY + 80); Point[] points = { point1, point2, point4, point3 }; g.DrawPolygon(pen, points); g.FillPolygon(brush, points); //1 палуба brush = new SolidBrush(EntityLainer.BodyColor); Rectangle deck = new Rectangle(_startPosX + 5, _startPosY + 30, _LainerWidth - 10, 20); g.FillRectangle(brush, deck); //стекла for (int i = 1; i < 5; i++) { g.FillEllipse(brBlue, _startPosX + i * 16, _startPosY + 33, 14, 14); } } public int GetPosX => _startPosX; public int GetPosY => _startPosY; private readonly int _LainerWidth = 150; private readonly int _LainerHeight = 80; public int GetWidth => _LainerWidth; public int GetHeight => _LainerHeight; public bool CanMove(DirectionType direction) { if (EntityLainer == null) { return false; } return direction switch { //влево DirectionType.Left => _startPosX - EntityLainer.Step > 0, //вверх DirectionType.Up => _startPosY - EntityLainer.Step > 0, // вправо DirectionType.Right => _startPosX + _LainerWidth + EntityLainer.Step < _pictureWidth, //вниз DirectionType.Down => _startPosY + _LainerHeight + EntityLainer.Step < _pictureHeight, _ => false, }; } public void MoveTransport(DirectionType direction) { if (!CanMove(direction) || EntityLainer == null) { return; } switch (direction) { //влево case DirectionType.Left: if (_startPosX - EntityLainer.Step > 0) { _startPosX -= (int)EntityLainer.Step; } else { _startPosX = 1; } break; //вверх case DirectionType.Up: if (_startPosY - EntityLainer.Step > 0) { _startPosY -= (int)EntityLainer.Step; } else { _startPosY = 1; } break; // вправо case DirectionType.Right: if (_startPosX + EntityLainer.Step <= _pictureWidth - _LainerWidth) { _startPosX += (int)EntityLainer.Step; } else { _startPosX = _pictureWidth - _LainerWidth; } break; //вниз case DirectionType.Down: if (_startPosY + EntityLainer.Step < _pictureHeight - _LainerHeight) { _startPosY += (int)EntityLainer.Step; } else { _startPosY = _pictureHeight - _LainerHeight; } break; } } } }