Pibd-22_Presnyakova.V.V_Cat.../Catamaran/DrawingBoat.cs
2022-11-07 01:27:11 +04:00

197 lines
7.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.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Catamaran
{
public class DrawingBoat
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityBoat Catamaran { set; get; }
/// <summary>
/// Левая координата отрисовки лодки
/// </summary>
protected float _startPosX;
/// <summary>
/// Верхняя кооридната отрисовки лодки
/// </summary>
protected float _startPosY;
/// <summary>
/// Ширина окна отрисовки
/// </summary>
protected int? _pictureWidth = null;
/// <summary>
/// Высота окна отрисовки
/// </summary>
protected int? _pictureHeight = null;
/// <summary>
/// Ширина отрисовки лодки
/// </summary>
protected readonly int _catamaranWidth = 80;
/// <summary>
/// Высота отрисовки лодки
/// </summary>
protected readonly int _catamaranHeight = 50;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес лодки</param>
/// <param name="bodyColor">Цвет лодки</param>
public DrawingBoat(int speed, float weight, Color bodyColor)
{
Catamaran = new EntityBoat(speed, weight, bodyColor);
}
public DrawingBoat Copy(int? speed = null, float? weight = null, Color? bodyColor = null)
{
return new DrawingBoat(speed ?? Catamaran.Speed, weight ?? Catamaran.Weight, bodyColor ?? Catamaran.BodyColor);
}
/// <summary>
/// Получение текущей позиции объекта
/// </summary>
/// <returns></returns>
public (float Left, float Right, float Top, float Bottom)
GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _catamaranWidth, _startPosY +
_catamaranHeight);
}
/// <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="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="carWidth">Ширина отрисовки автомобиля</param>
/// <param name="carHeight">Высота отрисовки автомобиля</param>
protected DrawingBoat(int speed, float weight, Color bodyColor, int
catamaranWidth, int catamaranHeight) :
this(speed, weight, bodyColor)
{
_catamaranWidth = catamaranWidth;
_catamaranHeight = catamaranHeight;
}
/// <summary>
/// Отрисовка лодки
/// </summary>
/// <param name="g"></param>
public virtual 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 - 11);
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillEllipse(brBlue, _startPosX + 5, _startPosY + 5, _catamaranWidth - 15, _catamaranHeight - 11);
}
/// <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;
}
}
}
}