264 lines
8.6 KiB
C#
Raw 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 ProjectAirbus.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectAirbus.Drawning;
public class DrawningBus
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityBus? EntityBus { 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 _drawningBusWidth = 135;
/// <summary>
/// Высота прорисовки аэробуса
/// </summary>
private readonly int _drawningBusHeight = 60;
/// <summary>
/// Координата X объекта
/// </summary>
public int? GetPosX => _startPosX;
/// <summary>
/// Координата Y объекта
/// </summary>
public int? GetPosY => _startPosY;
/// <summary>
/// Ширина объекта
/// </summary>
public int GetWidth => _drawningBusWidth;
/// <summary>
/// Высота объекта
/// </summary>
public int GetHeight => _drawningBusHeight;
/// <summary>
/// Пустой конструктор
/// </summary>
private DrawningBus()
{
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">Вес</param>
/// <param name="bodycolor">Основной цвет</param>
public DrawningBus(int speed, double weight, Color bodycolor) : this()
{
EntityBus = new EntityBus(speed, weight, bodycolor);
_pictureWidth = null;
_pictureHeight = null;
_startPosX = null;
_startPosY = null;
}
/// <summary>
/// Конструктор для наследников
/// </summary>
/// <param name="drawningBusWidth">Ширина прорисовки аэробуса</param>
/// <param name="drawningBusHeight">Высота прорисовки аэробуса</param>
protected DrawningBus(int drawningAirbusWidth, int drawningAirbusHeight) : this()
{
_drawningBusWidth = drawningAirbusWidth;
_drawningBusHeight = drawningAirbusHeight;
}
/// <summary>
/// Установка границ поля
/// </summary>
/// <param name="width">Ширина поля</param>
/// <param name="height">Высота поля</param>
/// <returns></returns>
public bool SetPictureSize(int width, int height)
{
// TODO проверка, что объект "влезает" в размеры поля
// если влезает, сохраняем границы и корректируем позицию объекта,если она была уже установлена
if (width >= _drawningBusWidth || height >= _drawningBusHeight)
{
_pictureWidth = width;
_pictureHeight = height;
if (_startPosX != null && _startPosY != null)
{
SetPosition(_startPosX.Value, _startPosY.Value);
}
return true;
}
return false;
}
/// <summary>
/// Установка позиции
/// </summary>
/// <param name="x">Координата Х</param>
/// <param name="y">Координата Y</param>
public void SetPosition(int x, int y)
{
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
{
return;
}
// TODO если при установке объекта в эти координаты, он будет "выходить" за границы формы
// то надо изменить координаты, чтобы он оставался в этих границах
if (x < 0)
{
x = 0;
}
else if (x > _pictureWidth - _drawningBusWidth)
{
x = _pictureWidth.Value - _drawningBusWidth;
}
if (y < 0)
{
y = 0;
}
else if (y > _pictureHeight - _drawningBusHeight)
{
y = _pictureHeight.Value - _drawningBusHeight;
}
_startPosX = x;
_startPosY = y;
}
/// <summary>
/// Изменение направления перемещения
/// </summary>
/// <param name="direction"></param>
/// <returns></returns>
public bool MoveTransport(DirectionType direction)
{
if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
switch (direction)
{
// Влево
case DirectionType.Left:
if (_startPosX.Value - EntityBus.Step > 0)
{
_startPosX -= (int)EntityBus.Step;
}
return true;
//Вверх
case DirectionType.Up:
if (_startPosY.Value - EntityBus.Step > 0)
{
_startPosY -= (int)EntityBus.Step;
}
return true;
//Вправо
case DirectionType.Right:
{
if (_startPosX.Value + _drawningBusWidth + EntityBus.Step < _pictureWidth)
{
_startPosX += (int)EntityBus.Step;
}
}
return true;
//Вниз
case DirectionType.Down:
{
if (_startPosY.Value + _drawningBusHeight + EntityBus.Step < _pictureHeight)
{
_startPosY += (int)EntityBus.Step;
}
}
return true;
default:
return false;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public virtual void DrawTransport(Graphics g)
{
if (EntityBus == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush bodyColorBrush = new SolidBrush(EntityBus.BodyColor);
//корпус
g.FillRectangle(bodyColorBrush, _startPosX.Value, _startPosY.Value + 20, 100, 20);
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 20, 100, 20);
//крыло
Brush darkBrush = new SolidBrush(Color.Black);
g.FillRectangle(darkBrush, _startPosX.Value + 25, _startPosY.Value + 29, 45, 4);
//Крыло сзади
g.FillPolygon(bodyColorBrush, new Point[]
{
new Point(_startPosX.Value, _startPosY.Value + 20), new Point(_startPosX.Value, _startPosY.Value),
new Point(_startPosX.Value + 20, _startPosY.Value + 20), });
g.DrawLine(pen, _startPosX.Value, _startPosY.Value + 20, _startPosX.Value, _startPosY.Value);
g.DrawLine(pen, _startPosX.Value, _startPosY.Value, _startPosX.Value + 20, _startPosY.Value + 20);
//заднее доп крыло
g.FillEllipse(darkBrush, _startPosX.Value, _startPosY.Value + 15, 20, 5);
//нос самолёта
g.DrawLine(pen, _startPosX.Value + 100, _startPosY.Value + 20, _startPosX.Value + 130, _startPosY.Value + 30);
g.DrawLine(pen, _startPosX.Value + 100, _startPosY.Value + 40, _startPosX.Value + 130, _startPosY.Value + 30);
g.DrawLine(pen, _startPosX.Value + 130, _startPosY.Value + 30, _startPosX.Value + 100, _startPosY.Value + 30);
//задние шасси
g.DrawLine(pen, _startPosX.Value + 21, _startPosY.Value + 40, _startPosX.Value + 21, _startPosY.Value + 45);
g.FillEllipse(darkBrush, _startPosX.Value + 15, _startPosY.Value + 45, 6, 6);
g.FillEllipse(darkBrush, _startPosX.Value + 21, _startPosY.Value + 45, 6, 6);
//переднее шасси
g.DrawLine(pen, _startPosX.Value + 90, _startPosY.Value + 40, _startPosX.Value + 90, _startPosY.Value + 45);
g.FillEllipse(darkBrush, _startPosX.Value + 87, _startPosY.Value + 45, 6, 6);
}
}