import AsyncTween from "./AsyncTween.js" import { Easing } from "./Easing.js" export class ActionQueue { constructor() { this.pendingActions = [] } push(action) { this.pendingActions.push(action) } execute(actionType, isAllowedCallback = () => true) { return new Promise((resolve, reject) => { const actionsToRun = this.pendingActions.filter((action) => action instanceof actionType) const pending = actionsToRun.map((action) => action.execute(isAllowedCallback)) 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(isAllowedCallback = () => true) { return new Promise((resolve, reject) => { if(!isAllowedCallback()) { 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(isAllowedCallback = () => true) { return new Promise((resolve, reject) => { if(!isAllowedCallback()) { 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 = [] }) }) } }