131 lines
4.5 KiB
Java
131 lines
4.5 KiB
Java
import java.awt.*;
|
|
|
|
class DrawningLocomotive {
|
|
public EntityLocomotive Locomotive;
|
|
/// Левая координата отрисовки локомотива
|
|
private float _startPosX;
|
|
/// Верхняя координата отрисовки локомотива
|
|
private float _startPosY;
|
|
/// Ширина окна отрисовки
|
|
private Integer _pictureWidth = null;
|
|
/// Высота окна отрисовки
|
|
private Integer _pictureHeight = null;
|
|
/// Ширина отрисовки локомотива
|
|
private final int _locomotiveWidth = 110;
|
|
/// Высота отрисовки локомотива
|
|
private final int _locomotiveHeight = 50;
|
|
/// Инициализация свойств
|
|
public void Init(int speed, float weight, Color bodyColor, EntityLocomotive entity)
|
|
{
|
|
Locomotive = entity;
|
|
Locomotive.Init(speed, weight, bodyColor);
|
|
}
|
|
/// Установка позиции локомотива
|
|
public void SetPosition(int x, int y, int width, int height)
|
|
{
|
|
if (x < 0 || x + _locomotiveWidth >= width)
|
|
{
|
|
return;
|
|
}
|
|
if (y < 0 || y + _locomotiveHeight >= height)
|
|
{
|
|
return;
|
|
}
|
|
_startPosX = x;
|
|
_startPosY = y;
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
}
|
|
|
|
public void MoveTransport(Direction direction)
|
|
{
|
|
if (_pictureWidth == null || _pictureHeight == null)
|
|
{
|
|
return;
|
|
}
|
|
switch (direction)
|
|
{
|
|
// вправо
|
|
case Right:
|
|
if (_startPosX + _locomotiveWidth + Locomotive.Step() < _pictureWidth)
|
|
{
|
|
_startPosX += Locomotive.Step();
|
|
}
|
|
else _startPosX = _pictureWidth - _locomotiveWidth;
|
|
break;
|
|
//влево
|
|
case Left:
|
|
if (_startPosX - Locomotive.Step() >= 0)
|
|
{
|
|
_startPosX -= Locomotive.Step();
|
|
}
|
|
else _startPosX = 0;
|
|
break;
|
|
//вверх
|
|
case Up:
|
|
if (_startPosY - Locomotive.Step() >= 0)
|
|
{
|
|
_startPosY -= Locomotive.Step();
|
|
}
|
|
else _startPosY = 0;
|
|
break;
|
|
//вниз
|
|
case Down:
|
|
if (_startPosY + _locomotiveHeight + Locomotive.Step() < _pictureHeight)
|
|
{
|
|
_startPosY += Locomotive.Step();
|
|
}
|
|
else _startPosY = _pictureHeight - _locomotiveHeight;
|
|
break;
|
|
}
|
|
}
|
|
|
|
public void DrawTransport(Graphics2D g) {
|
|
if (_startPosX < 0 || _startPosY < 0
|
|
|| _pictureHeight == null || _pictureWidth == null)
|
|
{
|
|
return;
|
|
}
|
|
//тело
|
|
g.setColor(Color.BLACK);
|
|
g.drawRect((int)_startPosX , (int)_startPosY, _locomotiveWidth - 10, _locomotiveHeight - 10);
|
|
//окна
|
|
g.setColor(Locomotive.getBodyColor());
|
|
g.fillRect((int)_startPosX + 10, (int)_startPosY + 10, 10, 10);
|
|
g.fillRect((int)_startPosX + 30, (int)_startPosY + 10, 10, 10);
|
|
g.fillRect((int)_startPosX + 80, (int)_startPosY + 10, 10, 10);
|
|
//дверь
|
|
g.setColor(Color.BLACK);
|
|
g.drawRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20);
|
|
//колеса
|
|
g.drawOval((int) _startPosX, (int)_startPosY + 40, 10, 10);
|
|
g.drawOval((int) _startPosX + 20, (int)_startPosY + 40, 10, 10);
|
|
g.drawOval((int) _startPosX + 70, (int)_startPosY + 40, 10, 10);
|
|
g.drawOval((int) _startPosX + 90, (int)_startPosY + 40, 10, 10);
|
|
//движок
|
|
g.setColor(Locomotive.getBodyColor());
|
|
g.fillRect((int)_startPosX + 100, (int)_startPosY + 10, 10, 30);
|
|
|
|
}
|
|
|
|
public void ChangeBorders(int width, int height)
|
|
{
|
|
_pictureWidth = width;
|
|
_pictureHeight = height;
|
|
if (_pictureWidth <= _locomotiveWidth || _pictureHeight <= _locomotiveHeight)
|
|
{
|
|
_pictureWidth = null;
|
|
_pictureHeight = null;
|
|
return;
|
|
}
|
|
if (_startPosX + _locomotiveWidth > _pictureWidth)
|
|
{
|
|
_startPosX = _pictureWidth - _locomotiveWidth;
|
|
}
|
|
if (_startPosY + _locomotiveHeight > _pictureHeight)
|
|
{
|
|
_startPosY = _pictureHeight - _locomotiveHeight;
|
|
}
|
|
}
|
|
}
|