feat: add support for the GamestateEvent in applyEvent

This commit is contained in:
punchready 2021-06-03 02:51:44 +02:00
parent c990356da0
commit 1601a2560d
4 changed files with 39 additions and 5 deletions

View File

@ -34,7 +34,7 @@ public class GameInstance {
/**
* Checks an array of {@link Request}s for validity and automatically applies them if valid.
* @param requests The requests to check
* @return The list of resulting {@link Event}s or `null` if the check failed
* @return The list of resulting {@link Event}s or {@link Optional#empty()} if the check failed
*/
public Optional<List<Event>> checkRequestsAndApply(ArrayList<Request> requests) {
if(manager.processRequests(requests, true)) {

View File

@ -13,10 +13,7 @@ import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.*;
/** Contains game logic handling. */
public class GameLogic {
@ -517,6 +514,22 @@ public class GameLogic {
state.winConditions.updateValue(target.id.type, WinCondition.MaxStones, target.inventory.getSize());
}
case GamestateEvent -> {
GamestateEvent data = (GamestateEvent)event;
state.entities.clear();
state.entities.addEntities(data.entities);
state.mapSize.set(data.mapSize);
state.turnOrder = new ArrayList<>(Arrays.asList(data.turnOrder));
state.activeCharacter = data.activeCharacter;
state.stoneCooldown.import_(data.stoneCooldowns);
state.won = data.winCondition;
}
}
}

View File

@ -82,4 +82,14 @@ public class StoneCooldownManager {
}
return data;
}
/**
* Imports the cooldowns from an array ordered according to the enum.
* @param data An array containing every cooldown
*/
public void import_(Integer[] data) {
for(int i = 0; i < data.length; i++) {
cooldowns.put(StoneType.valueOf(i), data[i]);
}
}
}

View File

@ -7,6 +7,7 @@ import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.config.*;
import uulm.teamname.marvelous.gamelibrary.entities.*;
import uulm.teamname.marvelous.gamelibrary.entities.Character;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.requests.*;
import java.util.*;
@ -167,6 +168,16 @@ class GameLogicTest {
}
@Test
void testGamestateEvent() {
GameInstance a = new GameInstance(partyConfig, characterConfig, scenarioConfig);
GameInstance b = new GameInstance(partyConfig, characterConfig, scenarioConfig);
b.applyEvents(a.startGame(player1Selection, player2Selection));
assertEquals(a.toString(), b.toString(), "Second GameInstance copies the state successfully");
}
@Test
void testRasterize() {
ArrayList<IntVector2> result = GameLogic.rasterize(new IntVector2(0, 0), new IntVector2(1, 1), false, false);