* update all objects related to this game active scene/stage * @param time - current timestamp as provided by the RAF callback
(time: number)
| 886 | * @param time - current timestamp as provided by the RAF callback |
| 887 | */ |
| 888 | update(time: number): void { |
| 889 | // handle frame skipping if required |
| 890 | if (++this.frameCounter % this.frameRate === 0) { |
| 891 | // reset the frame counter |
| 892 | this.frameCounter = 0; |
| 893 | |
| 894 | // publish notification |
| 895 | emit(GAME_BEFORE_UPDATE, time); |
| 896 | this.accumulator += timer.getDelta(); |
| 897 | this.accumulator = Math.min(this.accumulator, this.accumulatorMax); |
| 898 | |
| 899 | this.updateDelta = timer.interpolation ? timer.getDelta() : this.stepSize; |
| 900 | this.accumulatorUpdateDelta = timer.interpolation |
| 901 | ? this.updateDelta |
| 902 | : Math.max(this.updateDelta, this.updateAverageDelta); |
| 903 | |
| 904 | while ( |
| 905 | this.accumulator >= this.accumulatorUpdateDelta || |
| 906 | timer.interpolation |
| 907 | ) { |
| 908 | this.lastUpdateStart = globalThis.performance.now(); |
| 909 | |
| 910 | // game update event |
| 911 | if (!state.isPaused()) { |
| 912 | emit(GAME_UPDATE, time); |
| 913 | } |
| 914 | |
| 915 | // update all objects (and pass the elapsed time since last frame) |
| 916 | this.isDirty = this.world.update(this.updateDelta); |
| 917 | this.isDirty = |
| 918 | state.current()!.update(this.updateDelta) || this.isDirty; |
| 919 | |
| 920 | this.lastUpdate = globalThis.performance.now(); |
| 921 | this.updateAverageDelta = this.lastUpdate - this.lastUpdateStart; |
| 922 | |
| 923 | this.accumulator -= this.accumulatorUpdateDelta; |
| 924 | if (timer.interpolation) { |
| 925 | this.accumulator = 0; |
| 926 | break; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | // publish notification |
| 931 | emit(GAME_AFTER_UPDATE, this.lastUpdate); |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | /** |
| 936 | * draw the active scene/stage associated to this game |