199 lines
7.6 KiB
C#
199 lines
7.6 KiB
C#
using WarmlyLocomotive.Entities;
|
|
using WarmlyLocomotive.MovementStrategy;
|
|
|
|
namespace WarmlyLocomotive.DrawningObjects
|
|
{
|
|
/// <summary>
|
|
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
|
/// </summary>
|
|
public class DrawningWarmlyLocomotive
|
|
{
|
|
/// <summary>
|
|
/// Класс-сущность
|
|
/// </summary>
|
|
public EntityWarmlyLocomotive? EntityWarmlyLocomotive { get; protected set; }
|
|
public IMoveableObject GetMoveableObject => new DrawningObjectWarmlyLocomotive(this);
|
|
/// <summary>
|
|
/// Ширина окна
|
|
/// </summary>
|
|
private int _pictureWidth;
|
|
/// <summary>
|
|
/// Высота окна
|
|
/// </summary>
|
|
private int _pictureHeight;
|
|
/// <summary>
|
|
/// Левая координата прорисовки тепловоза
|
|
/// </summary>
|
|
protected int _startPosX;
|
|
/// <summary>
|
|
/// Верхняя кооридната прорисовки тепловоза
|
|
/// </summary>
|
|
protected int _startPosY;
|
|
/// <summary>
|
|
/// Ширина прорисовки тепловоза
|
|
/// </summary>
|
|
protected readonly int _WarmlyLocomotiveWidth = 200;
|
|
/// <summary>
|
|
/// Высота прорисовки тепловоза
|
|
/// </summary>
|
|
protected readonly int _WarmlyLocomotiveHeight = 75;
|
|
private readonly int _trumpetHeight = 25;
|
|
public int GetPosX => _startPosX;
|
|
/// <summary>
|
|
/// Координата Y объекта
|
|
/// </summary>
|
|
public int GetPosY => _startPosY;
|
|
/// <summary>
|
|
/// Ширина объекта
|
|
/// </summary>
|
|
public int GetWidth => _WarmlyLocomotiveWidth;
|
|
/// <summary>
|
|
/// Высота объекта
|
|
/// </summary>
|
|
public int GetHeight => _WarmlyLocomotiveHeight;
|
|
public DrawningWarmlyLocomotive(int speed, double weight, Color bodyColor, int width, int height)
|
|
{
|
|
if (width < _WarmlyLocomotiveWidth || height < _WarmlyLocomotiveHeight)
|
|
{
|
|
return;
|
|
}
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
|
|
EntityWarmlyLocomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor);
|
|
}
|
|
protected DrawningWarmlyLocomotive(int speed, double weight, Color bodyColor, int
|
|
width, int height, int carWidth, int carHeight)
|
|
{
|
|
if (width <= _WarmlyLocomotiveWidth || height <= _WarmlyLocomotiveHeight)
|
|
{
|
|
return;
|
|
}
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
_WarmlyLocomotiveWidth = carWidth;
|
|
_WarmlyLocomotiveHeight = carHeight;
|
|
EntityWarmlyLocomotive = new EntityWarmlyLocomotive(speed, weight, bodyColor);
|
|
}
|
|
/// <summary>
|
|
/// Установка позиции
|
|
/// </summary>
|
|
/// <param name="x">Координата X</param>
|
|
/// <param name="y">Координата Y</param>
|
|
public void SetPosition(int x, int y)
|
|
{
|
|
if (x < 0 || x >= _pictureWidth || y < 0 || y >= _pictureHeight)
|
|
{
|
|
_startPosX = 0;
|
|
_startPosY = 0;
|
|
}
|
|
_startPosX = x;
|
|
_startPosY = y;
|
|
|
|
}
|
|
public bool CanMove(Direction direction)
|
|
{
|
|
if (EntityWarmlyLocomotive == null)
|
|
{
|
|
return false;
|
|
}
|
|
return direction switch
|
|
{
|
|
//влево
|
|
Direction.Left => _startPosX - EntityWarmlyLocomotive.Step > 0,
|
|
//вверх
|
|
Direction.Up => _startPosY - EntityWarmlyLocomotive.Step > 0,
|
|
// вправо
|
|
Direction.Right => _startPosX + EntityWarmlyLocomotive.Step + _WarmlyLocomotiveWidth < _pictureWidth,
|
|
//вниз
|
|
Direction.Down => _startPosY + EntityWarmlyLocomotive.Step + _WarmlyLocomotiveHeight < _pictureHeight,
|
|
_ => false,
|
|
};
|
|
}
|
|
/// <param name="direction">Направление</param>
|
|
public void MoveTransport(Direction direction)
|
|
{
|
|
if (!CanMove(direction) || EntityWarmlyLocomotive == null)
|
|
{
|
|
return;
|
|
}
|
|
switch (direction)
|
|
{
|
|
//влево
|
|
case Direction.Left:
|
|
if (_startPosX - EntityWarmlyLocomotive.Step > 0)
|
|
{
|
|
_startPosX -= (int)EntityWarmlyLocomotive.Step;
|
|
}
|
|
break;
|
|
//вверх
|
|
case Direction.Up:
|
|
if (_startPosY - _trumpetHeight - EntityWarmlyLocomotive.Step > 0)
|
|
{
|
|
_startPosY -= (int)EntityWarmlyLocomotive.Step;
|
|
}
|
|
break;
|
|
// вправо
|
|
case Direction.Right:
|
|
|
|
if (_startPosX + _WarmlyLocomotiveWidth + EntityWarmlyLocomotive.Step < _pictureWidth)
|
|
{
|
|
_startPosX += (int)EntityWarmlyLocomotive.Step;
|
|
}
|
|
break;
|
|
//вниз
|
|
case Direction.Down:
|
|
if (_startPosY + _WarmlyLocomotiveHeight + EntityWarmlyLocomotive.Step < _pictureHeight)
|
|
{
|
|
_startPosY += (int)EntityWarmlyLocomotive.Step;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// Прорисовка объекта
|
|
/// </summary>
|
|
/// <param name="g"></param>
|
|
/// <summary>
|
|
/// Прорисовка объекта
|
|
/// </summary>
|
|
/// <param name="g"></param>
|
|
public virtual void DrawTransport(Graphics g)
|
|
{
|
|
if (EntityWarmlyLocomotive == null)
|
|
{
|
|
return;
|
|
}
|
|
Pen pen = new(Color.Black, 2);
|
|
Brush bodyBrush = new SolidBrush(EntityWarmlyLocomotive.BodyColor);
|
|
Brush wheelBrush = new SolidBrush(Color.Black);
|
|
|
|
//корпус
|
|
g.FillRectangle(bodyBrush, _startPosX + 50, _startPosY, 150, 50);
|
|
Point[] Points = { new Point(_startPosX + 50, _startPosY + 25), new Point(_startPosX + 125, _startPosY + 25) };
|
|
g.DrawPolygon(pen, Points);
|
|
Point[] Points2 = { new Point(_startPosX + 150, _startPosY + 25), new Point(_startPosX + 200, _startPosY + 25) };
|
|
g.DrawPolygon(pen, Points2);
|
|
//окна
|
|
g.DrawRectangle(pen, _startPosX + 125, _startPosY + 10, 25, 30);
|
|
g.DrawRectangle(pen, _startPosX + 60, _startPosY + 7, 10, 13);
|
|
g.DrawRectangle(pen, _startPosX + 160, _startPosY + 7, 10, 13);
|
|
g.DrawRectangle(pen, _startPosX + 175, _startPosY + 7, 10, 13);
|
|
//колеса
|
|
g.FillEllipse(wheelBrush, _startPosX + 60, _startPosY + 50, 20, 20);
|
|
g.FillEllipse(wheelBrush, _startPosX + 85, _startPosY + 50, 20, 20);
|
|
g.FillEllipse(wheelBrush, _startPosX + 145, _startPosY + 50, 20, 20);
|
|
g.FillEllipse(wheelBrush, _startPosX + 170, _startPosY + 50, 20, 20);
|
|
}
|
|
public void ChangePictureBoxSize(int pictureBoxWidth, int pictureBoxHeight)
|
|
{
|
|
_pictureWidth = pictureBoxWidth;
|
|
_pictureHeight = pictureBoxHeight;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|