using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Battleship.Entities; using Battleship.MovementStrategy; namespace Battleship.DrawningObjects { /// /// Класс, отвечающий за прорисовку и перемещение объекта-сущности /// public class DrawningShip { /// /// Класс-сущность /// public EntityShip? EntityShip { get; protected set; } /// /// Ширина окна /// public int _pictureWidth; /// /// Высота окна /// public int _pictureHeight; /// /// Левая координата прорисовки /// protected int _startPosX; /// /// Верхняя кооридната прорисовки /// protected int _startPosY; /// /// Ширина прорисовки /// protected readonly int _buttleshipWidth = 175; /// /// Высота прорисовки /// protected readonly int _buttleshipHeight = 80; /// /// Инициализация свойств /// /// Скорость /// Вес /// Цвет основы /// Ширина картинки /// Высота картинки /// true - объект создан, false - проверка не пройдена, ///нельзя создать объект в этих размерах /// /// Получение объекта IMoveableObject из объекта DrawningCar /// public IMoveableObject GetMoveableObject => new DrawningObjectShip(this); public DrawningShip(int speed, double weight, Color bodyColor, int width, int height) { if (width < _buttleshipWidth || height < _buttleshipHeight) { return; } _pictureWidth = width; _pictureHeight = height; EntityShip = new EntityShip(speed, weight, bodyColor); } /// /// Конструктор /// /// Скорость /// Вес /// Основной цвет /// Ширина картинки /// Высота картинки /// Ширина прорисовки автомобиля /// Высота прорисовки автомобиля protected DrawningShip(int speed, double weight, Color bodyColor, int width, int height, int buttleshipWidth, int buttleshipHeight) { if (width <= _buttleshipWidth || height <= _buttleshipHeight) return; _pictureWidth = width; _pictureHeight = height; _buttleshipWidth = buttleshipWidth; _buttleshipHeight = buttleshipHeight; EntityShip = new EntityShip(speed, weight, bodyColor); } /// /// Установка позиции /// /// Координата X /// Координата Y public void SetPosition(int x, int y) { if (x >= 0 && x + _buttleshipWidth <= _pictureWidth && y >= 0 && y + _buttleshipHeight <= _pictureHeight) { _startPosX = x; _startPosY = y; } } /// /// Изменение направления перемещения /// /// Направление /// /// Прорисовка объекта /// /// public virtual void DrawTransport(Graphics g) { if (EntityShip == null) { return; } Pen pen = new(Color.Black, 2); //основа Brush mainBrush = new SolidBrush(EntityShip.BodyColor); Point[] hull = new Point[] { new Point(_startPosX + 5, _startPosY + 0), new Point(_startPosX + 120, _startPosY + 0), new Point(_startPosX + 160, _startPosY + 35), new Point(_startPosX + 120, _startPosY + 70), new Point(_startPosX + 5, _startPosY + 70), }; g.FillPolygon(mainBrush, hull); g.DrawPolygon(pen, hull); //блоки Brush blockBrush = new SolidBrush(Color.DimGray); g.FillRectangle(blockBrush, _startPosX + 70, _startPosY + 15, 20, 40); g.DrawRectangle(pen, _startPosX + 70, _startPosY + 15, 20, 40); g.FillRectangle(blockBrush, _startPosX + 40, _startPosY + 25, 30, 20); g.DrawRectangle(pen, _startPosX + 40, _startPosY + 25, 30, 20); g.FillEllipse(blockBrush, _startPosX + 100, _startPosY + 20, 30, 30); g.DrawEllipse(pen, _startPosX + 100, _startPosY + 20, 30, 30); //для ускорения Brush speedBrush = new SolidBrush(Color.Gold); g.FillRectangle(speedBrush, _startPosX + 0, _startPosY + 10, 5, 20); g.DrawRectangle(pen, _startPosX + 0, _startPosY + 10, 5, 20); g.FillRectangle(speedBrush, _startPosX + 0, _startPosY + 40, 5, 20); g.DrawRectangle(pen, _startPosX + 0, _startPosY + 40, 5, 20); } /// /// Координата X объекта /// public int GetPosX => _startPosX; /// /// Координата Y объекта /// /// public int GetPosY => _startPosY; /// /// Ширина объекта /// public int GetWidth => _buttleshipWidth; /// /// Высота объекта /// public int GetHeight => _buttleshipHeight; /// /// Проверка, что объект может переместится по указанному направлению /// /// Направление /// true - можно переместится по указанному направлению public bool CanMove(DirectionType direction) { if (EntityShip == null) { return false; } return direction switch { //влево DirectionType.Left => _startPosX - EntityShip.Step > 0, //вверх DirectionType.Up => _startPosY - EntityShip.Step > 0, //вправо DirectionType.Right => _startPosX + EntityShip.Step + _buttleshipWidth < _pictureWidth, //вниз DirectionType.Down => _startPosY + EntityShip.Step + _buttleshipHeight < _pictureHeight, _ => false, }; } /// /// Изменение направления перемещения /// /// Направление public void MoveTransport(DirectionType direction) { if (!CanMove(direction) || EntityShip == null) { return; } switch (direction) { //влево case DirectionType.Left: _startPosX -= (int)EntityShip.Step; break; //вверх case DirectionType.Up: _startPosY -= (int)EntityShip.Step; break; // вправо case DirectionType.Right: _startPosX += (int)EntityShip.Step; break; //вниз case DirectionType.Down: _startPosY += (int)EntityShip.Step; break; } } } }