PIbd-21_Anisin_R.S._DumpTru.../DumpTruck/DrawingDumpTruck.cs

156 lines
6.2 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DumpTruck
{
public class DrawingDumpTruck
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityDumpTruck? EntityDumpTruck { 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 _truckWidth = 160;
/// <summary>
/// Высота прорисовки самосвала
/// </summary>
private readonly int _truckHeight = 90;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет самосвала</param>
/// <param name="tent">Признак наличия тента</param>
/// <param name="dumpBox">Признак наличия кузова</param>
/// <param name="dumpBoxColor">Цвет кузова</param>
/// <param name="tentColor">Цвет тента</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
public bool Init(int speed, double weight, Color bodyColor, bool tent, bool dumpBox, Color tentColor, Color dumpBoxColor, int width, int height)
{
_pictureWidth = width;
_pictureHeight = height;
if (_pictureHeight < _truckHeight || _pictureWidth < _truckWidth) return false;
EntityDumpTruck = new EntityDumpTruck();
EntityDumpTruck.Init(speed, weight, bodyColor, tent, dumpBox, tentColor, dumpBoxColor);
return true;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (x < 0 || x + _truckWidth > _pictureWidth) { x = 0; }
if (y < 0 || y + _truckHeight > _pictureHeight) { y = 0; }
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (EntityDumpTruck == null)
{
return;
}
switch (direction)
{
//влево
case Direction.Left:
if (_startPosX - EntityDumpTruck.Step > 0)
{
_startPosX -= (int)EntityDumpTruck.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - EntityDumpTruck.Step > 0)
{
_startPosY -= (int)EntityDumpTruck.Step;
}
break;
// вправо
case Direction.Right:
if (_startPosX + _truckWidth + EntityDumpTruck.Step < _pictureWidth)
{
_startPosX += (int)EntityDumpTruck.Step;
}
break;
//вниз
case Direction.Down:
if (_startPosY + _truckHeight + EntityDumpTruck.Step < _pictureHeight)
{
_startPosY += (int)EntityDumpTruck.Step;
}
break;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityDumpTruck == null)
{
return;
}
Brush brush = new SolidBrush(EntityDumpTruck.BodyColor);
g.FillRectangle(brush, _startPosX, _startPosY + 40, 160, 10);
g.FillRectangle(brush, _startPosX + 120, _startPosY, 40, 40);
g.FillEllipse(brush, _startPosX, _startPosY + 50, 40, 40);
g.FillEllipse(brush, _startPosX + 40, _startPosY + 50, 40, 40);
g.FillEllipse(brush, _startPosX + 120, _startPosY + 50, 40, 40);
if (EntityDumpTruck.DumpBox)
{
Brush brDumpBox = new SolidBrush(EntityDumpTruck.DumpBoxColor);
Point point1 = new Point(_startPosX + 20, _startPosY);
Point point2 = new Point(_startPosX + 120, _startPosY);
Point point3 = new Point(_startPosX + 100, _startPosY + 39);
Point point4 = new Point(_startPosX, _startPosY + 39);
Point[] dumpBoxPoints = { point1, point2, point3, point4};
g.FillPolygon(brDumpBox, dumpBoxPoints);
}
if (EntityDumpTruck.DumpBox && EntityDumpTruck.Tent)
{
Brush brTent = new SolidBrush(EntityDumpTruck.TentColor);
Point point1 = new Point(_startPosX + 15, _startPosY);
Point point2 = new Point(_startPosX + 120, _startPosY);
Point point3 = new Point(_startPosX + 115, _startPosY + 10);
Point point4 = new Point(_startPosX + 10, _startPosY + 10);
Point[] tentPoints = { point1, point2, point3, point4 };
g.FillPolygon(brTent, tentPoints);
}
}
}
}