190 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 DoubleDeckerBus
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningBus
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityBus Bus { 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 _busWidth = 115;
/// <summary>
/// Высота отрисовки автобуса
/// </summary>
private readonly int _busHeight = 50;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автобуса</param>
/// <param name="bodyColor">Цвет кузова</param>
public DrawningBus(int speed, float weight, Color bodyColor)
{
Bus = new EntityBus(speed, weight, bodyColor);
}
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автобуса</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="carWidth">Ширина отрисовки автобуса</param>
/// <param name="carHeight">Высота отрисовки автобуса</param>
protected DrawningBus(int speed, float weight, Color bodyColor, int busWidth, int busHeight) :
this(speed, weight, bodyColor)
{
_busWidth = busWidth;
_busHeight = busHeight;
}
/// <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 && width > 0 && height > 0 && x + _busWidth < width && y + _busHeight < height)
{
_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 + _busWidth + Bus.Step < _pictureWidth)
{
_startPosX += Bus.Step;
}
break;
//влево
case Direction.Left:
if (_startPosX - Bus.Step > 0)
{
_startPosX -= Bus.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - Bus.Step > 0)
{
_startPosY -= Bus.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _busHeight + Bus.Step < _pictureHeight)
{
_startPosY += Bus.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);
//кузов
Brush br = new SolidBrush(Bus?.BodyColor ?? Color.Black);
g.FillRectangle(br, _startPosX, _startPosY, 115, 40);
//граница автобуса
g.DrawRectangle(pen, _startPosX, _startPosY, 115, 40);
//стекла
Brush brBlue = new SolidBrush(Color.LightBlue);
g.FillEllipse(brBlue, _startPosX + 2, _startPosY + 2, 12, 20);
g.FillEllipse(brBlue, _startPosX + 20, _startPosY + 2, 12, 20);
g.FillEllipse(brBlue, _startPosX + 55, _startPosY + 2, 12, 20);
g.FillEllipse(brBlue, _startPosX + 70, _startPosY + 2, 12, 20);
g.FillEllipse(brBlue, _startPosX + 85, _startPosY + 2, 12, 20);
g.FillEllipse(brBlue, _startPosX + 100, _startPosY + 2, 12, 20);
//дверь
g.DrawRectangle(pen, _startPosX + 36, _startPosY + 8, 15, 32);
//колёса
Brush brBlack = new SolidBrush(Color.Black);
g.FillEllipse(brBlack, _startPosX + 14, _startPosY + 30, 18, 20);
g.FillEllipse(brBlack, _startPosX + 85, _startPosY + 30, 18, 20);
}
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _busWidth || _pictureHeight <= _busHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _busWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _busWidth;
}
if (_startPosY + _busHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _busHeight;
}
}
/// <summary>
/// Получение текущей позиции объекта
/// </summary>
/// <returns></returns>
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosX + _busWidth, _startPosY, _startPosY + _busHeight);
}
}
}