import { GameState } from "./states/GameState.js"; import { MenuState } from "./states/MenuState.js"; import { FiniteStateMachine } from "../libraries/FiniteStateMachine.js"; import { Global } from "../libraries/Global.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)) } async init() { // await this.loadPlayerData(); this.stateMachine.setCurrentState("menu"); this.stateMachine.getCurrentState().init(); this.lastTime = new Date().getTime() } // 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() { let currentTime = new Date().getTime(); let deltaTime = currentTime - this.lastTime; this.lastTime = currentTime; this.stateMachine.getCurrentState().update(deltaTime / Global.updateTickRate); } }