using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using RoadTrain.Entities; using RoadTrain.MovementStrategy; namespace RoadTrain.DrawningObjects { public class DrawningRoadTrain { /// /// Класс-сущность /// public EntityRoadTrain? EntityRoadTrain { get; protected set; } /// /// Ширина окна /// public int _pictureWidth; /// /// Высота окна /// public int _pictureHeight; /// /// Левая координата прорисовки автомобиля /// protected int _startPosX; /// /// Верхняя кооридната прорисовки автомобиля /// protected int _startPosY; /// /// Ширина прорисовки автомобиля /// protected readonly int _trainWidth = 70; /// /// Высота прорисовки автомобиля /// protected readonly int _trainHeight = 30; /// /// Координата X объекта /// public int GetPosX => _startPosX; /// /// Координата Y объекта /// public int GetPosY => _startPosY; /// /// Ширина объекта /// public int GetWidth => _trainWidth; /// /// Высота объекта /// public int GetHeight => _trainHeight; /// /// Проверка, что объект может переместится по указанному направлению /// /// Направление /// true - можно переместится по указанному направлению public bool CanMove(DirectionType direction) { if (EntityRoadTrain == null) { return false; } return direction switch { //влево DirectionType.Left => _startPosX - EntityRoadTrain.Step > 0, //вверх DirectionType.Up => _startPosY - EntityRoadTrain.Step > 0, // вправо DirectionType.Right => _startPosX + EntityRoadTrain.Step + _trainWidth < _pictureWidth, //вниз DirectionType.Down => _startPosY + EntityRoadTrain.Step + _trainHeight < _pictureHeight }; } /// /// Инициализация свойств /// /// Скорость /// Вес /// Цвет кузова /// Ширина картинки /// Высота картинки /// true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах public DrawningRoadTrain(int speed, double weight, Color bodyColor, int width, int height) { if (width < _trainWidth) { return; } if (height < _trainHeight) { return; } _pictureWidth = width; _pictureHeight = height; EntityRoadTrain = new EntityRoadTrain(speed, weight, bodyColor); } /// /// Установка позиции /// /// Координата X /// Координата Y public void SetPosition(int x, int y) { if (x < 0) { x = 0; } else if (x + _trainWidth > _pictureWidth) { x = _pictureWidth - _trainWidth; } if (y < 0) { y = 0; } else if (y + _trainHeight > _pictureHeight) { y = _pictureHeight - _trainHeight; } _startPosX = x; _startPosY = y; } /// /// Изменение направления перемещения /// /// Направление public void MoveTransport(DirectionType direction) { if (!CanMove(direction) || EntityRoadTrain == null) { return; } switch (direction) { //влево case DirectionType.Left: _startPosX -= (int)EntityRoadTrain.Step; break; //вверх case DirectionType.Up: _startPosY -= (int)EntityRoadTrain.Step; break; // вправо case DirectionType.Right: _startPosX += (int)EntityRoadTrain.Step; break; //вниз case DirectionType.Down: _startPosY += (int)EntityRoadTrain.Step; break; } } public IMoveableObject GetMoveableObject => new DrawningObjectTrain(this); /// /// Прорисовка объекта /// /// public virtual void DrawTransport(Graphics g) { if (EntityRoadTrain == null) { return; } Brush br = new SolidBrush(EntityRoadTrain.BodyColor); Pen pen = new(Color.Black); g.DrawLine(pen, _startPosX + 20, _startPosY + 20, _startPosX + 70, _startPosY + 20); g.DrawEllipse(pen, _startPosX + 20, _startPosY + 20, 10, 10); g.DrawEllipse(pen, _startPosX + 30, _startPosY + 20, 10, 10); g.DrawEllipse(pen, _startPosX + 60, _startPosY + 20, 10, 10); g.DrawRectangle(pen, _startPosX + 60, _startPosY, 10, 20); g.FillEllipse(br, _startPosX + 20, _startPosY + 20, 10, 10); g.FillEllipse(br, _startPosX + 30, _startPosY + 20, 10, 10); g.FillEllipse(br, _startPosX + 60, _startPosY + 20, 10, 10); g.FillRectangle(br, _startPosX + 60, _startPosY, 10, 20); } public void SetBodyColor(Color color) { EntityRoadTrain.SetBodyColor(color); } } }