2021-04-23 18:30:41 +02:00
|
|
|
import {ResizableScene} from "../Login/ResizableScene";
|
|
|
|
import GameObject = Phaser.GameObjects.GameObject;
|
|
|
|
import Events = Phaser.Scenes.Events;
|
|
|
|
import AnimationEvents = Phaser.Animations.Events;
|
2021-05-11 16:44:31 +02:00
|
|
|
import StructEvents = Phaser.Structs.Events;
|
2021-05-17 13:50:31 +02:00
|
|
|
import {SKIP_RENDER_OPTIMIZATIONS} from "../../Enum/EnvironmentVariable";
|
2021-04-23 18:30:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A scene that can track its dirty/pristine state.
|
|
|
|
*/
|
|
|
|
export abstract class DirtyScene extends ResizableScene {
|
|
|
|
private isAlreadyTracking: boolean = false;
|
|
|
|
protected dirty:boolean = true;
|
|
|
|
private objectListChanged:boolean = true;
|
2021-05-26 11:57:57 +02:00
|
|
|
private physicsEnabled: boolean = false;
|
2021-04-23 18:30:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Track all objects added to the scene and adds a callback each time an animation is added.
|
|
|
|
* Whenever an object is added, removed, or when an animation is played, the dirty state is set to true.
|
|
|
|
*
|
|
|
|
* Note: this does not work with animations from sprites inside containers.
|
|
|
|
*/
|
|
|
|
protected trackDirtyAnims(): void {
|
2021-05-17 13:50:31 +02:00
|
|
|
if (this.isAlreadyTracking || SKIP_RENDER_OPTIMIZATIONS) {
|
2021-04-23 18:30:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.isAlreadyTracking = true;
|
|
|
|
const trackAnimationFunction = this.trackAnimation.bind(this);
|
2021-05-11 16:44:31 +02:00
|
|
|
this.sys.updateList.on(StructEvents.PROCESS_QUEUE_ADD, (gameObject: GameObject) => {
|
2021-04-23 18:30:41 +02:00
|
|
|
this.objectListChanged = true;
|
|
|
|
gameObject.on(AnimationEvents.ANIMATION_UPDATE, trackAnimationFunction);
|
|
|
|
});
|
2021-05-11 16:44:31 +02:00
|
|
|
this.sys.updateList.on(StructEvents.PROCESS_QUEUE_REMOVE, (gameObject: GameObject) => {
|
2021-04-23 18:30:41 +02:00
|
|
|
this.objectListChanged = true;
|
|
|
|
gameObject.removeListener(AnimationEvents.ANIMATION_UPDATE, trackAnimationFunction);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.events.on(Events.RENDER, () => {
|
|
|
|
this.objectListChanged = false;
|
|
|
|
});
|
2021-05-26 11:57:57 +02:00
|
|
|
|
|
|
|
this.physics.disableUpdate();
|
|
|
|
this.events.on(Events.POST_UPDATE, () => {
|
|
|
|
let objectMoving = false;
|
|
|
|
for (const body of this.physics.world.bodies.entries) {
|
|
|
|
if (body.velocity.x !== 0 || body.velocity.y !== 0) {
|
|
|
|
this.objectListChanged = true;
|
|
|
|
objectMoving = true;
|
|
|
|
if (!this.physicsEnabled) {
|
|
|
|
this.physics.enableUpdate();
|
|
|
|
this.physicsEnabled = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!objectMoving && this.physicsEnabled) {
|
|
|
|
this.physics.disableUpdate();
|
|
|
|
this.physicsEnabled = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-23 18:30:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private trackAnimation(): void {
|
|
|
|
this.objectListChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public isDirty(): boolean {
|
|
|
|
return this.dirty || this.objectListChanged;
|
|
|
|
}
|
|
|
|
|
2021-05-31 10:20:30 +02:00
|
|
|
public onResize(): void {
|
2021-04-23 18:30:41 +02:00
|
|
|
this.objectListChanged = true;
|
|
|
|
}
|
|
|
|
}
|