refactor: introduce utils method for converting arrays into arraylists and make methods accessible with either

This commit is contained in:
punchready 2021-06-03 03:33:20 +02:00
parent 614b33adff
commit 09d798f37f
4 changed files with 61 additions and 1 deletions

View File

@ -0,0 +1,21 @@
package uulm.teamname.marvelous.gamelibrary;
import java.util.ArrayList;
/** Provides various tools for Arrays. */
public class ArrayTools {
/**
* Returns a variable-size array list backed by the specified array without allocating more memory than necessary.
* @param <E> the class of the objects in the array
* @param a the array by which the list will be backed
* @return a list view of the specified array
* @throws NullPointerException if the specified array is {@code null}
*/
public static <E> ArrayList<E> toArrayList(E[] a) {
ArrayList<E> l = new ArrayList<>(a.length);
for(E e: a) {
l.add(e);
}
return l;
}
}

View File

@ -1,5 +1,6 @@
package uulm.teamname.marvelous.gamelibrary.gamelogic;
import uulm.teamname.marvelous.gamelibrary.ArrayTools;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.Entity;
import uulm.teamname.marvelous.gamelibrary.events.Event;
@ -10,6 +11,7 @@ import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
@ -31,6 +33,15 @@ public class GameInstance {
manager = new GameStateManager(_state);
}
/**
* 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 {@link Optional#empty()} if the check failed
*/
public Optional<List<Event>> checkRequestsAndApply(Request... requests) {
return checkRequestsAndApply(ArrayTools.toArrayList(requests));
}
/**
* Checks an array of {@link Request}s for validity and automatically applies them if valid.
* @param requests The requests to check
@ -48,6 +59,15 @@ public class GameInstance {
return Optional.empty();
}
/**
* Checks an array of {@link Request}s for validity without applying it.
* @param requests The requests to check
* @return Whether or not the given set of requests is valid
*/
public boolean checkRequests(Request... requests) {
return checkRequests(ArrayTools.toArrayList(requests));
}
/**
* Checks an array of {@link Request}s for validity without applying it.
* @param requests The requests to check
@ -57,6 +77,14 @@ public class GameInstance {
return manager.processRequests(requests, false);
}
/**
* Applies an array of {@link Event}s to the game state.
* @param events The events to apply.
*/
public void applyEvents(Event... events) {
manager.applyEvents(events);
}
/**
* Applies an array of {@link Event}s to the game state.
* @param events The events to apply.

View File

@ -1,5 +1,6 @@
package uulm.teamname.marvelous.gamelibrary.gamelogic;
import uulm.teamname.marvelous.gamelibrary.ArrayTools;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.Character;
import uulm.teamname.marvelous.gamelibrary.entities.*;
@ -543,7 +544,7 @@ public class GameLogic {
state.mapSize.set(data.mapSize);
state.turnOrder = new ArrayList<>(Arrays.asList(data.turnOrder));
state.turnOrder = ArrayTools.toArrayList(data.turnOrder);
state.activeCharacter = data.activeCharacter;

View File

@ -49,6 +49,16 @@ class GameStateManager {
return true;
}
/**
* Applies an array of {@link Event}s to the game state.
* @param events The events to apply
*/
public void applyEvents(Event[] events) {
for(Event event: events) {
GameLogic.applyEvent(state, event);
}
}
/**
* Applies an array of {@link Event}s to the game state.
* @param events The events to apply