123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- 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 = []
- })
- })
- }
- }
|