MainMenuState.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { Camera } from "../../libraries/Camera.js"
  2. import { Point } from "../../libraries/spatial/Point.js"
  3. import { DomButton } from "../../libraries/components/DomButton.js"
  4. import { Theme } from "../../libraries/components/Theme.js"
  5. import { Save } from "../../libraries/Save.js"
  6. import AsyncTween from "../../libraries/AsyncTween.js"
  7. import { Easing } from "../../libraries/Easing.js"
  8. import TextWrapper from "../../libraries/TextWrapper.js"
  9. export default class MainMenuState {
  10. constructor(view) {
  11. this.view = view
  12. this.stateMachine = view.stateMachine
  13. this.canvasBounds = null
  14. this.camera = new Camera()
  15. this.camera.scale = new Point(1, 1)
  16. this.registeredEvents = {}
  17. this.playButton = new DomButton(50, 50, view.element, "begin")
  18. this.wasChanged = false
  19. this.save = new Save()
  20. this.textWrapper = new TextWrapper()
  21. this.deleteSaveButton = new DomButton(
  22. 50,
  23. 50,
  24. view.element,
  25. "delete save",
  26. "skipbutton"
  27. )
  28. this.muteButton = new DomButton(0, 0, view.element, "", "mutebutton")
  29. this.alpha = 0
  30. }
  31. init(scaledCanvas) {
  32. this.canvasBounds = scaledCanvas.bounds
  33. }
  34. draw(ctx, scaledCanvas) {
  35. let fontScale = this.canvasBounds.width / 500
  36. ctx.fillStyle = Theme.Colors.ivory
  37. const fontSize = Math.min(Math.floor(48 * fontScale), 48)
  38. ctx.font = `600 ${fontSize}px ${Theme.Fonts.Header}`
  39. ctx.textAlign = "center"
  40. this.camera.draw(ctx, scaledCanvas, () => {
  41. this.textWrapper.fontSize = fontSize
  42. ctx.save()
  43. ctx.translate(0, -this.canvasBounds.height / 2 + fontSize + 40)
  44. this.textWrapper.draw(ctx, "Marco Polo Returns", {width: this.canvasBounds.width})
  45. ctx.restore()
  46. })
  47. this.playButton.setPosition(
  48. this.canvasBounds.width / 2,
  49. this.canvasBounds.height - 200
  50. )
  51. this.playButton.draw(ctx, scaledCanvas)
  52. this.deleteSaveButton.setPosition(
  53. this.canvasBounds.width / 2,
  54. this.canvasBounds.height - 100
  55. )
  56. this.deleteSaveButton.draw(ctx, scaledCanvas)
  57. this.muteButton.setPosition(this.canvasBounds.width - 20, 20)
  58. this.muteButton.draw(ctx, scaledCanvas)
  59. ctx.fillStyle = `rgb(0,0,0,${this.alpha})`
  60. ctx.beginPath()
  61. ctx.rect(0,0, this.canvasBounds.width, this.canvasBounds.height)
  62. ctx.fill()
  63. }
  64. update(delta) {
  65. this.playButton.update(delta)
  66. this.deleteSaveButton.update(delta)
  67. this.muteButton.update(delta)
  68. this.camera.update(delta)
  69. AsyncTween.update()
  70. }
  71. enter() {
  72. this.registeredEvents["resize"] = this.onResize.bind(this)
  73. this.registeredEvents["keyup"] = this.onKeyUp.bind(this)
  74. for (let index in this.registeredEvents) {
  75. window.addEventListener(index, this.registeredEvents[index])
  76. }
  77. this.playButton.attach()
  78. this.playButton.onClick(this.goToGame.bind(this))
  79. this.deleteSaveButton.attach()
  80. this.deleteSaveButton.onClick(this.deleteSave.bind(this))
  81. this.muteButton.attach()
  82. this.muteButton.onClick(this.mute.bind(this))
  83. AsyncTween.create(this, {alpha: 0}, 500, Easing.Quadratic.EaseOut)
  84. }
  85. leave(target, leavingCallback) {
  86. for (let index in this.registeredEvents) {
  87. window.removeEventListener(index, this.registeredEvents[index])
  88. }
  89. this.registeredEvents = {}
  90. AsyncTween.clear()
  91. AsyncTween.create(this, {alpha: 1, playButton: {alpha: 0}, deleteSaveButton: {alpha: 0}, muteButton: {alpha: 0}}, 500, Easing.Quadratic.EaseIn).promise.then(() => {
  92. this.playButton.remove()
  93. this.deleteSaveButton.remove()
  94. this.muteButton.remove()
  95. leavingCallback()
  96. })
  97. }
  98. onResize() { }
  99. onKeyUp(event) {
  100. switch (event.code) {
  101. case "Digit1":
  102. this.stateMachine.transitionTo("matchthree")
  103. break
  104. case "Digit2":
  105. this.stateMachine.transitionTo("matchthree2", true)
  106. break
  107. case "Digit0":
  108. this.stateMachine.transitionTo("credits", true)
  109. break
  110. case "Enter":
  111. this.goToGame()
  112. break
  113. }
  114. }
  115. goToGame() {
  116. this.stateMachine.transitionTo("matchthree2")
  117. }
  118. deleteSave() {
  119. if (confirm("Are you sure you want to delete your save data?")) {
  120. this.save.delete()
  121. alert("Save data deleted")
  122. }
  123. }
  124. mute() {
  125. if(this.muteButton.element.className == "mutebutton") {
  126. this.muteButton.element.className = "unmutebutton"
  127. this.view.isMuted = true
  128. } else {
  129. this.muteButton.element.className = "mutebutton"
  130. this.view.isMuted = false
  131. }
  132. }
  133. }