2024-02-07 08:29:34 +04:00

159 lines
5.9 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.

namespace Seaplane;
/// <summary>
/// Класс отвечающий за прорисовку и перемещение объекта - сущности
/// </summary>
public class DrawingSeaplane
{
/// <summary>
/// Класс - сущность
/// </summary>
public EntitySeaplane? EntitySeaplane { get; set; }
/// <summary>
/// Ширина окна
/// </summary>
private int? _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int? _pictureHeight;
/// <summary>
/// Левая координата прорисовки гидросамолета
/// </summary>
private int? _startPosX;
/// <summary>
/// Верхняя координата прорисовки гидросамолета
/// </summary>
private int? _startPosY;
/// <summary>
/// Ширина прорисовки гидросамолета
/// </summary>
private readonly int _drawningPlaneWidth = 0;
/// <summary>
/// Высота прорисовки гидросамолета
/// </summary>
private readonly int _drawningPlaneHeight = 0;
/// <summary>
/// Инициализация полей объекта-класса гидросамолета
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bodyKit">Наличие обвеса</param>
/// <param name="floats">Наличие поплавков</param>
/// <param name="inflatableBoat">Наличие надувной лодки</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bodyKit, bool wing, bool sportLine)
{
EntitySeaplane = new EntitySeaplane();
EntitySeaplane.Init(speed, weight, bodyColor, additionalColor, bodyKit, wing, sportLine);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
public bool SetPictureSize(int width, int height)
{
// TODO проверка, что объект "влезает" в размеры поля
// если влезает, сохраняем границы и корректируем позицию объекта,если она была уже установлена
if (width < _drawningPlaneWidth || height < _drawningPlaneHeight) return false;
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null || _startPosY != null)
{
if (_startPosX + _drawningPlaneWidth > _pictureWidth)
{
_startPosX = _startPosX - (_startPosX + _drawningPlaneWidth - _pictureWidth);
}
else if (_startPosX < 0) _startPosX = 0;
if (_startPosY + _drawningPlaneHeight > _pictureHeight)
{
_startPosY = _startPosY - (_startPosY + _drawningPlaneHeight - _pictureHeight);
}
else if (_startPosY < 0) _startPosY = 0;
}
return true;
}
/// <summary>
/// Установка позиций
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
// то надо изменить координаты, чтобы он оставался в этих границах
if (x + _drawningPlaneWidth > _pictureWidth)
{
_startPosX = x - (x + _drawningPlaneWidth - _pictureWidth);
}
else if (x < 0) _startPosX = 0;
else _startPosX = x;
if (y + _drawningPlaneHeight > _pictureHeight)
{
_startPosY = y - (y + _drawningPlaneHeight - _pictureHeight);
}
else if (y < 0) _startPosY = 0;
else _startPosY = y;
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
public bool MoveTransport(DirectionType direction)
{
if (EntitySeaplane == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntitySeaplane.Step > 0)
{
_startPosX -= (int)EntitySeaplane.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntitySeaplane.Step > 0)
{
_startPosY -= (int)EntitySeaplane.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + _drawningPlaneWidth + EntitySeaplane.Step < _pictureWidth)
{
_startPosX += (int)EntitySeaplane.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + _drawningPlaneHeight + EntitySeaplane.Step < _pictureHeight)
{
_startPosY += (int)EntitySeaplane.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics g)
{
if (EntitySeaplane == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
}
}