-
Notifications
You must be signed in to change notification settings - Fork 0
/
InnerNode.ts
158 lines (145 loc) · 3.95 KB
/
InnerNode.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import type { Engine } from './Engine';
import { TreeNode } from './TreeNode';
/**
* An inner node is a node of the tree that has child nodes.
*
* In most cases those nodes will be used as entity nodes and
* will be composed of component nodes.
* But those can be used for any other purpose (utility, scene
* orchestrating, etc.).
*/
export class InnerNode extends TreeNode {
/**
* Node's children nodes.
*/
protected children: TreeNode[];
/**
* Setup children and calls onCreate method.
*/
constructor(parent: TreeNode | Engine) {
super(parent);
this.children = [];
this.onCreate();
}
/**
* Adds a child node to this node children and loads it.
* @param {TreeNode} node Node to add as child.
* @returns {number} New children array length.
* @sealed
*/
public add(node: TreeNode): number {
this.children.push(node);
// Load the added node if added after loading.
if (this.isLoaded) node.load();
return this.children.length;
}
/**
* Removes a child node from this node children.
* @param {TreeNode} node Node to remove of children.
* @returns {TreeNode} Removed node instance.
* @sealed
*/
public remove(node: TreeNode): TreeNode {
let removedNode;
this.children = this.children.filter((n: TreeNode) => {
// Unloads and filters the given node out.
if (n.id === node.id) {
removedNode = n;
n.unload();
return false;
}
return true;
});
return removedNode;
}
/**
* Called at parent nodes loading or directly by
* the engine's loading if this node is the `rootNode`.
*
* Calls the `onLoad` method and children's `load` methods.
* @sealed
*/
public load(): void {
this.onLoad();
for (let i = 0, len = this.children.length; i !== len; ++i) {
this.children[i].load();
}
this.isLoaded = true;
}
/**
* Called at parent nodes step or directly by
* the engine's step if this node is the `rootNode`.
*
* Calls the `onStep` method and children's `step` methods.
* @sealed
*/
public step(): void {
this.onStep();
for (let i = 0, len = this.children.length; i !== len; ++i) {
this.children[i].step();
}
}
/**
* Called at parent nodes fixedStep or directly by
* the engine's step if this node is the `rootNode`.
*
* Calls the `onFixedStep` and children's `fixedStep` methods.
* @sealed
*/
public fixedStep(): void {
this.onFixedStep();
for (let i = 0, len = this.children.length; i !== len; ++i) {
this.children[i].fixedStep();
}
}
/**
* Called at parent nodes unload or directly by
* the engine's unload if this node is the `rootNode`.
*
* Calls the `onUnload` method and children's `unload` methods.
* @sealed
*/
public unload(): void {
this.onUnload();
for (let i = 0, len = this.children.length; i !== len; ++i) {
this.children[i].unload();
}
this.isLoaded = false;
}
/**
* Called by the node's constructor at instantiation,
* this function is to be implemented when needed.
* @virtual
*/
protected onCreate(): void { }
/**
* Called by the parent node when loaded,
* this function is to be implemented when needed.
*
* If this node is the engine's root node, this
* function will be called by the engine instead.
* @virtual
*/
protected onLoad(): void { }
/**
* Called by the parent node at each step of the loop,
* this function is to be implemented when needed.
* @virtual
*/
protected onStep(): void { }
/**
* Called by the parent node at each fixed step of the loop,
* this function is to be implemented when needed.
* @virtual
*/
protected onFixedStep(): void { }
/**
* Called by the parent node at unload,
* this function is to be implemented when needed.
*
* If this node is the engine's root node, this
* function will be called by the engine instead.
* @virtual
*/
protected onUnload(): void { }
}