2022-11-07 10:00:07 +04:00

193 lines
7.0 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 Ship
{
public class DrawingShip
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityShip Ship { 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>
private readonly int _shipWidth = 80;
/// <summary>
/// Высота отрисовки корабля
/// </summary>
private readonly int _shipHeight = 30;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес корабля</param>
/// <param name="bodyColor">Цвет корпуса</param>
public DrawingShip(int speed, float weight, Color bodyColor)
{
Ship = new EntityShip(speed, weight, bodyColor);
}
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес корабля</param>
/// <param name="bodyColor">Цвет корпуса</param>
/// <param name="carWidth">Ширина отрисовки корабля</param>
/// <param name="carHeight">Высота отрисовки корабля</param>
protected DrawingShip(int speed, float weight, Color bodyColor, int shipWidth, int shipHeight) : this(speed, weight, bodyColor)
{
_shipWidth = shipWidth;
_shipHeight = shipHeight;
}
/// <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 + _shipWidth >= width)
{
return;
}
if (y < 0 || y + _shipHeight >= height)
{
return;
}
// TODO проверки
_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 + _shipWidth + Ship.Step < _pictureWidth)
{
_startPosX += Ship.Step;
}
else _startPosX = (float)_pictureWidth - _shipWidth;
break;
//влево
case Direction.Left:
if (_startPosX - Ship.Step > 0)
{
_startPosX -= Ship.Step;
}
else _startPosX = 0;
break;
//вверх
case Direction.Up:
if (_startPosY - Ship.Step > 0)
{
_startPosY -= Ship.Step;
}
else _startPosY = 0;
break;
//вниз
case Direction.Down:
if (_startPosY + _shipHeight + Ship.Step < _pictureHeight)
{
_startPosY += Ship.Step;
}
else _startPosY = (float)_pictureHeight - _shipHeight;
break;
}
}
/// <summary>
/// Отрисовка автомобиля
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (_startPosX < 0 || _startPosY < 0
|| !_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
//корпус
Brush br = new SolidBrush(Ship?.BodyColor ?? Color.Black);
Point[] points = { new Point((int)_startPosX, (int)_startPosY + 10),
new Point((int)_startPosX + 80, (int)_startPosY + 10),
new Point((int)_startPosX + 70, (int)_startPosY + 30),
new Point((int)_startPosX + 10, (int)_startPosY + 30)
};
g.FillPolygon(br, points);
//груз
Pen pen = new(Color.Black);
g.DrawRectangle(pen, (int)_startPosX + 20, (int)_startPosY, 50, 10);
}
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _shipWidth || _pictureHeight <= _shipHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _shipWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _shipWidth;
}
if (_startPosY + _shipHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _shipHeight;
}
}
virtual public void SetColor(Color color)
{
Ship = new EntityShip(Ship.Speed, Ship.Weight, color);
}
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosX + _shipWidth, _startPosY, _startPosY + _shipHeight);
}
}
}