PIbd-21_Valitov_D.F_Sailboa.../Sailboat/DrawingBoat.cs
2023-04-14 14:36:46 +04:00

178 lines
6.6 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 Sailboat
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawingBoat
{
/// <summary>
/// Класс-сущность
/// </summary>
public Boat 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>
private readonly int _boatWidth = 125;
/// <summary>
/// Высота отрисовки автомобиля
/// </summary>
private 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 Boat(speed, weight, bodyColor);
}
public DrawingBoat(int speed, float weight, Color bodyColor, int boatHeight, int boatWith):
this(speed, weight, bodyColor)
{
_boatHeight = boatHeight;
_boatWidth = boatWith;
}
/// <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 < width - _boatWidth && 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 + ((int)Boat.Step) < _pictureWidth)
{
_startPosX += ((int)Boat.Step);
}
break;
//влево
case Direction.Left:
if (_startPosX > ((int)Boat.Step))
{
_startPosX -= ((int)Boat.Step);
}
break;
//вверх
case Direction.Up:
if (_startPosY > ((int)Boat.Step))
{
_startPosY -= ((int)Boat.Step);
}
break;
//вниз
case Direction.Down:
if (_startPosY + _boatHeight + ((int)Boat.Step) < _pictureHeight)
{
_startPosY += ((int)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;
}
Pen pen = new(Color.Black);
//корма
g.DrawLine(pen, _startPosX, _startPosY, _startPosX + 70, _startPosY);
g.DrawLine(pen, _startPosX, _startPosY, _startPosX, _startPosY+40);
g.DrawLine(pen, _startPosX, _startPosY + 40, _startPosX + 70, _startPosY+40);
//нос
g.DrawLine(pen, _startPosX + 70, _startPosY + 40, _startPosX + 120, _startPosY+20);
g.DrawLine(pen, _startPosX + 70, _startPosY, _startPosX + 120, _startPosY+20);
//внутренность
Rectangle rect = new Rectangle((int)_startPosX + 5, (int)_startPosY + 5, 60, 30);
g.DrawRectangle(pen, rect);
Brush br = new SolidBrush(Boat.BodyColor);
g.FillRectangle(br, rect);
}
/// <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 Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _boatWidth, _startPosY + _boatHeight);
}
}
}