From 0eaa0c5ea10d19806c5bf6a1323845822ef4f3a2 Mon Sep 17 00:00:00 2001 From: punchready Date: Fri, 4 Jun 2021 07:47:20 +0200 Subject: [PATCH] fix: improve turn handling and fix typing issues --- .../gamelibrary/gamelogic/GameLogic.java | 49 +++++++++---------- .../gamelibrary/gamelogic/GameState.java | 7 ++- 2 files changed, 28 insertions(+), 28 deletions(-) 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 4b20705..223b64c 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -496,7 +496,7 @@ public class GameLogic { state.entities.removeEntity(((EntityEvent)event).targetEntity); } case TakenDamageEvent -> { - CharacterEvent data = (CharacterEvent)event; + EntityEvent data = (EntityEvent)event; Character target = (Character)state.entities.findEntity(data.targetEntity); target.hp.decreaseValue(data.amount); @@ -534,7 +534,7 @@ public class GameLogic { state.turnOrder = new ArrayList<>(); for(EntityID turn: data.characterOrder) { - if(turn.type != EntityType.NPC) { + if(turn.type != EntityType.NPC || turn.id == NPCType.Thanos.getID()) { state.turnOrder.add(turn); } } @@ -569,7 +569,7 @@ public class GameLogic { } } case HealedEvent -> { - CharacterEvent data = (CharacterEvent)event; + EntityEvent data = (EntityEvent)event; ((Character)state.entities.findEntity(data.targetEntity)).hp.increaseValue(data.amount); } @@ -784,6 +784,7 @@ public class GameLogic { */ protected static ArrayList checkTurnEnd(GameState state) { if( + (state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == NPCType.Thanos.getID()) || ((Character) state.entities.findEntity(state.activeCharacter)).ap.getValue() <= 0 && ((Character) state.entities.findEntity(state.activeCharacter)).mp.getValue() <= 0 ) { @@ -809,21 +810,24 @@ public class GameLogic { * @return The list of resulting {@link Event Events} */ private static ArrayList handleTurnEnd(GameState state) { - System.out.println("Turn ended"); - ArrayList result = new ArrayList<>(); - ArrayList alive = new ArrayList<>(); + boolean anyAlive = false; + ArrayList turns = new ArrayList<>(); for (EntityID id: state.turnOrder) { if(id.type == EntityType.NPC) { + if(id.id == NPCType.Thanos.getID()) { + turns.add(id); + } continue; } Character character = ((Character)state.entities.findEntity(id)); if(character.isAlive()){ - alive.add(id); + anyAlive = true; + turns.add(id); }else { // send empty turn for knocked out characters result.add(new EventBuilder(EventType.TurnEvent) .withTurnCount(state.turnOrder.size()) @@ -837,7 +841,7 @@ public class GameLogic { } } - if(alive.isEmpty()) { + if(!anyAlive) { EntityType winner = state.winConditions.getWinner(); if(winner == EntityType.None) { winner = rand.nextBoolean() ? EntityType.P1 : EntityType.P2; @@ -846,11 +850,11 @@ public class GameLogic { return result; } - int index = alive.indexOf(state.activeCharacter); - if(index == alive.size() - 1) { + int index = turns.indexOf(state.activeCharacter); + if(index == turns.size() - 1) { result.addAll(handleRoundStart(state)); }else { - state.activeCharacter = alive.get(index + 1); + state.activeCharacter = turns.get(index + 1); } result.addAll(handleTurnStart(state)); @@ -888,7 +892,6 @@ public class GameLogic { } if(state.roundNumber == state.partyConfig.maxRounds + 1) { - turns.add(new EntityID(EntityType.NPC, NPCType.Thanos.getID())); result.addAll(spawnThanos(state)); } @@ -911,8 +914,6 @@ public class GameLogic { // RoundSetupEvent has to be sent first, but the contents of it are determined later... ((GameEvent)result.get(0)).characterOrder = turns.toArray(new EntityID[0]); - System.out.println(turns); - return result; } @@ -922,8 +923,6 @@ public class GameLogic { * @return The list of resulting {@link Event Events} */ private static ArrayList handleGoose(GameState state) { - System.out.println("Handling goose"); - ArrayList result = new ArrayList<>(); ArrayList inventory = new ArrayList<>(state.unvomitedStones); @@ -967,8 +966,6 @@ public class GameLogic { * @return The list of resulting {@link Event Events} */ private static ArrayList handleStan(GameState state, HashSet revived) { - System.out.println("Handling stan"); - ArrayList result = new ArrayList<>(); ArrayList characters = new ArrayList<>(); @@ -1033,8 +1030,6 @@ public class GameLogic { * @return The list of resulting {@link Event Events} */ private static ArrayList spawnThanos(GameState state) { - System.out.println("Spawning thanos"); - ArrayList result = new ArrayList<>(); ArrayList free = getFreeFields(state); @@ -1050,11 +1045,13 @@ public class GameLogic { } EntityID thanos = new EntityID(EntityType.NPC, NPCType.Thanos.getID()); + NPC thanosNPC = new NPC(thanos, position, maxMP); result.add(new EventBuilder(EventType.SpawnEntityEvent) - .withEntity(new NPC(thanos, position, maxMP)) + .withEntity(thanosNPC) .buildEntityEvent()); state.turnOrder.add(thanos); + state.entities.addEntity(thanosNPC); return result; } @@ -1065,8 +1062,6 @@ public class GameLogic { * @return The list of resulting {@link Event Events} */ private static ArrayList handleThanos(GameState state, NPC thanos) { - System.out.println("Handling thanos"); - ArrayList result = new ArrayList<>(); result.add(new EventBuilder(EventType.TurnEvent) @@ -1132,7 +1127,7 @@ public class GameLogic { .withTargetEntity(thanos.id) .withTargetField(pos) .withAmount(1) - .buildCharacterEvent()); + .buildEntityEvent()); if(pos.equals(picked)) { for(Entity entity: state.entities.findByPosition(pos)) { @@ -1164,7 +1159,7 @@ public class GameLogic { }else { for(EntityID id: state.turnOrder) { - if(id.equals(thanos.id)) { + if(id.type == EntityType.NPC) { continue; } if(rand.nextBoolean()) { @@ -1186,8 +1181,6 @@ public class GameLogic { * @return The list of resulting {@link Event Events} */ private static ArrayList handleTurnStart(GameState state) { - System.out.println("Turn of " + state.activeCharacter + " started"); - ArrayList result = new ArrayList<>(); if(state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == NPCType.Thanos.getID()) { @@ -1211,6 +1204,8 @@ public class GameLogic { * @return The list of resulting {@link Event Events} */ private static ArrayList handlePlayerWin(GameState state, EntityType winner) { + System.out.println("Player " + winner + " won"); + ArrayList result = new ArrayList<>(); result.add(new EventBuilder(EventType.WinEvent) diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java index 99e7174..48edda8 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameState.java @@ -4,6 +4,7 @@ import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig; import uulm.teamname.marvelous.gamelibrary.config.PartyConfig; import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig; +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.StoneType; @@ -143,7 +144,11 @@ class GameState { } } case P1, P2 -> { - sb.append("X "); + if(((Character)entities.get(0)).isAlive()) { + sb.append("X "); + }else { + sb.append("x "); + } } case Rocks -> { sb.append("O ");