using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal class DrawingCatamaran
{
///
/// Класс-сущность
///
public EntityCatamaran Catamaran { private set; get; }
///
/// Левая координата отрисовки лодки
///
private float _startPosX;
///
/// Верхняя кооридната отрисовки лодки
///
private float _startPosY;
///
/// Ширина окна отрисовки
///
private int? _pictureWidth = null;
///
/// Высота окна отрисовки
///
private int? _pictureHeight = null;
///
/// Ширина отрисовки лодки
///
private readonly int _catamaranWidth = 80;
///
/// Высота отрисовки лодки
///
private readonly int _catamaranHeight = 50;
///
/// Инициализация свойств
///
/// Скорость
/// Вес лодки
/// Цвет лодки
public void Init(int speed, float weight, Color bodyColor)
{
Catamaran = new EntityCatamaran();
Catamaran.Init(speed, weight, bodyColor);
}
///
/// Установка позиции лодки
///
/// Координата X
/// Координата Y
/// Ширина картинки
/// Высота картинки
public void SetPosition(int x, int y, int width, int height)
{
if (x < 0 || y < 0 || x + _catamaranWidth > width || y + _catamaranHeight > 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 + _catamaranWidth + Catamaran.Step < _pictureWidth)
{
_startPosX += Catamaran.Step;
}
break;
//влево
case Direction.Left:
if (_startPosX - Catamaran.Step > 0)
{
_startPosX -= Catamaran.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - Catamaran.Step > 0)
{
_startPosY -= Catamaran.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _catamaranHeight + Catamaran.Step < _pictureHeight)
{
_startPosY += Catamaran.Step;
}
break;
}
}
///
/// Отрисовка лодки
///
///
public void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
Pen pen = new Pen(Color.Black);
//границы
g.DrawRectangle(pen, _startPosX, _startPosY, _catamaranWidth * 3 / 4, _catamaranHeight);
Point point_1 = new Point((int)(_startPosX + _catamaranWidth * 3 / 4), (int)_startPosY);
Point point_2 = new Point((int)(_startPosX + _catamaranWidth), (int)(_startPosY + _catamaranHeight / 2));
Point point_3 = new Point((int)(_startPosX + _catamaranWidth * 3 / 4), (int)(_startPosY + _catamaranHeight));
Point[] pointsArray = {point_1, point_2, point_3};
g.DrawPolygon(pen, pointsArray);
Brush br = new SolidBrush(Catamaran?.BodyColor ?? Color.Black);
g.FillRectangle(br, _startPosX, _startPosY, _catamaranWidth * 3 / 4, _catamaranHeight);
g.FillPolygon(br, pointsArray);
// середина
g.DrawEllipse(pen, _startPosX + 5, _startPosY + 5, _catamaranWidth - 15, _catamaranHeight - 15);
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillEllipse(brBlue, _startPosX + 5, _startPosY + 5, _catamaranWidth - 15, _catamaranHeight - 15);
}
///
/// Смена границ формы отрисовки
///
/// Ширина картинки
/// Высота картинки
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _catamaranWidth || _pictureHeight <= _catamaranHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _catamaranWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _catamaranWidth;
}
if (_startPosY + _catamaranHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _catamaranHeight;
}
}
}
}