PIBD-11_Basalov_A.D_Simple/ProjectElectricLocomotive/Drawnings/DrawningLocomotive.cs

224 lines
8.9 KiB
C#
Raw Normal View History

using ProjectElectricLocomotive.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive.Drawnings;
public class DrawningLocomotive
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityLocomotive? EntityLocomotive { get; protected set; }
/// <summary>
/// Ширина окна
/// </summary>
private int? _pictureWidth;
/// <summary>
/// Высота окна
/// </summary>
private int? _pictureHeight;
/// <summary>
/// Левая координата прорисовки Электровоза
/// </summary>
protected int? _startPosX;
/// <summary>
/// Верхняя кооридната прорисовки Электровоза
/// </summary>
protected int? _startPosY;
/// <summary>
/// Ширина прорисовки Локомотива
/// </summary>
private readonly int _drawningLocomotiveWidth = 155;
/// <summary>
/// Высота прорисовки локомотива
/// </summary>
private readonly int _drawningLocomotiveHeight = 90;
/// <summary>
/// Координата X объекта
/// </summary>
public int? GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int? GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _drawningLocomotiveWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _drawningLocomotiveHeight;
/// <summary>
/// Пустой конструктор
/// </summary>
private DrawningLocomotive()
{
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
public DrawningLocomotive(int speed, double weight, Color bodyColor) : this()
{
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningLocomotiveWidtе">Ширина прорисовки локомотива</param>
/// <param name="drawningLocomotiveHeight">Высота прорисовки локомотива</param>
protected DrawningLocomotive(int drawningLocomotiveWidtе, int drawningLocomotiveHeight) : this()
{
_drawningLocomotiveHeight = drawningLocomotiveHeight;
_drawningLocomotiveWidth = drawningLocomotiveWidtе;
}
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
public bool SetPictureSize(int width, int height)
{
// TODO проверка, что объект "влезает" в размеры поля
if (_drawningLocomotiveWidth > width || _drawningLocomotiveHeight > height)
{
return false;
}
if (_startPosX.HasValue && _startPosY.HasValue)
{
if (_startPosX + _drawningLocomotiveWidth > width)
{
_startPosX = width - _drawningLocomotiveWidth;
}
if (_startPosY + _drawningLocomotiveHeight > height)
{
_startPosY = height - _drawningLocomotiveHeight;
}
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена
_pictureWidth = width;
_pictureHeight = height;
return true;
}
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
if (x < 0 || x + _drawningLocomotiveWidth > _pictureWidth.Value)
{
x = _pictureWidth.Value - _drawningLocomotiveWidth;
}
if (y < 0 || y + _drawningLocomotiveHeight > _pictureHeight.Value)
{
y = _pictureHeight.Value - _drawningLocomotiveHeight;
}
// то надо изменить координаты, чтобы он оставался в этих границах
_startPosX = x;
_startPosY = y;
}
public bool MoveTransport(DirectionType direction)
{
if (EntityLocomotive == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityLocomotive.Step > 0)
{
_startPosX -= (int)EntityLocomotive.Step;
}
return true;
//вверх
case DirectionType.Up:
if (_startPosY.Value - EntityLocomotive.Step > 0)
{
_startPosY -= (int)EntityLocomotive.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + EntityLocomotive.Step < _pictureWidth - _drawningLocomotiveWidth)
{
_startPosX += (int)EntityLocomotive.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + EntityLocomotive.Step < _pictureHeight - _drawningLocomotiveHeight)
{
_startPosY += (int)EntityLocomotive.Step;
}
return true;
default:
return false;
}
}
public virtual void DrawTransport(Graphics g)
{
if (EntityLocomotive == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush bodyBrush = new SolidBrush(EntityLocomotive.BodyColor);
//границы Электровоза
//колеса
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 65, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 30, _startPosY.Value + 65, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 100, _startPosY.Value + 65, 20, 20);
g.DrawEllipse(pen, _startPosX.Value + 120, _startPosY.Value + 65, 20, 20);
//кузов
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 50, 155, 40);
g.DrawLine(pen, _startPosX.Value, _startPosY.Value + 50, _startPosX.Value + 20, _startPosY.Value + 30);
g.DrawRectangle(pen, _startPosX.Value + 20, _startPosY.Value + 30, 135, 20);
//покраска кузова
g.FillEllipse(Brushes.Black, _startPosX.Value + 10, _startPosY.Value + 85, 20, 20);
g.FillEllipse(Brushes.Black, _startPosX.Value + 30, _startPosY.Value + 85, 20, 20);
g.FillEllipse(Brushes.Black, _startPosX.Value + 100, _startPosY.Value + 85, 20, 20);
g.FillEllipse(Brushes.Black, _startPosX.Value + 120, _startPosY.Value + 85, 20, 20);
g.FillRectangle(bodyBrush, _startPosX.Value + 1, _startPosY.Value + 51, 154, 39);
g.FillRectangle(bodyBrush, _startPosX.Value + 21, _startPosY.Value + 31, 134, 19);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 50, 155, 10);
g.FillRectangle(bodyBrush, _startPosX.Value + 1, _startPosY.Value + 51, 154, 9);
}
}