271 lines
9.5 KiB
C#
271 lines
9.5 KiB
C#
using lab1.Entities;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace lab1.Drawnings;
|
||
/// <summary>
|
||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||
/// </summary>
|
||
public class DrawningTrackedVehicle
|
||
{ /// <summary>
|
||
/// Класс-сущность
|
||
/// </summary>
|
||
public EntityTrackedVehicle? EntityTrackedVehicle { 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 _drawningFighterWidth = 63;
|
||
|
||
/// <summary>
|
||
/// Высота прорисовки истребителя
|
||
/// </summary>
|
||
private readonly int _drawningFighterHeight = 69;
|
||
|
||
/// <summary>
|
||
/// Координата Х объекта
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public int? GetPosX => _startPosX;
|
||
/// <summary>
|
||
/// Координата У объекта
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
|
||
public int? GetPosY => _startPosY;
|
||
/// <summary>
|
||
/// Ширина объекта
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public int GetWidth => _drawningFighterWidth;
|
||
/// <summary>
|
||
/// Высота объекта
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public int GetHeight => _drawningFighterHeight;
|
||
|
||
|
||
/// <summary>
|
||
/// Пустой конструктор
|
||
/// </summary>
|
||
private DrawningTrackedVehicle()
|
||
{
|
||
_pictureWidth = null;
|
||
_pictureHeight = null;
|
||
_startPosX = null;
|
||
_startPosY = null;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
/// <param name="speed">Скорость</param>
|
||
/// <param name="weight">>Вес истребителя</param>
|
||
/// <param name="bodyColor">Основной цвет</param>
|
||
|
||
public DrawningTrackedVehicle(int speed, double weight, Color bodyColor) : this()
|
||
{
|
||
EntityTrackedVehicle = new EntityTrackedVehicle(speed, weight, bodyColor);
|
||
|
||
}
|
||
/// <summary>
|
||
/// Конструктор для наследников
|
||
/// </summary>
|
||
/// <param name="_drawningFighterWidth"> Ширина прорисовки истребителя</param>
|
||
/// <param name="_drawningFighterHeight">>Высота прорисовки истребителя</param>
|
||
|
||
|
||
protected DrawningTrackedVehicle(int drawningFighterWidth, int drawningFighterHeight) : this()
|
||
{
|
||
_drawningFighterWidth = drawningFighterWidth;
|
||
_drawningFighterHeight = drawningFighterHeight;
|
||
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// Установка границ поля
|
||
/// </summary>
|
||
/// <param name="width">Ширина поля</param>
|
||
/// <param name="height">Высота поля</param>
|
||
/// <returns>true - границы заданы, false - проверка не пройдена, нельзя разместить объект в этих размерах</returns>
|
||
public bool SetPictureSize(int width, int height)
|
||
{
|
||
if (width >= _drawningFighterWidth && height >= _drawningFighterHeight)
|
||
{
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
if (_startPosX.HasValue && _startPosY.HasValue)
|
||
{
|
||
SetPosition(_startPosX.Value, _startPosY.Value);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Установка позиции
|
||
/// </summary>
|
||
/// <param name="x">Координата X</param>
|
||
/// <param name="y">Координата Y</param>
|
||
public void SetPosition(int x, int y)
|
||
{
|
||
if (!_pictureHeight.HasValue || !_pictureWidth.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
if (x < 0)
|
||
{
|
||
x = 0;
|
||
}
|
||
else if (x > _pictureWidth.Value - _drawningFighterWidth)
|
||
{
|
||
x = _pictureWidth.Value - _drawningFighterWidth;
|
||
}
|
||
|
||
if (y < 0)
|
||
{
|
||
y = 0;
|
||
}
|
||
else if (y > _pictureHeight.Value - _drawningFighterHeight)
|
||
{
|
||
y = _pictureHeight.Value - _drawningFighterHeight;
|
||
}
|
||
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
/// <returns>true - перемещене выполнено, false - перемещение невозможно</returns>
|
||
public bool MoveTransport(DirectionType direction)
|
||
{
|
||
if (EntityTrackedVehicle == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return false;
|
||
}
|
||
switch (direction)
|
||
{
|
||
//влево
|
||
case DirectionType.Left:
|
||
if (_startPosX.Value - EntityTrackedVehicle.Step > 0)
|
||
{
|
||
_startPosX -= (int)EntityTrackedVehicle.Step;
|
||
}
|
||
return true;
|
||
//Вверх
|
||
case DirectionType.Up:
|
||
if (_startPosY.Value - EntityTrackedVehicle.Step > 0)
|
||
{
|
||
_startPosY -= (int)EntityTrackedVehicle.Step;
|
||
}
|
||
return true;
|
||
|
||
//Вправо
|
||
case DirectionType.Right:
|
||
if (_startPosX.Value + EntityTrackedVehicle.Step + _drawningFighterWidth < _pictureWidth)
|
||
{
|
||
_startPosX += (int)EntityTrackedVehicle.Step;
|
||
}
|
||
return true;
|
||
//Вниз
|
||
case DirectionType.Down:
|
||
if (_startPosY.Value + EntityTrackedVehicle.Step + _drawningFighterHeight < _pictureHeight)
|
||
{
|
||
_startPosY += (int)EntityTrackedVehicle.Step;
|
||
}
|
||
return true;
|
||
default:
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public virtual void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityTrackedVehicle == null || !_startPosX.HasValue || !_startPosY.HasValue)
|
||
{
|
||
return;
|
||
}
|
||
Pen pen = new(Color.Black);
|
||
|
||
Brush BodyBrush = new SolidBrush(EntityTrackedVehicle.BodyColor);
|
||
|
||
//корпус
|
||
g.FillRectangle(BodyBrush, _startPosX.Value, _startPosY.Value + 5, 62, 12);
|
||
g.DrawRectangle(pen, _startPosX.Value, _startPosY.Value + 5, 62, 12);
|
||
g.FillRectangle(BodyBrush, _startPosX.Value + 12, _startPosY.Value - 9, 4, 15);
|
||
g.DrawRectangle(pen, _startPosX.Value +12, _startPosY.Value - 9, 4, 14);
|
||
g.FillRectangle(BodyBrush, _startPosX.Value + 27, _startPosY.Value - 24, 5, 30);
|
||
g.DrawRectangle(pen, _startPosX.Value + 27, _startPosY.Value - 24, 5, 30);
|
||
|
||
//стекло
|
||
g.FillRectangle(BodyBrush, _startPosX.Value + 42, _startPosY.Value - 11, 21, 16);
|
||
g.DrawRectangle(pen, _startPosX.Value + 42, _startPosY.Value -11, 20, 16);
|
||
|
||
//гусеницы
|
||
g.DrawLine(pen, _startPosX.Value - 2, _startPosY.Value + 17, _startPosX.Value, _startPosY.Value + 17);
|
||
g.DrawLine(pen, _startPosX.Value - 3, _startPosY.Value + 18, _startPosX.Value - 3, _startPosY.Value + 28);
|
||
g.DrawLine(pen, _startPosX.Value - 2, _startPosY.Value + 29, _startPosX.Value + 2, _startPosY.Value + 29);
|
||
g.DrawLine(pen, _startPosX.Value + 3, _startPosY.Value + 30, _startPosX.Value + 59, _startPosY.Value + 30);
|
||
g.DrawLine(pen, _startPosX.Value + 60, _startPosY.Value + 29, _startPosX.Value + 63, _startPosY.Value + 29);
|
||
g.DrawLine(pen, _startPosX.Value + 64, _startPosY.Value + 28, _startPosX.Value + 64, _startPosY.Value + 18);
|
||
g.DrawLine(pen, _startPosX.Value + 61, _startPosY.Value + 17, _startPosX.Value + 63, _startPosY.Value + 17);
|
||
|
||
//колеса
|
||
g.FillEllipse(BodyBrush, _startPosX.Value, _startPosY.Value + 19, 9, 9);
|
||
g.DrawEllipse(pen, _startPosX.Value, _startPosY.Value + 19, 9, 9);
|
||
g.FillEllipse(BodyBrush, _startPosX.Value + 53, _startPosY.Value + 19, 9, 9);
|
||
g.DrawEllipse(pen, _startPosX.Value + 53, _startPosY.Value + 19, 9, 9);
|
||
|
||
g.FillEllipse(BodyBrush, _startPosX.Value + 15, _startPosY.Value + 23, 6, 6);
|
||
g.DrawEllipse(pen, _startPosX.Value + 15, _startPosY.Value + 23, 6, 6);
|
||
g.FillEllipse(BodyBrush, _startPosX.Value + 28, _startPosY.Value + 23, 6, 6);
|
||
g.DrawEllipse(pen, _startPosX.Value + 28, _startPosY.Value + 23, 6, 6);
|
||
g.FillEllipse(BodyBrush, _startPosX.Value + 40, _startPosY.Value + 23, 6, 6);
|
||
g.DrawEllipse(pen, _startPosX.Value + 40, _startPosY.Value + 23, 6, 6);
|
||
|
||
g.FillEllipse(BodyBrush, _startPosX.Value + 23, _startPosY.Value + 19, 4, 4);
|
||
g.DrawEllipse(pen, _startPosX.Value + 23, _startPosY.Value + 19, 4, 4);
|
||
g.FillEllipse(BodyBrush, _startPosX.Value + 35, _startPosY.Value + 19, 4, 4);
|
||
g.DrawEllipse(pen, _startPosX.Value + 35, _startPosY.Value + 19, 4, 4);
|
||
|
||
|
||
}
|
||
|
||
}
|