refactor: massively improve turn and round handling code

This commit is contained in:
punchready 2021-05-27 15:57:06 +02:00
parent 7dcffab0a6
commit 01ccabad14

View File

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