-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.ts
136 lines (111 loc) · 3.53 KB
/
scene.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { Body } from 'detect-collisions';
import { addStats, StatsJSAdapter } from 'pixi-stats';
import * as PIXI from 'pixi.js';
import { merge } from 'rxjs/internal/observable/merge';
import { takeUntil } from 'rxjs/internal/operators/takeUntil';
import { Subject } from 'rxjs/internal/Subject';
import { Inject } from '@pietal.dev/dependency-injection';
import { Application } from './application';
import { Resources } from './resources';
import { SceneSSR, SceneOptions } from './scene-ssr';
export class Scene<TBody extends Body = Body> extends SceneSSR<TBody> {
@Inject(Application) pixi: Application;
@Inject(Resources) resources: Resources;
/**
* When disableAutoSort is called, it emits this subject.
*/
readonly disableAutoSort$: Subject<void> = new Subject();
/**
* When disableDebug is called, it emits this subject.
*/
readonly disableDebug$: Subject<void> = new Subject();
constructor(options: SceneOptions = {}) {
super(options);
this.stage.visible = this.options.visible || false;
this.pixi.stage.addChild(this.stage);
if (this.options.autoSort) {
this.enableAutoSort();
}
if (this.options.debug) {
this.enableDebug();
}
// for chrome plugin pixi debug devtools
globalThis.__PIXI_APP__ = this.pixi;
}
static getQueryParams(): Record<string, string> {
const matches = location.search.matchAll(/[?&]([^=?&]+)=?([^?&]*)/g);
return [...matches].reduce(
(queryParams, [_wholeMatch, paramName, paramValue]) => ({
...queryParams,
[paramName]: paramValue
}),
{}
);
}
async init(options?: Partial<PIXI.ApplicationOptions>): Promise<void> {
await this.pixi.init(options);
if (this.pixi.canvas && !this.pixi.canvas.parentElement) {
document.body.appendChild(this.pixi.canvas);
}
const showFPS = this.options.showFPS;
if (showFPS) {
this.showFPS(typeof showFPS === 'string' ? showFPS : undefined);
}
}
start(): void {
this.pixi.start();
super.start();
}
stop(): void {
this.pixi.stop?.();
super.stop();
}
destroy(): void {
super.destroy();
this.pixi.stage.removeChild(this.stage);
}
enableAutoSort(): void {
this.update$
.pipe(takeUntil(merge(this.destroy$, this.disableAutoSort$)))
.subscribe(() => {
this.stage.children.sort(
(bodyA: PIXI.Container, bodyB: PIXI.Container) => bodyA.y - bodyB.y
);
});
}
disableAutoSort(): void {
this.disableAutoSort$.next();
}
enableDebug(): void {
const debug = new PIXI.Graphics();
const canvas = debug as unknown as CanvasRenderingContext2D;
this.pixi.stage.addChild(debug);
this.update$
.pipe(takeUntil(merge(this.destroy$, this.disableDebug$)))
.subscribe(() => {
debug.clear();
this.physics.drawBVH(canvas);
this.physics.draw(canvas);
debug.stroke();
});
}
disableDebug(): void {
this.disableDebug$.next();
this.pixi.stage.children.forEach((child) => {
if (child instanceof PIXI.Graphics) {
this.pixi.stage.removeChild(child);
child.destroy();
}
});
}
/**
* add body font family to set font of pixi-stats
*/
showFPS(style = 'position: fixed; top: 0; right: 0; z-index: 1000;'): void {
const stats: StatsJSAdapter = addStats(document, this.pixi);
const ticker: PIXI.Ticker = PIXI.Ticker.shared;
const canvas = stats.stats.domElement;
canvas.setAttribute('style', style);
ticker.add(stats.update, stats, PIXI.UPDATE_PRIORITY.UTILITY);
}
}