feat: wrote EntityDeserializer
This commit is contained in:
@ -1,10 +1,13 @@
|
||||
package uulm.teamname.marvelous.gamelibrary.entities;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
||||
import uulm.teamname.marvelous.gamelibrary.json.ingame.EntityDeserializer;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/** Represents an abstract entity. */
|
||||
@JsonDeserialize(using = EntityDeserializer.class)
|
||||
public abstract class Entity {
|
||||
/** Whether or not the entity is currently active in the game */
|
||||
protected boolean active = true;
|
||||
|
@ -0,0 +1,70 @@
|
||||
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 uulm.teamname.marvelous.gamelibrary.json.JSON;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class EntityDeserializer extends JsonDeserializer<Entity> {
|
||||
|
||||
private enum DeserializedEntityType {
|
||||
Character,
|
||||
Stone,
|
||||
Rock
|
||||
}
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public Entity deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
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,
|
||||
mapper.readValue(node.get("position").toString(), IntVector2.class),
|
||||
node.get("name").asText(),
|
||||
node.get("HP").asInt(),
|
||||
node.get("MP").asInt(),
|
||||
node.get("AP").asInt(),
|
||||
-1,
|
||||
-1,
|
||||
-1
|
||||
);
|
||||
}
|
||||
case Stone -> {
|
||||
result = new InfinityStone(
|
||||
new EntityID(EntityType.InfinityStones, node.get("ID").asInt()),
|
||||
mapper.readValue(node.get("position").toString(), IntVector2.class),
|
||||
StoneType.valueOf(node.get("ID").asInt())
|
||||
);
|
||||
}
|
||||
case Rock -> {
|
||||
result = new Rock(
|
||||
new EntityID(EntityType.Rocks, node.get("ID").asInt()),
|
||||
mapper.readValue(node.get("position").toString(), IntVector2.class),
|
||||
node.get("HP").asInt()
|
||||
);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user