2021-07-29 17:42:16 +02:00
|
|
|
import { SKIP_RENDER_OPTIMIZATIONS } from "../../Enum/EnvironmentVariable";
|
|
|
|
import { coWebsiteManager } from "../../WebRtc/CoWebsiteManager";
|
|
|
|
import { waScaleManager } from "../Services/WaScaleManager";
|
|
|
|
import { ResizableScene } from "../Login/ResizableScene";
|
2021-05-17 13:50:31 +02:00
|
|
|
|
2021-04-23 13:29:23 +02:00
|
|
|
const Events = Phaser.Core.Events;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A specialization of the main Phaser Game scene.
|
|
|
|
* It comes with an optimization to skip rendering.
|
|
|
|
*
|
|
|
|
* Beware, the "step" function might vary in future versions of Phaser.
|
2021-05-18 09:20:38 +02:00
|
|
|
*
|
|
|
|
* It also automatically calls "onResize" on any scenes extending ResizableScene.
|
2021-04-23 13:29:23 +02:00
|
|
|
*/
|
|
|
|
export class Game extends Phaser.Game {
|
2021-05-18 09:20:38 +02:00
|
|
|
private _isDirty = false;
|
|
|
|
|
|
|
|
constructor(GameConfig: Phaser.Types.Core.GameConfig) {
|
|
|
|
super(GameConfig);
|
|
|
|
|
2021-05-31 10:20:30 +02:00
|
|
|
this.scale.on(Phaser.Scale.Events.RESIZE, () => {
|
|
|
|
for (const scene of this.scene.getScenes(true)) {
|
|
|
|
if (scene instanceof ResizableScene) {
|
|
|
|
scene.onResize();
|
|
|
|
}
|
|
|
|
}
|
2021-07-29 17:42:16 +02:00
|
|
|
});
|
2021-05-31 10:20:30 +02:00
|
|
|
|
|
|
|
/*window.addEventListener('resize', (event) => {
|
2021-05-18 09:20:38 +02:00
|
|
|
// Let's trigger the onResize method of any active scene that is a ResizableScene
|
|
|
|
for (const scene of this.scene.getScenes(true)) {
|
|
|
|
if (scene instanceof ResizableScene) {
|
|
|
|
scene.onResize(event);
|
|
|
|
}
|
|
|
|
}
|
2021-05-31 10:20:30 +02:00
|
|
|
});*/
|
2021-05-18 09:20:38 +02:00
|
|
|
}
|
|
|
|
|
2021-07-29 17:42:16 +02:00
|
|
|
public step(time: number, delta: number) {
|
2021-04-23 13:29:23 +02:00
|
|
|
// @ts-ignore
|
2021-07-29 17:42:16 +02:00
|
|
|
if (this.pendingDestroy) {
|
2021-04-23 13:29:23 +02:00
|
|
|
// @ts-ignore
|
|
|
|
return this.runDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
const eventEmitter = this.events;
|
|
|
|
|
|
|
|
// Global Managers like Input and Sound update in the prestep
|
|
|
|
|
|
|
|
eventEmitter.emit(Events.PRE_STEP, time, delta);
|
|
|
|
|
|
|
|
// This is mostly meant for user-land code and plugins
|
|
|
|
|
|
|
|
eventEmitter.emit(Events.STEP, time, delta);
|
|
|
|
|
|
|
|
// Update the Scene Manager and all active Scenes
|
|
|
|
|
|
|
|
this.scene.update(time, delta);
|
|
|
|
|
|
|
|
// Our final event before rendering starts
|
|
|
|
|
|
|
|
eventEmitter.emit(Events.POST_STEP, time, delta);
|
|
|
|
|
|
|
|
// This "if" is the changed introduced by the new "Game" class to avoid rendering unnecessarily.
|
2021-05-17 13:50:31 +02:00
|
|
|
if (SKIP_RENDER_OPTIMIZATIONS || this.isDirty()) {
|
2021-04-23 13:29:23 +02:00
|
|
|
const renderer = this.renderer;
|
|
|
|
|
|
|
|
// Run the Pre-render (clearing the canvas, setting background colors, etc)
|
|
|
|
|
|
|
|
renderer.preRender();
|
|
|
|
|
|
|
|
eventEmitter.emit(Events.PRE_RENDER, renderer, time, delta);
|
|
|
|
|
|
|
|
// The main render loop. Iterates all Scenes and all Cameras in those scenes, rendering to the renderer instance.
|
|
|
|
|
|
|
|
this.scene.render(renderer);
|
|
|
|
|
|
|
|
// The Post-Render call. Tidies up loose end, takes snapshots, etc.
|
|
|
|
|
|
|
|
renderer.postRender();
|
|
|
|
|
|
|
|
// The final event before the step repeats. Your last chance to do anything to the canvas before it all starts again.
|
|
|
|
|
|
|
|
eventEmitter.emit(Events.POST_RENDER, renderer, time, delta);
|
2021-04-28 10:06:40 +02:00
|
|
|
} else {
|
|
|
|
// @ts-ignore
|
|
|
|
this.scene.isProcessing = false;
|
2021-04-23 13:29:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private isDirty(): boolean {
|
2021-05-18 09:20:38 +02:00
|
|
|
if (this._isDirty) {
|
|
|
|
this._isDirty = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-23 13:29:23 +02:00
|
|
|
// Loop through the scenes in forward order
|
2021-07-29 17:42:16 +02:00
|
|
|
for (let i = 0; i < this.scene.scenes.length; i++) {
|
2021-04-23 13:29:23 +02:00
|
|
|
const scene = this.scene.scenes[i];
|
|
|
|
const sys = scene.sys;
|
|
|
|
|
2021-07-29 17:42:16 +02:00
|
|
|
if (
|
|
|
|
sys.settings.visible &&
|
|
|
|
sys.settings.status >= Phaser.Scenes.LOADING &&
|
|
|
|
sys.settings.status < Phaser.Scenes.SLEEPING
|
|
|
|
) {
|
2021-04-23 13:29:23 +02:00
|
|
|
// @ts-ignore
|
2021-07-29 17:42:16 +02:00
|
|
|
if (typeof scene.isDirty === "function") {
|
2021-04-23 13:29:23 +02:00
|
|
|
// @ts-ignore
|
|
|
|
const isDirty = scene.isDirty() || scene.tweens.getAllTweens().length > 0;
|
|
|
|
if (isDirty) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-18 09:20:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks the game as needing to be redrawn.
|
|
|
|
*/
|
|
|
|
public markDirty(): void {
|
|
|
|
this._isDirty = true;
|
|
|
|
}
|
2021-07-29 17:42:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the first scene found in the game
|
|
|
|
*/
|
|
|
|
public findAnyScene(): Phaser.Scene {
|
|
|
|
return this.scene.getScenes()[0];
|
|
|
|
}
|
2021-04-23 13:29:23 +02:00
|
|
|
}
|