-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptNode.ts
More file actions
executable file
·144 lines (93 loc) · 3.19 KB
/
Copy pathScriptNode.ts
File metadata and controls
executable file
·144 lines (93 loc) · 3.19 KB
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
137
138
139
140
141
142
143
144
import Phaser from "phaser";
import ActionTargetComp from "./ActionTargetComp";
export default class ScriptNode {
private _scene: Phaser.Scene;
private _gameObject?: Phaser.GameObjects.GameObject;
private _parent: ScriptNode | Phaser.GameObjects.GameObject | Phaser.Scene;
private _children?: ScriptNode[];
constructor(parent: ScriptNode | Phaser.GameObjects.GameObject | Phaser.Scene) {
this._parent = parent;
if (parent instanceof ScriptNode) {
this._scene = parent.scene;
this._gameObject = parent.gameObject;
parent.add(this);
} else if (parent instanceof Phaser.GameObjects.GameObject) {
this._scene = parent.scene;
this._gameObject = parent;
} else {
this._scene = parent;
}
const listenAwake = this.awake !== ScriptNode.prototype.awake;
const listenStart = this.start !== ScriptNode.prototype.start;
const listenUpdate = this.update !== ScriptNode.prototype.update;
const listenDestroy = this.destroy !== ScriptNode.prototype.destroy;
if (listenAwake) {
this.scene.events.once("scene-awake", this.awake, this);
}
if (listenStart) {
this.scene.events.once(Phaser.Scenes.Events.UPDATE, this.start, this);
}
if (listenUpdate) {
this.scene.events.on(Phaser.Scenes.Events.UPDATE, this.update, this);
}
if (listenAwake || listenStart || listenUpdate || listenDestroy) {
const destroyCallback = () => {
this.scene.events.off("scene-awake", this.awake, this);
this.scene.events.off(Phaser.Scenes.Events.UPDATE, this.start, this);
this.scene.events.off(Phaser.Scenes.Events.UPDATE, this.update, this);
if (listenDestroy) {
this.destroy();
}
};
if (this.gameObject) {
this.gameObject.on(Phaser.GameObjects.Events.DESTROY, destroyCallback);
} else {
this.scene.events.on(Phaser.Scenes.Events.SHUTDOWN, destroyCallback);
}
}
}
protected getActionTargetObject(args: any[]) {
const target = ActionTargetComp.getTargetGameObject(this, args);
return target;
}
get scene() {
return this._scene;
}
get gameObject() {
return this._gameObject;
}
get parent() {
return this._parent;
}
get children(): ScriptNode[] {
if (!this._children) {
this._children = [];
}
return this._children;
}
add(child: ScriptNode) {
this.children.push(child);
}
executeChildren(...args: any[]) {
if (this._children) {
for (const child of this._children) {
child.execute(...args);
}
}
}
execute(...args: any[]): void {
// override this on executable nodes
}
protected awake() {
// override this
}
protected start() {
// override this
}
protected update() {
// override this
}
protected destroy() {
// override this
}
}