72 lines
2.6 KiB
Java
72 lines
2.6 KiB
Java
package uulm.teamname.marvelous.gamelibrary.json.ingame;
|
|
|
|
import com.fasterxml.jackson.core.JsonParser;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.core.ObjectCodec;
|
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
|
import uulm.teamname.marvelous.gamelibrary.entities.*;
|
|
import uulm.teamname.marvelous.gamelibrary.entities.Character;
|
|
|
|
import java.io.IOException;
|
|
|
|
public class EntityDeserializer extends JsonDeserializer<Entity> {
|
|
|
|
private enum DeserializedEntityType {
|
|
Character,
|
|
InfinityStone,
|
|
Rock
|
|
}
|
|
|
|
@Override
|
|
public Entity deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
|
ObjectCodec codec = p.getCodec();
|
|
JsonNode node = codec.readTree(p);
|
|
|
|
Entity result = null;
|
|
|
|
DeserializedEntityType type = DeserializedEntityType.valueOf(node.get("entityType").asText());
|
|
|
|
switch (type) {
|
|
case Character -> {
|
|
EntityID id = new EntityID(
|
|
EntityType.valueOf("P" + node.get("PID").intValue()),
|
|
node.get("ID").intValue());
|
|
result = new Character(
|
|
id,
|
|
codec.treeToValue(node.get("position"), IntVector2.class),
|
|
node.get("name").asText(),
|
|
node.get("HP").asInt(),
|
|
node.get("MP").asInt(),
|
|
node.get("AP").asInt(),
|
|
-1,
|
|
-1,
|
|
-1
|
|
);
|
|
|
|
for (var i: codec.treeToValue(node.get("stones"), Integer[].class)) {
|
|
((Character) result).inventory.addStone(StoneType.valueOf(i));
|
|
}
|
|
}
|
|
case InfinityStone -> {
|
|
result = new InfinityStone(
|
|
new EntityID(EntityType.InfinityStones, node.get("ID").asInt()),
|
|
codec.treeToValue(node.get("position"), IntVector2.class),
|
|
StoneType.valueOf(node.get("ID").asInt())
|
|
);
|
|
}
|
|
case Rock -> {
|
|
result = new Rock(
|
|
new EntityID(EntityType.Rocks, node.get("ID").asInt()),
|
|
codec.treeToValue(node.get("position"), IntVector2.class),
|
|
node.get("HP").asInt()
|
|
);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|