PIBD-11_Basalov_A.D_Simple/ProjectElectricLocomotive/DrawningElectricLocomotive.cs
2024-02-19 08:25:06 +03:00

210 lines
9.7 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectElectricLocomotive
{
/// <summary>
/// Класс, отвечающий за отрисовку и перемещение обьекта-сущности
/// </summary>
public class DrawningElectricLocomotive
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityElectricLocomotive? EntityElectricLocomotive { 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 _drawningElectriLocomotiveWidth = 155;
/// <summary>
/// Высота прорисовки локомотива
/// </summary>
private readonly int _drawningElectricLocomotiveHeight = 100;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="pantograph">Признак наличия обвеса</param>
/// <param name="batterystorage">Признак наличия антикрыла</param>
public void Init(int speed, double weight, Color bodyColor, Color
additionalColor, bool pantograph, bool batterystorage)
{
EntityElectricLocomotive = new EntityElectricLocomotive();
EntityElectricLocomotive.Init(speed, weight, bodyColor, additionalColor,
pantograph, batterystorage);
_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)
{
// TODO проверка, что объект "влезает" в размеры поля
if(_drawningElectriLocomotiveWidth > width || _drawningElectricLocomotiveHeight > height)
{
return false;
}
if(_startPosX.HasValue && _startPosY.HasValue)
{
if(_startPosX + _drawningElectriLocomotiveWidth > width)
{
_startPosX = width - _drawningElectriLocomotiveWidth;
}
if(_startPosY + _drawningElectricLocomotiveHeight > height)
{
_startPosY = height - _drawningElectricLocomotiveHeight;
}
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// если влезает, сохраняем границы и корректируем позицию объекта, если она была уже установлена
_pictureWidth = width;
_pictureHeight = height;
return true;
}
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
if(x < 0 || x + _drawningElectriLocomotiveWidth > _pictureWidth.Value)
{
x = _pictureWidth.Value - _drawningElectriLocomotiveWidth;
}
if(y < 0 || y + _drawningElectricLocomotiveHeight > _pictureHeight.Value)
{
y = _pictureHeight.Value - _drawningElectricLocomotiveHeight;
}
// то надо изменить координаты, чтобы он оставался в этих границах
_startPosX = x;
_startPosY = y;
}
public bool MoveTransport(DirectionType direction)
{
if (EntityElectricLocomotive == null || !_startPosX.HasValue ||
!_startPosY.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityElectricLocomotive.Step > 0)
{
_startPosX -= (int)EntityElectricLocomotive.Step;
}
return true;
//вверх
case DirectionType.Up:
if ((_startPosY.Value) - EntityElectricLocomotive.Step > 0)
{
_startPosY -= (int)EntityElectricLocomotive.Step;
}
return true;
// вправо
case DirectionType.Right:
if (_startPosX.Value + EntityElectricLocomotive.Step < _pictureWidth - _drawningElectriLocomotiveWidth)
{
_startPosX += (int)EntityElectricLocomotive.Step;
}
return true;
//вниз
case DirectionType.Down:
if (_startPosY.Value + EntityElectricLocomotive.Step < _pictureHeight - _drawningElectricLocomotiveHeight)
{
_startPosY += (int)EntityElectricLocomotive.Step;
}
return true;
default:
return false;
}
}
public void DrawTransport(Graphics g)
{
if (EntityElectricLocomotive == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additonalBrush = new SolidBrush(EntityElectricLocomotive.AdditionalColor);
Brush bodyBrush = new SolidBrush(EntityElectricLocomotive.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(additonalBrush, _startPosX.Value + 1, _startPosY.Value + 51, 154, 9);
//g.FillPolygon(Brushes.AliceBlue, _startPosX.Value + 1, _startPosY.Value + 31, _startPosX.Value + 19, _startPosY.Value + 9, _startPosX.Value + 19, _startPosY.Value + 1);
//Пантограф
if (EntityElectricLocomotive.Pantograph)
{
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 30, _startPosX.Value + 70, _startPosY.Value + 10);
g.DrawLine(pen, _startPosX.Value + 50, _startPosY.Value + 10, _startPosX.Value + 90, _startPosY.Value + 10);
}
//Отсек для батарей
if (EntityElectricLocomotive.BatteryStorage)
{
g.DrawRectangle(pen, _startPosX.Value + 100, _startPosY.Value + 30, 30, 20);
g.FillRectangle(additonalBrush, _startPosX.Value + 101, _startPosY.Value + 31, 29, 19);
g.DrawLine(pen, _startPosX.Value + 120, _startPosY.Value + 40, _startPosX.Value + 115, _startPosY.Value + 50);
g.DrawLine(pen, _startPosX.Value + 115, _startPosY.Value + 40, _startPosX.Value + 120, _startPosY.Value + 40);
g.DrawLine(pen, _startPosX.Value + 120, _startPosY.Value + 30, _startPosX.Value + 115, _startPosY.Value + 40);
}
}
}
}