MenuState.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { Button } from "../../libraries/components/Button.js";
  2. import { Global } from "../../libraries/Global.js";
  3. export class MenuState {
  4. constructor(fsm) {
  5. this.stateMachine = fsm;
  6. this.boundEvents = {};
  7. }
  8. init() {
  9. this.boundEvents['mousedown'] = this.mouseDown.bind(this);
  10. this.boundEvents['mousemove'] = this.mouseMove.bind(this);
  11. for(let key in this.boundEvents) {
  12. window.addEventListener(key, this.boundEvents[key]);
  13. }
  14. this.playButton = new Button(0, 0, 160, 70, "Play", {textFillStyle: "white", backgroundFillStyle: "rgba(255,255,255,0.5)",});
  15. this.playButton.hoverFunction((ctx) => {
  16. ctx.fillStyle = "rgba(255,255,255,0.8)";
  17. });
  18. this.playButton.clickFunction(() => {
  19. this.stateMachine.transitionTo("game");
  20. });
  21. // this.deleteButton = new Button(0,0, 80, 20, "delete save",
  22. // {textFillStyle: "red", fontSize: 14, fontFamily: "Ariel", textStrokeStyle: null, backgroundFillStyle: null}
  23. // );
  24. // this.deleteButton.clickFunction(() => {
  25. // if(confirm("Are you sure you want to reset your save data? This cannot be undone.")) {
  26. // localStorage.removeItem('ballsort-playerdata');
  27. // window.location.reload();
  28. // }
  29. // })
  30. }
  31. draw(ctx) {
  32. let canvasBounds = Global.screenBounds
  33. let center = canvasBounds.center;
  34. this.playButton.setPosition(center.x, center.y);
  35. this.playButton.draw(ctx);
  36. }
  37. update(delta) {
  38. }
  39. mouseDown(event) {
  40. this.playButton.mouseDown(event);
  41. }
  42. mouseMove(event) {
  43. document.body.style.cursor = "default";
  44. this.playButton.mouseMove(event);
  45. }
  46. enter() {
  47. this.init();
  48. }
  49. leave() {
  50. for(let key in this.boundEvents) {
  51. window.removeEventListener(key, this.boundEvents[key]);
  52. }
  53. }
  54. }