ISEbd-22. Baygulov A.A. Lab work 1 (hard) #4

Closed
tellsense wants to merge 7 commits from laba1_H into main
Showing only changes of commit 6c34a47e96 - Show all commits

View File

@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive
{
public class DrawningElectricLocomotive
{
public EntityElectricLocomotive? EntityElectricLocomotive { get; private set; }
private int _pictureWidth;
private int _pictureHeight;
private int _startPosX;
private int _startPosY;
private readonly int _locomWidth = 80;
private readonly int _locomHeight = 52;
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Цвет кузова</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="pantograph">Признак наличия токоприемника</param>
/// <param name="compartment">Признак наличия отсеков под электрические батареи</param>
/// <param name="width">Ширина картинки</param>
/// <param name="height">Высота картинки</param>
/// <returns>true - объект создан, false - проверка не пройдена, нельзя создать объект в этих размерах</returns>
public bool Init(int speed, double weight, Color bodyColor, Color additionalColor, bool pantograph, bool compartment, int width, int height)
{
if (width < _locomWidth || height < _locomHeight)
{
return false;
}
_pictureWidth = width;
_pictureHeight = height;
EntityElectricLocomotive = new EntityElectricLocomotive();
EntityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor, pantograph, compartment);
return true;
}
/// <param name="x">Координата X</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (x < 0 || x + _locomWidth > _pictureWidth)
{
x = _pictureWidth - _locomWidth;
}
if (y < 0 || y + _locomHeight > _pictureHeight)
{
y = _pictureHeight - _locomHeight;
}
_startPosX = x;
_startPosY = y;
}
/// <param name="direction">Направление</param>
public void MoveTransport(Direction direction)
{
if (EntityElectricLocomotive == null)
{
return;
}
switch (direction)
{
//влево
case Direction.Left:
if (_startPosX - EntityElectricLocomotive.Step > 0)
{
_startPosX -= (int)EntityElectricLocomotive.Step;
}
break;
//вверх
case Direction.Up:
if (_startPosY - EntityElectricLocomotive.Step > 0)
{
_startPosY -= (int)EntityElectricLocomotive.Step;
}
break;
//вправо
case Direction.Right:
if (_startPosX + EntityElectricLocomotive.Step + _locomWidth < _pictureWidth)
{
_startPosX += (int)EntityElectricLocomotive.Step;
}
break;
case Direction.Down:
if (_startPosY + EntityElectricLocomotive.Step + _locomHeight < _pictureHeight)
{
_startPosY += (int)EntityElectricLocomotive.Step;
}
break;
}
}
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityElectricLocomotive == null)
{
return;
}
Pen pen = new(Color.Black);
Brush blackBrush = new SolidBrush(Color.Black);
Brush windows = new SolidBrush(Color.LightBlue);
Brush bodyColor = new SolidBrush(EntityElectricLocomotive.BodyColor);
Brush additionalBrush = new SolidBrush(EntityElectricLocomotive.AdditionalColor);
if (EntityElectricLocomotive.Pantograph)
{
// Токоприемники
g.FillRectangle(blackBrush, _startPosX + 30, _startPosY + 15, 20, 5);
g.DrawLine(pen, _startPosX + 30, _startPosY + 15, _startPosX + 50, _startPosY + 2);
g.DrawLine(pen, _startPosX + 40, _startPosY + 15, _startPosX + 60, _startPosY + 2);
}
// Локомотив
g.FillPolygon(bodyColor, new Point[]
{
new Point(_startPosX, _startPosY + 40),
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 20, _startPosY + 20),
new Point(_startPosX + 70, _startPosY + 20),
new Point(_startPosX +80, _startPosY + 20),
new Point(_startPosX +80, _startPosY + 40),
new Point(_startPosX +75, _startPosY + 45),
new Point(_startPosX +5, _startPosY + 45),
new Point(_startPosX, _startPosY + 40),
}
);
if (EntityElectricLocomotive.Compartment)
{
g.DrawRectangle(pen, _startPosX + 40, _startPosY + 24, 25, 11);
g.FillPolygon(additionalBrush, new Point[]
{
new Point(_startPosX + 41, _startPosY + 25),
new Point(_startPosX + 65, _startPosY + 25),
new Point(_startPosX + 65, _startPosY + 35),
new Point(_startPosX + 41, _startPosY + 35),
new Point(_startPosX + 41, _startPosY + 25),
}
);
}
g.DrawPolygon(pen, new Point[]
{
new Point(_startPosX, _startPosY + 40),
new Point(_startPosX, _startPosY + 30),
new Point(_startPosX + 20, _startPosY + 20),
new Point(_startPosX + 70, _startPosY + 20),
new Point(_startPosX + 80, _startPosY + 20),
new Point(_startPosX + 80, _startPosY + 40),
new Point(_startPosX + 75, _startPosY + 45),
new Point(_startPosX + 5, _startPosY + 45),
new Point(_startPosX, _startPosY + 40),
}
);
// Окна
g.FillEllipse(windows, _startPosX + 10, _startPosY + 24, 10, 10);
g.DrawEllipse(pen, _startPosX + 10, _startPosY + 25, 10, 10);
g.FillRectangle(windows, _startPosX + 25, _startPosY + 25, 10, 5);
g.DrawRectangle(pen, _startPosX + 25, _startPosY + 25, 10, 5);
// Колёса
// Локомотив
g.FillEllipse(blackBrush, _startPosX + 10, _startPosY + 45, 10, 10);
g.FillEllipse(blackBrush, _startPosX + 25, _startPosY + 45, 10, 10);
g.FillEllipse(blackBrush, _startPosX + 50, _startPosY + 45, 10, 10);
g.FillEllipse(blackBrush, _startPosX + 65, _startPosY + 45, 10, 10);
}
}
}