using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using PIbd_21_Potapov_N.S._Catamaran_Base;
namespace PIbd_21_Potapov_N.S._Catamaran_Base
{
///
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
///
internal class DrawingBoat
{
///
/// Класс-сущность
///
public EntityBoat Boat { private set; get; }
///
/// Левая координата отрисовки лодки
///
private float _startPosX;
///
/// Верхняя кооридната отрисовки лодки
///
private float _startPosY;
///
/// Ширина окна отрисовки
///
private int? _pictureWidth = null;
///
/// Высота окна отрисовки
///
private int? _pictureHeight = null;
///
/// Ширина отрисовки лодки
///
private readonly int _boatWidth = 100;
///
/// Высота отрисовки лодки
///
private readonly int _boatHeight = 40;
///
/// Инициализация свойств
///
/// Скорость
/// Вес лодки
/// Цвет корпуса
public void Init(int speed, float weight, Color bodyColor)
{
Boat = new EntityBoat();
Boat.Init(speed, weight, bodyColor);
}
///
/// Установка позиции лодки
///
/// Координата X
/// Координата Y
/// Ширина картинки
/// Высота картинки
public void SetPosition(int x, int y, int width, int height)
{
// Проверки, что новая позиция и размеры объекта валидны
if (width < 0 || height < 0 || width > _pictureWidth || height > _pictureHeight)
{
return;
}
if (x < 0 || y < 0 || x >= _pictureWidth - width || y >= _pictureHeight - height)
{
return;
}
_startPosX = x;
_startPosY = y;
_pictureWidth = width;
_pictureHeight = height;
}
///
/// Изменение направления перемещения
///
/// Направление
public void MoveTransport(Direction direction)
{
if (!_pictureWidth.HasValue || !_pictureHeight.HasValue)
{
return;
}
switch (direction)
{
// вправо
case Direction.Right:
if (_startPosX + _boatWidth + Boat.Step < _pictureWidth)
{
_startPosX += Boat.Step;
}
break;
//влево
case Direction.Left:
if (_startPosX - Boat.Step > 0)
{
_startPosX -= Boat.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - Boat.Step > 0)
{
_startPosY -= Boat.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _boatHeight + Boat.Step < _pictureHeight)
{
_startPosY += Boat.Step;
}
break;
}
}
///
/// Отрисовка лодки
///
///
public void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
SolidBrush brush = new SolidBrush(Boat.BodyColor);
PointF[] bodyPoints = new PointF[5];
bodyPoints[0] = new PointF(_startPosX, _startPosY);
bodyPoints[1] = new PointF(_startPosX + _boatWidth - _boatWidth / 4, _startPosY);
bodyPoints[2] = new PointF(_startPosX + _boatWidth, _startPosY + _boatHeight / 2);
bodyPoints[3] = new PointF(_startPosX + _boatWidth - _boatWidth / 4, _startPosY + _boatHeight);
bodyPoints[4] = new PointF(_startPosX, _startPosY + _boatHeight);
// Отрисовка корпуса лодки
g.FillPolygon(brush, bodyPoints);
g.DrawPolygon(Pens.Black, bodyPoints);
// Отрисовка головы лодки
g.FillEllipse(Brushes.White, _startPosX + _boatWidth / 8, _startPosY + _boatHeight / 8,
_boatWidth / 2, _boatHeight - _boatHeight / 4);
g.DrawEllipse(Pens.Black, _startPosX + _boatWidth / 8, _startPosY + _boatHeight / 8,
_boatWidth / 2, _boatHeight - _boatHeight / 4);
}
///
/// Смена границ формы отрисовки
///
/// Ширина картинки
/// Высота картинки
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _boatWidth || _pictureHeight <= _boatHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _boatWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _boatWidth;
}
if (_startPosY + _boatHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _boatHeight;
}
}
}
}