PIBD-14_Lavrova_K.I._Simple/solution/lab1/DrawningEntityFighter.cs

242 lines
9.1 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.

namespace lab1;
/// <summary>
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
/// </summary>
public class DrawningEntityFighter
{
/// <summary>
/// Класс-сущность
/// </summary>
public EntityFighter? EntityFighter { 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 _drawningFighterWidth = 91;
/// <summary>
/// Высота прорисовки истребителя
/// </summary>
private readonly int _drawningFighterHeight = 65;
/// <summary>
/// Инициализация свойств
/// </summary>
/// <param name="speed">Скорость</param>
/// <param name="weight">>Вес истребителя</param>
/// <param name="bodyColor">Основной цвет</param>
/// <param name="additionalColor">Дополнительный цвет</param>
/// <param name="rockets">Признак наличия ракет</param>
/// <param name="extraWings">Признак наличия доп. крыльев</param>
public void Init(int speed, double weight, Color bodyColor, Color additionalColor, bool rockets, bool extraWings)
{
EntityFighter = new EntityFighter();
EntityFighter.Init(speed, weight, bodyColor, additionalColor, rockets, extraWings);
_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)
{
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 (EntityFighter == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return false;
}
switch (direction)
{
//влево
case DirectionType.Left:
if (_startPosX.Value - EntityFighter.Step > 0)
{
_startPosX -= (int)EntityFighter.Step;
}
return true;
//Вверх
case DirectionType.Up:
if (_startPosY.Value - EntityFighter.Step > 0)
{
_startPosY -= (int)EntityFighter.Step;
}
return true;
//Вправо
case DirectionType.Right:
if (_startPosX.Value + EntityFighter.Step + _drawningFighterWidth < _pictureWidth)
{
_startPosX += (int)EntityFighter.Step;
}
return true;
//Вниз
case DirectionType.Down:
if (_startPosY.Value + EntityFighter.Step + _drawningFighterHeight < _pictureHeight)
{
_startPosY += (int)EntityFighter.Step;
}
return true;
default:
return false;
}
}
/// <summary>
/// Прорисовка объекта
/// </summary>
/// <param name="g"></param>
public void DrawTransport(Graphics g)
{
if (EntityFighter == null || !_startPosX.HasValue || !_startPosY.HasValue)
{
return;
}
Pen pen = new(Color.Black);
Brush additionalBrush = new SolidBrush(EntityFighter.AdditionalColor);
Brush BodyBrush = new SolidBrush(EntityFighter.BodyColor);
Brush CraneBrush = new SolidBrush(EntityFighter.AdditionalColor);
//корпус
g.FillRectangle(BodyBrush, _startPosX.Value + 10, _startPosY.Value + 30, 62, 12);
g.DrawRectangle(pen, _startPosX.Value + 10, _startPosY.Value + 30, 62, 12);
g.FillRectangle(BodyBrush, _startPosX.Value + 22, _startPosY.Value + 16, 4, 15);
g.DrawRectangle(pen, _startPosX.Value + 22, _startPosY.Value + 16, 4, 14);
g.FillRectangle(BodyBrush, _startPosX.Value + 37, _startPosY.Value + 1, 5, 30);
g.DrawRectangle(pen, _startPosX.Value + 37, _startPosY.Value + 1, 5, 30);
//стекло
g.FillRectangle(additionalBrush, _startPosX.Value + 52, _startPosY.Value + 14, 21, 16);
g.DrawRectangle(pen, _startPosX.Value + 52, _startPosY.Value + 14, 20, 16);
//гусеницы
g.DrawLine(pen, _startPosX.Value + 8, _startPosY.Value + 42, _startPosX.Value + 10, _startPosY.Value + 42);
g.DrawLine(pen, _startPosX.Value + 7, _startPosY.Value + 43, _startPosX.Value + 7, _startPosY.Value + 53);
g.DrawLine(pen, _startPosX.Value + 8, _startPosY.Value + 54, _startPosX.Value + 12, _startPosY.Value + 54);
g.DrawLine(pen, _startPosX.Value + 13, _startPosY.Value + 55, _startPosX.Value + 69, _startPosY.Value + 55);
g.DrawLine(pen, _startPosX.Value + 70, _startPosY.Value + 54, _startPosX.Value + 73, _startPosY.Value + 54);
g.DrawLine(pen, _startPosX.Value + 74, _startPosY.Value + 53, _startPosX.Value + 74, _startPosY.Value + 43);
g.DrawLine(pen, _startPosX.Value + 71, _startPosY.Value + 42, _startPosX.Value + 73, _startPosY.Value + 42);
//колеса
g.FillEllipse(BodyBrush, _startPosX.Value + 10, _startPosY.Value + 44, 9, 9);
g.DrawEllipse(pen, _startPosX.Value + 10, _startPosY.Value + 44, 9, 9);
g.FillEllipse(BodyBrush, _startPosX.Value + 63, _startPosY.Value + 44, 9, 9);
g.DrawEllipse(pen, _startPosX.Value + 63, _startPosY.Value + 44, 9, 9);
g.FillEllipse(BodyBrush, _startPosX.Value + 25, _startPosY.Value + 48, 6, 6);
g.DrawEllipse(pen, _startPosX.Value + 25, _startPosY.Value + 48, 6, 6);
g.FillEllipse(BodyBrush, _startPosX.Value + 38, _startPosY.Value + 48, 6, 6);
g.DrawEllipse(pen, _startPosX.Value + 38, _startPosY.Value + 48, 6, 6);
g.FillEllipse(BodyBrush, _startPosX.Value + 50, _startPosY.Value + 48, 6, 6);
g.DrawEllipse(pen, _startPosX.Value + 50, _startPosY.Value + 48, 6, 6);
g.FillEllipse(BodyBrush, _startPosX.Value + 33, _startPosY.Value + 44, 4, 4);
g.DrawEllipse(pen, _startPosX.Value + 33, _startPosY.Value + 44, 4, 4);
g.FillEllipse(BodyBrush, _startPosX.Value + 45, _startPosY.Value + 44, 4, 4);
g.DrawEllipse(pen, _startPosX.Value + 45, _startPosY.Value + 44, 4, 4);
if (EntityFighter.Kovsh)
{
//ковш
g.DrawRectangle(pen, _startPosX.Value - 2, _startPosY.Value + 37, 8, 15);
g.FillRectangle(CraneBrush, _startPosX.Value - 2, _startPosY.Value + 37, 8, 15);
g.DrawRectangle(pen, _startPosX.Value - -6, _startPosY.Value + 37, 4, 1);
}
//противовес
if (EntityFighter.Otval)
{
g.DrawRectangle(pen, _startPosX.Value + 73, _startPosY.Value + 37, 17, 1);
g.DrawRectangle(pen, _startPosX.Value + 90, _startPosY.Value + 37, 1, 17);
}
}
}