ISEbd-12_Pravednikova_D._O..../Excavator/DrawningExcavator.cs
2024-03-23 20:49:41 +04:00

329 lines
11 KiB
C#
Raw Permalink 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.Drawing.Drawing2D;
using System.Net.Sockets;
namespace Excavator;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningExcavator
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityExcavator? EntityExcavator { 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 _drawningExcavatorWidth = 130;
/// <summary>
/// Высота прорисовки экскаватора
/// </summary>
private readonly int _drawningExcavatorHeight = 140;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="bucket">Признак наличия ковша</param>
/// <param name="supports">Признак наличия опор</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool bucket, bool supports)
{
EntityExcavator = new EntityExcavator();
EntityExcavator.Init(speed, weight, bodyColor, additionalColor, bucket, supports);
_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)
{
if (width > _drawningExcavatorWidth && height > _drawningExcavatorHeight)
{
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null && _startPosY != null)
{
if (_startPosX.Value < 0)
{
_startPosX = 0;
}
if (_startPosY.Value < 0)
{
_startPosY = 0;
}
if (_startPosX.Value + _drawningExcavatorWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawningExcavatorWidth;
}
if (_startPosY.Value + _drawningExcavatorHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawningExcavatorHeight;
}
}
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;
}
else
{
_startPosX = x;
_startPosY = y;
if (_startPosX.Value < 0)
{
_startPosX = 0;
}
if (_startPosY.Value < 0)
{
_startPosY = 0;
}
if (_startPosX + _drawningExcavatorWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _drawningExcavatorWidth;
}
if (_startPosY + _drawningExcavatorHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _drawningExcavatorHeight;
}
}
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction">Направление</param>
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityExcavator == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityExcavator.Step > 0)
{
_startPosX -= (int)EntityExcavator.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityExcavator.Step > 0)
{
_startPosY -= (int)EntityExcavator.Step;
}
return true;
// вправо
case DirectionType.Right:
//TODO прописать логику сдвига в право
if (_startPosX.Value + _drawningExcavatorWidth + EntityExcavator.Step < _pictureWidth)
{
_startPosX += (int)EntityExcavator.Step;
}
return true;
//вниз
case DirectionType.Down:
//TODO прописать логику сдвига в вниз
if (_startPosY.Value + _drawningExcavatorHeight + EntityExcavator.Step < _pictureHeight)
{
_startPosY += (int)EntityExcavator.Step;
}
return true;
default:
return false;
}
}
public static GraphicsPath RoundedRect(Graphics g, Rectangle bounds, int radius)
{
int diameter = radius * 2;
Size size = new Size(diameter, diameter);
Rectangle arc = new Rectangle(bounds.Location, size);
GraphicsPath path = new GraphicsPath();
if (radius == 0)
{
path.AddRectangle(bounds);
return path;
}
// top left arc
path.AddArc(arc, 180, 90);
// top right arc
arc.X = bounds.Right - diameter;
path.AddArc(arc, 270, 90);
// bottom right arc
arc.Y = bounds.Bottom - diameter;
path.AddArc(arc, 0, 90);
// bottom left arc
arc.X = bounds.Left;
path.AddArc(arc, 90, 90);
g.FillPath(Brushes.Black, path);
path.CloseFigure();
return path;
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
///
public void DrawTransport(Graphics g)
{
if (EntityExcavator == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityExcavator.AdditionalColor);
Brush brBlue = new SolidBrush(Color.LightBlue);
Brush brGray = new SolidBrush(Color.Gray);
Brush brRed = new SolidBrush(Color.Red);
Brush brYellow = new SolidBrush(Color.Yellow);
Brush brBlack = new SolidBrush(Color.Black);
//кузов
g.DrawRectangle(pen, _startPosX.Value , _startPosY.Value, 150, 150);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value, 90, 40);
g.FillRectangle(brGray, _startPosX.Value, _startPosY.Value, 90, 40);
//труба
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value - 35, 7, 35);
g.FillRectangle(brBlack, _startPosX.Value + 10, _startPosY.Value - 35, 7, 35);
//кабина
g.DrawRectangle(pen, _startPosX.Value + 50, _startPosY.Value - 40, 40, 40);
g.FillRectangle(brBlue, _startPosX.Value + 50, _startPosY.Value - 40, 40, 40);
//колеса
g.DrawPath(pen, RoundedRect(g, new Rectangle(_startPosX.Value - 10, _startPosY.Value + 44, 110, 40), 15));
Pen bPen = new(Color.Black);
bPen.Width = 3;
g.DrawEllipse(bPen, _startPosX.Value - 9, _startPosY.Value + 52, 25, 25);
g.DrawEllipse(bPen, _startPosX.Value + 73, _startPosY.Value + 52, 25, 25);
g.FillEllipse(brBlue, _startPosX.Value - 9, _startPosY.Value + 52, 25, 25);
g.FillEllipse(brBlue, _startPosX.Value + 73, _startPosY.Value + 52, 25, 25);
//колеса
g.DrawEllipse(bPen, _startPosX.Value + 20, _startPosY.Value + 67, 13, 13);
g.DrawEllipse(bPen, _startPosX.Value + 40, _startPosY.Value + 67, 13, 13);
g.DrawEllipse(bPen, _startPosX.Value + 60, _startPosY.Value + 67, 13, 13);
g.FillEllipse(brRed, _startPosX.Value + 20, _startPosY.Value + 67, 13, 13);
g.FillEllipse(brRed, _startPosX.Value + 40, _startPosY.Value + 67, 13, 13);
g.FillEllipse(brRed, _startPosX.Value + 60, _startPosY.Value + 67, 13, 13);
g.DrawEllipse(bPen, _startPosX.Value + 30, _startPosY.Value + 48, 9, 9);
g.DrawEllipse(bPen, _startPosX.Value + 55, _startPosY.Value + 48, 9, 9);
g.FillEllipse(brYellow, _startPosX.Value + 30, _startPosY.Value + 48, 9, 9);
g.FillEllipse(brYellow, _startPosX.Value + 55, _startPosY.Value + 48, 9, 9);
// ковш
if (EntityExcavator.Bucket)
{
g.DrawLine(pen, _startPosX.Value, _startPosY.Value + 40, _startPosX.Value - 30, _startPosY.Value - 10);
g.DrawLine(pen, _startPosX.Value - 30, _startPosY.Value - 10, _startPosX.Value, _startPosY.Value - 10);
g.DrawLine(pen, _startPosX.Value, _startPosY.Value - 10, _startPosX.Value, _startPosY.Value + 40);
PointF[] p = [new PointF(_startPosX.Value, _startPosY.Value + 40), new PointF(_startPosX.Value - 30, _startPosY.Value - 10), new PointF(_startPosX.Value, _startPosY.Value - 10)];
g.FillPolygon(additionalBrush, p);
}
// опоры
if (EntityExcavator.Supports)
{
g.DrawLine(bPen, _startPosX.Value + 90, _startPosY.Value + 20, _startPosX.Value + 112, _startPosY.Value + 20);
g.DrawLine(bPen, _startPosX.Value + 112, _startPosY.Value + 20, _startPosX.Value + 112, _startPosY.Value + 80);
g.DrawLine(bPen, _startPosX.Value + 112, _startPosY.Value + 80, _startPosX.Value + 120, _startPosY.Value + 80);
}
}
}