MatchThreeState2.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { Camera } from "../../libraries/Camera.js"
  2. import { Point } from "../../libraries/spatial/Point.js"
  3. import { Theme } from "../../libraries/components/Theme.js"
  4. import { Save } from "../../libraries/Save.js"
  5. import "../../libraries/RoundRectPolyfill.js"
  6. import AsyncTween from "../../libraries/AsyncTween.js"
  7. import { Easing } from "../../libraries/Easing.js"
  8. import Board from "../../libraries/components/matchthree/Board.js"
  9. export default class MatchThreeState {
  10. constructor(view) {
  11. this.stateMachine = view.stateMachine
  12. this.camera = new Camera()
  13. this.camera.scale = new Point(1, 1)
  14. this.save = new Save()
  15. this.debug = true
  16. this.mousePosition = new Point(0, 0)
  17. this.mouseButton = -1
  18. this.alpha = 1
  19. this.board = new Board()
  20. }
  21. init(scaledCanvas) {
  22. this.canvasBounds = scaledCanvas.bounds
  23. this.buttonCount = 6
  24. }
  25. enter() {
  26. this.registeredEvents = {}
  27. this.registeredEvents["resize"] = this.onResize.bind(this)
  28. this.registeredEvents["keydown"] = this.onKeyDown.bind(this)
  29. this.registeredEvents["keyup"] = this.onKeyUp.bind(this)
  30. this.registeredEvents["mousedown"] = this.onMouseDown.bind(this)
  31. this.registeredEvents["mousemove"] = this.onMouseMove.bind(this)
  32. this.registeredEvents["mouseup"] = this.onMouseUp.bind(this)
  33. this.registeredEvents["touchstart"] = this.onTouchStart.bind(this)
  34. this.registeredEvents["touchmove"] = this.onTouchMove.bind(this)
  35. this.registeredEvents["touchend"] = this.onTouchEnd.bind(this)
  36. for (let index in this.registeredEvents) {
  37. window.addEventListener(index, this.registeredEvents[index])
  38. }
  39. this.camera.targetPosition.x = 0
  40. this.camera.targetPosition.y = 0
  41. this.camera.position.x = 0
  42. this.camera.position.y = 0
  43. this.board.init(this.canvasBounds)
  44. this.alpha = 1
  45. AsyncTween.create(this, { alpha: 0 }, 500, Easing.Quadratic.EaseOut)
  46. }
  47. leave(target, leavingCallback) {
  48. for (let index in this.registeredEvents) {
  49. window.removeEventListener(index, this.registeredEvents[index])
  50. }
  51. AsyncTween.clear()
  52. AsyncTween.create(this, { alpha: 1 }, 500, Easing.Quadratic.EaseIn).promise.then(() => {
  53. leavingCallback()
  54. })
  55. }
  56. draw(ctx, scaledCanvas) {
  57. this.canvasBounds = scaledCanvas.bounds
  58. ctx.fillStyle = Theme.Colors.black
  59. ctx.fillRect(0, 0, this.canvasBounds.width, this.canvasBounds.height)
  60. this.camera.draw(ctx, scaledCanvas, () => {
  61. if (this.debug) {
  62. ctx.strokeStyle = Theme.Colors.seagreen
  63. ctx.beginPath()
  64. ctx.moveTo(0, -this.canvasBounds.height)
  65. ctx.lineTo(0, this.canvasBounds.height)
  66. ctx.stroke()
  67. ctx.beginPath()
  68. ctx.moveTo(-this.canvasBounds.width, 0)
  69. ctx.lineTo(this.canvasBounds.width, 0)
  70. ctx.stroke()
  71. ctx.beginPath()
  72. ctx.moveTo(-this.canvasBounds.width / 4, -this.canvasBounds.height / 4)
  73. ctx.lineTo(this.canvasBounds.width / 4, -this.canvasBounds.height / 4)
  74. ctx.stroke()
  75. ctx.beginPath()
  76. ctx.moveTo(-this.canvasBounds.width / 4, this.canvasBounds.height / 4)
  77. ctx.lineTo(this.canvasBounds.width / 4, this.canvasBounds.height / 4)
  78. ctx.stroke()
  79. this.board.draw(ctx)
  80. ctx.fillStyle = "rgba(120, 120, 120, 0.5)"
  81. ctx.strokeStyle = "rgba(120, 120, 120, 0.9)"
  82. ctx.strokeWidth = 2
  83. ctx.beginPath()
  84. ctx.arc(this.mousePosition.x, this.mousePosition.y, 10, 0, 2 * Math.PI)
  85. ctx.fill()
  86. ctx.stroke()
  87. }
  88. })
  89. ctx.fillStyle = `rgb(0,0,0,${this.alpha})`
  90. ctx.beginPath()
  91. ctx.rect(0, 0, this.canvasBounds.width, this.canvasBounds.height)
  92. ctx.fill()
  93. }
  94. update(delta) {
  95. this.camera.update(delta)
  96. this.board.update(delta)
  97. AsyncTween.update()
  98. }
  99. sceneComplete() {
  100. this.stateMachine.transitionTo("credits", true)
  101. }
  102. onResize() {
  103. this.board.onResize(this.canvasBounds)
  104. }
  105. onKeyDown(event) {
  106. }
  107. onKeyUp(event) {
  108. switch (event.code) {
  109. case "Escape":
  110. this.stateMachine.transitionTo("mainmenu", true)
  111. break
  112. case "Digit0":
  113. this.stateMachine.transitionTo("credits", true)
  114. break
  115. }
  116. }
  117. onMouseDown(event) {
  118. this.mouseButton = event.button
  119. this.onInputDown()
  120. }
  121. onMouseMove(event) {
  122. let position = this.camera.screenToWorld({ x: event.clientX, y: event.clientY })
  123. this.mousePosition.x = position.x
  124. this.mousePosition.y = position.y
  125. this.onInputMove(this.mouseButton != -1)
  126. }
  127. onMouseUp(event) {
  128. this.mouseButton = -1
  129. this.onInputUp()
  130. }
  131. onTouchStart(event) {
  132. let touch = event.touches[0]
  133. let position = this.camera.screenToWorld({ x: touch.clientX, y: touch.clientY })
  134. this.mousePosition.x = position.x
  135. this.mousePosition.y = position.y
  136. this.onInputDown()
  137. this.onInputMove(true)
  138. }
  139. onTouchEnd(event) {
  140. let touch = event.touches[0]
  141. let position = this.camera.screenToWorld({ x: touch.clientX, y: touch.clientY })
  142. this.mousePosition.x = position.x
  143. this.mousePosition.y = position.y
  144. this.onInputUp()
  145. }
  146. onTouchMove(event) {
  147. let touch = event.touches[0]
  148. let position = this.camera.screenToWorld({ x: touch.clientX, y: touch.clientY })
  149. this.mousePosition.x = position.x
  150. this.mousePosition.y = position.y
  151. this.onInputMove(true)
  152. }
  153. onInputDown() {
  154. this.board.onInputDown(this.mousePosition)
  155. }
  156. onInputUp() {
  157. this.board.onInputUp(this.mousePosition)
  158. }
  159. onInputMove(isDown) {
  160. document.body.style.cursor = "default"
  161. this.board.onInputMove(this.mousePosition, isDown)
  162. }
  163. }