feat: add proper game initialization
This commit is contained in:
parent
184c839c3a
commit
4b794e99e9
2
.gitignore
vendored
2
.gitignore
vendored
@ -119,4 +119,4 @@ fabric.properties
|
|||||||
|
|
||||||
|
|
||||||
# jqwik-database file
|
# jqwik-database file
|
||||||
.jqwik-database
|
.jqwik-database
|
||||||
|
BIN
.jqwik-database
BIN
.jqwik-database
Binary file not shown.
@ -1,6 +1,5 @@
|
|||||||
package uulm.teamname.marvelous.gamelibrary.gamelogic;
|
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.Event;
|
||||||
import uulm.teamname.marvelous.gamelibrary.events.EventType;
|
import uulm.teamname.marvelous.gamelibrary.events.EventType;
|
||||||
import uulm.teamname.marvelous.gamelibrary.json.config.CharacterConfig;
|
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.json.config.ScenarioConfig;
|
||||||
import uulm.teamname.marvelous.gamelibrary.requests.Request;
|
import uulm.teamname.marvelous.gamelibrary.requests.Request;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Observer;
|
import java.util.Observer;
|
||||||
|
|
||||||
/** Represents a game instance. */
|
/** 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<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) {
|
||||||
|
emit(manager.initGame(selectedCharacters1, selectedCharacters2));
|
||||||
emit(manager.startGame());
|
emit(manager.startGame());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import uulm.teamname.marvelous.gamelibrary.entities.*;
|
|||||||
import uulm.teamname.marvelous.gamelibrary.entities.Character;
|
import uulm.teamname.marvelous.gamelibrary.entities.Character;
|
||||||
import uulm.teamname.marvelous.gamelibrary.events.*;
|
import uulm.teamname.marvelous.gamelibrary.events.*;
|
||||||
import uulm.teamname.marvelous.gamelibrary.events.Event;
|
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.CharacterRequest;
|
||||||
import uulm.teamname.marvelous.gamelibrary.requests.Request;
|
import uulm.teamname.marvelous.gamelibrary.requests.Request;
|
||||||
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
|
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
|
||||||
@ -382,7 +383,6 @@ class GameLogic {
|
|||||||
* Verifies that a {@link Character} is alive.
|
* Verifies that a {@link Character} is alive.
|
||||||
*/
|
*/
|
||||||
private static void requireAlive(Character entity) throws InvalidRequestException {
|
private static void requireAlive(Character entity) throws InvalidRequestException {
|
||||||
//TODO: only characters are allowed here
|
|
||||||
if(entity.hp.getValue() <= 0 || !entity.isActive()) {
|
if(entity.hp.getValue() <= 0 || !entity.isActive()) {
|
||||||
throw new InvalidRequestException();
|
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<IntVector2> getFreeFields(GameState state) {
|
||||||
|
ArrayList<IntVector2> 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}.
|
* Applies an {@link Event} to a {@link GameState}.
|
||||||
* @param state The game state to apply to
|
* @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<Event> startGame(GameState state, ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) {
|
||||||
|
ArrayList<Event> 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<IntVector2> free = getFreeFields(state);
|
||||||
|
|
||||||
|
int p1 = selectedCharacters1.size();
|
||||||
|
int all = selectedCharacters1.size() + selectedCharacters2.size();
|
||||||
|
|
||||||
|
ArrayList<Integer> 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.
|
* Starts end of round handling if necessary.
|
||||||
* @param state The game state to work on
|
* @param state The game state to work on
|
||||||
|
@ -53,19 +53,29 @@ class GameStateManager {
|
|||||||
return (Event[])GameLogic.checkTurnEnd(state).toArray();
|
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<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) {
|
||||||
|
return GameLogic.startGame(state, selectedCharacters1, selectedCharacters2).toArray(new Event[0]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the game.
|
* Starts the game.
|
||||||
* @return The resulting {@link Event}s
|
* @return The resulting {@link Event}s
|
||||||
*/
|
*/
|
||||||
public Event[] startGame() {
|
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.
|
* Applies an array of {@link Event}s to the game state.
|
||||||
* @param events The events to apply
|
* @param events The events to apply
|
||||||
*/
|
*/
|
||||||
public void applyEvents(Event[] events) {
|
public void applyEvents(Event... events) {
|
||||||
for(Event event: events) {
|
for(Event event: events) {
|
||||||
GameLogic.applyEvent(state, event);
|
GameLogic.applyEvent(state, event);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user