Added DrawingArtillery serialization and deserialization

This commit is contained in:
Сергей Полевой 2022-11-20 22:20:41 +04:00
parent 63c379da6f
commit ade75a5dc3

68
ArtillerySerde.java Normal file
View File

@ -0,0 +1,68 @@
import java.awt.*;
public class ArtillerySerde { // Artillery Serialization/Deserialization
private static final String _separatorForObject = ":";
public static DrawingArtillery deserialize(String info) {
String[] strings = info.split(_separatorForObject);
int speed = Integer.parseInt(strings[0]);
float weight = Float.parseFloat(strings[1]);
Color bodyColor = new Color(Integer.parseInt(strings[2]));
IDrawingRollers rollers = switch (strings[3]) {
case "DrawingRollers" -> new DrawingRollers(Integer.parseInt(strings[4]), bodyColor);
case "DrawingCrossRollers" -> new DrawingCrossRollers(Integer.parseInt(strings[4]), bodyColor);
case "DrawingSquaredRollers" -> new DrawingSquaredRollers(Integer.parseInt(strings[4]), bodyColor);
};
if (strings.length == 5) {
EntityArtillery entity = new EntityArtillery(speed, weight, bodyColor);
return new DrawingArtillery(entity, rollers);
}
if (strings.length == 8) {
Color dopColor = new Color(Integer.parseInt(strings[5]));
boolean weapon = Boolean.parseBoolean(strings[6]);
boolean salvoBattery = Boolean.parseBoolean(strings[7]);
EntityAdvancedArtillery entity = new EntityAdvancedArtillery(speed, weight, bodyColor, dopColor, weapon, salvoBattery);
return new DrawingAdvancedArtillery(entity, rollers);
}
return null;
}
public static String serialize(DrawingArtillery drawingArtillery) {
EntityArtillery artillery = drawingArtillery.getArtillery();
String result = String.format(
"%d%s%f%s%d%s%s%s%d",
artillery.getSpeed(),
_separatorForObject,
artillery.getWeight(),
_separatorForObject,
artillery.getBodyColor().getRGB(),
_separatorForObject,
drawingArtillery.getRollers().getClass().getSimpleName(),
_separatorForObject,
drawingArtillery.getRollers().getRollersCount()
);
if (!(artillery instanceof EntityAdvancedArtillery advanced)) {
return result;
}
return String.format(
"%s%s%d%s%b%s%b",
result,
_separatorForObject,
advanced.getDopColor().getRGB(),
_separatorForObject,
advanced.getWeapon(),
_separatorForObject,
advanced.getSalvoBattery()
);
}
}