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 9f72431..a00b9f1 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -13,6 +13,8 @@ import uulm.teamname.marvelous.gamelibrary.requests.RequestType; import java.awt.*; import java.awt.geom.Line2D; import java.util.ArrayList; +import java.util.Collections; +import java.util.concurrent.atomic.AtomicInteger; /** Contains game logic handling. */ class GameLogic { @@ -173,6 +175,9 @@ class GameLogic { } } } + case EndRoundRequest -> { + result.addAll(handleTurnEnd(state)); //why is it called end round request when it ends a turn... + } case Req -> { result.add(buildGameStateEvent(state)); } @@ -493,13 +498,9 @@ class GameLogic { public static ArrayList handleTurnEnd(GameState state) { ArrayList result = new ArrayList<>(); - ArrayList order = state.turnOrder; ArrayList alive = new ArrayList<>(); - state.turnNumber++; - boolean newRound = false; - - for (EntityID id: order) { + for (EntityID id: state.turnOrder) { Character character = ((Character)state.entities.findEntity(id)); if(character.hp.getValue() > 0){ @@ -507,56 +508,124 @@ class GameLogic { } if(character.inventory.getFreeSlots() == 0) { // no slots => has all infinity stones - state.won = true; - result.add(new EventBuilder(EventType.WinEvent) - .withPlayerWon(character.id.id) - .buildGameEvent()); + result.addAll(handlePlayerWin(state, character)); return result; } } if(alive.isEmpty()) { + result.addAll(handleThanosWin(state)); + return result; + } - //thanos win handling - + int index = alive.indexOf(state.activeCharacter); + AtomicInteger turnOrderSize = new AtomicInteger(alive.size()); + if(index == alive.size() - 1) { + result.addAll(handleRoundStart(state, turnOrderSize)); }else { + state.activeCharacter = alive.get(index + 1); + } - int activeIndex = state.activeCharacter != null ? alive.indexOf(state.activeCharacter) : -1; - if(activeIndex == -1 || activeIndex == alive.size() - 1) { - state.activeCharacter = alive.get(0); - //reached end of turn order, new round - state.roundNumber++; - newRound = true; - }else { - state.activeCharacter = alive.get(activeIndex + 1); + result.addAll(handleTurnStart(state, turnOrderSize.get())); + + return result; + } + + /** + * Handles everything that happens at the beginning of new rounds. + * @param state The game state to work on + * @return The list of resulting {@link Event}s + */ + public static ArrayList handleRoundStart(GameState state, AtomicInteger turnOrderSize) { + ArrayList result = new ArrayList<>(); + + state.roundNumber++; + + Collections.shuffle(state.turnOrder); + + ArrayList alive = new ArrayList<>(); + for (EntityID id: state.turnOrder) { + Character character = ((Character)state.entities.findEntity(id)); + if(character.hp.getValue() > 0){ + alive.add(id); } - Character activeCharacter = (Character)state.entities.findEntity(state.activeCharacter); + } + turnOrderSize.set(alive.size()); + + state.activeCharacter = alive.get(0); + + result.add(new EventBuilder(EventType.RoundSetupEvent) + .withRoundCount(state.roundNumber) + .withCharacterOrder(alive.toArray(new EntityID[0])) + .buildGameEvent()); + + return result; + } + + /** + * Handles everything that happens at the beginning of a turn. + * @param state The game state to work on + * @return The list of resulting {@link Event}s + */ + public static ArrayList handleTurnStart(GameState state, int turnOrderSize) { + ArrayList result = new ArrayList<>(); + + state.turnNumber++; + + Character activeCharacter = (Character)state.entities.findEntity(state.activeCharacter); + + if(activeCharacter.ap.getValue() != activeCharacter.ap.max) { result.add(new EventBuilder(EventType.ConsumedAPEvent) .withTargetEntity(state.activeCharacter) .withTargetField(activeCharacter.getPosition()) .withAmount(activeCharacter.ap.getValue() - activeCharacter.ap.max) .buildGameEvent()); + } + if(activeCharacter.mp.getValue() != activeCharacter.mp.max) { result.add(new EventBuilder(EventType.ConsumedMPEvent) .withTargetEntity(state.activeCharacter) .withTargetField(activeCharacter.getPosition()) .withAmount(activeCharacter.mp.getValue() - activeCharacter.mp.max) .buildGameEvent()); - result.add(new EventBuilder(EventType.TurnEvent) - .withTurnCount(alive.size()) - .withNextCharacter(state.activeCharacter) - .buildGameEvent()); - } + result.add(new EventBuilder(EventType.TurnEvent) + .withTurnCount(turnOrderSize) + .withNextCharacter(state.activeCharacter) + .buildGameEvent()); - if(newRound) { - //special round handling - //shuffle turn order - /* - result.add(new EventBuilder(EventType.RoundSetupEvent) - .buildGameEvent()); - */ - } + return result; + } + + /** + * Handles the victory of a player through one character. + * @param state The game state to work on + * @param winner The winning character + * @return The list of resulting {@link Event}s + */ + public static ArrayList handlePlayerWin(GameState state, Character winner) { + ArrayList result = new ArrayList<>(); + + state.won = true; + + result.add(new EventBuilder(EventType.WinEvent) + .withPlayerWon(winner.id.id) + .buildGameEvent()); + + return result; + } + + /** + * Handles the victory of thanos after all characters are knocked out. + * @param state The game state to work on + * @return The list of resulting {@link Event}s + */ + public static ArrayList handleThanosWin(GameState state) { + ArrayList result = new ArrayList<>(); + + state.won = true; + + //TODO: add thanos victory event return result; }