ISEbd-21_Filippow_Kirill_Co.../ContainerShip/ContainerShip/DrawningShip.cs

196 lines
7.6 KiB
C#
Raw Normal View History

2023-11-13 13:26:03 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProjectContainerShip;
using ProjectContainerShip.Entities;
using ProjectContainerShip.MovementStrategy;
using static ProjectContainerShip.Direction;
namespace ProjectContainerShip.DrawningObjects
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningShip
{
/// <summary>
/// Получение объекта IMoveableObject из объекта DrawningShip
/// </summary>
public IMoveableObject GetMoveableObject => new DrawningObjectShip(this);
/// <summary>
/// Класс-сущность
/// </summary>
public EntityShip? EntityShip { get; protected set; }
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight;
/// <summary>
/// Левая координата прорисовки корабля
/// </summary>
protected int _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки корабля
/// </summary>
protected int _startPosY;
/// <summary>
/// Ширина прорисовки корабля
/// </summary>
protected readonly int _shipWidth = 200;
/// <summary>
/// Высота прорисовки корабля
/// </summary>
protected readonly int _shipHeight = 30;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public DrawningShip(int speed, double weight, Color bodyColor, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
EntityShip = new EntityShip(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <param name="shipWidth">Ширина прорисовки корабля</param>
/// <param name="shipHeight">Высота прорисовки корабля</param>
protected DrawningShip(int speed, double weight, Color bodyColor, int width, int height, int shipWidth, int shipHeight)
{
if (width > _shipWidth && height > _shipHeight)
{
_pictureWidth = width;
_pictureHeight = height;
_shipWidth = shipWidth;
_shipHeight = shipHeight;
EntityShip = new EntityShip(speed, weight, bodyColor);
}
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if ((x > 0) && (x < _pictureWidth))
_startPosX = x;
else _startPosX = 0;
if ((y > 0) && (y < _pictureHeight))
_startPosY = y;
else _startPosY = 0;
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Координата X объекта
/// </summary>
public int GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _shipWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _shipHeight;
/// <summary>
/// Проверка, что объект может переместится по указанному направлению
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - можно переместится по указанному направлению</returns>
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 + _shipWidth + EntityShip.Step < _pictureWidth,
//вниз
DirectionType.Down => _startPosY + _shipHeight + EntityShip.Step < _pictureHeight,
};
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(DirectionType direction)
{
if (!CanMove(direction) || EntityShip == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
_startPosX -= (int)EntityShip.Step;
2023-12-22 13:40:17 +04:00
break;
2023-11-13 13:26:03 +04:00
//вверх
case DirectionType.Up:
_startPosY -= (int)EntityShip.Step;
2023-12-22 13:40:17 +04:00
break;
2023-11-13 13:26:03 +04:00
// вправо
case DirectionType.Right:
_startPosX += (int)EntityShip.Step;
2023-12-22 13:40:17 +04:00
break;
2023-11-13 13:26:03 +04:00
//вниз
case DirectionType.Down:
_startPosY += (int)EntityShip.Step;
2023-12-22 13:40:17 +04:00
break;
2023-11-13 13:26:03 +04:00
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
2023-12-22 13:40:17 +04:00
{
2023-11-13 13:26:03 +04:00
if (EntityShip == null)
{
return;
2023-12-22 13:40:17 +04:00
}
2023-12-06 18:54:22 +04:00
Brush bodyColor = new SolidBrush(EntityShip.BodyColor);
2023-11-13 13:26:03 +04:00
//границы корабля
2023-12-06 13:45:35 +04:00
Brush brRd = new SolidBrush(Color.Red);
2023-11-13 13:26:03 +04:00
Point point1 = new Point(_startPosX, _startPosY + 12);
Point point2 = new Point(_startPosX + 25, _startPosY + 35);
Point point3 = new Point(_startPosX + 175, _startPosY + 35);
Point point4 = new Point(_startPosX + 200, _startPosY + 12);
Point[] curvePoints1 = { point1, point2, point3, point4 };
2023-12-06 18:54:22 +04:00
g.FillPolygon(bodyColor, curvePoints1);
2023-11-13 13:26:03 +04:00
//граница палубы
Brush brBlue = new SolidBrush(Color.Blue);
2023-12-06 18:54:22 +04:00
g.FillRectangle(bodyColor, _startPosX + 35, _startPosY + 0, 16, 12);
2023-11-13 13:26:03 +04:00
}
}
}