Созданы методы в DrawningLocomotive для сохранения данных об объекте и загрузки объекта

This commit is contained in:
Данила Мочалов 2022-11-19 12:51:23 +04:00
parent 64a31c1a76
commit 1812efd16e
5 changed files with 84 additions and 0 deletions

View File

@ -150,4 +150,57 @@ public class DrawningLocomotive {
{ {
return new float[] {/*UP*/_startPosY, /*RIGHT*/ _startPosX + _locomotiveWidth, /*DOWN*/ _startPosY + _locomotiveHeight, /*LEFT*/ _startPosX}; 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] == "Simple") drawningExtra = new ExtraWheelsDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor());
if (strs[5] == "Star") drawningExtra = new ExtraStarWheelDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor());
if (strs[5] == "Round") drawningExtra = new ExtraRoundWheelDraw(Integer.parseInt(strs[6]), Locomotive.getBodyColor());
if (drawningExtra == 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.getBoolean(strs[11])
);
return new DrawningWarmlyLocomotive(Locomotive, drawningExtra);
}
return null;
}
} }

View File

@ -19,6 +19,15 @@ public class ExtraRoundWheelDraw implements IDrawningExtra{
} }
} }
public String TypeString() {
return "Round";
}
public int getWheelsCount() {
if (wheelsCount == WheelsCount.Two) return 2;
if (wheelsCount == WheelsCount.Three) return 3;
else return 4;
}
public ExtraRoundWheelDraw (int num, Color bodyColor) { public ExtraRoundWheelDraw (int num, Color bodyColor) {
setExtraNum(num); setExtraNum(num);
extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor);

View File

@ -19,6 +19,16 @@ public class ExtraStarWheelDraw implements IDrawningExtra{
} }
} }
public String TypeString() {
return "Star";
}
public int getWheelsCount() {
if (wheelsCount == WheelsCount.Two) return 2;
if (wheelsCount == WheelsCount.Three) return 3;
else return 4;
}
public ExtraStarWheelDraw (int num, Color bodyColor) { public ExtraStarWheelDraw (int num, Color bodyColor) {
setExtraNum(num); setExtraNum(num);
extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor); extraWheelsDraw = new ExtraWheelsDraw(num, bodyColor);

View File

@ -26,6 +26,16 @@ public class ExtraWheelsDraw implements IDrawningExtra{
this.color = color; this.color = color;
} }
public String TypeString() {
return "Simple";
}
public int getWheelsCount() {
if (wheelsCount == WheelsCount.Two) return 2;
if (wheelsCount == WheelsCount.Three) return 3;
else return 4;
}
public void DrawExtra(int startPosX, int startPosY, Graphics2D g) { public void DrawExtra(int startPosX, int startPosY, Graphics2D g) {
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.drawOval(startPosX, startPosY + 30, 20, 20); g.drawOval(startPosX, startPosY + 30, 20, 20);

View File

@ -4,4 +4,6 @@ public interface IDrawningExtra {
void setExtraNum(int num); void setExtraNum(int num);
void DrawExtra(int startPosX, int startPosY, Graphics2D g); void DrawExtra(int startPosX, int startPosY, Graphics2D g);
void SetColor(Color color); void SetColor(Color color);
String TypeString();
int getWheelsCount();
} }