wip: implement post turn handling

This commit is contained in:
punchready 2021-05-19 19:59:41 +02:00
parent 7bac3bd46a
commit f3b5fc277e
4 changed files with 107 additions and 1 deletions

View File

@ -8,7 +8,6 @@
<option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="$PROJECT_DIR$/../../../../../../../utils/gradle-7.0" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />

View File

@ -5,6 +5,7 @@ import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventType;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
import java.util.ArrayList;
import java.util.Observer;
/** Represents a game instance. */
@ -36,6 +37,12 @@ public class GameInstance {
public boolean checkRequestsAndApply(Request... requests) {
if(manager.processRequests(requests, true)) {
emit(manager.apply());
Event[] result = manager.checkPostPhase();
if(result.length > 0) {
emit(result);
}
return true;
}
return false;

View File

@ -469,6 +469,98 @@ class GameLogic {
}
}
/**
* Starts end of round handling if necessary.
* @param state The game state to work on
* @return The list of resulting {@link Event}s
*/
public static ArrayList<Event> checkTurnEnd(GameState state) {
if(
((Character) state.entities.findEntity(state.activeCharacter)).ap.getValue() <= 0 &&
((Character) state.entities.findEntity(state.activeCharacter)).mp.getValue() <= 0
) {
return handleTurnEnd(state);
}
return new ArrayList<>();
}
/**
* Handles everything that happens at the end of a turn, including new rounds.
* @param state The game state to work on
* @return The list of resulting {@link Event}s
*/
private static ArrayList<Event> handleTurnEnd(GameState state) {
ArrayList<Event> result = new ArrayList<>();
ArrayList<EntityID> order = state.turnOrder;
ArrayList<EntityID> alive = new ArrayList<>();
state.turnNumber++;
boolean newRound = false;
for (EntityID id: order) {
Character character = ((Character)state.entities.findEntity(id));
if(character.hp.getValue() > 0){
alive.add(id);
}
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());
return result;
}
}
if(alive.isEmpty()) {
//thanos win handling
}else {
int activeIndex = alive.indexOf(state.activeCharacter);
if(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);
}
Character activeCharacter = (Character)state.entities.findEntity(state.activeCharacter);
result.add(new EventBuilder(EventType.ConsumedAPEvent)
.withTargetEntity(state.activeCharacter)
.withTargetField(activeCharacter.getPosition())
.withAmount(activeCharacter.ap.getValue() - activeCharacter.ap.max)
.buildGameEvent());
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());
}
if(newRound) {
//special round handling
//shuffle turn order
/*
result.add(new EventBuilder(EventType.RoundSetupEvent)
.buildGameEvent());
*/
}
return result;
}
/**
* Checks a {@link GameState} for the current overtime win condition.
* @param state The game state to check

View File

@ -45,6 +45,14 @@ class GameStateManager {
return true;
}
/**
* Handles events that happen after a turn phase.
* @return The optionally resulting {@link Event}s
*/
public Event[] checkPostPhase() {
return (Event[])GameLogic.checkTurnEnd(state).toArray();
}
/**
* Applies an array of {@link Event}s to the game state.
* @param events The events to apply