Skip to content

Instantly share code, notes, and snippets.

@RaheelYawar
Created August 23, 2020 19:06
Show Gist options
  • Save RaheelYawar/e3b260ec4e3855e10804de2e268696a7 to your computer and use it in GitHub Desktop.
Save RaheelYawar/e3b260ec4e3855e10804de2e268696a7 to your computer and use it in GitHub Desktop.
Coroutine class for JavaScript games (WIP)
/**
* A utility class to mimic the Phaser Timer class but dependent on an external delta time.
* */
export default class CoroutineUtility {
/**
* @param {Phaser.Game} game
* */
constructor(game) {
this.game = game;
this.routines = [];
}
/**
* @param {number} delta
* */
update(delta) {
for (let i = 0; i < this.routines.length; i++) {
this.routines[i].time -= delta;
if (this.routines[i].time <= 0) {
this.routines[i].f();
this.routines.splice(i, 1);
break;
}
}// end of for i
}
/**
* Executes the passed function when the time runs out.
* @param {number} time
* @param {function} f
* */
executeAfterTime(time, f) {
this.routines.push({time, f});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment