From 4b794e99e9bfbf23b4695e3dae40b132ec47596d Mon Sep 17 00:00:00 2001 From: punchready Date: Mon, 31 May 2021 21:37:18 +0200 Subject: [PATCH] feat: add proper game initialization --- .gitignore | 2 +- .jqwik-database | Bin 525 -> 0 bytes .../gamelibrary/gamelogic/GameInstance.java | 9 +- .../gamelibrary/gamelogic/GameLogic.java | 78 +++++++++++++++++- .../gamelogic/GameStateManager.java | 14 +++- 5 files changed, 96 insertions(+), 7 deletions(-) delete mode 100644 .jqwik-database diff --git a/.gitignore b/.gitignore index f48c2a4..95afbc7 100644 --- a/.gitignore +++ b/.gitignore @@ -119,4 +119,4 @@ fabric.properties # jqwik-database file -.jqwik-database \ No newline at end of file +.jqwik-database diff --git a/.jqwik-database b/.jqwik-database deleted file mode 100644 index 3073250aff72af8e98d630f0dada87c2f183ecaa..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 525 zcmcJKu}%U(5QaBI8xzs!8)&03?!kePU}Z%}F41syNHKE5WpQus*qKGJ@QG}V58z|? z47`JToC%?`ak`oLGT;2~E3r#foET&|oxP}Mi6&JBrNwb>6J=8QfW!l5Ru>=Nr?;ce zn%L=!ePUoocO?Nj5(g8|q$UcJ7_vemi^FINa|j$O9Yl)wCK7uVOp<3YV#4C6+G{Xr z5VQ3nPt=`JGl%`8n#{zTs4WWNt6*__+3JLiRx=E*+nr{o5w_b`EWU=-!`=1wqhL7J zK-6_kXOa;z0~yK;>>PFONT$Bk>dAs#$|>bWdT~h%(0hqvvL$bFycRr#r=(-A@^YK3uZL8|rsvn;gyg>i} diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java index 64647bd..bfe99ce 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java @@ -1,6 +1,5 @@ package uulm.teamname.marvelous.gamelibrary.gamelogic; -import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.events.EventType; import uulm.teamname.marvelous.gamelibrary.json.config.CharacterConfig; @@ -8,6 +7,7 @@ import uulm.teamname.marvelous.gamelibrary.json.config.PartyConfig; import uulm.teamname.marvelous.gamelibrary.json.config.ScenarioConfig; import uulm.teamname.marvelous.gamelibrary.requests.Request; +import java.util.ArrayList; import java.util.Observer; /** Represents a game instance. */ @@ -60,9 +60,12 @@ public class GameInstance { } /** - * Initializes and starts the game. + * Initializes and starts the game. Selected characters are given as a list of indices from the {@link CharacterConfig#characters} array. + * @param selectedCharacters1 The characters selected by player 1 + * @param selectedCharacters2 The characters selected by player 2 */ - public void startGame() { + public void startGame(ArrayList selectedCharacters1, ArrayList selectedCharacters2) { + emit(manager.initGame(selectedCharacters1, selectedCharacters2)); emit(manager.startGame()); } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java index 1cfb093..c79d38e 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -5,6 +5,7 @@ import uulm.teamname.marvelous.gamelibrary.entities.*; import uulm.teamname.marvelous.gamelibrary.entities.Character; import uulm.teamname.marvelous.gamelibrary.events.*; import uulm.teamname.marvelous.gamelibrary.events.Event; +import uulm.teamname.marvelous.gamelibrary.json.config.FieldType; import uulm.teamname.marvelous.gamelibrary.requests.CharacterRequest; import uulm.teamname.marvelous.gamelibrary.requests.Request; import uulm.teamname.marvelous.gamelibrary.requests.RequestType; @@ -382,7 +383,6 @@ class GameLogic { * Verifies that a {@link Character} is alive. */ private static void requireAlive(Character entity) throws InvalidRequestException { - //TODO: only characters are allowed here if(entity.hp.getValue() <= 0 || !entity.isActive()) { throw new InvalidRequestException(); } @@ -484,6 +484,26 @@ class GameLogic { } } + /** + * Finds free field options. + * @param state The game state to work on + * @return A list of free field options + */ + private static ArrayList getFreeFields(GameState state) { + ArrayList options = new ArrayList<>(); + + for(int x = 0; x < state.mapSize.getX(); x++) { + for(int y = 0; y < state.mapSize.getY(); y++) { + IntVector2 pos = new IntVector2(x, y); + if(state.entities.findByPosition(pos).size() == 0) { + options.add(pos); + } + } + } + + return options; + } + /** * Applies an {@link Event} to a {@link GameState}. * @param state The game state to apply to @@ -540,6 +560,62 @@ class GameLogic { } } + /** + * Starts the game and initializes all entities. + * @param state The game state to work on + * @param selectedCharacters1 The characters selected by player 1 + * @param selectedCharacters2 The characters selected by player 2 + * @return The list of resulting {@link Event}s + */ + public static ArrayList startGame(GameState state, ArrayList selectedCharacters1, ArrayList selectedCharacters2) { + ArrayList result = new ArrayList<>(); + + int rockIndex = 0; + for(int x = 0; x < state.mapSize.getX(); x++) { + for(int y = 0; y < state.mapSize.getY(); y++) { + if(state.scenarioConfig.scenario[y][x] == FieldType.ROCK) { + result.add(new EventBuilder(EventType.SpawnEntityEvent) + .withEntity(new Rock(new EntityID(EntityType.Rocks, rockIndex++), new IntVector2(x, y), 100)) + .buildEntityEvent()); + } + } + } + + ArrayList free = getFreeFields(state); + + int p1 = selectedCharacters1.size(); + int all = selectedCharacters1.size() + selectedCharacters2.size(); + + ArrayList characters = new ArrayList<>(selectedCharacters1); + characters.addAll(selectedCharacters2); + + for(int i = 0; i < all; i++) { + int choice = rand.nextInt(free.size()); + IntVector2 position = free.get(choice); + free.remove(choice); + + int selected = characters.get(i); + + EntityID id = new EntityID(i < p1 ? EntityType.P1 : EntityType.P2, selected); + result.add(new EventBuilder(EventType.SpawnEntityEvent) + .withEntity(new Character( + id, position, + state.characterConfig.characters[selected].name, + state.characterConfig.characters[selected].HP, + state.characterConfig.characters[selected].MP, + state.characterConfig.characters[selected].AP, + state.characterConfig.characters[selected].attackRange, + state.characterConfig.characters[selected].rangedDamage, + state.characterConfig.characters[selected].meleeDamage + )) + .buildEntityEvent()); + + state.turnOrder.add(id); + } + + return result; + } + /** * Starts end of round handling if necessary. * @param state The game state to work on diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateManager.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateManager.java index cc6117c..72cab13 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateManager.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateManager.java @@ -53,19 +53,29 @@ class GameStateManager { return (Event[])GameLogic.checkTurnEnd(state).toArray(); } + /** + * Initializes the game. + * @param selectedCharacters1 The characters selected by player 1 + * @param selectedCharacters2 The characters selected by player 2 + * @return The resulting {@link Event}s + */ + public Event[] initGame(ArrayList selectedCharacters1, ArrayList selectedCharacters2) { + return GameLogic.startGame(state, selectedCharacters1, selectedCharacters2).toArray(new Event[0]); + } + /** * Starts the game. * @return The resulting {@link Event}s */ public Event[] startGame() { - return (Event[])GameLogic.handleTurnEnd(state).toArray(); + return GameLogic.handleRoundStart(state).toArray(new Event[0]); } /** * Applies an array of {@link Event}s to the game state. * @param events The events to apply */ - public void applyEvents(Event[] events) { + public void applyEvents(Event... events) { for(Event event: events) { GameLogic.applyEvent(state, event); }