diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Entity.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Entity.java index 256a8a0..9f77fe5 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Entity.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Entity.java @@ -1,13 +1,16 @@ package uulm.teamname.marvelous.gamelibrary.entities; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.json.ingame.deserialize.EntityDeserializer; +import uulm.teamname.marvelous.gamelibrary.json.ingame.serialize.EntitySerializer; import java.util.Objects; /** Represents an abstract entity. */ @JsonDeserialize(using = EntityDeserializer.class) +@JsonSerialize(using = EntitySerializer.class) public abstract class Entity { /** Whether or not the entity is currently active in the game */ protected boolean active = true; diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EntitySerializer.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EntitySerializer.java new file mode 100644 index 0000000..3cb6081 --- /dev/null +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EntitySerializer.java @@ -0,0 +1,77 @@ +package uulm.teamname.marvelous.gamelibrary.json.ingame.serialize; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; +import uulm.teamname.marvelous.gamelibrary.entities.*; +import uulm.teamname.marvelous.gamelibrary.entities.Character; +import uulm.teamname.marvelous.gamelibrary.json.ingame.SerializedEntityType; + +import java.io.IOException; +import java.util.Arrays; + +public class EntitySerializer extends StdSerializer { + public EntitySerializer() { + this(null); + } + + protected EntitySerializer(Class t) { + super(t); + } + + @Override + public void serialize(Entity value, JsonGenerator gen, SerializerProvider provider) throws IOException { + gen.writeStartObject(); + gen.writeObjectField("entityType", SerializedEntityType.valueOf(value.id.type)); + + if (value instanceof NPC) { + System.out.println("instanceof NPC"); + serializeNPC((NPC) value, gen, provider); + } else if (value instanceof Character) { + System.out.println("instanceof Chara"); + serializeCharacter((Character) value, gen, provider); + } else if (value instanceof Rock) { + serializeRock((Rock) value, gen, provider); + } else if (value instanceof InfinityStone) { + serializeInfinityStone((InfinityStone) value, gen, provider); + } + + gen.writeObjectField("position", value.getPosition()); + gen.writeEndObject(); + } + + private void serializeNPC(NPC value, JsonGenerator gen, SerializerProvider provider) + throws IOException { + gen.writeNumberField("ID", value.id.id); + // gen.writeNumberField("MP",value.MP); // TODO: Doesn't exist yet? +// gen.writeArray(value.); // TODO: Put values here + } + + private void serializeCharacter(Character value, JsonGenerator gen, SerializerProvider provider) + throws IOException { + gen.writeNumberField("PID", value.id.type == EntityType.P1 ? 1 : 2); + gen.writeNumberField("ID", value.id.id); + gen.writeNumberField("HP", value.hp.getValue()); + gen.writeNumberField("MP", value.mp.getValue()); + gen.writeNumberField("AP", value.ap.getValue()); + + gen.writeFieldName("stones"); + gen.writeArray( + Arrays.stream(value.inventory.getStonesAsArray()) + .mapToInt(StoneType::getID) + .sorted() + .toArray(), + 0, value.inventory.getSize()); + } + + private void serializeRock(Rock value, JsonGenerator gen, SerializerProvider provider) + throws IOException { + gen.writeNumberField("ID", value.id.id); + gen.writeNumberField("HP", value.getHp()); + } + + private void serializeInfinityStone(InfinityStone value, JsonGenerator gen, SerializerProvider provider) + throws IOException { + gen.writeNumberField("ID", value.id.id); + } +} diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EntitySerializerTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EntitySerializerTest.java new file mode 100644 index 0000000..5aa6309 --- /dev/null +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EntitySerializerTest.java @@ -0,0 +1,43 @@ +package uulm.teamname.marvelous.gamelibrary.json.ingame.serialize; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +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.EntityID; +import uulm.teamname.marvelous.gamelibrary.entities.EntityType; +import uulm.teamname.marvelous.gamelibrary.entities.StoneType; +import uulm.teamname.marvelous.gamelibrary.json.ingame.deserialize.EntityDeserializer; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; + +class EntitySerializerTest { + + + @Test + void notATestJustSomethingToRun() throws JsonProcessingException { + var mapper = new ObjectMapper(); + + var chara = new Character( + new EntityID(EntityType.P2, 3), + new IntVector2(12, 24), + "CoolHeroThing", + 200, + 300, + 400, + 500, + 600, + 700); + + chara.inventory.addStone(StoneType.SoulStone); + chara.inventory.addStone(StoneType.SpaceStone); + chara.inventory.addStone(StoneType.MindStone); + + var result = mapper.writeValueAsString(chara); + + System.out.println(result); + } + +} \ No newline at end of file