PIbd-23_Dolgova_D.N._Warmly.../WarmlyShip/DrawningObjects/DrawningShip.cs

243 lines
8.9 KiB
C#
Raw Normal View History

2023-11-24 22:12:36 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-12-01 18:41:28 +04:00
using WarmlyShip.Entities;
2023-12-01 21:18:58 +04:00
using WarmlyShip.MovementStrategy;
2023-11-24 22:12:36 +04:00
2023-12-01 18:41:28 +04:00
namespace WarmlyShip.DrawningObjects
2023-11-24 22:12:36 +04:00
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
2023-12-01 18:41:28 +04:00
public class DrawningShip
2023-11-24 22:12:36 +04:00
{
/// <summary>
/// Класс-сущность
/// </summary>
2023-12-01 18:41:28 +04:00
public EntityShip? EntityShip { get; protected set; }
2023-12-01 21:18:58 +04:00
/// <summary>
/// Получение объекта IMoveableObject из объекта DrawningShip
/// </summary>
public IMoveableObject GetMoveableObject => new DrawningObjectShip(this);
2023-11-24 22:12:36 +04:00
/// <summary>
/// Ширина окна
/// </summary>
private int _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int _pictureHeight;
/// <summary>
/// Левая координата прорисовки корабля
/// </summary>
2023-12-01 18:41:28 +04:00
protected int _startPosX;
2023-11-24 22:12:36 +04:00
/// <summary>
/// Верхняя кооридната прорисовки корабля
/// </summary>
2023-12-01 18:41:28 +04:00
protected int _startPosY;
2023-11-24 22:12:36 +04:00
/// <summary>
/// Ширина прорисовки корабля
/// </summary>
2023-12-01 18:41:28 +04:00
protected readonly int _shipWidth = 140;
2023-11-24 22:12:36 +04:00
/// <summary>
/// Высота прорисовки корабля
/// </summary>
2023-12-01 18:41:28 +04:00
protected readonly int _shipHeight = 60;
2023-11-24 22:12:36 +04:00
/// <summary>
2023-12-01 18:41:28 +04:00
/// Координата 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>
/// Конструктор
2023-11-24 22:12:36 +04:00
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
2023-12-01 18:41:28 +04:00
/// <param name="bodyColor">Основной цвет</param>
2023-11-24 22:12:36 +04:00
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
2023-12-01 18:41:28 +04:00
public DrawningShip(int speed, double weight, Color bodyColor, int
width, int height)
2023-11-24 22:12:36 +04:00
{
if (width < _shipWidth || height < _shipHeight)
{
2023-12-01 18:41:28 +04:00
return;
2023-11-24 22:12:36 +04:00
}
_pictureWidth = width;
_pictureHeight = height;
2023-12-01 18:41:28 +04:00
EntityShip = new EntityShip(speed, weight, bodyColor);
}
2023-11-24 22:12:36 +04:00
2023-12-01 18:41:28 +04:00
/// <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);
2023-11-24 22:12:36 +04:00
}
2023-12-01 18:41:28 +04:00
2023-11-24 22:12:36 +04:00
/// <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;
}
2023-12-01 18:41:28 +04:00
/// <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,
};
}
2023-11-24 22:12:36 +04:00
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(DirectionType direction)
{
2023-12-01 18:41:28 +04:00
if (!CanMove(direction) || EntityShip == null)
2023-11-24 22:12:36 +04:00
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
2023-12-01 18:41:28 +04:00
_startPosX -= (int)EntityShip.Step;
2023-11-24 22:12:36 +04:00
break;
//вверх
case DirectionType.Up:
2023-12-01 18:41:28 +04:00
_startPosY -= (int)EntityShip.Step;
2023-11-24 22:12:36 +04:00
break;
// вправо
case DirectionType.Right:
2023-12-01 18:41:28 +04:00
_startPosX += (int)EntityShip.Step;
2023-11-24 22:12:36 +04:00
break;
//вниз
case DirectionType.Down:
2023-12-01 18:41:28 +04:00
_startPosY += (int)EntityShip.Step;
2023-11-24 22:12:36 +04:00
break;
}
}
2023-12-01 18:41:28 +04:00
2023-11-24 22:12:36 +04:00
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
2023-12-01 18:41:28 +04:00
public virtual void DrawTransport(Graphics g)
2023-11-24 22:12:36 +04:00
{
2023-12-01 18:41:28 +04:00
if (EntityShip == null)
2023-11-24 22:12:36 +04:00
{
return;
}
2023-12-01 18:41:28 +04:00
2023-11-24 22:12:36 +04:00
Pen pen = new(Color.Black);
2023-12-01 18:41:28 +04:00
Brush BodyBrush = new SolidBrush(EntityShip.BodyColor);
2023-11-24 22:12:36 +04:00
// корпус
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);
}
2023-12-10 23:51:40 +04:00
public void SetColor(Color color)
{
if (EntityShip == null)
{
return;
}
EntityShip.BodyColor = color;
}
public void ChangePictureBoxSize(int pictureBoxWidth, int pictureBoxHeight)
{
_pictureWidth = pictureBoxWidth;
_pictureHeight = pictureBoxHeight;
}
2023-11-24 22:12:36 +04:00
}
2023-12-01 18:41:28 +04:00
}