243 lines
8.9 KiB
C#
243 lines
8.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using WarmlyShip.Entities;
|
||
using WarmlyShip.MovementStrategy;
|
||
|
||
namespace WarmlyShip.DrawningObjects
|
||
{
|
||
/// <summary>
|
||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||
/// </summary>
|
||
public class DrawningShip
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityShip? EntityShip { get; protected set; }
|
||
|
||
/// <summary>
|
||
/// Получение объекта IMoveableObject из объекта DrawningShip
|
||
/// </summary>
|
||
public IMoveableObject GetMoveableObject => new DrawningObjectShip(this);
|
||
|
||
/// <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 = 140;
|
||
/// <summary>
|
||
/// Высота прорисовки корабля
|
||
/// </summary>
|
||
protected readonly int _shipHeight = 60;
|
||
/// <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="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)
|
||
{
|
||
if (width < _shipWidth || height < _shipHeight)
|
||
{
|
||
return;
|
||
}
|
||
_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)
|
||
{
|
||
return;
|
||
}
|
||
_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 + _shipWidth > _pictureWidth)
|
||
{
|
||
x = Math.Max(0, _pictureWidth - _shipWidth);
|
||
}
|
||
if (y < 0 || y + _shipHeight > _pictureHeight)
|
||
{
|
||
y = Math.Max(0, _pictureHeight - _shipHeight);
|
||
}
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
}
|
||
|
||
/// <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,
|
||
_ => false,
|
||
};
|
||
}
|
||
|
||
/// <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;
|
||
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;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public virtual void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityShip == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
Pen pen = new(Color.Black);
|
||
Brush BodyBrush = new SolidBrush(EntityShip.BodyColor);
|
||
|
||
// корпус
|
||
Point point1 = new Point(_startPosX, _startPosY + 30);
|
||
Point point2 = new Point(_startPosX + 140, _startPosY + 30);
|
||
Point point3 = new Point(_startPosX + 120, _startPosY + 60);
|
||
Point point4 = new Point(_startPosX + 20, _startPosY + 60);
|
||
Point[] curvePoints = { point1, point2, point3, point4 };
|
||
g.FillPolygon(BodyBrush, curvePoints);
|
||
g.DrawPolygon(pen, curvePoints);
|
||
// палуба
|
||
Brush brBeige = new SolidBrush(Color.Beige);
|
||
g.FillRectangle(brBeige, _startPosX + 10, _startPosY + 20, 110, 10);
|
||
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 20, 110, 10);
|
||
// якорь
|
||
Point point_1 = new Point(_startPosX + 130, _startPosY + 40);
|
||
Point point_2 = new Point(_startPosX + 130, _startPosY + 33);
|
||
g.DrawLine(pen, point_1, point_2);
|
||
Point point_3 = new Point(_startPosX + 130, _startPosY + 40);
|
||
Point point_4 = new Point(_startPosX + 127, _startPosY + 37);
|
||
g.DrawLine(pen, point_3, point_4);
|
||
Point point_5 = new Point(_startPosX + 130, _startPosY + 40);
|
||
Point point_6 = new Point(_startPosX + 133, _startPosY + 37);
|
||
g.DrawLine(pen, point_5, point_6);
|
||
Point point_7 = new Point(_startPosX + 129, _startPosY + 35);
|
||
Point point_8 = new Point(_startPosX + 131, _startPosY + 35);
|
||
g.DrawLine(pen, point_7, point_8);
|
||
}
|
||
|
||
public void SetColor(Color color)
|
||
{
|
||
if (EntityShip == null)
|
||
{
|
||
return;
|
||
}
|
||
EntityShip.BodyColor = color;
|
||
}
|
||
|
||
public void ChangePictureBoxSize(int pictureBoxWidth, int pictureBoxHeight)
|
||
{
|
||
_pictureWidth = pictureBoxWidth;
|
||
_pictureHeight = pictureBoxHeight;
|
||
}
|
||
}
|
||
} |