using ProjectCruiser.Entities; namespace ProjectCruiser.DrawningSamples; public class DrawningBase { // Класс-сущность public EntityBase? EntityTransport { get; protected set; } private int? _pictureWidth; // Ширина окна private int? _pictureHeight; // Высота окна protected int? _startPosX; // < protected protected int? _startPosY; // < protected private readonly int _drawningWidth = 302; // Ширина прорисовки автомобиля private readonly int _drawningHeight = 42; // Высота прорисовки автомобиля // Инициализация свойств (теперь через конструктор) public DrawningBase(int speed, double weight, Color bodyColor) : this() { EntityTransport = new EntityBase(speed, weight, bodyColor); } private DrawningBase() { _pictureWidth = null; _pictureHeight = null; _startPosX = null; _startPosY = null; } // protected > protected DrawningBase(int drawningCarWidth, int drawningCarHeight) : this() { _drawningWidth = drawningCarWidth; _pictureHeight = drawningCarHeight; } public int getHeight() // для создания объекта в нижнем левом углу (*) { return _drawningHeight; } // Установка границ поля public bool SetPictureSize(int w, int h) { if (w < _drawningWidth || h < _drawningHeight) // canvas always bigger then obj to fit it { return false; } _pictureWidth = w; _pictureHeight = h; if (_startPosX != null || _startPosY != null) { SetPosition(_startPosX.Value, _startPosY.Value); } return true; } public void SetPosition(int x, int y) { if (!_pictureHeight.HasValue || !_pictureWidth.HasValue) { return; } if (x > _pictureWidth - _drawningWidth) _startPosX = _pictureWidth - _drawningWidth; else if (x < 0) _startPosX = 0; else _startPosX = x; if (y > _pictureHeight - _drawningHeight) _startPosY = _pictureHeight.Value - _drawningHeight; else if (y < 0) _startPosY = 0; else _startPosY = y; } public bool MoveTransport(DirectionType direction) { if (EntityTransport == null || !_startPosX.HasValue || !_startPosY.HasValue) { return false; } switch (direction) { //влево case DirectionType.Left: if (_startPosX.Value - EntityTransport.Step > 0) { _startPosX -= (int)EntityTransport.Step; } return true; //вверх case DirectionType.Up: if (_startPosY.Value - EntityTransport.Step > 0) { _startPosY -= (int)EntityTransport.Step; } return true; // вправо case DirectionType.Right: if (_startPosX.Value + _drawningWidth + EntityTransport.Step < _pictureWidth.Value) { _startPosX += (int)EntityTransport.Step; } return true; //вниз case DirectionType.Down: if (_startPosY.Value + _drawningHeight + EntityTransport.Step < _pictureHeight.Value) { _startPosY += (int)EntityTransport.Step; } return true; default: return false; // Перемещение объекта (удалось перемещение - true \ false) } } public virtual void DrawTransport(Graphics g) // < virtual { if (EntityTransport == null || !_startPosX.HasValue || !_startPosY.HasValue) { return; } Pen pen = new(Color.Black, 2); Brush mainBrush = new SolidBrush(EntityTransport.MainColor); //границы cruiser Point point0 = new Point(_startPosX.Value + 2, _startPosY.Value + 7); Point point1 = new Point(_startPosX.Value + 2, _startPosY.Value + 30); Point point2 = new Point(_startPosX.Value + 184, _startPosY.Value + 42); Point point3 = new Point(_startPosX.Value + 260, _startPosY.Value + 34); Point point4 = new Point(_startPosX.Value + 300, _startPosY.Value + 22); Point point5 = new Point(_startPosX.Value + 260, _startPosY.Value + 10); Point point6 = new Point(_startPosX.Value + 184, _startPosY.Value + 2); Point[] boarders = { point0, point1, point2, point3, point4, point5, point6 }; g.DrawPolygon(pen, boarders); g.FillPolygon(mainBrush, boarders); // салон на верхней палубе int y_h = EntityTransport.values[1]; g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value + y_h, 38, 24); g.DrawRectangle(pen, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12); g.FillRectangle(mainBrush, _startPosX.Value + 110, _startPosY.Value + y_h + 6, 20, 12); g.DrawRectangle(pen, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20); g.FillRectangle(mainBrush, _startPosX.Value + 117, _startPosY.Value + y_h + 18, 6, 20); } public int? GetPosX => _startPosX; public int? GetPosY => _startPosY; public int GetWidth => _drawningWidth; public int GetHeight => _drawningHeight; }