-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifecycle.ts
97 lines (79 loc) · 2.46 KB
/
lifecycle.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
import * as PIXI from 'pixi.js';
import { Subject } from 'rxjs/internal/Subject';
import { GameObject, GameObjectParent } from './game-object';
import { Scene } from './scene';
import { SceneSSR } from './scene-ssr';
export type LifecycleParent = GameObject | SceneSSR | Scene | PIXI.Container;
export type LifecycleChild = GameObject | PIXI.Container;
export interface LifecycleProps {
/**
* When Lifecycle Object is updated, it emits this subject.
* Along with updating his children, which in turn behave the same.
*/
readonly update$: Subject<number>;
/**
* When Lifecycle Object is destroyed, it emits and closes this subject.
* Along with destroying his children, which in turn behave the same.
*/
readonly destroy$: Subject<void>;
/**
* Parent GameObject is assigned at creation.
* BaseScene/Scene has no gameObject
*/
gameObject?: LifecycleParent;
/**
* Each Lifecycle Object has label for pixi debugging.
*/
label: string;
/**
* Updates the Lifecycle with actual deltaTime = 1.0 for 60FPS
*/
update(deltaTime: number): void;
/**
* Called to destroy can cleanup Lifecycle Object.
*/
destroy(): void;
}
export abstract class Lifecycle implements LifecycleProps {
/**
* When Lifecycle Object is updated, it emits this subject.
* Along with updating his children, which in turn behave the same.
*/
readonly update$: Subject<number> = new Subject();
/**
* When Lifecycle Object is destroyed, it emits and closes this subject.
* Along with destroying his children, which in turn behave the same.
*/
readonly destroy$: Subject<void> = new Subject();
/**
* Parent GameObject is assigned at creation.
*/
gameObject?: GameObjectParent;
/**
* Lifecycles can have children Lifecycles
*/
children: LifecycleProps[] = [];
/**
* Each Lifecycle Object has label for pixi debugging.
*/
label = 'Lifecycle';
static destroy(lifecycle: LifecycleProps): void {
// tslint:disable-next-line: no-any
lifecycle.gameObject?.removeChild?.(lifecycle as any);
lifecycle.update$.complete();
lifecycle.destroy$.next();
lifecycle.destroy$.complete();
}
static update(lifecycle: LifecycleProps, deltaTime: number): void {
lifecycle.update$.next(deltaTime);
}
destroy(): void {
Lifecycle.destroy(this);
}
/**
* Updates the Lifecycle with actual deltaTime = 1.0 for 60FPS
*/
update(deltaTime: number): void {
Lifecycle.update(this, deltaTime);
}
}