import { GameState } from "./states/GameState.js"; import { MenuState } from "./states/MenuState.js"; import { FiniteStateMachine } from "../libraries/FiniteStateMachine.js"; import { AsyncDataReader } from "../libraries/AsyncDataReader.js"; import { LevelSelectState } from "./states/LevelSelectState.js"; export class Game { constructor() { this.stateMachine = new FiniteStateMachine(); this.stateMachine.registerState("game", new GameState(this.stateMachine)) this.stateMachine.registerState("menu", new MenuState(this.stateMachine)) this.stateMachine.registerState("levelselect", new LevelSelectState(this.stateMachine)) } async init() { await this.loadLevelData(); await this.loadPlayerData(); this.stateMachine.setCurrentState("menu"); this.stateMachine.getCurrentState().init(); } async loadLevelData() { let levelData = await new AsyncDataReader().readJson("./js/game/leveldata/levels.json"); this.stateMachine.getState("game").setLevelData(levelData); this.stateMachine.getState("levelselect").setLevelData(levelData); return levelData; } async loadPlayerData() { let playerData = localStorage.getItem("ballsort-playerdata"); if(!playerData) { playerData = {level: 1}; localStorage.setItem('ballsort-playerdata', JSON.stringify(playerData)); } else { playerData = JSON.parse(playerData); } this.stateMachine.getState("game").setPlayerData(playerData); this.stateMachine.getState("levelselect").setPlayerData(playerData); return playerData; } draw(ctx, scaledCanvas) { this.stateMachine.getCurrentState().draw(ctx, scaledCanvas); } update(delta) { this.stateMachine.getCurrentState().update(delta); } }