feat: wrote EntityDeserializer
This commit is contained in:
parent
28ac25fa55
commit
f7b1514491
@ -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;
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package uulm.teamname.marvelous.gamelibrary.json.ingame;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.Character;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.Entity;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EntityDeserializerTest {
|
||||
|
||||
ObjectMapper mapper;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
mapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
@Test
|
||||
void simpleCharacterTest() throws JsonProcessingException {
|
||||
var character = new Character(
|
||||
new EntityID(EntityType.P1, 3),
|
||||
new IntVector2(4, 2),
|
||||
"God",
|
||||
255,
|
||||
65535,
|
||||
4,
|
||||
-1,
|
||||
-1,
|
||||
-1
|
||||
);
|
||||
|
||||
var jsonRepresentingCharacter = """
|
||||
{
|
||||
"entityType": "Character",
|
||||
"name": "God",
|
||||
"PID": 1,
|
||||
"ID": 3,
|
||||
"HP": 255,
|
||||
"MP": 65535,
|
||||
"AP": 4,
|
||||
"position": [4, 2]
|
||||
}
|
||||
""";
|
||||
assertThat(mapper.readValue(jsonRepresentingCharacter, Entity.class))
|
||||
.isEqualTo(character);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user