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
1 changed files with 102 additions and 33 deletions

View File

@ -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<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) {
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<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)
.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<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;
}