PIbd-21_Krasnikov_Lab1.base/WinFormsApp1/TractorDraw.cs

191 lines
7.8 KiB
C#
Raw Normal View History

2022-11-13 20:39:23 +04:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Drawing;
namespace WinFormsApp1
{
class TractorDraw
{
//Сущность
public EntityTractor Tractor { get; protected set; }
2022-11-13 20:39:23 +04:00
/// Левая координата отрисовки сущности
protected float startPosX;
2022-11-13 20:39:23 +04:00
/// Верхняя кооридната отрисовки сущности
protected float startPosY;
2022-11-13 20:39:23 +04:00
/// Ширина окна отрисовки
private int? pictureWidth = null;
/// Высота окна отрисовки
private int? pictureHeight = null;
/// Ширина отрисовки сущности
private readonly int entWidth = 115;
2022-11-13 20:39:23 +04:00
/// Высота отрисовки сущности
private readonly int entHeight = 100;
public TractorDraw(int speed, float weight, Color bodycolor)
2022-11-13 20:39:23 +04:00
{
Tractor = new EntityTractor(speed, weight, bodycolor);
2022-11-13 20:39:23 +04:00
}
/// Инициализация свойств
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес автомобиля</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="trktrWidth">Ширина отрисовки автомобиля</param>
/// <param name="trktrHeight">Высота отрисовки автомобиля</param>
///
protected TractorDraw(int speed, float weight, Color bodyColor, int trktrWidth, int trktrHeight) : this(speed, weight, bodyColor)
{
entWidth = trktrWidth;
entHeight = trktrHeight;
}
2022-11-13 20:39:23 +04:00
//Установка позиции сущности
public void SetPosition(int x, int y, int width, int height)
{
if (x < 0 || y < 0 || width < x + entWidth || height < y + entHeight)
{
pictureHeight = null;
pictureWidth = null;
return;
}
startPosX = x;
startPosY = y;
pictureHeight = height;
pictureWidth = width;
}
//Изменение направления перемещения
public void MoveTransport(Direction direction)
{
if (!pictureWidth.HasValue || !pictureHeight.HasValue)
{
return;
}
switch (direction)
{
case Direction.Right:
if (startPosX + entWidth + Tractor.Step < pictureWidth)
{
startPosX += Tractor.Step;
}
break;
case Direction.Left:
if (startPosX - 10 - Tractor.Step > 0)
{
startPosX -= Tractor.Step;
}
break;
case Direction.Down:
if (startPosY + entHeight + Tractor.Step < pictureHeight)
{
startPosY += Tractor.Step;
}
break;
case Direction.Up:
if (startPosY - Tractor.Step > 0)
{
startPosY -= Tractor.Step;
}
break;
case Direction.DRDiagonal:
if (startPosX + entWidth + Tractor.Step < pictureWidth && startPosY + entHeight + Tractor.Step < pictureHeight)
{
startPosX += Tractor.Step;
startPosY += Tractor.Step;
}
break;
case Direction.DLDiagonal:
if (startPosY + entHeight + Tractor.Step < pictureHeight && startPosX - 10 - Tractor.Step > 0)
{
startPosY += Tractor.Step;
startPosX -= Tractor.Step;
}
break;
case Direction.URDiagonal:
if (startPosY - Tractor.Step > 0 && startPosX + entWidth + Tractor.Step < pictureWidth)
{
startPosY -= Tractor.Step;
startPosX += Tractor.Step;
}
break;
case Direction.ULDiagonal:
if (startPosY - Tractor.Step > 0 && startPosX - 10 - Tractor.Step > 0)
{
startPosY -= Tractor.Step;
startPosX -= Tractor.Step;
}
break;
}
}
//Отрисовка сущности
public virtual void DrawEntity(Graphics g)
2022-11-13 20:39:23 +04:00
{
if (startPosX < 0 || startPosY < 0 || !pictureHeight.HasValue || !pictureWidth.HasValue)
{
return;
}
Pen pen_Black_1pxl = new Pen(Color.Black, 1);
Pen pen_Black_2pxl = new Pen(Color.Black, 2);
g.DrawRectangle(pen_Black_1pxl, startPosX, startPosY, 40, 30);
g.DrawRectangle(pen_Black_1pxl, startPosX, startPosY + 30, 100, 35);
g.DrawRectangle(pen_Black_1pxl, startPosX + 72, startPosY + 10, 5, 20);
g.DrawEllipse(pen_Black_1pxl, startPosX - 15, startPosY + 67, 30, 30);
g.DrawEllipse(pen_Black_1pxl, startPosX + 85, startPosY + 67, 30, 30);
g.DrawEllipse(pen_Black_1pxl, startPosX + 20, startPosY + 82, 15, 15);
g.DrawEllipse(pen_Black_1pxl, startPosX + 40, startPosY + 82, 15, 15);
g.DrawEllipse(pen_Black_1pxl, startPosX + 60, startPosY + 82, 15, 15);
g.DrawEllipse(pen_Black_1pxl, startPosX + 35, startPosY + 68, 10, 10);
g.DrawEllipse(pen_Black_1pxl, startPosX + 55, startPosY + 68, 10, 10);
g.DrawLine(pen_Black_2pxl, startPosX, startPosY + 67, startPosX + 100, startPosY + 67);
g.DrawLine(pen_Black_2pxl, startPosX, startPosY + 97, startPosX + 100, startPosY + 97);
Brush br = new SolidBrush(Tractor?.BodyColor ?? Color.Black);
g.FillRectangle(br, startPosX, startPosY, 40, 30);
g.FillRectangle(br, startPosX, startPosY + 30, 100, 35);
Brush brBlack = new SolidBrush(Color.Black);
g.FillRectangle(brBlack, startPosX + 72, startPosY + 10, 5, 20);
g.FillEllipse(brBlack, startPosX - 15, startPosY + 67, 30, 30);
g.FillEllipse(brBlack, startPosX + 85, startPosY + 67, 30, 30);
g.FillEllipse(brBlack, startPosX + 20, startPosY + 82, 15, 15);
g.FillEllipse(brBlack, startPosX + 40, startPosY + 82, 15, 15);
g.FillEllipse(brBlack, startPosX + 60, startPosY + 82, 15, 15);
g.FillEllipse(brBlack, startPosX + 35, startPosY + 68, 10, 10);
g.FillEllipse(brBlack, startPosX + 55, startPosY + 68, 10, 10);
}
//Смена границ формы
public void ChangeBorders(int width, int height)
{
pictureWidth = width;
pictureHeight = height;
if (pictureWidth <= entWidth || pictureHeight <= entHeight)
{
pictureWidth = null;
pictureHeight = null;
return;
}
if (startPosX + entWidth > pictureWidth)
{
startPosX = pictureWidth.Value - entWidth;
}
if (startPosY + entHeight > pictureHeight)
{
startPosY = pictureHeight.Value - entHeight;
}
}
public (float Left, float Right, float Top, float Bottom) GetCurrentPosition()
{
return (startPosX, startPosY, startPosX + entWidth, startPosY + entHeight);
}
2022-11-13 20:39:23 +04:00
}
}