241 lines
10 KiB
C#
241 lines
10 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace WarmlyLocomotive
|
||
{
|
||
public class EntityWarmlyLocomotive
|
||
{
|
||
/// <summary>
|
||
/// Скорость
|
||
/// </summary>
|
||
public int Speed { get; set; }
|
||
/// <summary>
|
||
/// Вес
|
||
/// </summary>
|
||
public double Weight { get; private set; }
|
||
/// <summary>
|
||
/// Основной цвет
|
||
/// </summary>
|
||
public Color BodyColor { get; private set; }
|
||
/// <summary>
|
||
/// Дополнительный цвет (для опциональных элементов)
|
||
/// </summary>
|
||
public Color AdditionalColor { get; private set; }
|
||
/// <summary>
|
||
/// Признак (опция) наличия трубы
|
||
/// </summary>
|
||
public bool Trumpet { get; private set; }
|
||
/// <summary>
|
||
/// Признак (опция) наличия прицепа
|
||
/// </summary>
|
||
public bool Luggage { get; private set; }
|
||
/// <summary>
|
||
/// Шаг перемещения
|
||
/// </summary>
|
||
public double Step => (double)Speed * 100 / Weight;
|
||
/// <summary>
|
||
/// Инициализация полей объекта-класса тепловоза
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес тепловоза</param>
|
||
/// <param name="bodyColor">Основной цвет</param>
|
||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||
/// <param name="trumpet">Признак наличия трубы</param>
|
||
/// <param name="luggage">Признак наличия отсека под топливо</param>
|
||
|
||
public void Init(int speed, double weight, Color bodyColor, Color
|
||
additionalColor, bool trumpet, bool luggage, bool v, int width, int height)
|
||
{
|
||
Speed = speed;
|
||
Weight = weight;
|
||
BodyColor = bodyColor;
|
||
AdditionalColor = additionalColor;
|
||
Trumpet = trumpet;
|
||
Luggage = luggage;
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||
/// </summary>
|
||
public class DrawningWarmlyLocomotive
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityWarmlyLocomotive? EntityWarmlyLocomotive { get; private set; }
|
||
/// <summary>
|
||
/// Ширина окна
|
||
/// </summary>
|
||
private int _pictureWidth;
|
||
/// <summary>
|
||
/// Высота окна
|
||
/// </summary>
|
||
private int _pictureHeight;
|
||
/// <summary>
|
||
/// /// Левая координата прорисовки тепловоза
|
||
/// </summary>
|
||
private int _startPosX;
|
||
/// <summary>
|
||
/// Верхняя кооридната прорисовки тепловоза
|
||
/// </summary>
|
||
private int _startPosY;
|
||
/// <summary>
|
||
/// Ширина прорисовки тепловоза
|
||
/// </summary>
|
||
private readonly int _carWidth = 200;
|
||
/// <summary>
|
||
/// Высота прорисовки тепловоза
|
||
/// </summary>
|
||
private readonly int _carHeight = 75;
|
||
//высота трубы
|
||
private readonly int _trumpetHeight = 25;
|
||
/// <summary>
|
||
/// Инициализация свойств
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес</param>
|
||
/// <param name="bodyColor">Цвет кузова</param>
|
||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||
/// <param name="trumpet">Признак наличия трубы</param>
|
||
/// <param name="luggage">Признак наличия отсека под топливо</param>
|
||
/// <param name="width">Ширина картинки</param>
|
||
/// <param name="height">Высота картинки</param>
|
||
/// <returns>true - объект создан, false - проверка не пройдена,нельзя создать объект в этих размерах</returns>
|
||
public bool Init(int speed, double weight, Color bodyColor, Color
|
||
additionalColor, bool trumpet, bool luggage, bool v, int width, int height)
|
||
{
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
if (_carWidth < _pictureWidth || _carHeight < _pictureHeight)
|
||
{
|
||
EntityWarmlyLocomotive = new EntityWarmlyLocomotive();
|
||
EntityWarmlyLocomotive.Init(speed, weight, bodyColor, additionalColor, trumpet, luggage, v, width, height);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Установка позиции
|
||
/// </summary>
|
||
/// <param name="x">Координата X</param>
|
||
/// <param name="y">Координата Y</param>
|
||
public void SetPosition(int x, int y)
|
||
{
|
||
|
||
if (EntityWarmlyLocomotive == null) return;
|
||
while (x + _carWidth > _pictureWidth)
|
||
{
|
||
x -= (int)EntityWarmlyLocomotive.Step;
|
||
}
|
||
while (x < 0)
|
||
{
|
||
x += (int)EntityWarmlyLocomotive.Step;
|
||
}
|
||
while (y + _carHeight > _pictureHeight)
|
||
{
|
||
y -= (int)EntityWarmlyLocomotive.Step;
|
||
}
|
||
while (y < 0)
|
||
{
|
||
y += (int)EntityWarmlyLocomotive.Step;
|
||
}
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
}
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
public void MoveTransport(Direction direction)
|
||
{
|
||
if (EntityWarmlyLocomotive == null)
|
||
{
|
||
return;
|
||
}
|
||
switch (direction)
|
||
{
|
||
//влево
|
||
case Direction.Left:
|
||
if (_startPosX - EntityWarmlyLocomotive.Step > 0)
|
||
{
|
||
_startPosX -= (int)EntityWarmlyLocomotive.Step;
|
||
}
|
||
break;
|
||
//вверх
|
||
case Direction.Up:
|
||
if (_startPosY - _trumpetHeight - EntityWarmlyLocomotive.Step > 0)
|
||
{
|
||
_startPosY -= (int)EntityWarmlyLocomotive.Step;
|
||
}
|
||
break;
|
||
// вправо
|
||
case Direction.Right:
|
||
|
||
if (_startPosX + _carWidth + EntityWarmlyLocomotive.Step < _pictureWidth)
|
||
{
|
||
_startPosX += (int)EntityWarmlyLocomotive.Step;
|
||
}
|
||
break;
|
||
//вниз
|
||
case Direction.Down:
|
||
if (_startPosY + _carHeight + EntityWarmlyLocomotive.Step < _pictureHeight)
|
||
{
|
||
_startPosY += (int)EntityWarmlyLocomotive.Step;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityWarmlyLocomotive == null)
|
||
{
|
||
return;
|
||
}
|
||
Pen pen = new(Color.Black, 2);
|
||
Brush bodyBrush = new SolidBrush(EntityWarmlyLocomotive.BodyColor);
|
||
Brush addBrush = new SolidBrush(EntityWarmlyLocomotive.AdditionalColor);
|
||
Brush wheelBrush = new SolidBrush(Color.Black);
|
||
|
||
//корпус
|
||
g.FillRectangle(bodyBrush, _startPosX + 50, _startPosY, 150, 50);
|
||
Point[] Points = { new Point(_startPosX + 50, _startPosY + 25), new Point(_startPosX + 125, _startPosY + 25) };
|
||
g.DrawPolygon(pen, Points);
|
||
Point[] Points2 = { new Point(_startPosX + 150, _startPosY + 25), new Point(_startPosX + 200, _startPosY + 25) };
|
||
g.DrawPolygon(pen, Points2);
|
||
//окна
|
||
g.DrawRectangle(pen, _startPosX + 125, _startPosY + 10, 25, 30);
|
||
g.DrawRectangle(pen, _startPosX + 60, _startPosY + 7, 10, 13);
|
||
g.DrawRectangle(pen, _startPosX + 160, _startPosY + 7, 10, 13);
|
||
g.DrawRectangle(pen, _startPosX + 175, _startPosY + 7, 10, 13);
|
||
//колеса
|
||
g.FillEllipse(wheelBrush, _startPosX + 60, _startPosY + 50, 20, 20);
|
||
g.FillEllipse(wheelBrush, _startPosX + 85, _startPosY + 50, 20, 20);
|
||
g.FillEllipse(wheelBrush, _startPosX + 145, _startPosY + 50, 20, 20);
|
||
g.FillEllipse(wheelBrush, _startPosX + 170, _startPosY + 50, 20, 20);
|
||
//труба
|
||
if (EntityWarmlyLocomotive.Trumpet)
|
||
{
|
||
g.FillRectangle(addBrush, _startPosX + 165, _startPosY - 25, 25, 25);
|
||
}
|
||
//багаж
|
||
if (EntityWarmlyLocomotive.Luggage)
|
||
{
|
||
g.FillRectangle(addBrush, _startPosX, _startPosY, 50, 50);
|
||
g.FillEllipse(wheelBrush, _startPosX + 10, _startPosY + 50, 20, 20);
|
||
g.FillEllipse(wheelBrush, _startPosX + 35, _startPosY + 50, 20, 20);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|