PIbd-21_Krasnikov_Lab1.base/WinFormsApp1/TractorDraw.cs
2022-11-13 20:39:23 +04:00

176 lines
6.9 KiB
C#

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; private set; }
/// Левая координата отрисовки сущности
private float startPosX;
/// Верхняя кооридната отрисовки сущности
private float startPosY;
/// Ширина окна отрисовки
private int? pictureWidth = null;
/// Высота окна отрисовки
private int? pictureHeight = null;
/// Ширина отрисовки сущности
private readonly int entWidth = 130;
/// Высота отрисовки сущности
private readonly int entHeight = 100;
public void Init(int speed, float weight, Color bodycolor)
{
Tractor = new EntityTractor();
Tractor.Init(speed, weight, bodycolor);
}
//Установка позиции сущности
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 void DrawEntity(Graphics g)
{
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;
}
}
}
}