WIP: Moving most globals into gamestate

The gamestate object is now where most of the "state" of both the map
and the board live. The units load, just working on loading the colors
of the map ownership again.
This commit is contained in:
Tyler Marques 2025-03-13 11:33:30 -04:00
parent dff01db83f
commit ae2db39729
No known key found for this signature in database
GPG key ID: 7672EFD79378341C
13 changed files with 1363 additions and 1322 deletions

58
ai_animation/src/phase.ts Normal file
View file

@ -0,0 +1,58 @@
import { gameState } from "./gameState";
import { logger } from "./logger";
import { phaseDisplay } from "./domElements";
import { createSupplyCenters } from "./units/create";
import { createUnitMesh } from "./units/create";
import { updateSupplyCenterOwnership, updateLeaderboard, updateMapOwnership } from "./map/state";
// New function to display initial state without messages
export function displayInitialPhase() {
let index = 0
if (!gameState.gameData || !gameState.gameData.phases || index < 0 || index >= gameState.gameData.phases.length) {
logger.log("Invalid phase index.")
return;
}
// Clear any existing units
const supplyCenters = gameState.unitMeshes.filter(m => m.userData && m.userData.isSupplyCenter);
const oldUnits = gameState.unitMeshes.filter(m => m.userData && !m.userData.isSupplyCenter);
oldUnits.forEach(m => gameState.scene.remove(m));
gameState.unitMeshes = supplyCenters;
const phase = gameState.gameData.phases[index];
phaseDisplay.textContent = `Era: ${phase.name || 'Unknown Era'} (${index + 1}/${gameState.gameData.phases.length})`;
// Show supply centers
let newSCs = createSupplyCenters();
newSCs.forEach((sc) => gameState.scene.add(sc))
if (phase.state?.centers) {
updateSupplyCenterOwnership(phase.state.centers);
}
// Show units
if (phase.state?.units) {
for (const [power, unitArr] of Object.entries(phase.state.units)) {
unitArr.forEach(unitStr => {
const match = unitStr.match(/^([AF])\s+(.+)$/);
if (match) {
let newUnit = createUnitMesh({
power: power.toUpperCase(),
type: match[1],
province: match[2],
});
gameState.scene.add(newUnit)
gameState.unitMeshes.push(newUnit)
}
});
}
}
updateLeaderboard(phase);
updateMapOwnership(phase)
logger.log(`Phase: ${phase.name}\nSCs: ${phase.state?.centers ? JSON.stringify(phase.state.centers) : 'None'}\nUnits: ${phase.state?.units ? JSON.stringify(phase.state.units) : 'None'}`)
// Add: Update info panel
logger.updateInfoPanel();
}