2023-09-27 09:51:02 +04:00

191 lines
7.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.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectricLocomotive
{
internal class DrawningElectricLocomotive
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityElectricLocomotive? EntityElectricLocomotive { 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 _locomotiveWidth = 120;
/// <summary>
/// Высота прорисовки автомобиля
/// </summary>
private readonly int _locomotiveHeight = 70;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="horns">Признак наличия рогов</param>
/// <param name="battery">Признак наличия отсека для батарей</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 horns, bool battery, int width, int height)
{
if (width < _locomotiveWidth || height < _locomotiveHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
EntityElectricLocomotive = new EntityElectricLocomotive();
EntityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor,
horns, battery);
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
x = Math.Min(Math.Max(0, x), _pictureWidth - _locomotiveWidth);
y = Math.Min(Math.Max(0, y), _pictureHeight - _locomotiveHeight);
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(DirectionType direction)
{
if (EntityElectricLocomotive == null)
{
return;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX - EntityElectricLocomotive.Step > 0)
{
_startPosX -= (int)EntityElectricLocomotive.Step;
}
break;
//вверх
case DirectionType.Up:
if (_startPosY - EntityElectricLocomotive.Step > 0)
{
_startPosY -= (int)EntityElectricLocomotive.Step;
}
break;
// вправо
case DirectionType.Right:
if (_startPosX + _locomotiveWidth + EntityElectricLocomotive.Step < _pictureWidth)
{
_startPosX += (int)EntityElectricLocomotive.Step;
}
break;
//вниз
case DirectionType.Down:
if (_startPosY + _locomotiveHeight + EntityElectricLocomotive.Step < _pictureHeight)
{
_startPosY += (int)EntityElectricLocomotive.Step;
}
break;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityElectricLocomotive == null)
{
return;
}
// корпус электровоза
Pen pen = new(Color.Black, 5);
Brush brush = new SolidBrush(EntityElectricLocomotive.BodyColor);
Point[] points = new Point[5];
points[0] = new Point(_startPosX, _startPosY + 40);
points[1] = new Point(_startPosX + 10, _startPosY + 20);
points[2] = new Point(_startPosX + 120, _startPosY + 20);
points[3] = new Point(_startPosX + 120, _startPosY + 60);
points[4] = new Point(_startPosX, _startPosY + 60);
g.FillPolygon(brush, points);
g.DrawLine(pen,
new Point(_startPosX, _startPosY + 40),
new Point(_startPosX + 120, _startPosY + 40));
// окна
brush = new SolidBrush(Color.FromArgb(0, 162, 232));
g.FillRectangle(brush, _startPosX + 12, _startPosY + 24, 30, 11);
g.FillEllipse(brush, _startPosX + 55, _startPosY + 24, 11, 11);
g.FillEllipse(brush, _startPosX + 75, _startPosY + 24, 11, 11);
g.FillEllipse(brush, _startPosX + 95, _startPosY + 24, 11, 11);
// колёса
brush = new SolidBrush(Color.Black);
g.FillEllipse(brush, _startPosX + 10, _startPosY + 55, 15, 15);
g.FillEllipse(brush, _startPosX + 25, _startPosY + 55, 15, 15);
g.FillEllipse(brush, _startPosX + 80, _startPosY + 55, 15, 15);
g.FillEllipse(brush, _startPosX + 95, _startPosY + 55, 15, 15);
// рога
if (EntityElectricLocomotive.Horns)
{
pen = new(Color.Black, 2);
points = new Point[4];
points[0] = new Point(_startPosX + 50, _startPosY + 20);
points[1] = new Point(_startPosX + 40, _startPosY + 10);
points[2] = new Point(_startPosX + 50, _startPosY);
points[3] = new Point(_startPosX + 60, _startPosY + 10);
g.DrawPolygon(pen, points);
}
// отсек для батарей
if (EntityElectricLocomotive.Battery)
{
brush = new SolidBrush(EntityElectricLocomotive.AdditionalColor);
g.FillRectangle(brush, _startPosX + 80, _startPosY + 45, 35, 9);
}
}
/// <summary>
/// Изменение размера поля
/// </summary>
/// <param name="newSize">Новый размер</param>
public void SetPictureSize(Size newSize)
{
_pictureHeight = newSize.Height;
_pictureWidth = newSize.Width;
}
}
}