GameView.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { FiniteStateMachine } from "../libraries/FiniteStateMachine.js";
  2. import { SpritesheetAtlas } from "../libraries/SpritesheetAtlas.js";
  3. import { WebsocketCommunicator } from "../libraries/WebsocketCommunicator.js";
  4. import { CharacterCreationState } from "./states/CharacterCreationState.js";
  5. import { OverworldState } from "./states/OverworldState.js";
  6. export class GameView {
  7. constructor() {
  8. this.stateMachine = new FiniteStateMachine();
  9. this.characterAtlas = new SpritesheetAtlas()
  10. this.stateMachine.registerState("charactercreation", new CharacterCreationState(this))
  11. this.stateMachine.registerState("play", new OverworldState(this))
  12. this.websocket = new WebsocketCommunicator()
  13. }
  14. async init() {
  15. this.websocket.subscribe(this)
  16. this.websocket.connect().then((websocket) => {
  17. websocket.send("connect")
  18. }, (e) => {
  19. console.error(e)
  20. })
  21. let initialLoading = []
  22. initialLoading.push(this.characterAtlas.load("./assets/roguelikeChar_transparent.png", { width: 16, height: 16, margin: 1 }))
  23. await Promise.all(initialLoading)
  24. this.stateMachine.setCurrentState("charactercreation");
  25. this.stateMachine.getCurrentState().enter();
  26. }
  27. draw(ctx, scaledCanvas) {
  28. this.stateMachine.getCurrentState().draw(ctx, scaledCanvas);
  29. }
  30. update(delta) {
  31. this.stateMachine.getCurrentState().update(delta);
  32. }
  33. handleMessage(message) {
  34. console.log("got message", message)
  35. }
  36. handleClose() {
  37. console.log("socket closed")
  38. }
  39. }