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 46beb62..a230f4b 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -396,36 +396,7 @@ public class GameLogic { .buildEntityEvent()); Entity targetEntity = state.entities.findEntity(data.targetEntity); - if(targetEntity instanceof Character) { - Character target = (Character)targetEntity; - if(target.hp.getValue() <= data.value) { - - List stones = Arrays.asList(target.inventory.getStonesAsArray()); - Collections.shuffle(stones); // required by documents - - ArrayList used = new ArrayList<>(); - for(StoneType stone: stones) { - ArrayList options = getFreeNeighbour(state, target.getPosition(), used); - IntVector2 picked = options.get(rand.nextInt(options.size())); - used.add(picked); - result.add(new EventBuilder(EventType.SpawnEntityEvent) - .withEntity(new InfinityStone( - new EntityID(EntityType.InfinityStones, stone.getID()), - picked, - stone - )) - .buildEntityEvent()); - } - } - }else if(targetEntity instanceof Rock) { - Rock target = (Rock)targetEntity; - if(target.getHp() <= data.value) { - result.add(new EventBuilder(EventType.DestroyedEntityEvent) - .withTargetField(data.targetField) - .withTargetEntity(target.id) - .buildEntityEvent()); - } - } + result.addAll(checkDeath(state, targetEntity, data.value)); } case MoveRequest -> { CharacterRequest data = (CharacterRequest)request; @@ -522,36 +493,7 @@ public class GameLogic { .withAmount(state.partyConfig.mindStoneDMG) .buildEntityEvent()); - if(targetEntity instanceof Character) { - Character target = (Character)targetEntity; - if(target.hp.getValue() <= state.partyConfig.mindStoneDMG) { - - List stones = Arrays.asList(target.inventory.getStonesAsArray()); - Collections.shuffle(stones); // required by documents - - ArrayList used = new ArrayList<>(); - for(StoneType stone: stones) { - ArrayList options = getFreeNeighbour(state, target.getPosition(), used); - IntVector2 picked = options.get(rand.nextInt(options.size())); - used.add(picked); - result.add(new EventBuilder(EventType.SpawnEntityEvent) - .withEntity(new InfinityStone( - new EntityID(EntityType.InfinityStones, stone.getID()), - picked, - stone - )) - .buildEntityEvent()); - } - } - }else if(targetEntity instanceof Rock) { - Rock target = (Rock)targetEntity; - if(target.getHp() <= state.partyConfig.mindStoneDMG) { - result.add(new EventBuilder(EventType.DestroyedEntityEvent) - .withTargetField(data.targetField) - .withTargetEntity(target.id) - .buildEntityEvent()); - } - } + result.addAll(checkDeath(state, targetEntity, state.partyConfig.mindStoneDMG)); } case RealityStone -> { EntityID target = null; @@ -595,6 +537,8 @@ public class GameLogic { .withTargetField(data.targetField) .withAmount(origin.meleeDamage * 2) .buildEntityEvent()); + + result.addAll(checkDeath(state, targetEntity, origin.meleeDamage * 2)); } case TimeStone -> { Character origin = (Character)state.entities.findEntity(data.originEntity); @@ -639,6 +583,49 @@ public class GameLogic { return result; } + /** + * Checks for death of a Character or Rock and returns the resulting {@link Event Events}. + * @param state The game state to apply to + * @param targetEntity The entity to check + * @param damage The damage taken + * @return The resulting events + */ + private static ArrayList checkDeath(GameState state, Entity targetEntity, int damage) { + ArrayList result = new ArrayList<>(); + + if(targetEntity instanceof Character) { + Character target = (Character)targetEntity; + if(target.hp.getValue() <= damage) { + + List stones = Arrays.asList(target.inventory.getStonesAsArray()); + Collections.shuffle(stones); // required by documents + + ArrayList used = new ArrayList<>(); + for(StoneType stone: stones) { + ArrayList options = getFreeNeighbour(state, target.getPosition(), used); + IntVector2 picked = options.get(rand.nextInt(options.size())); + used.add(picked); + result.add(new EventBuilder(EventType.SpawnEntityEvent) + .withEntity(new InfinityStone( + new EntityID(EntityType.InfinityStones, stone.getID()), + picked, + stone + )) + .buildEntityEvent()); + } + } + }else if(targetEntity instanceof Rock) { + Rock target = (Rock)targetEntity; + if(target.getHp() <= damage) { + result.add(new EventBuilder(EventType.DestroyedEntityEvent) + .withTargetField(target.getPosition()) + .withTargetEntity(target.id) + .buildEntityEvent()); + } + } + + return result; + } /** * Applies an {@link Event} to a {@link GameState}.