diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Character.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Character.java index 695409a..5d67196 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Character.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Character.java @@ -104,6 +104,7 @@ public class Character extends Entity { ", rangedDamage=" + rangedDamage + ", meleeDamage=" + meleeDamage + ", inventory=" + inventory + + ", position=" + position + '}'; } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/InfinityStone.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/InfinityStone.java index 6f59f4c..ca43a1d 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/InfinityStone.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/InfinityStone.java @@ -46,6 +46,7 @@ public class InfinityStone extends Entity { public String toString() { return "InfinityStone{" + "type=" + type + + ", position=" + position + '}'; } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java index 1b6a2ce..28b589a 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java @@ -23,10 +23,10 @@ public class Inventory implements Iterable { } for(StoneType stone: content) { - if(content.contains(stone)) { + if(this.content.contains(stone)) { throw new IllegalArgumentException("Attempted to construct an inventory with duplicate entries."); } - content.add(stone); + this.content.add(stone); } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/NPC.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/NPC.java index 8408fc2..55238ae 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/NPC.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/NPC.java @@ -2,21 +2,66 @@ package uulm.teamname.marvelous.gamelibrary.entities; import uulm.teamname.marvelous.gamelibrary.IntVector2; +import java.util.ArrayList; + /** Represents an NPC inside the game. */ public class NPC extends Entity { + /** The {@link StatType#MP} of the NPC */ + public final Stat mp; + + /** The {@link Inventory} of the NPC */ + public final Inventory inventory; + + /** + * Constructs a new {@link NPC}. + * @param id The {@link EntityID} of the NPC + * @param position The position of the NPC + * @param inventory The starting inventory the NPC should have + */ + public NPC(EntityID id, IntVector2 position, int maxMP, ArrayList inventory) { + super(id, position); + solid = false; + opaque = true; + this.inventory = new Inventory(inventory); + this.mp = new Stat(StatType.MP, maxMP); + } + + /** + * Constructs a new {@link NPC} with an empty inventory. + * @param id The {@link EntityID} of the NPC + * @param position The position of the NPC + * @param maxMP The maximum MP of the NPC + */ + public NPC(EntityID id, IntVector2 position, int maxMP) { + this(id, position, maxMP, new ArrayList<>()); + } + + /** + * Constructs a new {@link NPC} with an empty inventory. + * @param id The {@link EntityID} of the NPC + * @param position The position of the NPC + * @param inventory The starting inventory of the NPC + */ + public NPC(EntityID id, IntVector2 position, ArrayList inventory) { + this(id, position, 0, inventory); + } + /** * Constructs a new {@link NPC} with an empty inventory. * @param id The {@link EntityID} of the NPC * @param position The position of the NPC */ public NPC(EntityID id, IntVector2 position) { - super(id, position); - solid = false; - opaque = true; + this(id, position, 0); } @Override public NPC clone() { - return new NPC(id, position); + NPC clone = new NPC(id, position, mp.getMax()); + clone.mp.setValue(mp.getValue()); + for(StoneType stone: inventory) { + clone.inventory.addStone(stone); + } + return clone; } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java index ba7e94d..3d0fcb9 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java @@ -63,6 +63,7 @@ public class Rock extends Entity { return "Rock{" + "maxHP=" + maxHP + ", hp=" + hp + + ", position=" + position + '}'; } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EntityManager.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EntityManager.java index 0f8ffd9..2178435 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EntityManager.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/EntityManager.java @@ -104,7 +104,7 @@ public class EntityManager { */ public Entity findEntity(EntityID id) { for(Entity entity: entities) { - if(entity.id == id) { + if(entity.id.equals(id)) { return entity; } } @@ -119,7 +119,7 @@ public class EntityManager { public ArrayList findByPosition(IntVector2 pos) { ArrayList found = new ArrayList<>(); for(Entity entity: entities) { - if(entity.getPosition() == pos) { + if(entity.getPosition().equals(pos)) { found.add(entity); } } @@ -133,7 +133,7 @@ public class EntityManager { */ public boolean blocksVision(IntVector2 pos) { for(Entity entity: entities) { - if(entity.getPosition() == pos && entity.blocksVision()) { + if(entity.getPosition().equals(pos) && entity.blocksVision()) { return true; } } @@ -147,7 +147,7 @@ public class EntityManager { */ public boolean blocksMovement(IntVector2 pos) { for(Entity entity: entities) { - if(entity.getPosition() == pos && entity.blocksMovement()) { + if(entity.getPosition().equals(pos) && entity.blocksMovement()) { return true; } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java index acefd7a..6a668b3 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -582,7 +582,7 @@ class GameLogic { int selected = characters.get(i); - EntityID id = new EntityID(i < p1 ? EntityType.P1 : EntityType.P2, selected); + EntityID id = new EntityID(i < p1 ? EntityType.P1 : EntityType.P2, i % p1); result.add(new EventBuilder(EventType.SpawnEntityEvent) .withEntity(new Character( id, position, @@ -693,7 +693,7 @@ class GameLogic { Collections.shuffle(state.turnOrder); for (EntityID id: state.turnOrder) { - if(revived.contains(id) || ((Character)state.entities.findEntity(id)).hp.getValue() > 0){ + if(id.type == EntityType.NPC || revived.contains(id) || ((Character)state.entities.findEntity(id)).hp.getValue() > 0){ state.activeCharacter = id; break; } @@ -717,8 +717,10 @@ class GameLogic { public static ArrayList handleGoose(GameState state) { ArrayList result = new ArrayList<>(); - StoneType[] available = state.unvomitedStones.toArray(new StoneType[0]); - StoneType stone = available[rand.nextInt(available.length)]; + ArrayList inventory = new ArrayList<>(state.unvomitedStones); + int picked = rand.nextInt(inventory.size()); + StoneType stone = inventory.get(picked); + inventory.remove(picked); state.unvomitedStones.remove(stone); ArrayList free = new ArrayList<>(); @@ -734,7 +736,7 @@ class GameLogic { EntityID goose = new EntityID(EntityType.NPC, 0); result.add(new EventBuilder(EventType.SpawnEntityEvent) - .withEntity(new NPC(goose, position)) + .withEntity(new NPC(goose, position, inventory)) .buildEntityEvent()); result.add(new EventBuilder(EventType.SpawnEntityEvent) .withEntity(new InfinityStone(new EntityID(EntityType.InfinityStones, stone.getID()), position, stone)) @@ -829,7 +831,7 @@ class GameLogic { EntityID thanos = new EntityID(EntityType.NPC, 2); result.add(new EventBuilder(EventType.SpawnEntityEvent) - .withEntity(new Character(thanos, position, "Thanos", 1, maxMP, 0, 0, 0, 0)) + .withEntity(new NPC(thanos, position, maxMP)) .buildEntityEvent()); state.turnOrder.add(thanos); @@ -842,10 +844,10 @@ class GameLogic { * @param state The game state to work on * @return The list of resulting {@link Event}s */ - public static ArrayList handleThanos(GameState state, Character thanos) { + public static ArrayList handleThanos(GameState state, NPC thanos) { ArrayList result = new ArrayList<>(); - //TODO: implement thanos ai + return result; } @@ -860,13 +862,31 @@ class GameLogic { state.turnNumber++; - Character activeCharacter = (Character)state.entities.findEntity(state.activeCharacter); - boolean isThanos = state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == 2; + if(state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == 2) { + NPC thanos = (NPC)state.entities.findEntity(state.activeCharacter); - if(isThanos && state.roundNumber > state.partyConfig.maxRounds + 1) { - activeCharacter.mp.setMax(activeCharacter.mp.getMax() + 1);//TODO: use event for this... + if(state.roundNumber > state.partyConfig.maxRounds + 1) { + thanos.mp.setMax(thanos.mp.getMax() + 1);//TODO: use event for this... + } + + if(thanos.mp.getValue() != thanos.mp.getMax()) { + result.add(new EventBuilder(EventType.ConsumedMPEvent) + .withTargetEntity(state.activeCharacter) + .withTargetField(thanos.getPosition()) + .withAmount(thanos.mp.getValue() - thanos.mp.getMax()) + .buildGameEvent()); + } + result.add(new EventBuilder(EventType.TurnEvent) + .withTurnCount(state.turnOrder.size()) + .withNextCharacter(state.activeCharacter) + .buildGameEvent()); + result.addAll(handleThanos(state, thanos)); + + return result; } + Character activeCharacter = (Character)state.entities.findEntity(state.activeCharacter); + if(activeCharacter.ap.getValue() != activeCharacter.ap.getMax()) { result.add(new EventBuilder(EventType.ConsumedAPEvent) .withTargetEntity(state.activeCharacter) @@ -886,10 +906,6 @@ class GameLogic { .withNextCharacter(state.activeCharacter) .buildGameEvent()); - if(isThanos) { - result.addAll(handleThanos(state, activeCharacter)); - } - return result; } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java index 5829813..fe39bd9 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java @@ -13,7 +13,7 @@ import uulm.teamname.marvelous.gamelibrary.events.GamestateEvent; import uulm.teamname.marvelous.gamelibrary.json.basic.BasicMessage; import uulm.teamname.marvelous.gamelibrary.json.basic.EventMessage; import uulm.teamname.marvelous.gamelibrary.json.config.CharacterConfig; -import uulm.teamname.marvelous.gamelibrary.json.ingame.EntityDeserializer; +import uulm.teamname.marvelous.gamelibrary.json.ingame.deserialize.EntityDeserializer; /** * Class that contains JSON encoding and decoding. It is initiated with the Character configuration.