import AsyncTween from "./AsyncTween.js" import { Easing } from "./Easing.js" export class ActionQueue { constructor() { this.pendingActions = [] } push(action) { this.pendingActions.push(action) } execute(actionType, eachCallback = () => true) { return new Promise((resolve, reject) => { const actionsToRun = this.pendingActions.filter((action) => action instanceof actionType) const pending = actionsToRun.map((action) => action.execute(eachCallback)) Promise.all(pending).then(() => { this.pendingActions = this.pendingActions.filter((action) => !(action instanceof actionType)) resolve() }, () => { this.pendingActions = this.pendingActions.filter((action) => !(action instanceof actionType)) reject() }) }) } } export class SwapTileAction { constructor(firstTile, otherTile) { this.firstTile = firstTile this.otherTile = otherTile this.tweens = [] } execute(eachCallback = () => true) { return new Promise((resolve, reject) => { if(!eachCallback()) { reject() return } this.tweens = [ AsyncTween.create(this.firstTile.position, this.otherTile.position, 500, Easing.Elastic.EaseOut), AsyncTween.create(this.otherTile.position, this.firstTile.position, 500, Easing.Elastic.EaseOut)] Promise.all(this.tweens.map((tween) => tween.promise)).then(() => { resolve() this.tweens = [] }) }) } } export class MatchTilesAction { constructor(tilesToMatch) { this.tilesToMatch = tilesToMatch this.tweens = [] } execute(eachCallback = () => true) { return new Promise((resolve, reject) => { if(!eachCallback()) { reject() return } this.tweens = [] this.tilesToMatch.forEach((tile) => { this.tweens.push(AsyncTween.create(tile, {scale: 0}, 500, Easing.Quadratic.EaseInOut)) }) Promise.all(this.tweens.map((tween) => tween.promise)).then(() => { resolve() this.tweens = [] }) }) } } export class TileFallAction { constructor(tile, bottom) { this.tile = tile this.bottom = bottom } execute(eachCallback = () => true) { return new Promise((resolve, reject) => { const promiseData = AsyncTween.create(this.tile, {position: {y: this.bottom}}, 800, Easing.Bounce.EaseOut) promiseData.promise.then(() => { resolve() }) }) } } export class SpawnTileAction { constructor(column, numberToSpawn) { this.column = column this.numberToSpawn = numberToSpawn this.tweens = [] } execute(eachCallback = () => true) { return new Promise((resolve, reject) => { this.tweens = [] for(let i = 0; i < this.numberToSpawn; i++) { const newTile = eachCallback(this.column, -i - 1) newTile.scale = 0 this.tweens.push(AsyncTween.create(newTile, {scale: 1}, 300, Easing.Quadratic.EaseInOut)) } Promise.all(this.tweens.map((tween) => tween.promise)).then(() => { resolve() this.tweens = [] }) }) } }