MainMenuState.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { Camera } from "../../libraries/Camera.js"
  2. import { Point } from "../../libraries/spatial/Point.js"
  3. import { TweenManager} from "../../libraries/Tween.js"
  4. import { DomButton } from "../../libraries/components/DomButton.js"
  5. import { Theme } from "../../libraries/components/Theme.js"
  6. import { Save } from "../../libraries/Save.js"
  7. import { Picture } from "../../libraries/CanvasArtist.js"
  8. export class MainMenuState {
  9. constructor(view) {
  10. this.stateMachine = view.stateMachine
  11. this.canvasBounds = null
  12. this.camera = new Camera()
  13. this.camera.scale = new Point(1, 1)
  14. this.tweenManager = new TweenManager()
  15. this.registeredEvents = {}
  16. this.playerPosition = new Point(0, 0)
  17. this.playButton = new DomButton(50, 50, view.element, "begin")
  18. this.wasChanged = false
  19. this.save = new Save()
  20. this.deleteSaveButton = new DomButton(
  21. 50,
  22. 50,
  23. view.element,
  24. "delete save",
  25. "skipbutton"
  26. )
  27. }
  28. init(scaledCanvas) {
  29. this.canvasBounds = scaledCanvas.bounds
  30. }
  31. draw(ctx, scaledCanvas) {
  32. let fontScale = this.canvasBounds.width / 500
  33. ctx.fillStyle = Theme.Colors.ivory
  34. ctx.font = `600 ${Math.min(Math.floor(48 * fontScale), 48)}px ${Theme.Fonts.Header}`
  35. ctx.textAlign = "center"
  36. this.camera.draw(ctx, scaledCanvas, () => {
  37. ctx.save()
  38. ctx.translate(0, -150)
  39. this.drawCameo(ctx)
  40. ctx.restore()
  41. ctx.fillText("Merchant", 0, -40)
  42. ctx.fillText("🔸 of 🔸", 0, 0)
  43. ctx.fillText("DEATH", 0, 40)
  44. })
  45. this.playButton.setPosition(
  46. this.canvasBounds.width * (2 / 4),
  47. this.canvasBounds.height * (3 / 4)
  48. )
  49. this.playButton.draw(ctx, scaledCanvas)
  50. this.deleteSaveButton.setPosition(
  51. this.canvasBounds.width * (2 / 4),
  52. this.canvasBounds.height * (3 / 4) + 100
  53. )
  54. this.deleteSaveButton.draw(ctx, scaledCanvas)
  55. // this.muteButton.setPosition(this.canvasBounds.width * (3 / 4), this.canvasBounds.height * (1 / 8))
  56. // this.muteButton.draw(ctx, scaledCanvas)
  57. }
  58. drawPlayer(ctx) {
  59. ctx.fillStyle = Theme.Colors.black
  60. ctx.beginPath()
  61. ctx.arc(-10, -8, 8, Math.PI, (3 * Math.PI) / 2)
  62. ctx.lineTo(10, -16)
  63. ctx.arc(10, -8, 8, (3 * Math.PI) / 2, 2 * Math.PI)
  64. ctx.lineTo(24, 28)
  65. ctx.lineTo(-24, 28)
  66. ctx.closePath()
  67. ctx.fill()
  68. }
  69. drawDashHead(ctx) {
  70. ctx.save()
  71. ctx.translate(0, -16)
  72. ctx.scale(0.7, 0.7)
  73. Picture.DashHead(ctx, {})
  74. ctx.restore()
  75. }
  76. drawCameo(ctx) {
  77. Picture.Cameo(ctx, {width: 128})
  78. }
  79. update(delta) {
  80. this.playButton.update(delta)
  81. this.deleteSaveButton.update(delta)
  82. // this.muteButton.update(delta)
  83. this.camera.targetPosition.y = 8 * Math.sin(new Date().getTime() / 500)
  84. this.camera.update(delta)
  85. this.tweenManager.update()
  86. }
  87. enter() {
  88. this.registeredEvents["resize"] = this.onResize.bind(this)
  89. this.registeredEvents["keyup"] = this.onKeyUp.bind(this)
  90. for (let index in this.registeredEvents) {
  91. window.addEventListener(index, this.registeredEvents[index])
  92. }
  93. this.playButton.attach()
  94. this.playButton.onClick(this.goToGame.bind(this))
  95. this.deleteSaveButton.attach()
  96. this.deleteSaveButton.onClick(this.deleteSave.bind(this))
  97. // this.muteButton.attach()
  98. // this.muteButton.onClick(this.mute.bind(this))
  99. }
  100. leave() {
  101. for (let index in this.registeredEvents) {
  102. window.removeEventListener(index, this.registeredEvents[index])
  103. }
  104. this.registeredEvents = {}
  105. this.tweenManager.clear()
  106. this.playButton.remove()
  107. this.deleteSaveButton.remove()
  108. // this.muteButton.remove()
  109. }
  110. onResize() { }
  111. onKeyUp(event) {
  112. switch (event.code) {
  113. case "Enter":
  114. this.goToGame()
  115. break
  116. }
  117. }
  118. goToGame() {
  119. this.stateMachine.transitionTo("storyupdate")
  120. // this.stateMachine.transitionTo("shop")
  121. }
  122. deleteSave() {
  123. if (confirm("Are you sure you want to delete your save data?")) {
  124. this.save.delete()
  125. alert("Save data deleted")
  126. }
  127. }
  128. mute() {
  129. console.log("mute")
  130. }
  131. }