Pibd-22_Presnyakova.V.V_Cat.../Catamaran/DrawingCatamaran.cs

165 lines
6.2 KiB
C#
Raw Permalink Normal View History

2022-09-25 11:25:29 +04:00
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
internal class DrawingCatamaran
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityCatamaran Catamaran { 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 _catamaranWidth = 80;
/// <summary>
/// Высота отрисовки лодки
/// </summary>
private readonly int _catamaranHeight = 50;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес лодки</param>
/// <param name="bodyColor">Цвет лодки</param>
public void Init(int speed, float weight, Color bodyColor)
{
Catamaran = new EntityCatamaran();
Catamaran.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 (x < 0 || y < 0 || x + _catamaranWidth > width || y + _catamaranHeight > height) 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 + _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;
}
}
/// <summary>
/// Отрисовка лодки
/// </summary>
/// <param name="g"></param>
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);
}
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
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;
}
}
}
}