202 lines
7.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftCarrier
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
internal class DrawingWarship
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityWarship Ship { protected set; get; }
/// <summary>
/// Левая координата отрисовки корабля
/// </summary>
protected float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки корабля
/// </summary>
protected float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private int? _pictureWidth = null;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private int? _pictureHeight = null;
/// <summary>
/// Ширина отрисовки корабля
/// </summary>
private readonly int _shipWidth = 200;
/// <summary>
/// Высота отрисовки корабля
/// </summary>
private readonly int _shipHeight = 50;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес корабля</param>
/// <param name="bodyColor">Цвет</param>
public DrawingWarship(int speed, float weight, Color bodyColor)
{
Ship = new EntityWarship(speed, weight, bodyColor);
}
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес корабля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="shipWidth">Ширина отрисовки корабля</param>
/// <param name="shipHeight">Высота отрисовки корабля</param>
protected DrawingWarship(int speed, float weight, Color bodyColor, int
shipWidth, int shipHeight) :
this(speed, weight, bodyColor)
{
_shipWidth = shipWidth;
_shipHeight = shipHeight;
}
/// <summary>
/// Установка позиции корабля
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void SetPosition(int x, int y, int width, int height)
{
if (x < 0 || width < x + _shipWidth)
{
_pictureWidth = null;
return;
}
if (y < 0 || height < y + _shipHeight)
{
_pictureHeight = null;
return;
}
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
switch (direction)
{
// вправо
case Direction.Right:
if (_startPosX + _shipWidth + Ship.Step < _pictureWidth)
{
_startPosX += Ship.Step;
}
break;
//влево
case Direction.Left:
if (_startPosX - Ship.Step > 0)
{
_startPosX -= Ship.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - Ship.Step > 0)
{
_startPosY -= Ship.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _shipHeight + Ship.Step < _pictureHeight)
{
_startPosY += Ship.Step;
}
break;
}
}
/// <summary>
/// Отрисовка корабля
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
Pen pen = new(Color.Black);
//границы корабля
g.DrawRectangle(pen, _startPosX + 5, _startPosY - 1, 140, 52);
g.DrawLine(pen, _startPosX + 145, _startPosY + 0, _startPosX + 200, _startPosY + 25);
g.DrawLine(pen, _startPosX + 145, _startPosY + 50, _startPosX + 200, _startPosY + 25);
//Пропеллеры
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, _startPosX, _startPosY + 3, 5, 20);
g.FillRectangle(brBlack, _startPosX, _startPosY + 30, 5, 20);
//Блоки на палубе
g.DrawRectangle(pen, _startPosX + 70, _startPosY + 5, 35, 40);
g.DrawRectangle(pen, _startPosX + 35, _startPosY + 15, 35, 20);
//Труба
Brush br = new SolidBrush(Ship?.BodyColor ?? Color.Black);
g.FillEllipse(br, _startPosX + 120, _startPosY + 15, 20, 20);
}
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _shipWidth || _pictureHeight <= _shipHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _shipWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _shipWidth;
}
if (_startPosY + _shipHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _shipHeight;
}
}
/// <summary>
/// Получение текущей позиции объекта
/// </summary>
/// <returns></returns>
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _shipWidth, _startPosY + _shipHeight);
}
}
}