Refactor game state management to ensure proper initialization and transitions during new game starts. Introduce roster clearing functionality in RosterManager and update GameStateManager to handle state transitions more effectively. Integrate game viewport loading for combat and deployment phases.

This commit is contained in:
Matthew Mone 2025-12-21 21:33:05 -08:00
parent da55cafc8f
commit 33a64c460c
4 changed files with 19 additions and 3 deletions

View file

@ -27,11 +27,13 @@ class GameStateManagerClass {
} }
#gameLoopInitialized = Promise.withResolvers(); #gameLoopInitialized = Promise.withResolvers();
get gameLoopInitialized() { get gameLoopInitialized() {
return this.#gameLoopInitialized.promise; return this.#gameLoopInitialized.promise;
} }
#rosterLoaded = Promise.withResolvers(); #rosterLoaded = Promise.withResolvers();
get rosterLoaded() { get rosterLoaded() {
return this.#rosterLoaded.promise; return this.#rosterLoaded.promise;
} }
@ -103,6 +105,12 @@ class GameStateManagerClass {
} }
startNewGame() { startNewGame() {
// Clear roster for a fresh start
this.rosterManager.clear();
// Clear any active run data
this.activeRunData = null;
// Save the cleared roster
this._saveRoster();
this.transitionTo(GameStateManagerClass.STATES.TEAM_BUILDER); this.transitionTo(GameStateManagerClass.STATES.TEAM_BUILDER);
} }
@ -125,6 +133,8 @@ class GameStateManagerClass {
}); });
this._saveRoster(); this._saveRoster();
} }
// We must transition to deployment before initializing the run so that the game loop gets set.
this.transitionTo(GameStateManagerClass.STATES.DEPLOYMENT);
// Will transition to DEPLOYMENT after run is initialized // Will transition to DEPLOYMENT after run is initialized
await this._initializeRun(e.detail.squad); await this._initializeRun(e.detail.squad);
} }
@ -170,8 +180,6 @@ class GameStateManagerClass {
// Give GameLoop a reference to GameStateManager so it can notify about state changes // Give GameLoop a reference to GameStateManager so it can notify about state changes
this.gameLoop.gameStateManager = this; this.gameLoop.gameStateManager = this;
await this.gameLoop.startLevel(this.activeRunData); await this.gameLoop.startLevel(this.activeRunData);
// Transition to deployment state after level is initialized
this.transitionTo(GameStateManagerClass.STATES.DEPLOYMENT);
} }
async _resumeRun() { async _resumeRun() {

View file

@ -22,6 +22,7 @@ window.addEventListener("gamestate-changed", async (e) => {
break; break;
case "STATE_DEPLOYMENT": case "STATE_DEPLOYMENT":
case "STATE_COMBAT": case "STATE_COMBAT":
import("./ui/game-viewport.js");
loadingMessage.textContent = "INITIALIZING GAME ENGINE..."; loadingMessage.textContent = "INITIALIZING GAME ENGINE...";
break; break;
} }

View file

@ -69,4 +69,12 @@ export class RosterManager {
getDeployableUnits() { getDeployableUnits() {
return this.roster.filter((u) => u.status === "READY"); return this.roster.filter((u) => u.status === "READY");
} }
/**
* Clears the roster and graveyard. Used when starting a new game.
*/
clear() {
this.roster = [];
this.graveyard = [];
}
} }

View file

@ -98,4 +98,3 @@ export class CombatHUD extends LitElement {
} }
customElements.define("combat-hud", CombatHUD); customElements.define("combat-hud", CombatHUD);