MenuState.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { Board } from "../Board.js";
  2. import { Button } from "../../libraries/components/Button.js";
  3. export class MenuState {
  4. constructor(fsm) {
  5. this.stateMachine = fsm;
  6. this.boundMouseEvent = {};
  7. }
  8. init() {
  9. this.boundMouseEvent['mousedown'] = this.mouseDown.bind(this);
  10. this.boundMouseEvent['mousemove'] = this.mouseMove.bind(this);
  11. for(let key in this.boundMouseEvent) {
  12. window.addEventListener(key, this.boundMouseEvent[key]);
  13. }
  14. this.board = new Board();
  15. this.board.initDemo();
  16. this.playButton = new Button(0, 0, 160, 70, "Play");
  17. this.playButton.hoverFunction((ctx) => {
  18. ctx.fillStyle = "rgba(255,255,255,0.8)";
  19. });
  20. this.playButton.clickFunction(() => {
  21. this.stateMachine.transitionTo("levelselect");
  22. });
  23. this.deleteButton = new Button(0,0, 80, 20, "delete save",
  24. {textFillStyle: "red", fontSize: 14, fontFamily: "Ariel", textStrokeStyle: null, backgroundFillStyle: null}
  25. );
  26. this.deleteButton.clickFunction(() => {
  27. if(confirm("Are you sure you want to reset your save data? This cannot be undone.")) {
  28. localStorage.removeItem('ballsort-playerdata');
  29. window.location.reload();
  30. }
  31. })
  32. }
  33. draw(ctx, scaledCanvas) {
  34. this.board.drawDemo(ctx,scaledCanvas);
  35. let canvasBounds = scaledCanvas.bounds;
  36. let center = canvasBounds.center;
  37. this.playButton.setPosition(center.x, center.y);
  38. this.playButton.draw(ctx,scaledCanvas);
  39. this.deleteButton.setPosition(center.x, canvasBounds.height - 20);
  40. this.deleteButton.draw(ctx, scaledCanvas);
  41. ctx.fillStyle = "white";
  42. ctx.strokeStyle = "black";
  43. ctx.font = "72px Luckiest Guy";
  44. ctx.textAlign = "center";
  45. ctx.lineWidth = 4;
  46. ctx.fillText(`Ballsort`, center.x, center.y - 82);
  47. ctx.strokeText(`Ballsort`, center.x, center.y - 82);
  48. }
  49. update(delta) {
  50. }
  51. mouseDown(event) {
  52. this.playButton.mouseDown(event);
  53. this.deleteButton.mouseDown(event);
  54. }
  55. mouseMove(event) {
  56. document.body.style.cursor = "default";
  57. this.playButton.mouseMove(event);
  58. this.deleteButton.mouseMove(event);
  59. }
  60. enter() {
  61. this.init();
  62. }
  63. leave() {
  64. for(let key in this.boundMouseEvent) {
  65. window.removeEventListener(key, this.boundMouseEvent[key]);
  66. }
  67. }
  68. }