177 lines
6.5 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.Drawing;
using System.Threading.Tasks;
using PIbd_21_Potapov_N.S._Catamaran_Base;
namespace PIbd_21_Potapov_N.S._Catamaran_Base
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
internal class DrawingBoat
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityBoat Boat { private set; get; }
/// <summary>
/// Левая координата отрисовки лодки
/// </summary>
private float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки лодки
/// </summary>
private float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
private int? _pictureWidth = null;
/// <summary>
/// Высота окна отрисовки
/// </summary>
private int? _pictureHeight = null;
/// <summary>
/// Ширина отрисовки лодки
/// </summary>
private readonly int _boatWidth = 100;
/// <summary>
/// Высота отрисовки лодки
/// </summary>
private readonly int _boatHeight = 40;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес лодки</param>
/// <param name="bodyColor">Цвет корпуса</param>
public void Init(int speed, float weight, Color bodyColor)
{
Boat = new EntityBoat();
Boat.Init(speed, weight, bodyColor);
}
/// <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 (width < 0 || height < 0)
{
return;
}
if (x < 0 || y < 0)
{
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 + _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;
}
}
/// <summary>
/// Отрисовка лодки
/// </summary>
/// <param name="g"></param>
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);
}
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
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;
}
}
}
}