319 lines
12 KiB
C#
319 lines
12 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace ProjectMonorail;
|
||
|
||
/// <summary>
|
||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||
/// </summary>
|
||
public class DrawingMonorail
|
||
{
|
||
/// <summary>
|
||
/// Класс-сущность "монорельс"
|
||
/// </summary>
|
||
public EntityMonorail? EntityMonorail { get; private set; }
|
||
|
||
/// <summary>
|
||
/// Высота окна
|
||
/// </summary>
|
||
private int? _pictureHeight;
|
||
|
||
/// <summary>
|
||
/// Ширина окна
|
||
/// </summary>
|
||
private int? _pictureWidth;
|
||
|
||
/// <summary>
|
||
/// Левая координата прорисовки монорельса
|
||
/// </summary>
|
||
private int? _startPosX;
|
||
|
||
/// <summary>
|
||
/// Верхняя координата прорисовки монорельса
|
||
/// </summary>
|
||
private int? _startPosY;
|
||
|
||
/// <summary>
|
||
/// Высота прорисовки монорельса
|
||
/// </summary>
|
||
private readonly int _drawingMonorailHeight = 40;
|
||
|
||
/// <summary>
|
||
/// Ширина прорисовки монорельса
|
||
/// </summary>
|
||
private readonly int _drawingMonorailWidth = 100;
|
||
|
||
/// <summary>
|
||
/// Инициализация свойств
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">Вес</param>
|
||
/// <param name="wheels">Количество колёс</param>
|
||
/// <param name="mainColor">Основной цвет</param>
|
||
/// <param name="additionalColor">Дополнительный цвет</param>
|
||
/// <param name="rail">Признак наличия магнитного рельса</param>
|
||
/// <param name="secondCarriage">Признак наличия второго вагона</param>
|
||
public void Init(int speed, double weight, int wheels, Color mainColor, Color additionalColor, bool rail, bool secondCarriage)
|
||
{
|
||
EntityMonorail = new EntityMonorail();
|
||
EntityMonorail.Init(speed, weight, wheels, mainColor, additionalColor, rail, secondCarriage);
|
||
_pictureHeight = null;
|
||
_pictureWidth = null;
|
||
_startPosX = null;
|
||
_startPosY = null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Установка границ поля
|
||
/// </summary>
|
||
/// <param name="width">Ширина поля</param>
|
||
/// <param name="height">Высота поля</param>
|
||
/// <returns></returns>
|
||
public bool SetPictureSize(int width, int height)
|
||
{
|
||
//TODO!
|
||
if (_drawingMonorailWidth <= width && _drawingMonorailHeight <= height)
|
||
{
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
if (_startPosX.HasValue && _startPosY.HasValue)
|
||
{
|
||
if (_startPosX + _drawingMonorailWidth > _pictureWidth)
|
||
{
|
||
_startPosX = _pictureWidth - _drawingMonorailWidth;
|
||
}
|
||
if (_startPosY + _drawingMonorailHeight > _pictureHeight)
|
||
{
|
||
_startPosY = _pictureHeight - _drawingMonorailHeight;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/// <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;
|
||
}
|
||
if (x + _drawingMonorailWidth > _pictureWidth.Value)
|
||
{
|
||
x = _pictureWidth.Value - _drawingMonorailWidth;
|
||
}
|
||
if (y + _drawingMonorailHeight > _pictureHeight.Value)
|
||
{
|
||
y = _pictureHeight.Value - _drawingMonorailHeight;
|
||
}
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
/// <returns>true - перемещение выполнено; false - перемещение невозможно</returns>
|
||
public bool MoveTransport(DirectionType direction)
|
||
{
|
||
if (EntityMonorail == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
switch (direction)
|
||
{
|
||
//Перемещение влево
|
||
case DirectionType.Left:
|
||
if (_startPosX.Value - EntityMonorail.Step > 0)
|
||
{
|
||
_startPosX -= (int)EntityMonorail.Step;
|
||
}
|
||
return true;
|
||
//Перемещение вправо
|
||
case DirectionType.Right:
|
||
if (EntityMonorail.SecondСarriage)
|
||
{
|
||
if (_startPosX.Value + 180 + EntityMonorail.Step < _pictureWidth)
|
||
{
|
||
_startPosX += (int)EntityMonorail.Step;
|
||
}
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
if (_startPosX.Value + _drawingMonorailWidth + EntityMonorail.Step < _pictureWidth)
|
||
{
|
||
_startPosX += (int)EntityMonorail.Step;
|
||
}
|
||
return true;
|
||
}
|
||
//Перемещение вверх
|
||
case DirectionType.Up:
|
||
if (_startPosY.Value - EntityMonorail.Step > 0)
|
||
{
|
||
_startPosY -= (int)EntityMonorail.Step;
|
||
}
|
||
return true;
|
||
//Перемещение вниз
|
||
case DirectionType.Down:
|
||
if (_startPosY.Value + _drawingMonorailHeight + EntityMonorail.Step < _pictureHeight)
|
||
{
|
||
_startPosY += (int)EntityMonorail.Step;
|
||
}
|
||
return true;
|
||
default: return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityMonorail == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
|
||
Pen pen = new(Color.Black);
|
||
Brush mainBrush = new SolidBrush(EntityMonorail.MainColor);
|
||
Brush additionalBrush = new SolidBrush(EntityMonorail.AdditionalColor);
|
||
Brush brBlue = new SolidBrush(Color.LightBlue);
|
||
Brush brWhite = new SolidBrush(Color.White);
|
||
Brush brBlack = new SolidBrush(Color.Black);
|
||
|
||
//Границы монорельса
|
||
g.DrawRectangle(pen, _startPosX.Value + 5, _startPosY.Value + 15, 80, 15);
|
||
Point[] points1 = {
|
||
new Point(_startPosX.Value + 10, _startPosY.Value),
|
||
new Point(_startPosX.Value + 85, _startPosY.Value),
|
||
new Point(_startPosX.Value + 85, _startPosY.Value + 15),
|
||
new Point(_startPosX.Value + 5, _startPosY.Value + 15)
|
||
};
|
||
g.DrawPolygon(pen, points1);
|
||
g.FillRectangle(mainBrush, _startPosX.Value + 6, _startPosY.Value + 16, 79, 14);
|
||
Point[] points2 = {
|
||
new Point(_startPosX.Value + 10, _startPosY.Value + 1),
|
||
new Point(_startPosX.Value + 85, _startPosY.Value + 1),
|
||
new Point(_startPosX.Value + 85, _startPosY.Value + 15),
|
||
new Point(_startPosX.Value + 5, _startPosY.Value + 15)
|
||
};
|
||
g.FillPolygon(mainBrush, points2);
|
||
|
||
//Держатель 1
|
||
Point[] points_cart1 = {
|
||
new Point(_startPosX.Value, _startPosY.Value + 35),
|
||
new Point(_startPosX.Value + 15, _startPosY.Value + 35),
|
||
new Point(_startPosX.Value + 15, _startPosY.Value + 30),
|
||
new Point(_startPosX.Value + 5, _startPosY.Value + 30)
|
||
};
|
||
g.DrawPolygon(pen, points_cart1);
|
||
g.FillPolygon(brBlack, points_cart1);
|
||
|
||
//Держатель 2
|
||
Point[] points_cart2 = {
|
||
new Point(_startPosX.Value + 20, _startPosY.Value + 30),
|
||
new Point(_startPosX.Value + 25, _startPosY.Value + 35),
|
||
new Point(_startPosX.Value + 30, _startPosY.Value + 35),
|
||
new Point(_startPosX.Value + 35, _startPosY.Value + 30)
|
||
};
|
||
g.DrawPolygon(pen, points_cart2);
|
||
g.FillPolygon(brBlack, points_cart2);
|
||
|
||
//Держатель 3
|
||
Point[] points_cart3 = {
|
||
new Point(_startPosX.Value + 55, _startPosY.Value + 30),
|
||
new Point(_startPosX.Value + 60, _startPosY.Value + 35),
|
||
new Point(_startPosX.Value + 65, _startPosY.Value + 35),
|
||
new Point(_startPosX.Value + 70, _startPosY.Value + 30)
|
||
};
|
||
g.DrawPolygon(pen, points_cart3);
|
||
g.FillPolygon(brBlack, points_cart3);
|
||
|
||
//Держатель 4
|
||
Point[] points_cart4 = {
|
||
new Point(_startPosX.Value + 70, _startPosY.Value + 30),
|
||
new Point(_startPosX.Value + 75, _startPosY.Value + 35),
|
||
new Point(_startPosX.Value + 90, _startPosY.Value + 35),
|
||
new Point(_startPosX.Value + 85, _startPosY.Value + 30)
|
||
};
|
||
g.DrawPolygon(pen, points_cart4);
|
||
g.FillPolygon(brBlack, points_cart4);
|
||
|
||
//Окна монорельса
|
||
g.DrawRectangle(pen, _startPosX.Value + 17, _startPosY.Value + 5, 17, 7);
|
||
g.FillRectangle(brBlue, _startPosX.Value + 18, _startPosY.Value + 6, 16, 6);
|
||
g.DrawRectangle(pen, _startPosX.Value + 55, _startPosY.Value + 5, 27, 7);
|
||
g.FillRectangle(brBlue, _startPosX.Value + 56, _startPosY.Value + 6, 26, 6);
|
||
|
||
//Дверь монорельса
|
||
g.DrawRectangle(pen, _startPosX.Value + 40, _startPosY.Value + 7, 10, 21);
|
||
g.FillRectangle(brWhite, _startPosX.Value + 41, _startPosY.Value + 8, 9, 20);
|
||
|
||
//магнитная рельса
|
||
if (EntityMonorail.Rail)
|
||
{
|
||
if (EntityMonorail.SecondСarriage)
|
||
{
|
||
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 40, 180, 3);
|
||
}
|
||
else
|
||
{
|
||
g.FillRectangle(additionalBrush, _startPosX.Value, _startPosY.Value + 40, 90, 3);
|
||
}
|
||
}
|
||
|
||
//Второй вагон
|
||
if (EntityMonorail.SecondСarriage)
|
||
{
|
||
g.DrawRectangle(pen, _startPosX.Value + 95, _startPosY.Value, 80, 30);
|
||
g.FillRectangle(additionalBrush, _startPosX.Value + 96, _startPosY.Value + 1, 79, 29);
|
||
g.FillRectangle(brBlack, _startPosX.Value + 85, _startPosY.Value + 5, 10, 23);
|
||
//Окно
|
||
g.DrawRectangle(pen, _startPosX.Value + 105, _startPosY.Value + 5, 60, 10);
|
||
g.FillRectangle(brBlue, _startPosX.Value + 106, _startPosY.Value + 6, 59, 9);
|
||
//Дверь
|
||
g.DrawRectangle(pen, _startPosX.Value + 105, _startPosY.Value + 5, 10, 23);
|
||
g.FillRectangle(brWhite, _startPosX.Value + 106, _startPosY.Value + 16, 9, 12);
|
||
//Колёса
|
||
g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 30, 10, 10);
|
||
g.DrawEllipse(pen, _startPosX.Value + 115, _startPosY.Value + 30, 10, 10);
|
||
g.DrawEllipse(pen, _startPosX.Value + 145, _startPosY.Value + 30, 10, 10);
|
||
g.DrawEllipse(pen, _startPosX.Value + 160, _startPosY.Value + 30, 10, 10);
|
||
}
|
||
|
||
//колёса
|
||
//1, 4 колесо
|
||
g.DrawEllipse(pen, _startPosX.Value + 15, _startPosY.Value + 30, 10, 10);
|
||
g.FillEllipse(brWhite, _startPosX.Value + 15, _startPosY.Value + 30, 10, 10);
|
||
g.DrawEllipse(pen, _startPosX.Value + 65, _startPosY.Value + 30, 10, 10);
|
||
g.FillEllipse(brWhite, _startPosX.Value + 65, _startPosY.Value + 30, 10, 10);
|
||
|
||
switch (EntityMonorail.Wheels)
|
||
{
|
||
case 3:
|
||
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 30, 10, 10);
|
||
g.FillEllipse(brWhite, _startPosX.Value + 30, _startPosY.Value + 30, 10, 10);
|
||
break;
|
||
case 4:
|
||
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 30, 10, 10);
|
||
g.FillEllipse(brWhite, _startPosX.Value + 30, _startPosY.Value + 30, 10, 10);
|
||
g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 30, 10, 10);
|
||
g.FillEllipse(brWhite, _startPosX.Value + 50, _startPosY.Value + 30, 10, 10);
|
||
break;
|
||
}
|
||
|
||
}
|
||
}
|