ActionQueue.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import AsyncTween from "./AsyncTween.js"
  2. import { Easing } from "./Easing.js"
  3. export class ActionQueue {
  4. constructor() {
  5. this.pendingActions = []
  6. }
  7. push(action) {
  8. this.pendingActions.push(action)
  9. }
  10. execute(actionType, eachCallback = () => true) {
  11. return new Promise((resolve, reject) => {
  12. const actionsToRun = this.pendingActions.filter((action) => action instanceof actionType)
  13. const pending = actionsToRun.map((action) => action.execute(eachCallback))
  14. Promise.all(pending).then(() => {
  15. this.pendingActions = this.pendingActions.filter((action) => !(action instanceof actionType))
  16. resolve()
  17. }, () => {
  18. this.pendingActions = this.pendingActions.filter((action) => !(action instanceof actionType))
  19. reject()
  20. })
  21. })
  22. }
  23. }
  24. export class SwapTileAction {
  25. constructor(firstTile, otherTile) {
  26. this.firstTile = firstTile
  27. this.otherTile = otherTile
  28. this.tweens = []
  29. }
  30. execute(eachCallback = () => true) {
  31. return new Promise((resolve, reject) => {
  32. if(!eachCallback()) {
  33. reject()
  34. return
  35. }
  36. this.tweens = [
  37. AsyncTween.create(this.firstTile.position, this.otherTile.position, 500, Easing.Elastic.EaseOut),
  38. AsyncTween.create(this.otherTile.position, this.firstTile.position, 500, Easing.Elastic.EaseOut)]
  39. Promise.all(this.tweens.map((tween) => tween.promise)).then(() => {
  40. resolve()
  41. this.tweens = []
  42. })
  43. })
  44. }
  45. }
  46. export class MatchTilesAction {
  47. constructor(tilesToMatch) {
  48. this.tilesToMatch = tilesToMatch
  49. this.tweens = []
  50. }
  51. execute(eachCallback = () => true) {
  52. return new Promise((resolve, reject) => {
  53. if(!eachCallback()) {
  54. reject()
  55. return
  56. }
  57. this.tweens = []
  58. this.tilesToMatch.forEach((tile) => {
  59. this.tweens.push(AsyncTween.create(tile, {scale: 0}, 500, Easing.Quadratic.EaseInOut))
  60. })
  61. Promise.all(this.tweens.map((tween) => tween.promise)).then(() => {
  62. resolve()
  63. this.tweens = []
  64. })
  65. })
  66. }
  67. }
  68. export class TileFallAction {
  69. constructor(tile, bottom) {
  70. this.tile = tile
  71. this.bottom = bottom
  72. }
  73. execute(eachCallback = () => true) {
  74. return new Promise((resolve, reject) => {
  75. const promiseData = AsyncTween.create(this.tile, {position: {y: this.bottom}}, 800, Easing.Bounce.EaseOut)
  76. promiseData.promise.then(() => {
  77. resolve()
  78. })
  79. })
  80. }
  81. }
  82. export class SpawnTileAction {
  83. constructor(column, numberToSpawn) {
  84. this.column = column
  85. this.numberToSpawn = numberToSpawn
  86. this.tweens = []
  87. }
  88. execute(eachCallback = () => true) {
  89. return new Promise((resolve, reject) => {
  90. this.tweens = []
  91. for(let i = 0; i < this.numberToSpawn; i++) {
  92. const newTile = eachCallback(this.column, -i - 1)
  93. newTile.scale = 0
  94. this.tweens.push(AsyncTween.create(newTile, {scale: 1}, 300, Easing.Quadratic.EaseInOut))
  95. }
  96. Promise.all(this.tweens.map((tween) => tween.promise)).then(() => {
  97. resolve()
  98. this.tweens = []
  99. })
  100. })
  101. }
  102. }