Game.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { GameState } from "./states/GameState.js";
  2. import { MenuState } from "./states/MenuState.js";
  3. import { FiniteStateMachine } from "../libraries/FiniteStateMachine.js";
  4. import { Global } from "../libraries/Global.js";
  5. export class Game {
  6. constructor() {
  7. this.stateMachine = new FiniteStateMachine();
  8. this.stateMachine.registerState("game", new GameState(this.stateMachine))
  9. this.stateMachine.registerState("menu", new MenuState(this.stateMachine))
  10. }
  11. async init() {
  12. // await this.loadPlayerData();
  13. this.stateMachine.setCurrentState("menu");
  14. this.stateMachine.getCurrentState().init();
  15. this.lastTime = new Date().getTime()
  16. }
  17. // async loadPlayerData() {
  18. // let playerData = localStorage.getItem("ballsort-playerdata");
  19. // if(!playerData) {
  20. // playerData = {level: 1};
  21. // localStorage.setItem('ballsort-playerdata', JSON.stringify(playerData));
  22. // } else {
  23. // playerData = JSON.parse(playerData);
  24. // }
  25. // this.stateMachine.getState("game").setPlayerData(playerData);
  26. // this.stateMachine.getState("levelselect").setPlayerData(playerData);
  27. // return playerData;
  28. // }
  29. draw(ctx, scaledCanvas) {
  30. this.stateMachine.getCurrentState().draw(ctx, scaledCanvas);
  31. }
  32. update() {
  33. let currentTime = new Date().getTime();
  34. let deltaTime = currentTime - this.lastTime;
  35. this.lastTime = currentTime;
  36. this.stateMachine.getCurrentState().update(deltaTime / Global.updateTickRate);
  37. }
  38. }