MatchThreeState.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 Tile from "../../libraries/components/matchthree/Tile.js"
  7. import Button from "../../libraries/components/matchthree/Button.js"
  8. import AsyncTween from "../../libraries/AsyncTween.js"
  9. import { Easing } from "../../libraries/Easing.js"
  10. export default class MatchThreeState {
  11. constructor(view) {
  12. this.stateMachine = view.stateMachine
  13. this.camera = new Camera()
  14. this.camera.scale = new Point(1, 1)
  15. this.save = new Save()
  16. this.debug = true
  17. this.mousePosition = {x: 0, y: 0}
  18. this.mouseButton = -1
  19. this.tiles = []
  20. this.buttons = []
  21. this.selectedTiles = []
  22. this.tilesToRemove = []
  23. this.matchPopPromises = []
  24. this.matchCascadePromises = []
  25. }
  26. init(scaledCanvas) {
  27. this.canvasBounds = scaledCanvas.bounds
  28. this.boardSize = { x: 7, y: 7 }
  29. this.buttonCount = 6
  30. }
  31. enter() {
  32. this.registeredEvents = {}
  33. this.registeredEvents["resize"] = this.onResize.bind(this)
  34. this.registeredEvents["keydown"] = this.onKeyDown.bind(this)
  35. this.registeredEvents["keyup"] = this.onKeyUp.bind(this)
  36. this.registeredEvents["mousedown"] = this.onMouseDown.bind(this)
  37. this.registeredEvents["mousemove"] = this.onMouseMove.bind(this)
  38. this.registeredEvents["mouseup"] = this.onMouseUp.bind(this)
  39. this.registeredEvents["touchstart"] = this.onTouchStart.bind(this)
  40. this.registeredEvents["touchmove"] = this.onTouchMove.bind(this)
  41. this.registeredEvents["touchend"] = this.onTouchEnd.bind(this)
  42. for (let index in this.registeredEvents) {
  43. window.addEventListener(index, this.registeredEvents[index])
  44. }
  45. this.camera.targetPosition.x = 0
  46. this.camera.targetPosition.y = 0
  47. this.camera.position.x = 0
  48. this.camera.position.y = 0
  49. this.tiles = []
  50. this.buttons = []
  51. this.selectedTiles = []
  52. for (let y = 0; y < this.boardSize.y; y++) {
  53. for (let x = 0; x < this.boardSize.x; x++) {
  54. const tileType = Math.floor(Math.random() * Object.values(Theme.Colors.TileColors).length)
  55. this.tiles.push(new Tile(this, {x: x, y: y}, tileType))
  56. }
  57. }
  58. for (let x = 0; x < this.buttonCount; x++) {
  59. this.buttons.push(new Button(this, {x: x, y: 0}))
  60. }
  61. this.tiles.forEach((tile) => {
  62. tile.onResize(this.canvasBounds, this.boardSize)
  63. })
  64. this.buttons.forEach((button) => {
  65. button.setButtonCount(this.buttonCount)
  66. button.onResize(this.canvasBounds, this.boardSize)
  67. })
  68. this.checkForMatches()
  69. }
  70. leave(target, leavingCallback) {
  71. for (let index in this.registeredEvents) {
  72. window.removeEventListener(index, this.registeredEvents[index])
  73. }
  74. leavingCallback()
  75. }
  76. draw(ctx, scaledCanvas) {
  77. this.canvasBounds = scaledCanvas.bounds
  78. ctx.fillStyle = Theme.Colors.black
  79. ctx.fillRect(0, 0, this.canvasBounds.width, this.canvasBounds.height)
  80. this.camera.draw(ctx, scaledCanvas, () => {
  81. if (this.debug) {
  82. ctx.strokeStyle = Theme.Colors.seagreen
  83. ctx.beginPath()
  84. ctx.moveTo(0, -this.canvasBounds.height)
  85. ctx.lineTo(0, this.canvasBounds.height)
  86. ctx.stroke()
  87. ctx.beginPath()
  88. ctx.moveTo(-this.canvasBounds.width, 0)
  89. ctx.lineTo(this.canvasBounds.width, 0)
  90. ctx.stroke()
  91. ctx.beginPath()
  92. ctx.moveTo(-this.canvasBounds.width / 4, -this.canvasBounds.height / 4)
  93. ctx.lineTo(this.canvasBounds.width / 4, -this.canvasBounds.height / 4)
  94. ctx.stroke()
  95. ctx.beginPath()
  96. ctx.moveTo(-this.canvasBounds.width / 4, this.canvasBounds.height / 4)
  97. ctx.lineTo(this.canvasBounds.width / 4, this.canvasBounds.height / 4)
  98. ctx.stroke()
  99. ctx.strokeStyle = null
  100. }
  101. this.tiles.forEach((tile) => {
  102. tile.draw(ctx)
  103. })
  104. this.buttons.forEach((button) => {
  105. button.draw(ctx)
  106. })
  107. })
  108. }
  109. update(delta) {
  110. this.camera.update(delta)
  111. AsyncTween.update()
  112. this.tiles = this.tiles.filter((tile) => !this.tilesToRemove.includes(tile))
  113. this.tilesToRemove = []
  114. }
  115. trackSelectedTile(tile) {
  116. if(this.selectedTiles.includes(tile) || this.selectedTiles.length == 2) {
  117. return
  118. }
  119. this.selectedTiles.push(tile)
  120. if(this.selectedTiles.length == 2) {
  121. this.tradeTiles(this.selectedTiles[0], this.selectedTiles[1])
  122. }
  123. }
  124. tradeTiles(firstTile, otherTile) {
  125. if(!this.validToSwap(firstTile, otherTile)) {
  126. this.selectedTiles = []
  127. return
  128. }
  129. let swap = []
  130. swap.push(AsyncTween.create(firstTile.position, otherTile.position, 500, Easing.Elastic.EaseOut).promise)
  131. swap.push(AsyncTween.create(otherTile.position, firstTile.position, 500, Easing.Elastic.EaseOut).promise)
  132. Promise.all(swap).then(() => {
  133. this.selectedTiles = []
  134. this.checkForMatches()
  135. })
  136. }
  137. validToSwap(firstTile, otherTile) {
  138. if(firstTile.position.x + 1 == otherTile.position.x && firstTile.position.y == otherTile.position.y) {
  139. return true
  140. }
  141. if(firstTile.position.x - 1 == otherTile.position.x && firstTile.position.y == otherTile.position.y) {
  142. return true
  143. }
  144. if(firstTile.position.x == otherTile.position.x && firstTile.position.y + 1== otherTile.position.y) {
  145. return true
  146. }
  147. if(firstTile.position.x == otherTile.position.x && firstTile.position.y - 1 == otherTile.position.y) {
  148. return true
  149. }
  150. return false
  151. }
  152. checkForMatches() {
  153. const tilesToPop = []
  154. //five in a row horizontal
  155. //five in a row vertical
  156. //four in a row horizontal
  157. //four in a row vertical
  158. //5 in an L shape
  159. //three in a row horizontal
  160. this.tiles.forEach((tile, index, array) => {
  161. if(tilesToPop.includes(tile)) {
  162. return
  163. }
  164. const validTiles = this.tiles.filter((checkedTile) => tile.type == checkedTile.type && checkedTile.position.y == tile.position.y && (checkedTile.position.x == tile.position.x + 1 || checkedTile.position.x == tile.position.x - 1))
  165. if(validTiles.length == 2) {
  166. tilesToPop.push(tile, ...validTiles)
  167. }
  168. })
  169. //three in a row vertical
  170. this.tiles.forEach((tile, index, array) => {
  171. if(tilesToPop.includes(tile)) {
  172. return
  173. }
  174. const validTiles = this.tiles.filter((checkedTile) => tile.type == checkedTile.type && checkedTile.position.x == tile.position.x && (checkedTile.position.y == tile.position.y + 1 || checkedTile.position.y == tile.position.y - 1))
  175. if(validTiles.length == 2) {
  176. tilesToPop.push(tile, ...validTiles)
  177. }
  178. })
  179. //push matches into poplist
  180. this.popAndCascade(tilesToPop)
  181. }
  182. popAndCascade(listToPop) {
  183. if(listToPop.length == 0) {
  184. return
  185. }
  186. if(this.matchPopPromises.length > 0) {
  187. return
  188. }
  189. let remainingTiles = this.tiles.filter((tile) => !listToPop.includes(tile))
  190. listToPop.forEach((tile) => {
  191. this.matchPopPromises.push(AsyncTween.create(tile, {position: {y: (this.canvasBounds.height / 2) + Tile.Size}}, 1000, Easing.Quadratic.EaseIn).promise)
  192. })
  193. if(this.matchCascadePromises.length > 0) {
  194. return
  195. }
  196. for(let x = 0; x < this.boardSize.x; x++){
  197. const columnOfTiles = remainingTiles.filter((tile) => tile.position.x == x)
  198. let shiftDownAmount = {}
  199. for(let y = this.boardSize.y - 1; y > 0; y--) {
  200. let spotFilled = columnOfTiles.find((t) => t.position.y == y)
  201. if(spotFilled == null) {
  202. shiftDownAmount[y] = 1 ?? 0
  203. }
  204. }
  205. columnOfTiles.forEach((tile) => {
  206. let tileShift = 0
  207. Object.keys(shiftDownAmount).forEach((y) => {
  208. if(y > tile.position.y) {
  209. tileShift++
  210. }
  211. })
  212. this.matchCascadePromises.push(AsyncTween.create(tile, {position: {y: tile.position.y + tileShift}}, 200 * tileShift, Easing.Quadratic.EaseIn).promise)
  213. })
  214. }
  215. Promise.all(this.matchCascadePromises).then(() => {
  216. this.matchCascadePromises = []
  217. Promise.all(this.matchPopPromises).then(() => {
  218. this.matchPopPromises = []
  219. this.checkForMatches()
  220. })
  221. })
  222. }
  223. removeTile(tileToRemove) {
  224. this.tilesToRemove.push(tileToRemove)
  225. }
  226. sceneComplete() {
  227. this.stateMachine.transitionTo("credits")
  228. }
  229. getRandomIndex(array, condition) {
  230. let item = array[Math.floor(array.length * Math.random())]
  231. let timeout = 10
  232. while (!condition(item)) {
  233. timeout--
  234. if (timeout <= 0) {
  235. throw new Error("unable to get item that meets conditions")
  236. }
  237. item = array[Math.floor(array.length * Math.random())]
  238. }
  239. return item
  240. }
  241. onResize() {
  242. this.tiles.forEach((tile) => {
  243. tile.onResize(this.canvasBounds, this.boardSize)
  244. })
  245. this.buttons.forEach((button) => {
  246. button.onResize(this.canvasBounds, this.boardSize)
  247. })
  248. }
  249. onKeyDown(event) {
  250. }
  251. onKeyUp(event) {
  252. }
  253. onMouseDown(event) {
  254. this.mouseButton = event.button
  255. this.onInputDown()
  256. }
  257. onMouseMove(event) {
  258. let position = this.camera.screenToWorld({x: event.clientX, y: event.clientY})
  259. this.mousePosition.x = position.x
  260. this.mousePosition.y = position.y
  261. this.onInputMove(this.mouseButton != -1)
  262. }
  263. onMouseUp(event) {
  264. this.mouseButton = -1
  265. this.onInputUp()
  266. }
  267. onTouchStart(event) {
  268. let touch = event.touches[0]
  269. let position = this.camera.screenToWorld({x: touch.clientX, y: touch.clientY})
  270. this.mousePosition.x = position.x
  271. this.mousePosition.y = position.y
  272. this.onInputDown()
  273. this.onInputMove(true)
  274. }
  275. onTouchEnd(event) {
  276. let touch = event.touches[0]
  277. let position = this.camera.screenToWorld({x: touch.clientX, y: touch.clientY})
  278. this.mousePosition.x = position.x
  279. this.mousePosition.y = position.y
  280. this.onInputUp()
  281. }
  282. onTouchMove(event) {
  283. let touch = event.touches[0]
  284. let position = this.camera.screenToWorld({x: touch.clientX, y: touch.clientY})
  285. this.mousePosition.x = position.x
  286. this.mousePosition.y = position.y
  287. this.onInputMove(true)
  288. }
  289. onInputDown() {
  290. this.tiles.forEach((tile) => {
  291. tile.onInputDown(this.mousePosition)
  292. })
  293. this.buttons.forEach((button) => {
  294. button.onInputDown(this.mousePosition)
  295. })
  296. }
  297. onInputUp() {
  298. this.tiles.forEach((tile) => {
  299. tile.onInputUp(this.mousePosition)
  300. })
  301. this.buttons.forEach((button) => {
  302. button.onInputUp(this.mousePosition)
  303. })
  304. }
  305. onInputMove(isDown) {
  306. document.body.style.cursor = "default"
  307. this.tiles.forEach((tile) => {
  308. tile.onInputMove(this.mousePosition, isDown)
  309. })
  310. this.buttons.forEach((button) => {
  311. button.onInputMove(this.mousePosition, isDown)
  312. })
  313. }
  314. // digSound1(i, t) {
  315. // var n = 5e3;
  316. // if (i > n) return null;
  317. // return ((Math.pow(i + Math.sin(i * 0.01) * 1000, 0.25) & 200) ? 1 : -1) * Math.pow(t(i, n), 1);
  318. // }
  319. // digSound2(i, t) {
  320. // var n = 5e3;
  321. // if (i > n) return null;
  322. // return (Math.random() * 2 - 1) * t(i, n);
  323. // }
  324. // gatherSound(i, t) {
  325. // var n=1.6e4;
  326. // var c=n/7;
  327. // if (i > n) return null;
  328. // var q=Math.pow(t(i,n),2.1);
  329. // return (i<c ? ((i+Math.sin(-i/900)*10)&16) : i&13) ?q:-q;
  330. // }
  331. // playSound(soundFunction) {
  332. // // Sound player
  333. // let t = (i, n) => (n - i) / n,
  334. // A = new AudioContext(),
  335. // m = A.createBuffer(1, 96e3, 48e3),
  336. // b = m.getChannelData(0)
  337. // for (let i = 96e3; i--;)b[i] = soundFunction(i, t)
  338. // let s = A.createBufferSource()
  339. // s.buffer = m
  340. // s.connect(A.destination)
  341. // s.start()
  342. // }
  343. }