feat: implemented still-missing JSON ser/deser for portalEntities

This commit is contained in:
2021-07-12 21:16:02 +02:00
parent 518e458c5f
commit e9b4bc5f58
5 changed files with 59 additions and 1 deletions

View File

@ -9,7 +9,8 @@ public enum SerializedEntityType {
NPC,
Character,
InfinityStone,
Rock;
Rock,
Portal;
private static final Map<EntityType, SerializedEntityType> entityTypeToDeserialized;
@ -21,6 +22,7 @@ public enum SerializedEntityType {
entityTypeToDeserialized.put(EntityType.P2, Character);
entityTypeToDeserialized.put(EntityType.InfinityStones, InfinityStone);
entityTypeToDeserialized.put(EntityType.Rocks, Rock);
entityTypeToDeserialized.put(EntityType.Portals, Portal);
}
public static SerializedEntityType valueOf(EntityType entityType) {

View File

@ -75,6 +75,10 @@ public class EntityDeserializer extends JsonDeserializer<Entity> {
codec.treeToValue(node.get("position"), IntVector2.class),
node.get("HP").asInt()
);
case Portal -> result = new Portal(
new EntityID(EntityType.Portals, node.get("ID").asInt()),
codec.treeToValue(node.get("position"), IntVector2.class)
);
}
return result;
}

View File

@ -32,6 +32,8 @@ public class EntitySerializer extends StdSerializer<Entity> {
serializeRock((Rock) value, gen, provider);
} else if (value instanceof InfinityStone) {
serializeInfinityStone((InfinityStone) value, gen, provider);
} else if (value instanceof Portal) {
serializePortal((Portal) value, gen, provider);
}
gen.writeObjectField("position", value.getPosition());
@ -72,6 +74,10 @@ public class EntitySerializer extends StdSerializer<Entity> {
gen.writeNumberField("ID", value.id.id);
}
private void serializePortal(Portal value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeNumberField("ID", value.id.id);
}
/** Returns an int[] with the sorted integer representations of the stones in the given Inventory */
private int[] inventoryToIntArray(Inventory inventory) {
return Arrays.stream(inventory.getStonesAsArray())