219 lines
7.7 KiB
C#
219 lines
7.7 KiB
C#
using ElectricLocomotive.Entities;
|
||
using ElectricLocomotive.MovementStrategy;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace ElectricLocomotive.DrawningObject
|
||
{
|
||
/// <summary>
|
||
/// Класс, отвечающий за прорисовку и перемещение объекта-сущности
|
||
/// </summary>
|
||
public class DrawningLocomotive
|
||
{
|
||
public EntityLocomotive? EntityLocomotive { get; protected set; }
|
||
|
||
public int _pictureWidth;
|
||
|
||
public int _pictureHeight;
|
||
|
||
protected int _startPosX;
|
||
|
||
protected int _startPosY;
|
||
|
||
protected readonly int _locoWidth = 95;
|
||
|
||
protected readonly int _locoHeight = 50;
|
||
public int GetPosX => _startPosX;
|
||
public int GetPosY => _startPosY;
|
||
public int GetWidth => _locoWidth;
|
||
public int GetHeight => _locoHeight;
|
||
|
||
public DrawningLocomotive(int speed, double weight, Color bodyColor, int width, int heigth)
|
||
{
|
||
if (width < _locoWidth || heigth < _locoHeight)
|
||
{
|
||
return;
|
||
}
|
||
_pictureWidth = width;
|
||
_pictureHeight = heigth;
|
||
EntityLocomotive = new EntityLocomotive(speed, weight, bodyColor);
|
||
}
|
||
|
||
protected DrawningLocomotive(int speed, double weight, Color bodyColor, Color additionalColor, int width,
|
||
int height, int locoWidth, int locoHeight)
|
||
{
|
||
if (width < _locoWidth || height < _locoHeight)
|
||
{
|
||
return;
|
||
}
|
||
_pictureWidth = width;
|
||
_pictureHeight = height;
|
||
_locoWidth = locoWidth;
|
||
_locoHeight = locoHeight;
|
||
EntityLocomotive = new EntityLocomotive(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 + _locoWidth > _pictureWidth)
|
||
{
|
||
x = _pictureWidth - _locoWidth;
|
||
}
|
||
if (y < 0 || y + _locoHeight > _pictureHeight)
|
||
{
|
||
y = _pictureHeight - _locoHeight;
|
||
}
|
||
_startPosX = x;
|
||
_startPosY = y;
|
||
}
|
||
/// <summary>
|
||
/// Изменение направления перемещения
|
||
/// </summary>
|
||
/// <param name="direction">Направление</param>
|
||
|
||
public void MoveTransport(DirectionType direction)
|
||
{
|
||
if (EntityLocomotive == null)
|
||
{
|
||
return;
|
||
}
|
||
switch (direction)
|
||
{
|
||
case DirectionType.Left:
|
||
if (_startPosX - EntityLocomotive.Step > 0)
|
||
{
|
||
_startPosX -= (int)EntityLocomotive.Step;
|
||
}
|
||
break;
|
||
case DirectionType.Up:
|
||
if (_startPosY - EntityLocomotive.Step > 0)
|
||
{
|
||
_startPosY -= (int)EntityLocomotive.Step;
|
||
}
|
||
break;
|
||
case DirectionType.Right:
|
||
if (_startPosX + EntityLocomotive.Step + _locoWidth < _pictureWidth)
|
||
{
|
||
_startPosX += (int)EntityLocomotive.Step;
|
||
}
|
||
break;
|
||
case DirectionType.Down:
|
||
if (_startPosY + EntityLocomotive.Step + _locoHeight < _pictureHeight)
|
||
{
|
||
_startPosY += (int)EntityLocomotive.Step;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Прорисовка объекта
|
||
/// </summary>
|
||
/// <param name="g"></param>
|
||
public virtual void DrawTransport(Graphics g)
|
||
{
|
||
if (EntityLocomotive == null)
|
||
{
|
||
return;
|
||
}
|
||
Pen pen = new(Color.Black);
|
||
Brush brush = new SolidBrush(EntityLocomotive.BodyColor);
|
||
///ВЛ60к-1595
|
||
|
||
///ходовая
|
||
g.FillRectangle(brush, new Rectangle(_startPosX + 20, _startPosY + 45, 15, 5));
|
||
g.FillRectangle(brush, new Rectangle(_startPosX + 65, _startPosY + 45, 15, 5));
|
||
|
||
g.FillEllipse(brush, _startPosX + 15, _startPosY + 45, 10, 10);
|
||
g.FillEllipse(brush, _startPosX + 30, _startPosY + 45, 10, 10);
|
||
g.FillEllipse(brush, _startPosX + 60, _startPosY + 45, 10, 10);
|
||
g.FillEllipse(brush, _startPosX + 75, _startPosY + 45, 10, 10);
|
||
PointF[] chassis =
|
||
{
|
||
new Point(_startPosX+10, _startPosY+50),
|
||
new Point(_startPosX, _startPosY+50),
|
||
new Point(_startPosX+10, _startPosY +40),
|
||
new Point(_startPosX + 90, _startPosY+40),
|
||
new Point(_startPosX+100, _startPosY+50),
|
||
new Point(_startPosX+90,_startPosY+ 50),
|
||
new Point(_startPosX+85, _startPosY+45),
|
||
new Point(_startPosX+15, _startPosY+45),
|
||
};
|
||
g.FillPolygon(brush, chassis);
|
||
|
||
///обводка ходовой
|
||
Point[] chassisCont =
|
||
{
|
||
new Point(_startPosX+10, _startPosY+50),
|
||
new Point(_startPosX, _startPosY+50),
|
||
new Point(_startPosX+10, _startPosY +40),
|
||
new Point(_startPosX + 90, _startPosY+40),
|
||
new Point(_startPosX+100, _startPosY+50),
|
||
new Point(_startPosX+90,_startPosY+ 50),
|
||
new Point(_startPosX+85, _startPosY+45),
|
||
new Point(_startPosX+15, _startPosY+45),
|
||
};
|
||
g.DrawPolygon(pen, chassisCont);
|
||
|
||
///кабина
|
||
Point[] cabin =
|
||
{
|
||
new Point(_startPosX+20, _startPosY+10),
|
||
new Point(_startPosX+90,_startPosY+10),
|
||
new Point(_startPosX+90,_startPosY + 40),
|
||
new Point(_startPosX+10,_startPosY+40),
|
||
new Point(_startPosX+10,_startPosY+25)
|
||
};
|
||
g.DrawPolygon(pen, cabin);
|
||
g.DrawLine(pen, _startPosX + 10, _startPosY + 25, _startPosX + 90, _startPosY + 25);
|
||
|
||
///окна
|
||
g.FillRectangle(brush, _startPosX + 20, _startPosY + 15, 10, 10);
|
||
g.FillRectangle(brush, _startPosX + 35, _startPosY + 15, 10, 10);
|
||
|
||
g.FillRectangle(brush, _startPosX + 75, _startPosY + 15, 10, 10);
|
||
|
||
///дверь
|
||
g.FillRectangle(brush, _startPosX + 50, _startPosY + 15, 10, 25);
|
||
|
||
///Батарея
|
||
g.FillRectangle(brush, _startPosX + 45, _startPosY + 5, 15, 5);
|
||
|
||
}
|
||
|
||
public bool CanMove(DirectionType direction)
|
||
{
|
||
if (EntityLocomotive == null)
|
||
{
|
||
return false;
|
||
}
|
||
return direction switch
|
||
{
|
||
//влево
|
||
DirectionType.Left => _startPosX - EntityLocomotive.Step > 0,
|
||
//вверх
|
||
DirectionType.Up => _startPosY - EntityLocomotive.Step > 0,
|
||
// вправо
|
||
DirectionType.Right => _startPosX + EntityLocomotive.Step < _pictureWidth,
|
||
//вниз
|
||
DirectionType.Down => _startPosY + EntityLocomotive.Step < _pictureHeight,
|
||
};
|
||
}
|
||
|
||
public IMoveableObject GetMoveableObject => new DrawningObjectLocomotive(this);
|
||
|
||
public void SetBodyColor(Color color)
|
||
{
|
||
EntityLocomotive?.SetBodyColor(color);
|
||
}
|
||
}
|
||
}
|