278 lines
11 KiB
C#
278 lines
11 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Media;
|
||
using ProjectBus;
|
||
|
||
namespace ProjectMain;
|
||
/// <summary>
|
||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||
/// </summary>
|
||
public class DrawningSportCar
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityBus? EntityBus { 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 _drawningCarWidth = 110;
|
||
/// <summary>
|
||
/// Высота прорисовки автомобиля
|
||
/// </summary>
|
||
private readonly int _drawningCarHeight = 60;
|
||
/// <summary>
|
||
/// Инициализация свойств
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес</param>
|
||
/// <param name="bodyColor">Основной цвет</param>
|
||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||
/// <param name="bodyKit">Признак наличия обвеса</param>
|
||
/// <param name="wing">Признак наличия антикрыла</param>
|
||
/// <param name="sportLine">Признак наличия гоночной полосы</param>
|
||
public void Init(int speed, double weight, Color bodyColor, Color
|
||
additionalColor, bool bodyKit, bool wing, bool sportLine)
|
||
{
|
||
EntityBus = new EntityBus();
|
||
EntityBus.Init(speed, weight, bodyColor, additionalColor,
|
||
bodyKit, wing, sportLine);
|
||
_pictureWidth = null;
|
||
_pictureHeight = null;
|
||
_startPosX = null;
|
||
_startPosY = null;
|
||
}
|
||
/// <summary>
|
||
/// Установка границ поля
|
||
/// </summary>
|
||
/// <param name="width">Ширина поля</param>
|
||
/// <param name="height">Высота поля</param>
|
||
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
|
||
public bool SetPictureSize(int width, int height)
|
||
{
|
||
// TODO проверка, что объект "влезает" в размеры поля
|
||
// если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
return true;
|
||
}
|
||
/// <summary>
|
||
/// Установка позиции
|
||
/// </summary>
|
||
/// <param name="x">Координата X</param>
|
||
/// <param name="y">Координата Y</param>
|
||
public void SetPosition(int x, int y)
|
||
{
|
||
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
|
||
// то надо изменить координаты, чтобы он оставался в этих границах
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
}
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
|
||
public bool MoveTransport(DirectionType direction)
|
||
{
|
||
if (EntityBus == null || !_startPosX.HasValue ||
|
||
!_startPosY.HasValue)
|
||
{
|
||
return false;
|
||
}
|
||
switch (direction)
|
||
{
|
||
//влево
|
||
case DirectionType.Left:
|
||
if (_startPosX.Value - EntityBus.Step > 0)
|
||
{
|
||
_startPosX -= (int)EntityBus.Step;
|
||
}
|
||
return true;
|
||
//вверх
|
||
case DirectionType.Up:
|
||
if (_startPosY.Value - EntityBus.Step > 0)
|
||
{
|
||
_startPosY -= (int)EntityBus.Step;
|
||
}
|
||
return true;
|
||
// вправо
|
||
case DirectionType.Right:
|
||
if (_startPosX.Value - EntityBus.Step > 0)
|
||
{
|
||
_startPosX += (int)EntityBus.Step;
|
||
}
|
||
return true;
|
||
//вниз
|
||
case DirectionType.Down:
|
||
if (_startPosY.Value - EntityBus.Step > 0)
|
||
{
|
||
_startPosY += (int)EntityBus.Step;
|
||
}
|
||
return true;
|
||
default:
|
||
return false;
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityBus == null || !_startPosX.HasValue ||
|
||
!_startPosY.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
Pen pen = new(Color.Black);
|
||
Brush additionalBrush = new
|
||
SolidBrush(EntityBus.AdditionalColor);
|
||
// обвесы
|
||
if (EntityBus.BodyKit)
|
||
{
|
||
g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value,
|
||
20, 20);
|
||
g.DrawEllipse(pen, _startPosX.Value + 90, _startPosY.Value +
|
||
40, 20, 20);
|
||
g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value +
|
||
10, 20, 40);
|
||
g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value,
|
||
15, 15);
|
||
g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value +
|
||
45, 15, 15);
|
||
g.FillEllipse(additionalBrush, _startPosX.Value + 90,
|
||
_startPosY.Value, 20, 20);
|
||
g.FillEllipse(additionalBrush, _startPosX.Value + 90,
|
||
_startPosY.Value + 40, 20, 20);
|
||
g.FillRectangle(additionalBrush, _startPosX.Value + 90,
|
||
_startPosY.Value + 10, 20, 40);
|
||
g.FillRectangle(additionalBrush, _startPosX.Value + 90,
|
||
_startPosY.Value + 1, 15, 15);
|
||
g.FillRectangle(additionalBrush, _startPosX.Value + 90,
|
||
_startPosY.Value + 45, 15, 15);
|
||
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value, 20,
|
||
20);
|
||
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 40,
|
||
20, 20);
|
||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 10,
|
||
20, 40);
|
||
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value,
|
||
14, 15);
|
||
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value +
|
||
45, 14, 15);
|
||
g.FillEllipse(additionalBrush, _startPosX.Value,
|
||
_startPosY.Value, 20, 20);
|
||
g.FillEllipse(additionalBrush, _startPosX.Value,
|
||
_startPosY.Value + 40, 20, 20);
|
||
g.FillRectangle(additionalBrush, _startPosX.Value + 1,
|
||
_startPosY.Value + 10, 25, 40);
|
||
g.FillRectangle(additionalBrush, _startPosX.Value + 5,
|
||
_startPosY.Value + 1, 15, 15);
|
||
g.FillRectangle(additionalBrush, _startPosX.Value + 5,
|
||
_startPosY.Value + 45, 15, 15);
|
||
g.DrawRectangle(pen, _startPosX.Value + 35, _startPosY.Value,
|
||
39, 15);
|
||
g.DrawRectangle(pen, _startPosX.Value + 35, _startPosY.Value +
|
||
45, 39, 15);
|
||
g.FillRectangle(additionalBrush, _startPosX.Value + 35,
|
||
_startPosY.Value + 1, 40, 15);
|
||
g.FillRectangle(additionalBrush, _startPosX.Value + 35,
|
||
_startPosY.Value + 45, 40, 15);
|
||
}
|
||
//границы автомобиля
|
||
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 5, 20,
|
||
20);
|
||
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 35, 20,
|
||
20);
|
||
g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 5, 20,
|
||
20);
|
||
g.DrawEllipse(pen, _startPosX.Value + 80, _startPosY.Value + 35, 20,
|
||
20);
|
||
g.DrawRectangle(pen, _startPosX.Value + 9, _startPosY.Value + 15, 10,
|
||
30);
|
||
g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value + 15,
|
||
10, 30);
|
||
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 4, 70,
|
||
52);
|
||
//задние фары
|
||
Brush brRed = new SolidBrush(Color.Red);
|
||
g.FillEllipse(brRed, _startPosX.Value + 10, _startPosY.Value + 5, 20,
|
||
20);
|
||
g.FillEllipse(brRed, _startPosX.Value + 10, _startPosY.Value + 35,
|
||
20, 20);
|
||
//передние фары
|
||
Brush brYellow = new SolidBrush(Color.Yellow);
|
||
g.FillEllipse(brYellow, _startPosX.Value + 80, _startPosY.Value + 5,
|
||
20, 20);
|
||
g.FillEllipse(brYellow, _startPosX.Value + 80, _startPosY.Value + 35,
|
||
20, 20);
|
||
//кузов
|
||
Brush br = new SolidBrush(EntitySportCar.BodyColor);
|
||
g.FillRectangle(br, _startPosX.Value + 10, _startPosY.Value + 15, 10,
|
||
30);
|
||
g.FillRectangle(br, _startPosX.Value + 90, _startPosY.Value + 15, 10,
|
||
30);
|
||
g.FillRectangle(br, _startPosX.Value + 20, _startPosY.Value + 5, 70,
|
||
50);
|
||
//стекла
|
||
Brush brBlue = new SolidBrush(Color.LightBlue);
|
||
g.FillRectangle(brBlue, _startPosX.Value + 70, _startPosY.Value + 10,
|
||
5, 40);
|
||
g.FillRectangle(brBlue, _startPosX.Value + 30, _startPosY.Value + 10,
|
||
5, 40);
|
||
g.FillRectangle(brBlue, _startPosX.Value + 35, _startPosY.Value + 8,
|
||
35, 2);
|
||
g.FillRectangle(brBlue, _startPosX.Value + 35, _startPosY.Value + 51,
|
||
35, 2);
|
||
//выделяем рамкой крышу
|
||
g.DrawRectangle(pen, _startPosX.Value + 35, _startPosY.Value + 10,
|
||
35, 40);
|
||
g.DrawRectangle(pen, _startPosX.Value + 75, _startPosY.Value + 15,
|
||
25, 30);
|
||
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 15,
|
||
15, 30);
|
||
// спортивная линия
|
||
if (EntityBus.SportLine)
|
||
{
|
||
g.FillRectangle(additionalBrush, _startPosX.Value + 75,
|
||
_startPosY.Value + 23, 25, 15);
|
||
g.FillRectangle(additionalBrush, _startPosX.Value + 35,
|
||
_startPosY.Value + 23, 35, 15);
|
||
g.FillRectangle(additionalBrush, _startPosX.Value + 10,
|
||
_startPosY.Value + 23, 20, 15);
|
||
}
|
||
// крыло
|
||
if (EntityBus.Wing)
|
||
{
|
||
g.FillRectangle(additionalBrush, _startPosX.Value,
|
||
_startPosY.Value + 5, 10, 50);
|
||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 5,
|
||
10, 50);
|
||
}
|
||
}
|
||
}
|