192 lines
8.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 WarmlyLocomotive
{
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningLocomotive
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityLocomotive Locomotive { get; protected set; }
/// <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 _locomotiveWidth = 210;
/// <summary>
/// Высота отрисовки локомотива
/// </summary>
private readonly int _locomotiveHeight = 105;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес локомотива</param>
/// <param name="bodyColor">Цвет кузова</param>
public DrawningLocomotive(int speed, float weight, Color bodyColor)
{
Locomotive = new EntityLocomotive(speed, weight, bodyColor);
}
protected DrawningLocomotive(int speed, float weight, Color bodyColor, int locomotiveWidth, int locomotiveHeight) : this(speed, weight, bodyColor)
{
_locomotiveWidth = locomotiveWidth;
_locomotiveHeight = locomotiveHeight;
}
/// <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 + _locomotiveWidth <= width && y + _locomotiveHeight <= height && x >= 0 && y >= 0)
{
_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 + _locomotiveWidth + Locomotive.Step < _pictureWidth)
{
_startPosX += Locomotive.Step;
}
break;
//влево
case Direction.Left:
if (_startPosX - Locomotive.Step > 0)
{
_startPosX -= Locomotive.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - Locomotive.Step > 0)
{
_startPosY -= Locomotive.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _locomotiveHeight + Locomotive.Step < _pictureHeight)
{
_startPosY += Locomotive.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(Locomotive?.BodyColor ?? Color.Black);
//кузов
PointF[] polygonPoints = new PointF[4];
polygonPoints[0] = new PointF(_startPosX + 10, _startPosY);
polygonPoints[1] = new PointF(_startPosX + 190, _startPosY);
polygonPoints[2] = new PointF(_startPosX + 210, _startPosY + 40);
polygonPoints[3] = new PointF(_startPosX + 10, _startPosY + 40);
g.FillPolygon(br, polygonPoints);
g.DrawPolygon(pen, polygonPoints);
g.FillRectangle(br, _startPosX + 10, _startPosY + 40, 200, 40);
g.DrawRectangle(pen, _startPosX + 10, _startPosY + 40, 200, 40);
//колеса
Brush brGray = new SolidBrush(Color.Gray);
g.FillEllipse(brGray, _startPosX + 30, _startPosY + 80, 30, 25);
g.FillEllipse(brGray, _startPosX + 70, _startPosY + 80, 30, 25);
g.FillEllipse(brGray, _startPosX + 130, _startPosY + 80, 30, 25);
g.FillEllipse(brGray, _startPosX + 170, _startPosY + 80, 30, 25);
Pen dopPen = new(Color.Black, 2);
g.DrawEllipse(dopPen, _startPosX + 30, _startPosY + 80, 30, 25);
g.DrawEllipse(dopPen, _startPosX + 70, _startPosY + 80, 30, 25);
g.DrawEllipse(dopPen, _startPosX + 130, _startPosY + 80, 30, 25);
g.DrawEllipse(dopPen, _startPosX + 170, _startPosY + 80, 30, 25);
//стекла и дверь
Brush brBrown = new SolidBrush(Color.Brown);
g.FillRectangle(brBrown, _startPosX + 100, _startPosY + 20, 25, 45);
g.DrawRectangle(pen, _startPosX + 100, _startPosY + 20, 25, 45);
pen = new Pen(Color.Blue);
g.DrawRectangle(pen, _startPosX + 20, _startPosY + 10, 15, 20);
g.DrawRectangle(pen, _startPosX + 140, _startPosY + 10, 15, 20);
g.DrawRectangle(pen, _startPosX + 170, _startPosY + 10, 15, 20);
//задняя часть
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, _startPosX, _startPosY + 10, 10, 60);
}
/// <summary>
/// Смена границ формы отрисовки
/// </summary>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
public void ChangeBorders(int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureWidth <= _locomotiveWidth || _pictureHeight <= _locomotiveHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _locomotiveWidth > _pictureWidth)
{
_startPosX = _pictureWidth.Value - _locomotiveWidth;
}
if (_startPosY + _locomotiveHeight > _pictureHeight)
{
_startPosY = _pictureHeight.Value - _locomotiveHeight;
}
}
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (_startPosX, _startPosY, _startPosX + _locomotiveWidth, _startPosY + _locomotiveHeight);
}
}
}