Created
August 23, 2020 19:06
-
-
Save RaheelYawar/e3b260ec4e3855e10804de2e268696a7 to your computer and use it in GitHub Desktop.
Coroutine class for JavaScript games (WIP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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