using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WarmlyLocomotive.Entities; namespace WarmlyLocomotive.DrawningObjects { /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// public class DrawningWarmlyLocomotive { /// /// Класс-сущность /// public EntityWarmlyLocomotive? EntityWarmlyLocomotive { get; protected set; } /// /// Ширина окна /// private int _pictureWidth; /// /// Высота окна /// private int _pictureHeight; /// /// Левая координата прорисовки локомотива /// protected int _startPosX; /// /// Верхняя кооридната прорисовки локомотива /// protected int _startPosY; /// /// Ширина прорисовки локомотива /// protected readonly int _WarmlyLocomotiveWidth = 200; /// /// Высота прорисовки локомотива /// protected readonly int _WarmlyLocomotiveHeight = 75; private readonly int _trumpetHeight = 25; public int GetPosX => _startPosX; /// /// Координата Y объекта /// public int GetPosY => _startPosY; /// /// Ширина объекта /// public int GetWidth => _WarmlyLocomotiveWidth; /// /// Высота объекта /// public int GetHeight => _WarmlyLocomotiveHeight; public DrawningWarmlyLocomotive(int speed, double weight, Color bodyColor, int width, int height) { if (width < _WarmlyLocomotiveWidth || height < _WarmlyLocomotiveHeight) { return; } _pictureWidth = width; _pictureHeight = height; EntityWarmlyLocomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor); } protected DrawningWarmlyLocomotive(int speed, double weight, Color bodyColor, int width, int height, int carWidth, int carHeight) { if (width <= _WarmlyLocomotiveWidth || height <= _WarmlyLocomotiveHeight) { return; } _pictureWidth = width; _pictureHeight = height; _WarmlyLocomotiveWidth = carWidth; _WarmlyLocomotiveHeight = carHeight; EntityWarmlyLocomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor); } /// /// Установка позиции /// /// Координата X /// Координата Y public void SetPosition(int x, int y) { if (x < 0 || x >= _pictureWidth || y < 0 || y >= _pictureHeight) { _startPosX = 0; _startPosY = 0; } _startPosX = x; _startPosY = y; } public bool CanMove(Direction direction) { if (EntityWarmlyLocomotive == null) { return false; } return direction switch { //влево Direction.Left => _startPosX - EntityWarmlyLocomotive.Step > 0, //вверх Direction.Up => _startPosY - EntityWarmlyLocomotive.Step > 0, // вправо Direction.Right => _startPosX + EntityWarmlyLocomotive.Step + _WarmlyLocomotiveWidth < _pictureWidth, //вниз Direction.Down => _startPosY + EntityWarmlyLocomotive.Step + _WarmlyLocomotiveHeight < _pictureHeight, _ => false, }; } /// Направление public void MoveTransport(Direction direction) { if (!CanMove(direction) || EntityWarmlyLocomotive == null) { return; } switch (direction) { //влево case Direction.Left: if (_startPosX - EntityWarmlyLocomotive.Step > 0) { _startPosX -= (int)EntityWarmlyLocomotive.Step; } break; //вверх case Direction.Up: if (_startPosY - _trumpetHeight - EntityWarmlyLocomotive.Step > 0) { _startPosY -= (int)EntityWarmlyLocomotive.Step; } break; // вправо case Direction.Right: if (_startPosX + _WarmlyLocomotiveWidth + EntityWarmlyLocomotive.Step < _pictureWidth) { _startPosX += (int)EntityWarmlyLocomotive.Step; } break; //вниз case Direction.Down: if (_startPosY + _WarmlyLocomotiveHeight + EntityWarmlyLocomotive.Step < _pictureHeight) { _startPosY += (int)EntityWarmlyLocomotive.Step; } break; } } /// /// Прорисовка объекта /// /// public virtual void DrawTransport(Graphics g) { if (EntityWarmlyLocomotive == null) { return; } Pen pen = new(Color.Black, 2); Brush bodyBrush = new SolidBrush(EntityWarmlyLocomotive.BodyColor); Brush wheelBrush = new SolidBrush(Color.Black); //корпус g.FillRectangle(bodyBrush, _startPosX + 50, _startPosY, 150, 50); Point[] Points = { new Point(_startPosX + 50, _startPosY + 25), new Point(_startPosX + 125, _startPosY + 25) }; g.DrawPolygon(pen, Points); Point[] Points2 = { new Point(_startPosX + 150, _startPosY + 25), new Point(_startPosX + 200, _startPosY + 25) }; g.DrawPolygon(pen, Points2); //окна g.DrawRectangle(pen, _startPosX + 125, _startPosY + 10, 25, 30); g.DrawRectangle(pen, _startPosX + 60, _startPosY + 7, 10, 13); g.DrawRectangle(pen, _startPosX + 160, _startPosY + 7, 10, 13); g.DrawRectangle(pen, _startPosX + 175, _startPosY + 7, 10, 13); //колеса g.FillEllipse(wheelBrush, _startPosX + 60, _startPosY + 50, 20, 20); g.FillEllipse(wheelBrush, _startPosX + 85, _startPosY + 50, 20, 20); g.FillEllipse(wheelBrush, _startPosX + 145, _startPosY + 50, 20, 20); g.FillEllipse(wheelBrush, _startPosX + 170, _startPosY + 50, 20, 20); } } }