2022-11-04 15:18:56 +04:00

191 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 Boats
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawingBoat
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityBoat Boat { 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>
protected readonly int _boatWidth = 100;
/// <summary>
/// Высота отрисовки лодки
/// </summary>
protected readonly int _boatHeight = 40;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес лодки</param>
/// <param name="bodyColor">Цвет корпуса</param>
public DrawingBoat(int speed, float weight, Color bodyColor)
{
Boat = new EntityBoat(speed, weight, bodyColor);
}
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес лодки</param>
/// <param name="bodyColor">Цвет корпуса</param>
/// <param name="boatWidth">Ширина отрисовки лодки</param>
/// <param name="boatHeight">Высота отрисовки лодки</param>
protected DrawingBoat(int speed, float weight, Color bodyColor, int boatWidth, int boatHeight) :
this(speed, weight, bodyColor)
{
_boatWidth = boatWidth;
_boatHeight = boatHeight;
}
/// <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 && x < width - _boatWidth && y > 0 && y < height - _boatHeight)
{
_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 virtual 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;
}
}
/// <summary>
/// Получение текущей позиции объекта
/// </summary>
/// <returns></returns>
public (float Left, float Top, float Right, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _boatWidth, _startPosY + _boatHeight);
}
}
}