PIbd-23_Mochalov_D.V._Locom.../DrawningLocomotive.java
2022-11-19 19:58:47 +04:00

208 lines
8.2 KiB
Java

import java.awt.*;
import java.util.Objects;
import java.util.Random;
public class DrawningLocomotive {
public EntityLocomotive Locomotive;
public IDrawningExtra drawningExtra;
/// Левая координата отрисовки локомотива
protected float _startPosX;
/// Верхняя координата отрисовки локомотива
protected float _startPosY;
/// Ширина окна отрисовки
private Integer _pictureWidth = null;
/// Высота окна отрисовки
private Integer _pictureHeight = null;
/// Ширина отрисовки локомотива
private int _locomotiveWidth = 110;
/// Высота отрисовки локомотива
private int _locomotiveHeight = 50;
/// Инициализация свойств
private final Random random = new Random();
public DrawningLocomotive(int speed, float weight, Color bodyColor, int wheelsCount)
{
drawningExtra = new ExtraWheelsDraw(wheelsCount, bodyColor);
Locomotive = new EntityLocomotive(speed, weight, bodyColor);
}
public DrawningLocomotive(EntityLocomotive locomotive, IDrawningExtra extra) {
drawningExtra = extra;
Locomotive = locomotive;
}
// Новый конструктор
protected DrawningLocomotive (int speed, float weight, Color bodyColor, int wheelsCount, int locomotiveWidth, int locomotiveHeight)
{
this(speed, weight, bodyColor, wheelsCount);
_locomotiveWidth = locomotiveWidth;
_locomotiveHeight = locomotiveHeight;
}
public void SetColor(Color color) {
Locomotive.SetColor(color);
}
/// Установка позиции локомотива
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(Locomotive.getBodyColor());
g.fillRect((int)_startPosX , (int)_startPosY, 110 - 10, 50 - 10);
//окна
g.setColor(Color.BLUE);
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.fillRect( (int)_startPosX + 50, (int)_startPosY + 10, 10, 20);
//extra
drawningExtra.DrawExtra((int)_startPosX, (int)_startPosY, g);
//движок
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 - 75;
if (_pictureWidth <= _locomotiveWidth || _pictureHeight <= _locomotiveHeight)
{
_pictureWidth = null;
_pictureHeight = null;
return;
}
if (_startPosX + _locomotiveWidth > _pictureWidth)
{
_startPosX = _pictureWidth - _locomotiveWidth;
}
if (_startPosY + _locomotiveHeight > _pictureHeight)
{
_startPosY = _pictureHeight - _locomotiveHeight;
}
}
// Получение текущей позиции объекта
public float[] GetCurrentPosition()
{
return new float[] {/*UP*/_startPosY, /*RIGHT*/ _startPosX + _locomotiveWidth, /*DOWN*/ _startPosY + _locomotiveHeight, /*LEFT*/ _startPosX};
}
private static final char _separatorForObject = ':';
public String getDataForSave()
{
var str = "" + Locomotive.getSpeed() + _separatorForObject
+ Locomotive.getWeight() + _separatorForObject
+ Locomotive.getBodyColor().getRed() + _separatorForObject
+ Locomotive.getBodyColor().getGreen() + _separatorForObject
+ Locomotive.getBodyColor().getBlue() + _separatorForObject
+ drawningExtra.TypeString() + _separatorForObject
+ drawningExtra.getWheelsCount();
if (!(Locomotive instanceof EntityWarmlyLocomotive))
{
return str;
}
return str + _separatorForObject
+ ((EntityWarmlyLocomotive) Locomotive).ExtraColor.getRed() + _separatorForObject
+ ((EntityWarmlyLocomotive) Locomotive).ExtraColor.getGreen() + _separatorForObject
+ ((EntityWarmlyLocomotive) Locomotive).ExtraColor.getBlue() + _separatorForObject
+ ((EntityWarmlyLocomotive) Locomotive).Pipe + _separatorForObject
+ ((EntityWarmlyLocomotive) Locomotive).FuelStorage;
}
public static DrawningLocomotive createDrawningLocomotive(String info) {
IDrawningExtra drawningExtra = null;
EntityLocomotive Locomotive = null;
String[] strs = info.split(Character.toString(_separatorForObject));
if (strs[5].equals("Simple")) drawningExtra = new ExtraWheelsDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])));
if (strs[5].equals("Star")) drawningExtra = new ExtraStarWheelDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])));
if (Objects.equals(strs[5], "Round")) drawningExtra = new ExtraRoundWheelDraw(Integer.parseInt(strs[6]), new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])));
if (drawningExtra == null) return null;
if (strs.length == 7)
{
Locomotive = new EntityLocomotive(
Integer.parseInt(strs[0]),
Float.parseFloat(strs[1]),
new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4]))
);
return new DrawningLocomotive(Locomotive, drawningExtra);
}
if (strs.length == 12) {
Locomotive = new EntityWarmlyLocomotive(
Integer.parseInt(strs[0]),
Float.parseFloat(strs[1]),
new Color(Integer.parseInt(strs[2]), Integer.parseInt(strs[3]), Integer.parseInt(strs[4])),
new Color(Integer.parseInt(strs[7]), Integer.parseInt(strs[8]), Integer.parseInt(strs[9])),
Boolean.parseBoolean(strs[10]),
Boolean.parseBoolean(strs[11])
);
return new DrawningWarmlyLocomotive(Locomotive, drawningExtra);
}
return null;
}
}