2022-09-24 17:19:00 +04:00
|
|
|
import java.awt.*;
|
2022-10-08 17:29:51 +04:00
|
|
|
import java.util.Random;
|
2022-09-24 17:19:00 +04:00
|
|
|
|
2022-10-21 16:38:26 +04:00
|
|
|
public class DrawningLocomotive {
|
2022-09-24 17:19:00 +04:00
|
|
|
public EntityLocomotive Locomotive;
|
2022-10-11 16:29:28 +04:00
|
|
|
public IDrawningExtra drawningExtra;
|
2022-09-24 17:19:00 +04:00
|
|
|
/// Левая координата отрисовки локомотива
|
2022-10-08 17:54:48 +04:00
|
|
|
protected float _startPosX;
|
2022-09-24 17:19:00 +04:00
|
|
|
/// Верхняя координата отрисовки локомотива
|
2022-10-08 17:54:48 +04:00
|
|
|
protected float _startPosY;
|
2022-09-24 17:19:00 +04:00
|
|
|
/// Ширина окна отрисовки
|
|
|
|
private Integer _pictureWidth = null;
|
|
|
|
/// Высота окна отрисовки
|
|
|
|
private Integer _pictureHeight = null;
|
|
|
|
/// Ширина отрисовки локомотива
|
2022-10-08 17:54:48 +04:00
|
|
|
private int _locomotiveWidth = 110;
|
2022-09-24 17:19:00 +04:00
|
|
|
/// Высота отрисовки локомотива
|
2022-10-08 17:54:48 +04:00
|
|
|
private int _locomotiveHeight = 50;
|
2022-09-24 17:19:00 +04:00
|
|
|
/// Инициализация свойств
|
2022-10-08 17:29:51 +04:00
|
|
|
private final Random random = new Random();
|
2022-11-15 20:22:49 +04:00
|
|
|
public DrawningLocomotive(int speed, float weight, Color bodyColor, int wheelsCount)
|
2022-09-24 17:19:00 +04:00
|
|
|
{
|
2022-11-15 20:22:49 +04:00
|
|
|
/*int randExtra = random.nextInt(2);
|
2022-10-11 16:29:28 +04:00
|
|
|
switch (random.nextInt(3)){
|
|
|
|
case 0:
|
2022-10-22 01:15:02 +04:00
|
|
|
drawningExtra = new ExtraStarWheelDraw(randExtra, bodyColor);
|
2022-10-11 16:29:28 +04:00
|
|
|
break;
|
|
|
|
case 1:
|
2022-10-22 01:15:02 +04:00
|
|
|
drawningExtra = new ExtraRoundWheelDraw(randExtra, bodyColor);
|
2022-10-11 16:29:28 +04:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
drawningExtra = new ExtraWheelsDraw(randExtra, bodyColor);
|
|
|
|
break;
|
2022-11-15 20:22:49 +04:00
|
|
|
}*/
|
|
|
|
drawningExtra = new ExtraWheelsDraw(wheelsCount, bodyColor);
|
2022-10-08 17:29:51 +04:00
|
|
|
Locomotive = new EntityLocomotive(speed, weight, bodyColor);
|
2022-09-24 17:19:00 +04:00
|
|
|
}
|
2022-10-08 17:54:48 +04:00
|
|
|
|
2022-10-22 01:15:02 +04:00
|
|
|
public DrawningLocomotive(EntityLocomotive locomotive, IDrawningExtra extra) {
|
|
|
|
drawningExtra = extra;
|
|
|
|
Locomotive = locomotive;
|
|
|
|
}
|
|
|
|
|
2022-10-08 17:54:48 +04:00
|
|
|
// Новый конструктор
|
2022-11-15 20:22:49 +04:00
|
|
|
protected DrawningLocomotive (int speed, float weight, Color bodyColor, int wheelsCount, int locomotiveWidth, int locomotiveHeight)
|
2022-10-08 17:54:48 +04:00
|
|
|
{
|
2022-11-15 20:22:49 +04:00
|
|
|
this(speed, weight, bodyColor, wheelsCount);
|
2022-10-08 17:54:48 +04:00
|
|
|
_locomotiveWidth = locomotiveWidth;
|
|
|
|
_locomotiveHeight = locomotiveHeight;
|
|
|
|
}
|
|
|
|
|
2022-11-15 20:22:49 +04:00
|
|
|
public void SetColor(Color color) {
|
|
|
|
Locomotive.SetColor(color);
|
|
|
|
}
|
|
|
|
|
2022-09-24 17:19:00 +04:00
|
|
|
/// Установка позиции локомотива
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 19:51:41 +04:00
|
|
|
public void DrawTransport(Graphics2D g) {
|
2022-09-24 18:00:56 +04:00
|
|
|
if (_startPosX < 0 || _startPosY < 0
|
|
|
|
|| _pictureHeight == null || _pictureWidth == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//тело
|
|
|
|
g.setColor(Locomotive.getBodyColor());
|
2022-10-08 23:13:09 +04:00
|
|
|
g.fillRect((int)_startPosX , (int)_startPosY, 110 - 10, 50 - 10);
|
|
|
|
//окна
|
|
|
|
g.setColor(Color.BLUE);
|
2022-09-24 18:00:56 +04:00
|
|
|
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);
|
2022-10-08 23:13:09 +04:00
|
|
|
g.fillRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20);
|
|
|
|
//extra
|
2022-10-11 16:29:28 +04:00
|
|
|
drawningExtra.DrawExtra((int)_startPosX, (int)_startPosY, g);
|
2022-09-24 18:00:56 +04:00
|
|
|
//движок
|
|
|
|
g.setColor(Locomotive.getBodyColor());
|
|
|
|
g.fillRect((int)_startPosX + 100, (int)_startPosY + 10, 10, 30);
|
2022-09-24 17:19:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ChangeBorders(int width, int height)
|
|
|
|
{
|
|
|
|
_pictureWidth = width;
|
2022-09-24 21:33:44 +04:00
|
|
|
_pictureHeight = height - 75;
|
2022-09-24 17:19:00 +04:00
|
|
|
if (_pictureWidth <= _locomotiveWidth || _pictureHeight <= _locomotiveHeight)
|
|
|
|
{
|
|
|
|
_pictureWidth = null;
|
|
|
|
_pictureHeight = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_startPosX + _locomotiveWidth > _pictureWidth)
|
|
|
|
{
|
|
|
|
_startPosX = _pictureWidth - _locomotiveWidth;
|
|
|
|
}
|
|
|
|
if (_startPosY + _locomotiveHeight > _pictureHeight)
|
|
|
|
{
|
|
|
|
_startPosY = _pictureHeight - _locomotiveHeight;
|
|
|
|
}
|
|
|
|
}
|
2022-10-08 17:54:48 +04:00
|
|
|
|
|
|
|
// Получение текущей позиции объекта
|
|
|
|
public float[] GetCurrentPosition()
|
|
|
|
{
|
|
|
|
return new float[] {/*UP*/_startPosY, /*RIGHT*/ _startPosX + _locomotiveWidth, /*DOWN*/ _startPosY + _locomotiveHeight, /*LEFT*/ _startPosX};
|
|
|
|
}
|
2022-09-24 17:19:00 +04:00
|
|
|
}
|