@needle-tools/engine 4.3.0-alpha → 4.3.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -0
- package/dist/needle-engine.bundle.js +1467 -222
- package/dist/needle-engine.bundle.light.js +1467 -222
- package/dist/needle-engine.bundle.light.min.js +32 -32
- package/dist/needle-engine.bundle.light.umd.cjs +3 -3
- package/dist/needle-engine.bundle.min.js +3 -3
- package/dist/needle-engine.bundle.umd.cjs +3 -3
- package/dist/needle-engine.light.d.ts +9 -9
- package/lib/engine/engine_types.d.ts +162 -17
- package/lib/engine-components/Animator.d.ts +129 -21
- package/lib/engine-components/Animator.js +115 -21
- package/lib/engine-components/Animator.js.map +1 -1
- package/lib/engine-components/AnimatorController.d.ts +161 -32
- package/lib/engine-components/AnimatorController.js +176 -29
- package/lib/engine-components/AnimatorController.js.map +1 -1
- package/lib/engine-components/AudioListener.d.ts +16 -5
- package/lib/engine-components/AudioListener.js +16 -5
- package/lib/engine-components/AudioListener.js.map +1 -1
- package/lib/engine-components/AudioSource.d.ts +120 -28
- package/lib/engine-components/AudioSource.js +120 -37
- package/lib/engine-components/AudioSource.js.map +1 -1
- package/lib/engine-components/AvatarLoader.d.ts +61 -0
- package/lib/engine-components/AvatarLoader.js +61 -1
- package/lib/engine-components/AvatarLoader.js.map +1 -1
- package/lib/engine-components/AxesHelper.d.ts +19 -1
- package/lib/engine-components/AxesHelper.js +19 -1
- package/lib/engine-components/AxesHelper.js.map +1 -1
- package/lib/engine-components/BoxHelperComponent.d.ts +26 -0
- package/lib/engine-components/BoxHelperComponent.js +26 -0
- package/lib/engine-components/BoxHelperComponent.js.map +1 -1
- package/lib/engine-components/Camera.d.ts +126 -37
- package/lib/engine-components/Camera.js +139 -37
- package/lib/engine-components/Camera.js.map +1 -1
- package/lib/engine-components/CameraUtils.js +20 -0
- package/lib/engine-components/CameraUtils.js.map +1 -1
- package/lib/engine-components/Collider.d.ts +95 -21
- package/lib/engine-components/Collider.js +100 -23
- package/lib/engine-components/Collider.js.map +1 -1
- package/lib/engine-components/Component.d.ts +554 -106
- package/lib/engine-components/Component.js +352 -81
- package/lib/engine-components/Component.js.map +1 -1
- package/lib/engine-components/DragControls.d.ts +95 -21
- package/lib/engine-components/DragControls.js +126 -32
- package/lib/engine-components/DragControls.js.map +1 -1
- package/lib/engine-components/DropListener.d.ts +99 -16
- package/lib/engine-components/DropListener.js +119 -14
- package/lib/engine-components/DropListener.js.map +1 -1
- package/lib/engine-components/Light.d.ts +102 -5
- package/lib/engine-components/Light.js +102 -44
- package/lib/engine-components/Light.js.map +1 -1
- package/lib/engine-components/NeedleMenu.d.ts +28 -11
- package/lib/engine-components/NeedleMenu.js +28 -11
- package/lib/engine-components/NeedleMenu.js.map +1 -1
- package/lib/engine-components/Networking.d.ts +37 -5
- package/lib/engine-components/Networking.js +37 -5
- package/lib/engine-components/Networking.js.map +1 -1
- package/lib/engine-components/SceneSwitcher.js +44 -0
- package/lib/engine-components/SceneSwitcher.js.map +1 -1
- package/lib/engine-components/SpatialTrigger.d.ts +66 -1
- package/lib/engine-components/SpatialTrigger.js +74 -2
- package/lib/engine-components/SpatialTrigger.js.map +1 -1
- package/lib/engine-components/SpectatorCamera.d.ts +66 -4
- package/lib/engine-components/SpectatorCamera.js +132 -6
- package/lib/engine-components/SpectatorCamera.js.map +1 -1
- package/lib/engine-components/SyncedTransform.d.ts +45 -6
- package/lib/engine-components/SyncedTransform.js +45 -6
- package/lib/engine-components/SyncedTransform.js.map +1 -1
- package/lib/engine-components/TransformGizmo.d.ts +49 -3
- package/lib/engine-components/TransformGizmo.js +49 -3
- package/lib/engine-components/TransformGizmo.js.map +1 -1
- package/lib/engine-components/webxr/WebXR.d.ts +131 -22
- package/lib/engine-components/webxr/WebXR.js +132 -23
- package/lib/engine-components/webxr/WebXR.js.map +1 -1
- package/lib/engine-components-experimental/networking/PlayerSync.d.ts +82 -9
- package/lib/engine-components-experimental/networking/PlayerSync.js +76 -11
- package/lib/engine-components-experimental/networking/PlayerSync.js.map +1 -1
- package/package.json +1 -1
- package/src/engine/engine_types.ts +179 -18
- package/src/engine-components/Animator.ts +142 -22
- package/src/engine-components/AnimatorController.ts +184 -34
- package/src/engine-components/AudioListener.ts +16 -5
- package/src/engine-components/AudioSource.ts +126 -37
- package/src/engine-components/AvatarLoader.ts +61 -2
- package/src/engine-components/AxesHelper.ts +21 -1
- package/src/engine-components/BoxHelperComponent.ts +26 -0
- package/src/engine-components/Camera.ts +147 -41
- package/src/engine-components/CameraUtils.ts +20 -0
- package/src/engine-components/Collider.ts +102 -27
- package/src/engine-components/Component.ts +605 -129
- package/src/engine-components/DragControls.ts +134 -38
- package/src/engine-components/DropListener.ts +143 -23
- package/src/engine-components/Light.ts +105 -44
- package/src/engine-components/NeedleMenu.ts +29 -11
- package/src/engine-components/Networking.ts +37 -6
- package/src/engine-components/SceneSwitcher.ts +48 -1
- package/src/engine-components/SpatialTrigger.ts +80 -3
- package/src/engine-components/SpectatorCamera.ts +136 -18
- package/src/engine-components/SyncedTransform.ts +50 -7
- package/src/engine-components/TransformGizmo.ts +49 -4
- package/src/engine-components/webxr/WebXR.ts +144 -27
- package/src/engine-components-experimental/networking/PlayerSync.ts +85 -13
|
@@ -23,57 +23,167 @@ import { type IPointerEventHandler, PointerEventData } from "./ui/PointerEvents.
|
|
|
23
23
|
// }
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
26
|
+
* Base class for objects in Needle Engine. Extends {@link Object3D} from three.js.
|
|
27
|
+
* GameObjects can have components attached to them, which can be used to add functionality to the object.
|
|
28
|
+
* They manage their components and provide methods to add, remove and get components.
|
|
29
|
+
*
|
|
30
|
+
* All {@link Object3D} types loaded in Needle Engine have methods like {@link addComponent}.
|
|
31
|
+
* These methods are available directly on the GameObject instance:
|
|
32
|
+
* ```typescript
|
|
33
|
+
* target.addComponent(MyComponent);
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* And can be called statically on the GameObject class as well:
|
|
28
37
|
* ```typescript
|
|
29
|
-
*
|
|
38
|
+
* GameObject.setActive(target, true);
|
|
30
39
|
* ```
|
|
31
40
|
*/
|
|
32
41
|
export abstract class GameObject extends Object3D implements Object3D, IGameObject {
|
|
33
42
|
|
|
34
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Indicates if the GameObject is currently active. Inactive GameObjects will not be rendered or updated.
|
|
45
|
+
* When the activeSelf state changes, components will receive {@link Component.onEnable} or {@link Component.onDisable} callbacks.
|
|
46
|
+
*/
|
|
35
47
|
abstract activeSelf: boolean;
|
|
36
48
|
|
|
37
|
-
/** @deprecated
|
|
49
|
+
/** @deprecated Use {@link addComponent} instead */
|
|
38
50
|
// eslint-disable-next-line deprecation/deprecation
|
|
39
51
|
abstract addNewComponent<T extends IComponent>(type: ConstructorConcrete<T>, init?: ComponentInit<T>): T;
|
|
40
|
-
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Creates a new component on this gameObject or adds an existing component instance
|
|
55
|
+
* @param comp Component type constructor or existing component instance
|
|
56
|
+
* @param init Optional initialization values for the component
|
|
57
|
+
* @returns The newly created or added component
|
|
58
|
+
*/
|
|
41
59
|
abstract addComponent<T extends IComponent>(comp: T | ConstructorConcrete<T>, init?: ComponentInit<T>): T;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Removes a component from this GameObject
|
|
63
|
+
* @param comp Component instance to remove
|
|
64
|
+
* @returns The removed component
|
|
65
|
+
*/
|
|
42
66
|
abstract removeComponent<T extends IComponent>(comp: T): T;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Gets an existing component of the specified type or adds a new one if it doesn't exist
|
|
70
|
+
* @param typeName Constructor of the component type to get or add
|
|
71
|
+
* @returns The existing or newly added component
|
|
72
|
+
*/
|
|
43
73
|
abstract getOrAddComponent<T>(typeName: ConstructorConcrete<T> | null): T;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Gets a component of the specified type attached to this GameObject
|
|
77
|
+
* @param type Constructor of the component type to get
|
|
78
|
+
* @returns The component if found, otherwise null
|
|
79
|
+
*/
|
|
44
80
|
abstract getComponent<T>(type: Constructor<T>): T | null;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Gets all components of the specified type attached to this GameObject
|
|
84
|
+
* @param type Constructor of the component type to get
|
|
85
|
+
* @param arr Optional array to populate with the components
|
|
86
|
+
* @returns Array of components
|
|
87
|
+
*/
|
|
45
88
|
abstract getComponents<T>(type: Constructor<T>, arr?: T[]): Array<T>;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Gets a component of the specified type in this GameObject's children hierarchy
|
|
92
|
+
* @param type Constructor of the component type to get
|
|
93
|
+
* @returns The first matching component if found, otherwise null
|
|
94
|
+
*/
|
|
46
95
|
abstract getComponentInChildren<T>(type: Constructor<T>): T | null;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Gets all components of the specified type in this GameObject's children hierarchy
|
|
99
|
+
* @param type Constructor of the component type to get
|
|
100
|
+
* @param arr Optional array to populate with the components
|
|
101
|
+
* @returns Array of components
|
|
102
|
+
*/
|
|
47
103
|
abstract getComponentsInChildren<T>(type: Constructor<T>, arr?: T[]): Array<T>;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Gets a component of the specified type in this GameObject's parent hierarchy
|
|
107
|
+
* @param type Constructor of the component type to get
|
|
108
|
+
* @returns The first matching component if found, otherwise null
|
|
109
|
+
*/
|
|
48
110
|
abstract getComponentInParent<T>(type: Constructor<T>): T | null;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Gets all components of the specified type in this GameObject's parent hierarchy
|
|
114
|
+
* @param type Constructor of the component type to get
|
|
115
|
+
* @param arr Optional array to populate with the components
|
|
116
|
+
* @returns Array of components
|
|
117
|
+
*/
|
|
49
118
|
abstract getComponentsInParent<T>(type: Constructor<T>, arr?: T[]): Array<T>;
|
|
50
119
|
|
|
51
120
|
|
|
121
|
+
/**
|
|
122
|
+
* The position of this GameObject in world space
|
|
123
|
+
*/
|
|
52
124
|
abstract get worldPosition(): Vector3
|
|
53
125
|
abstract set worldPosition(val: Vector3);
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The rotation of this GameObject in world space as a quaternion
|
|
129
|
+
*/
|
|
54
130
|
abstract set worldQuaternion(val: Quaternion);
|
|
55
131
|
abstract get worldQuaternion(): Quaternion;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* The rotation of this GameObject in world space in euler angles (degrees)
|
|
135
|
+
*/
|
|
56
136
|
abstract set worldRotation(val: Vector3);
|
|
57
137
|
abstract get worldRotation(): Vector3;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The scale of this GameObject in world space
|
|
141
|
+
*/
|
|
58
142
|
abstract set worldScale(val: Vector3);
|
|
59
143
|
abstract get worldScale(): Vector3;
|
|
60
144
|
|
|
145
|
+
/**
|
|
146
|
+
* The forward direction vector of this GameObject in world space
|
|
147
|
+
*/
|
|
61
148
|
abstract get worldForward(): Vector3;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The right direction vector of this GameObject in world space
|
|
152
|
+
*/
|
|
62
153
|
abstract get worldRight(): Vector3;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The up direction vector of this GameObject in world space
|
|
157
|
+
*/
|
|
63
158
|
abstract get worldUp(): Vector3;
|
|
64
159
|
|
|
160
|
+
/**
|
|
161
|
+
* Unique identifier for this GameObject
|
|
162
|
+
*/
|
|
65
163
|
guid: string | undefined;
|
|
66
164
|
|
|
67
|
-
|
|
165
|
+
/**
|
|
166
|
+
* Destroys this GameObject and all its components.
|
|
167
|
+
* Internally, this is added to the three.js {@link Object3D} prototype.
|
|
168
|
+
*/
|
|
68
169
|
abstract destroy();
|
|
69
170
|
|
|
70
171
|
|
|
71
|
-
|
|
72
|
-
|
|
172
|
+
/**
|
|
173
|
+
* Checks if a GameObject has been destroyed
|
|
174
|
+
* @param go The GameObject to check
|
|
175
|
+
* @returns True if the GameObject has been destroyed
|
|
176
|
+
*/
|
|
73
177
|
public static isDestroyed(go: Object3D): boolean {
|
|
74
178
|
return isDestroyed(go);
|
|
75
179
|
}
|
|
76
180
|
|
|
181
|
+
/**
|
|
182
|
+
* Sets the active state of a GameObject
|
|
183
|
+
* @param go The GameObject to modify
|
|
184
|
+
* @param active Whether the GameObject should be active
|
|
185
|
+
* @param processStart Whether to process the start callbacks if being activated
|
|
186
|
+
*/
|
|
77
187
|
public static setActive(go: Object3D, active: boolean, processStart: boolean = true) {
|
|
78
188
|
if (!go) return;
|
|
79
189
|
setActive(go, active);
|
|
@@ -84,47 +194,68 @@ export abstract class GameObject extends Object3D implements Object3D, IGameObje
|
|
|
84
194
|
main.processStart(Context.Current, go);
|
|
85
195
|
}
|
|
86
196
|
|
|
87
|
-
/**
|
|
197
|
+
/**
|
|
198
|
+
* Checks if the GameObject itself is active (same as go.visible)
|
|
199
|
+
* @param go The GameObject to check
|
|
200
|
+
* @returns True if the GameObject is active
|
|
201
|
+
*/
|
|
88
202
|
public static isActiveSelf(go: Object3D): boolean {
|
|
89
203
|
return isActiveSelf(go);
|
|
90
204
|
}
|
|
91
205
|
|
|
92
|
-
/**
|
|
93
|
-
*
|
|
94
|
-
|
|
206
|
+
/**
|
|
207
|
+
* Checks if the GameObject is active in the hierarchy (e.g. if any parent is invisible or not in the scene it will be false)
|
|
208
|
+
* @param go The GameObject to check
|
|
209
|
+
* @returns True if the GameObject is active in the hierarchy
|
|
210
|
+
*/
|
|
95
211
|
public static isActiveInHierarchy(go: Object3D): boolean {
|
|
96
212
|
return isActiveInHierarchy(go);
|
|
97
213
|
}
|
|
98
214
|
|
|
215
|
+
/**
|
|
216
|
+
* Marks a GameObject to be rendered using instancing
|
|
217
|
+
* @param go The GameObject to mark
|
|
218
|
+
* @param instanced Whether the GameObject should use instanced rendering
|
|
219
|
+
*/
|
|
99
220
|
public static markAsInstancedRendered(go: Object3D, instanced: boolean) {
|
|
100
221
|
markAsInstancedRendered(go, instanced);
|
|
101
222
|
}
|
|
102
223
|
|
|
224
|
+
/**
|
|
225
|
+
* Checks if a GameObject is using instanced rendering
|
|
226
|
+
* @param instance The GameObject to check
|
|
227
|
+
* @returns True if the GameObject is using instanced rendering
|
|
228
|
+
*/
|
|
103
229
|
public static isUsingInstancing(instance: Object3D): boolean { return isUsingInstancing(instance); }
|
|
104
230
|
|
|
105
|
-
/**
|
|
106
|
-
*
|
|
107
|
-
* @param
|
|
108
|
-
* @param
|
|
109
|
-
* @
|
|
231
|
+
/**
|
|
232
|
+
* Executes a callback for all components of the provided type on the provided object and its children
|
|
233
|
+
* @param instance Object to run the method on
|
|
234
|
+
* @param cb Callback to run on each component, "return undefined;" to continue and "return <anything>;" to break the loop
|
|
235
|
+
* @param recursive If true, the method will be run on all children as well
|
|
236
|
+
* @returns The last return value of the callback
|
|
110
237
|
*/
|
|
111
238
|
public static foreachComponent(instance: Object3D, cb: (comp: Component) => any, recursive: boolean = true): any {
|
|
112
239
|
return foreachComponent(instance, cb as (comp: IComponent) => any, recursive);
|
|
113
240
|
}
|
|
114
241
|
|
|
115
|
-
/**
|
|
116
|
-
*
|
|
117
|
-
* @param
|
|
242
|
+
/**
|
|
243
|
+
* Creates a new instance of the provided object that will be replicated to all connected clients
|
|
244
|
+
* @param instance Object to instantiate
|
|
245
|
+
* @param opts Options for the instantiation
|
|
246
|
+
* @returns The newly created instance or null if creation failed
|
|
118
247
|
*/
|
|
119
248
|
public static instantiateSynced(instance: GameObject | Object3D | null, opts: SyncInstantiateOptions): GameObject | null {
|
|
120
249
|
if (!instance) return null;
|
|
121
250
|
return syncInstantiate(instance, opts) as GameObject | null;
|
|
122
251
|
}
|
|
123
252
|
|
|
124
|
-
/**
|
|
125
|
-
*
|
|
126
|
-
* @param
|
|
127
|
-
|
|
253
|
+
/**
|
|
254
|
+
* Creates a new instance of the provided object (like cloning it including all components and children)
|
|
255
|
+
* @param instance Object to instantiate
|
|
256
|
+
* @param opts Options for the instantiation (e.g. with what parent, position, etc.)
|
|
257
|
+
* @returns The newly created instance
|
|
258
|
+
*/
|
|
128
259
|
public static instantiate(instance: AssetReference, opts?: IInstantiateOptions | null | undefined): Promise<Object3D | null>
|
|
129
260
|
public static instantiate(instance: GameObject | Object3D, opts?: IInstantiateOptions | null | undefined): GameObject
|
|
130
261
|
public static instantiate(instance: AssetReference | GameObject | Object3D, opts: IInstantiateOptions | null | undefined = null): GameObject | Promise<Object3D | null> {
|
|
@@ -134,9 +265,12 @@ export abstract class GameObject extends Object3D implements Object3D, IGameObje
|
|
|
134
265
|
return instantiate(instance as GameObject | Object3D, opts) as GameObject;
|
|
135
266
|
}
|
|
136
267
|
|
|
137
|
-
/**
|
|
138
|
-
*
|
|
139
|
-
|
|
268
|
+
/**
|
|
269
|
+
* Destroys an object on all connected clients (if in a networked session)
|
|
270
|
+
* @param instance Object to destroy
|
|
271
|
+
* @param context Optional context to use
|
|
272
|
+
* @param recursive If true, all children will be destroyed as well
|
|
273
|
+
*/
|
|
140
274
|
public static destroySynced(instance: Object3D | Component, context?: Context, recursive: boolean = true) {
|
|
141
275
|
if (!instance) return;
|
|
142
276
|
const go = instance as GameObject;
|
|
@@ -144,16 +278,20 @@ export abstract class GameObject extends Object3D implements Object3D, IGameObje
|
|
|
144
278
|
syncDestroy(go as any, context.connection, recursive);
|
|
145
279
|
}
|
|
146
280
|
|
|
147
|
-
/**
|
|
148
|
-
*
|
|
149
|
-
* @param
|
|
281
|
+
/**
|
|
282
|
+
* Destroys an object
|
|
283
|
+
* @param instance Object to destroy
|
|
284
|
+
* @param recursive If true, all children will be destroyed as well. Default: true
|
|
150
285
|
*/
|
|
151
286
|
public static destroy(instance: Object3D | Component, recursive: boolean = true) {
|
|
152
287
|
return destroy(instance, recursive);
|
|
153
288
|
}
|
|
154
289
|
|
|
155
290
|
/**
|
|
156
|
-
*
|
|
291
|
+
* Adds an object to parent and ensures all components are properly registered
|
|
292
|
+
* @param instance Object to add
|
|
293
|
+
* @param parent Parent to add the object to
|
|
294
|
+
* @param context Optional context to use
|
|
157
295
|
*/
|
|
158
296
|
public static add(instance: Object3D | null | undefined, parent: Object3D, context?: Context) {
|
|
159
297
|
if (!instance || !parent) return;
|
|
@@ -183,6 +321,7 @@ export abstract class GameObject extends Object3D implements Object3D, IGameObje
|
|
|
183
321
|
|
|
184
322
|
/**
|
|
185
323
|
* Removes the object from its parent and deactivates all of its components
|
|
324
|
+
* @param instance Object to remove
|
|
186
325
|
*/
|
|
187
326
|
public static remove(instance: Object3D | null | undefined) {
|
|
188
327
|
if (!instance) return;
|
|
@@ -194,14 +333,22 @@ export abstract class GameObject extends Object3D implements Object3D, IGameObje
|
|
|
194
333
|
}, true);
|
|
195
334
|
}
|
|
196
335
|
|
|
197
|
-
/**
|
|
336
|
+
/**
|
|
337
|
+
* Invokes a method on all components including children (if a method with that name exists)
|
|
338
|
+
* @param go GameObject to invoke the method on
|
|
339
|
+
* @param functionName Name of the method to invoke
|
|
340
|
+
* @param args Arguments to pass to the method
|
|
341
|
+
*/
|
|
198
342
|
public static invokeOnChildren(go: Object3D | null | undefined, functionName: string, ...args: any) {
|
|
199
343
|
this.invoke(go, functionName, true, args);
|
|
200
344
|
}
|
|
201
345
|
|
|
202
|
-
/**
|
|
203
|
-
*
|
|
204
|
-
* @param
|
|
346
|
+
/**
|
|
347
|
+
* Invokes a method on all components that have a method matching the provided name
|
|
348
|
+
* @param go GameObject to invoke the method on
|
|
349
|
+
* @param functionName Name of the method to invoke
|
|
350
|
+
* @param children Whether to invoke on children as well
|
|
351
|
+
* @param args Arguments to pass to the method
|
|
205
352
|
*/
|
|
206
353
|
public static invoke(go: Object3D | null | undefined, functionName: string, children: boolean = false, ...args: any) {
|
|
207
354
|
if (!go) return;
|
|
@@ -220,38 +367,53 @@ export abstract class GameObject extends Object3D implements Object3D, IGameObje
|
|
|
220
367
|
}
|
|
221
368
|
|
|
222
369
|
/**
|
|
223
|
-
*
|
|
224
|
-
* @param go
|
|
225
|
-
* @param instanceOrType
|
|
226
|
-
* @param init
|
|
227
|
-
* @param
|
|
370
|
+
* Adds a new component (or moves an existing component) to the provided object
|
|
371
|
+
* @param go Object to add the component to
|
|
372
|
+
* @param instanceOrType If an instance is provided it will be moved to the new object, if a type is provided a new instance will be created
|
|
373
|
+
* @param init Optional init object to initialize the component with
|
|
374
|
+
* @param opts Optional options for adding the component
|
|
375
|
+
* @returns The added or moved component
|
|
228
376
|
*/
|
|
229
377
|
public static addComponent<T extends IComponent>(go: IGameObject | Object3D, instanceOrType: T | ConstructorConcrete<T>, init?: ComponentInit<T>, opts?: { callAwake: boolean }): T {
|
|
230
378
|
return addComponent(go, instanceOrType, init, opts);
|
|
231
379
|
}
|
|
232
380
|
|
|
233
381
|
/**
|
|
234
|
-
* Moves a component to a new object
|
|
235
|
-
* @param go
|
|
236
|
-
* @param instance
|
|
382
|
+
* Moves a component to a new object
|
|
383
|
+
* @param go GameObject to move the component to
|
|
384
|
+
* @param instance Component to move
|
|
385
|
+
* @returns The moved component
|
|
237
386
|
*/
|
|
238
387
|
public static moveComponent<T extends IComponent>(go: IGameObject | Object3D, instance: T | ConstructorConcrete<T>): T {
|
|
239
388
|
return addComponent(go, instance);
|
|
240
389
|
}
|
|
241
390
|
|
|
242
|
-
/**
|
|
243
|
-
*
|
|
391
|
+
/**
|
|
392
|
+
* Removes a component from its object
|
|
393
|
+
* @param instance Component to remove
|
|
394
|
+
* @returns The removed component
|
|
244
395
|
*/
|
|
245
396
|
public static removeComponent<T extends IComponent>(instance: T): T {
|
|
246
397
|
removeComponent(instance.gameObject, instance as any);
|
|
247
398
|
return instance;
|
|
248
399
|
}
|
|
249
400
|
|
|
401
|
+
/**
|
|
402
|
+
* Gets or adds a component of the specified type
|
|
403
|
+
* @param go GameObject to get or add the component to
|
|
404
|
+
* @param typeName Constructor of the component type
|
|
405
|
+
* @returns The existing or newly added component
|
|
406
|
+
*/
|
|
250
407
|
public static getOrAddComponent<T extends IComponent>(go: IGameObject | Object3D, typeName: ConstructorConcrete<T>): T {
|
|
251
408
|
return getOrAddComponent<any>(go, typeName);
|
|
252
409
|
}
|
|
253
410
|
|
|
254
|
-
/**
|
|
411
|
+
/**
|
|
412
|
+
* Gets a component on the provided object
|
|
413
|
+
* @param go GameObject to get the component from
|
|
414
|
+
* @param typeName Constructor of the component type
|
|
415
|
+
* @returns The component if found, otherwise null
|
|
416
|
+
*/
|
|
255
417
|
public static getComponent<T extends IComponent>(go: IGameObject | Object3D | null, typeName: Constructor<T> | null): T | null {
|
|
256
418
|
if (go === null) return null;
|
|
257
419
|
// if names are minified we could also use the type store and work with strings everywhere
|
|
@@ -261,42 +423,99 @@ export abstract class GameObject extends Object3D implements Object3D, IGameObje
|
|
|
261
423
|
return getComponent(go, typeName as any);
|
|
262
424
|
}
|
|
263
425
|
|
|
426
|
+
/**
|
|
427
|
+
* Gets all components of the specified type on the provided object
|
|
428
|
+
* @param go GameObject to get the components from
|
|
429
|
+
* @param typeName Constructor of the component type
|
|
430
|
+
* @param arr Optional array to populate with the components
|
|
431
|
+
* @returns Array of components
|
|
432
|
+
*/
|
|
264
433
|
public static getComponents<T extends IComponent>(go: IGameObject | Object3D | null, typeName: Constructor<T>, arr: T[] | null = null): T[] {
|
|
265
434
|
if (go === null) return arr ?? [];
|
|
266
435
|
return getComponents(go, typeName, arr);
|
|
267
436
|
}
|
|
268
437
|
|
|
438
|
+
/**
|
|
439
|
+
* Finds an object or component by its unique identifier
|
|
440
|
+
* @param guid Unique identifier to search for
|
|
441
|
+
* @param hierarchy Root object to search in
|
|
442
|
+
* @returns The found GameObject or Component, or null/undefined if not found
|
|
443
|
+
*/
|
|
269
444
|
public static findByGuid(guid: string, hierarchy: Object3D): GameObject | Component | null | undefined {
|
|
270
445
|
const res = findByGuid(guid, hierarchy);
|
|
271
446
|
return res as GameObject | Component | null | undefined;
|
|
272
447
|
}
|
|
273
448
|
|
|
449
|
+
/**
|
|
450
|
+
* Finds the first object of the specified component type in the scene
|
|
451
|
+
* @param typeName Constructor of the component type
|
|
452
|
+
* @param context Context or root object to search in
|
|
453
|
+
* @param includeInactive Whether to include inactive objects in the search
|
|
454
|
+
* @returns The first matching component if found, otherwise null
|
|
455
|
+
*/
|
|
274
456
|
public static findObjectOfType<T extends IComponent>(typeName: Constructor<T>, context?: Context | Object3D, includeInactive: boolean = true): T | null {
|
|
275
457
|
return findObjectOfType(typeName, context ?? Context.Current, includeInactive);
|
|
276
458
|
}
|
|
277
459
|
|
|
460
|
+
/**
|
|
461
|
+
* Finds all objects of the specified component type in the scene
|
|
462
|
+
* @param typeName Constructor of the component type
|
|
463
|
+
* @param context Context or root object to search in
|
|
464
|
+
* @returns Array of matching components
|
|
465
|
+
*/
|
|
278
466
|
public static findObjectsOfType<T extends IComponent>(typeName: Constructor<T>, context?: Context | Object3D): Array<T> {
|
|
279
467
|
const arr = [];
|
|
280
468
|
findObjectsOfType(typeName, arr, context);
|
|
281
469
|
return arr;
|
|
282
470
|
}
|
|
283
471
|
|
|
472
|
+
/**
|
|
473
|
+
* Gets a component of the specified type in the gameObject's children hierarchy
|
|
474
|
+
* @param go GameObject to search in
|
|
475
|
+
* @param typeName Constructor of the component type
|
|
476
|
+
* @returns The first matching component if found, otherwise null
|
|
477
|
+
*/
|
|
284
478
|
public static getComponentInChildren<T extends IComponent>(go: IGameObject | Object3D, typeName: Constructor<T>): T | null {
|
|
285
479
|
return getComponentInChildren(go, typeName);
|
|
286
480
|
}
|
|
287
481
|
|
|
482
|
+
/**
|
|
483
|
+
* Gets all components of the specified type in the gameObject's children hierarchy
|
|
484
|
+
* @param go GameObject to search in
|
|
485
|
+
* @param typeName Constructor of the component type
|
|
486
|
+
* @param arr Optional array to populate with the components
|
|
487
|
+
* @returns Array of components
|
|
488
|
+
*/
|
|
288
489
|
public static getComponentsInChildren<T extends IComponent>(go: IGameObject | Object3D, typeName: Constructor<T>, arr: T[] | null = null): Array<T> {
|
|
289
490
|
return getComponentsInChildren<T>(go, typeName, arr ?? undefined) as T[]
|
|
290
491
|
}
|
|
291
492
|
|
|
493
|
+
/**
|
|
494
|
+
* Gets a component of the specified type in the gameObject's parent hierarchy
|
|
495
|
+
* @param go GameObject to search in
|
|
496
|
+
* @param typeName Constructor of the component type
|
|
497
|
+
* @returns The first matching component if found, otherwise null
|
|
498
|
+
*/
|
|
292
499
|
public static getComponentInParent<T extends IComponent>(go: IGameObject | Object3D, typeName: Constructor<T>): T | null {
|
|
293
500
|
return getComponentInParent(go, typeName);
|
|
294
501
|
}
|
|
295
502
|
|
|
503
|
+
/**
|
|
504
|
+
* Gets all components of the specified type in the gameObject's parent hierarchy
|
|
505
|
+
* @param go GameObject to search in
|
|
506
|
+
* @param typeName Constructor of the component type
|
|
507
|
+
* @param arr Optional array to populate with the components
|
|
508
|
+
* @returns Array of components
|
|
509
|
+
*/
|
|
296
510
|
public static getComponentsInParent<T extends IComponent>(go: IGameObject | Object3D, typeName: Constructor<T>, arr: Array<T> | null = null): Array<T> {
|
|
297
511
|
return getComponentsInParent(go, typeName, arr);
|
|
298
512
|
}
|
|
299
513
|
|
|
514
|
+
/**
|
|
515
|
+
* Gets all components on the gameObject
|
|
516
|
+
* @param go GameObject to get components from
|
|
517
|
+
* @returns Array of all components
|
|
518
|
+
*/
|
|
300
519
|
public static getAllComponents(go: IGameObject | Object3D): Component[] {
|
|
301
520
|
const componentsList = go.userData?.components;
|
|
302
521
|
if (!componentsList) return [];
|
|
@@ -304,6 +523,11 @@ export abstract class GameObject extends Object3D implements Object3D, IGameObje
|
|
|
304
523
|
return newList;
|
|
305
524
|
}
|
|
306
525
|
|
|
526
|
+
/**
|
|
527
|
+
* Iterates through all components on the gameObject
|
|
528
|
+
* @param go GameObject to iterate components on
|
|
529
|
+
* @returns Generator yielding each component
|
|
530
|
+
*/
|
|
307
531
|
public static *iterateComponents(go: IGameObject | Object3D) {
|
|
308
532
|
const list = go?.userData?.components;
|
|
309
533
|
if (list && Array.isArray(list)) {
|
|
@@ -346,27 +570,43 @@ export abstract class Component implements IComponent, EventTarget,
|
|
|
346
570
|
Partial<INeedleXRSessionEventReceiver>,
|
|
347
571
|
Partial<IPointerEventHandler>
|
|
348
572
|
{
|
|
349
|
-
/**
|
|
573
|
+
/**
|
|
574
|
+
* Indicates whether this object is a component
|
|
575
|
+
* @internal
|
|
576
|
+
*/
|
|
350
577
|
get isComponent(): boolean { return true; }
|
|
351
578
|
|
|
352
579
|
private __context: Context | undefined;
|
|
353
|
-
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* The context this component belongs to, providing access to the runtime environment
|
|
583
|
+
* including physics, timing utilities, camera, and scene
|
|
584
|
+
*/
|
|
354
585
|
get context(): Context {
|
|
355
586
|
return this.__context ?? Context.Current;
|
|
356
587
|
}
|
|
357
588
|
set context(context: Context) {
|
|
358
589
|
this.__context = context;
|
|
359
590
|
}
|
|
360
|
-
|
|
361
|
-
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* Shorthand accessor for the current scene from the context
|
|
594
|
+
* @returns The scene this component belongs to
|
|
595
|
+
*/
|
|
362
596
|
get scene(): Scene { return this.context.scene; }
|
|
363
597
|
|
|
364
|
-
/**
|
|
598
|
+
/**
|
|
599
|
+
* The layer value of the GameObject this component is attached to
|
|
600
|
+
* Used for visibility and physics filtering
|
|
601
|
+
*/
|
|
365
602
|
get layer(): number {
|
|
366
603
|
return this.gameObject?.userData?.layer;
|
|
367
604
|
}
|
|
368
605
|
|
|
369
|
-
/**
|
|
606
|
+
/**
|
|
607
|
+
* The name of the GameObject this component is attached to
|
|
608
|
+
* Used for debugging and finding objects
|
|
609
|
+
*/
|
|
370
610
|
get name(): string {
|
|
371
611
|
if (this.gameObject?.name) {
|
|
372
612
|
return this.gameObject.name;
|
|
@@ -384,7 +624,11 @@ export abstract class Component implements IComponent, EventTarget,
|
|
|
384
624
|
this.__name = str;
|
|
385
625
|
}
|
|
386
626
|
}
|
|
387
|
-
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* The tag of the GameObject this component is attached to
|
|
630
|
+
* Used for categorizing objects and efficient lookup
|
|
631
|
+
*/
|
|
388
632
|
get tag(): string {
|
|
389
633
|
return this.gameObject?.userData.tag;
|
|
390
634
|
}
|
|
@@ -394,7 +638,11 @@ export abstract class Component implements IComponent, EventTarget,
|
|
|
394
638
|
this.gameObject.userData.tag = str;
|
|
395
639
|
}
|
|
396
640
|
}
|
|
397
|
-
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* Indicates whether the GameObject is marked as static
|
|
644
|
+
* Static objects typically don't move and can be optimized by the engine
|
|
645
|
+
*/
|
|
398
646
|
get static() {
|
|
399
647
|
return this.gameObject?.userData.static;
|
|
400
648
|
}
|
|
@@ -408,7 +656,11 @@ export abstract class Component implements IComponent, EventTarget,
|
|
|
408
656
|
// return this.gameObject?.hideFlags;
|
|
409
657
|
// }
|
|
410
658
|
|
|
411
|
-
/**
|
|
659
|
+
/**
|
|
660
|
+
* Checks if this component is currently active (enabled and part of an active GameObject hierarchy)
|
|
661
|
+
* Components that are inactive won't receive lifecycle method calls
|
|
662
|
+
* @returns True if the component is enabled and all parent GameObjects are active
|
|
663
|
+
*/
|
|
412
664
|
get activeAndEnabled(): boolean {
|
|
413
665
|
if (this.destroyed) return false;
|
|
414
666
|
if (this.__isEnabled === false) return false;
|
|
@@ -426,6 +678,7 @@ export abstract class Component implements IComponent, EventTarget,
|
|
|
426
678
|
private get __isActive(): boolean {
|
|
427
679
|
return this.gameObject.visible;
|
|
428
680
|
}
|
|
681
|
+
|
|
429
682
|
private get __isActiveInHierarchy(): boolean {
|
|
430
683
|
if (!this.gameObject) return false;
|
|
431
684
|
const res = this.gameObject[activeInHierarchyFieldName];
|
|
@@ -438,139 +691,286 @@ export abstract class Component implements IComponent, EventTarget,
|
|
|
438
691
|
this.gameObject[activeInHierarchyFieldName] = val;
|
|
439
692
|
}
|
|
440
693
|
|
|
441
|
-
/**
|
|
694
|
+
/**
|
|
695
|
+
* Reference to the GameObject this component is attached to
|
|
696
|
+
* This is a three.js Object3D with additional GameObject functionality
|
|
697
|
+
*/
|
|
442
698
|
gameObject!: GameObject;
|
|
443
|
-
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* Unique identifier for this component instance,
|
|
702
|
+
* used for finding and tracking components
|
|
703
|
+
*/
|
|
444
704
|
guid: string = "invalid";
|
|
445
|
-
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Identifier for the source asset that created this component.
|
|
708
|
+
* For example, URL to the glTF file this component was loaded from
|
|
709
|
+
*/
|
|
446
710
|
sourceId?: SourceIdentifier;
|
|
447
|
-
// transform: Object3D = nullObject;
|
|
448
711
|
|
|
449
|
-
/**
|
|
712
|
+
/**
|
|
713
|
+
* Called when this component needs to remap guids after an instantiate operation.
|
|
714
|
+
* @param guidsMap Mapping from old guids to newly generated guids
|
|
715
|
+
*/
|
|
450
716
|
resolveGuids?(guidsMap: GuidsMap): void;
|
|
451
717
|
|
|
452
|
-
/**
|
|
453
|
-
*
|
|
718
|
+
/**
|
|
719
|
+
* Called once when the component becomes active for the first time.
|
|
720
|
+
* This is the first lifecycle callback to be invoked
|
|
721
|
+
*/
|
|
454
722
|
awake() { }
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
723
|
+
|
|
724
|
+
/**
|
|
725
|
+
* Called every time the component becomes enabled or active in the hierarchy.
|
|
726
|
+
* Invoked after {@link awake} and before {@link start}.
|
|
727
|
+
*/
|
|
458
728
|
onEnable() { }
|
|
459
|
-
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* Called every time the component becomes disabled or inactive in the hierarchy.
|
|
732
|
+
* Invoked when the component or any parent GameObject becomes invisible
|
|
733
|
+
*/
|
|
460
734
|
onDisable() { }
|
|
461
|
-
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* Called when the component is destroyed.
|
|
738
|
+
* Use for cleanup operations like removing event listeners
|
|
739
|
+
*/
|
|
462
740
|
onDestroy() {
|
|
463
741
|
this.__destroyed = true;
|
|
464
742
|
}
|
|
465
|
-
|
|
466
|
-
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* Called when a field decorated with @validate() is modified.
|
|
746
|
+
* @param prop The name of the field that was changed
|
|
467
747
|
*/
|
|
468
748
|
onValidate?(prop?: string): void;
|
|
469
749
|
|
|
470
|
-
/**
|
|
750
|
+
/**
|
|
751
|
+
* Called when the context's pause state changes.
|
|
752
|
+
* @param isPaused Whether the context is currently paused
|
|
753
|
+
* @param wasPaused The previous pause state
|
|
754
|
+
*/
|
|
471
755
|
onPausedChanged?(isPaused: boolean, wasPaused: boolean): void;
|
|
472
756
|
|
|
473
|
-
/**
|
|
757
|
+
/**
|
|
758
|
+
* Called once at the beginning of the first frame after the component is enabled.
|
|
759
|
+
* Use for initialization that requires other components to be awake.
|
|
760
|
+
*/
|
|
474
761
|
start?(): void;
|
|
475
|
-
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Called at the beginning of each frame before regular updates.
|
|
765
|
+
* Use for logic that needs to run before standard update callbacks.
|
|
766
|
+
*/
|
|
476
767
|
earlyUpdate?(): void;
|
|
477
|
-
|
|
768
|
+
|
|
769
|
+
/**
|
|
770
|
+
* Called once per frame during the main update loop.
|
|
771
|
+
* The primary location for frame-based game logic.
|
|
772
|
+
*/
|
|
478
773
|
update?(): void;
|
|
479
|
-
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* Called after all update functions have been called.
|
|
777
|
+
* Use for calculations that depend on other components being updated first.
|
|
778
|
+
*/
|
|
480
779
|
lateUpdate?(): void;
|
|
481
|
-
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* Called immediately before the scene is rendered.
|
|
783
|
+
* @param frame Current XRFrame if in an XR session, null otherwise
|
|
784
|
+
*/
|
|
482
785
|
onBeforeRender?(frame: XRFrame | null): void;
|
|
483
|
-
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* Called after the scene has been rendered.
|
|
789
|
+
* Use for post-processing or UI updates that should happen after rendering
|
|
790
|
+
*/
|
|
484
791
|
onAfterRender?(): void;
|
|
485
792
|
|
|
793
|
+
/**
|
|
794
|
+
* Called when this component's collider begins colliding with another collider.
|
|
795
|
+
* @param col Information about the collision that occurred
|
|
796
|
+
*/
|
|
486
797
|
onCollisionEnter?(col: Collision);
|
|
798
|
+
|
|
799
|
+
/**
|
|
800
|
+
* Called when this component's collider stops colliding with another collider.
|
|
801
|
+
* @param col Information about the collision that ended
|
|
802
|
+
*/
|
|
487
803
|
onCollisionExit?(col: Collision);
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* Called each frame while this component's collider is colliding with another collider
|
|
807
|
+
* @param col Information about the ongoing collision
|
|
808
|
+
*/
|
|
488
809
|
onCollisionStay?(col: Collision);
|
|
489
810
|
|
|
811
|
+
/**
|
|
812
|
+
* Called when this component's trigger collider is entered by another collider
|
|
813
|
+
* @param col The collider that entered this trigger
|
|
814
|
+
*/
|
|
490
815
|
onTriggerEnter?(col: ICollider);
|
|
816
|
+
|
|
817
|
+
/**
|
|
818
|
+
* Called each frame while another collider is inside this component's trigger collider
|
|
819
|
+
* @param col The collider that is inside this trigger
|
|
820
|
+
*/
|
|
491
821
|
onTriggerStay?(col: ICollider);
|
|
822
|
+
|
|
823
|
+
/**
|
|
824
|
+
* Called when another collider exits this component's trigger collider
|
|
825
|
+
* @param col The collider that exited this trigger
|
|
826
|
+
*/
|
|
492
827
|
onTriggerExit?(col: ICollider);
|
|
493
828
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
* @
|
|
497
|
-
|
|
829
|
+
/**
|
|
830
|
+
* Determines if this component supports a specific XR mode
|
|
831
|
+
* @param mode The XR session mode to check support for
|
|
832
|
+
* @returns True if the component supports the specified mode
|
|
833
|
+
*/
|
|
498
834
|
supportsXR?(mode: XRSessionMode): boolean;
|
|
499
|
-
|
|
835
|
+
|
|
836
|
+
/**
|
|
837
|
+
* Called before an XR session is requested
|
|
838
|
+
* Use to modify session initialization parameters
|
|
839
|
+
* @param mode The XR session mode being requested
|
|
840
|
+
* @param args The session initialization parameters that can be modified
|
|
841
|
+
*/
|
|
500
842
|
onBeforeXR?(mode: XRSessionMode, args: XRSessionInit): void;
|
|
501
|
-
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* Called when this component joins an XR session or becomes active in a running session
|
|
846
|
+
* @param args Event data for the XR session
|
|
847
|
+
*/
|
|
502
848
|
onEnterXR?(args: NeedleXREventArgs): void;
|
|
503
|
-
|
|
849
|
+
|
|
850
|
+
/**
|
|
851
|
+
* Called each frame while this component is active in an XR session
|
|
852
|
+
* @param args Event data for the current XR frame
|
|
853
|
+
*/
|
|
504
854
|
onUpdateXR?(args: NeedleXREventArgs): void;
|
|
505
|
-
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* Called when this component exits an XR session or becomes inactive during a session
|
|
858
|
+
* @param args Event data for the XR session
|
|
859
|
+
*/
|
|
506
860
|
onLeaveXR?(args: NeedleXREventArgs): void;
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
*
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* Called when an XR controller is connected or when this component becomes active
|
|
864
|
+
* in a session with existing controllers
|
|
865
|
+
* @param args Event data for the controller that was added
|
|
866
|
+
*/
|
|
510
867
|
onXRControllerAdded?(args: NeedleXRControllerEventArgs): void;
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
868
|
+
|
|
869
|
+
/**
|
|
870
|
+
* Called when an XR controller is disconnected or when this component becomes inactive
|
|
871
|
+
* during a session with controllers
|
|
872
|
+
* @param args Event data for the controller that was removed
|
|
873
|
+
*/
|
|
514
874
|
onXRControllerRemoved?(args: NeedleXRControllerEventArgs): void;
|
|
515
875
|
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
876
|
+
/**
|
|
877
|
+
* Called when a pointer enters this component's GameObject
|
|
878
|
+
* @param args Data about the pointer event
|
|
879
|
+
*/
|
|
519
880
|
onPointerEnter?(args: PointerEventData);
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* Called when a pointer moves while over this component's GameObject
|
|
884
|
+
* @param args Data about the pointer event
|
|
885
|
+
*/
|
|
520
886
|
onPointerMove?(args: PointerEventData);
|
|
887
|
+
|
|
888
|
+
/**
|
|
889
|
+
* Called when a pointer exits this component's GameObject
|
|
890
|
+
* @param args Data about the pointer event
|
|
891
|
+
*/
|
|
521
892
|
onPointerExit?(args: PointerEventData);
|
|
893
|
+
|
|
894
|
+
/**
|
|
895
|
+
* Called when a pointer button is pressed while over this component's GameObject
|
|
896
|
+
* @param args Data about the pointer event
|
|
897
|
+
*/
|
|
522
898
|
onPointerDown?(args: PointerEventData);
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* Called when a pointer button is released while over this component's GameObject
|
|
902
|
+
* @param args Data about the pointer event
|
|
903
|
+
*/
|
|
523
904
|
onPointerUp?(args: PointerEventData);
|
|
905
|
+
|
|
906
|
+
/**
|
|
907
|
+
* Called when a pointer completes a click interaction with this component's GameObject
|
|
908
|
+
* @param args Data about the pointer event
|
|
909
|
+
*/
|
|
524
910
|
onPointerClick?(args: PointerEventData);
|
|
525
911
|
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
*
|
|
529
|
-
*
|
|
530
|
-
*
|
|
531
|
-
*
|
|
532
|
-
* @param
|
|
533
|
-
* @
|
|
534
|
-
* @
|
|
535
|
-
*
|
|
912
|
+
/**
|
|
913
|
+
* Starts a coroutine that can yield to wait for events.
|
|
914
|
+
* Coroutines allow for time-based sequencing of operations without blocking.
|
|
915
|
+
* Coroutines are based on generator functions, a JavaScript language feature.
|
|
916
|
+
*
|
|
917
|
+
* @param routine Generator function to start
|
|
918
|
+
* @param evt Event to register the coroutine for (default: FrameEvent.Update)
|
|
919
|
+
* @returns The generator function that can be used to stop the coroutine
|
|
920
|
+
* @example
|
|
921
|
+
* Time-based sequencing of operations
|
|
922
|
+
* ```ts
|
|
923
|
+
* *myCoroutine() {
|
|
924
|
+
* yield WaitForSeconds(1); // wait for 1 second
|
|
925
|
+
* yield WaitForFrames(10); // wait for 10 frames
|
|
926
|
+
* yield new Promise(resolve => setTimeout(resolve, 1000)); // wait for a promise to resolve
|
|
927
|
+
* }
|
|
928
|
+
* ```
|
|
929
|
+
* @example
|
|
930
|
+
* Coroutine that logs a message every 5 frames
|
|
536
931
|
* ```ts
|
|
537
|
-
* onEnable() {
|
|
932
|
+
* onEnable() {
|
|
933
|
+
* this.startCoroutine(this.myCoroutine());
|
|
934
|
+
* }
|
|
538
935
|
* private *myCoroutine() {
|
|
539
|
-
*
|
|
540
|
-
*
|
|
541
|
-
*
|
|
542
|
-
*
|
|
543
|
-
*
|
|
936
|
+
* while(this.activeAndEnabled) {
|
|
937
|
+
* console.log("Hello World", this.context.time.frame);
|
|
938
|
+
* // wait for 5 frames
|
|
939
|
+
* for(let i = 0; i < 5; i++) yield;
|
|
940
|
+
* }
|
|
544
941
|
* }
|
|
545
942
|
* ```
|
|
546
943
|
*/
|
|
547
944
|
startCoroutine(routine: Generator, evt: FrameEvent = FrameEvent.Update): Generator {
|
|
548
945
|
return this.context.registerCoroutineUpdate(this, routine, evt);
|
|
549
946
|
}
|
|
947
|
+
|
|
550
948
|
/**
|
|
551
|
-
*
|
|
552
|
-
* @param routine
|
|
553
|
-
* @param evt
|
|
949
|
+
* Stops a coroutine that was previously started with startCoroutine
|
|
950
|
+
* @param routine The routine to be stopped
|
|
951
|
+
* @param evt The frame event the routine was registered with
|
|
554
952
|
*/
|
|
555
953
|
stopCoroutine(routine: Generator, evt: FrameEvent = FrameEvent.Update): void {
|
|
556
954
|
this.context.unregisterCoroutineUpdate(routine, evt);
|
|
557
955
|
}
|
|
558
956
|
|
|
559
|
-
/**
|
|
957
|
+
/**
|
|
958
|
+
* Checks if this component has been destroyed
|
|
959
|
+
* @returns True if the component or its GameObject has been destroyed
|
|
960
|
+
*/
|
|
560
961
|
public get destroyed(): boolean {
|
|
561
962
|
return this.__destroyed;
|
|
562
963
|
}
|
|
563
964
|
|
|
564
965
|
/**
|
|
565
|
-
* Destroys this component
|
|
966
|
+
* Destroys this component and removes it from its GameObject
|
|
967
|
+
* After destruction, the component will no longer receive lifecycle callbacks
|
|
566
968
|
*/
|
|
567
969
|
public destroy() {
|
|
568
970
|
if (this.__destroyed) return;
|
|
569
971
|
this.__internalDestroy();
|
|
570
972
|
}
|
|
571
973
|
|
|
572
|
-
|
|
573
|
-
|
|
574
974
|
/** @internal */
|
|
575
975
|
protected __didAwake: boolean = false;
|
|
576
976
|
|
|
@@ -589,7 +989,6 @@ export abstract class Component implements IComponent, EventTarget,
|
|
|
589
989
|
/** @internal */
|
|
590
990
|
get __internalDidAwakeAndStart() { return this.__didAwake && this.__didStart; }
|
|
591
991
|
|
|
592
|
-
|
|
593
992
|
/** @internal */
|
|
594
993
|
constructor(init?: ComponentInit<Component>) {
|
|
595
994
|
this.__didAwake = false;
|
|
@@ -611,6 +1010,11 @@ export abstract class Component implements IComponent, EventTarget,
|
|
|
611
1010
|
return this;
|
|
612
1011
|
}
|
|
613
1012
|
|
|
1013
|
+
/**
|
|
1014
|
+
* Initializes component properties from an initialization object
|
|
1015
|
+
* @param init Object with properties to copy to this component
|
|
1016
|
+
* @internal
|
|
1017
|
+
*/
|
|
614
1018
|
_internalInit(init?: ComponentInit<this>) {
|
|
615
1019
|
if (typeof init === "object") {
|
|
616
1020
|
for (const key of Object.keys(init)) {
|
|
@@ -690,7 +1094,10 @@ export abstract class Component implements IComponent, EventTarget,
|
|
|
690
1094
|
destroyComponentInstance(this as any);
|
|
691
1095
|
}
|
|
692
1096
|
|
|
693
|
-
|
|
1097
|
+
/**
|
|
1098
|
+
* Controls whether this component is enabled
|
|
1099
|
+
* Disabled components don't receive lifecycle callbacks
|
|
1100
|
+
*/
|
|
694
1101
|
get enabled(): boolean {
|
|
695
1102
|
return typeof this.__isEnabled === "boolean" ? this.__isEnabled : true; // if it has no enabled field it is always enabled
|
|
696
1103
|
}
|
|
@@ -720,85 +1127,154 @@ export abstract class Component implements IComponent, EventTarget,
|
|
|
720
1127
|
}
|
|
721
1128
|
}
|
|
722
1129
|
|
|
1130
|
+
/**
|
|
1131
|
+
* Gets the position of this component's GameObject in world space
|
|
1132
|
+
*/
|
|
723
1133
|
get worldPosition(): Vector3 {
|
|
724
1134
|
return threeutils.getWorldPosition(this.gameObject);
|
|
725
1135
|
}
|
|
726
1136
|
|
|
1137
|
+
/**
|
|
1138
|
+
* Sets the position of this component's GameObject in world space
|
|
1139
|
+
* @param val The world position vector to set
|
|
1140
|
+
*/
|
|
727
1141
|
set worldPosition(val: Vector3) {
|
|
728
1142
|
threeutils.setWorldPosition(this.gameObject, val);
|
|
729
1143
|
}
|
|
730
1144
|
|
|
1145
|
+
/**
|
|
1146
|
+
* Sets the position of this component's GameObject in world space using individual coordinates
|
|
1147
|
+
* @param x X-coordinate in world space
|
|
1148
|
+
* @param y Y-coordinate in world space
|
|
1149
|
+
* @param z Z-coordinate in world space
|
|
1150
|
+
*/
|
|
731
1151
|
setWorldPosition(x: number, y: number, z: number) {
|
|
732
1152
|
threeutils.setWorldPositionXYZ(this.gameObject, x, y, z);
|
|
733
1153
|
}
|
|
734
1154
|
|
|
735
|
-
|
|
1155
|
+
/**
|
|
1156
|
+
* Gets the rotation of this component's GameObject in world space as a quaternion
|
|
1157
|
+
*/
|
|
736
1158
|
get worldQuaternion(): Quaternion {
|
|
737
1159
|
return threeutils.getWorldQuaternion(this.gameObject);
|
|
738
1160
|
}
|
|
1161
|
+
|
|
1162
|
+
/**
|
|
1163
|
+
* Sets the rotation of this component's GameObject in world space using a quaternion
|
|
1164
|
+
* @param val The world rotation quaternion to set
|
|
1165
|
+
*/
|
|
739
1166
|
set worldQuaternion(val: Quaternion) {
|
|
740
1167
|
threeutils.setWorldQuaternion(this.gameObject, val);
|
|
741
1168
|
}
|
|
1169
|
+
|
|
1170
|
+
/**
|
|
1171
|
+
* Sets the rotation of this component's GameObject in world space using quaternion components
|
|
1172
|
+
* @param x X component of the quaternion
|
|
1173
|
+
* @param y Y component of the quaternion
|
|
1174
|
+
* @param z Z component of the quaternion
|
|
1175
|
+
* @param w W component of the quaternion
|
|
1176
|
+
*/
|
|
742
1177
|
setWorldQuaternion(x: number, y: number, z: number, w: number) {
|
|
743
1178
|
threeutils.setWorldQuaternionXYZW(this.gameObject, x, y, z, w);
|
|
744
1179
|
}
|
|
745
1180
|
|
|
746
|
-
|
|
1181
|
+
/**
|
|
1182
|
+
* Gets the rotation of this component's GameObject in world space as Euler angles (in radians)
|
|
1183
|
+
*/
|
|
747
1184
|
get worldEuler(): Euler {
|
|
748
1185
|
return threeutils.getWorldEuler(this.gameObject);
|
|
749
1186
|
}
|
|
750
1187
|
|
|
751
|
-
|
|
1188
|
+
/**
|
|
1189
|
+
* Sets the rotation of this component's GameObject in world space using Euler angles (in radians)
|
|
1190
|
+
* @param val The world rotation Euler angles to set
|
|
1191
|
+
*/
|
|
752
1192
|
set worldEuler(val: Euler) {
|
|
753
1193
|
threeutils.setWorldEuler(this.gameObject, val);
|
|
754
1194
|
}
|
|
755
1195
|
|
|
756
|
-
|
|
1196
|
+
/**
|
|
1197
|
+
* Gets the rotation of this component's GameObject in world space as Euler angles (in degrees)
|
|
1198
|
+
*/
|
|
757
1199
|
get worldRotation(): Vector3 {
|
|
758
|
-
return this.gameObject.worldRotation
|
|
1200
|
+
return this.gameObject.worldRotation;
|
|
759
1201
|
}
|
|
760
1202
|
|
|
1203
|
+
/**
|
|
1204
|
+
* Sets the rotation of this component's GameObject in world space using Euler angles (in degrees)
|
|
1205
|
+
* @param val The world rotation vector to set (in degrees)
|
|
1206
|
+
*/
|
|
761
1207
|
set worldRotation(val: Vector3) {
|
|
762
1208
|
this.setWorldRotation(val.x, val.y, val.z, true);
|
|
763
1209
|
}
|
|
764
1210
|
|
|
1211
|
+
/**
|
|
1212
|
+
* Sets the rotation of this component's GameObject in world space using individual Euler angles
|
|
1213
|
+
* @param x X-axis rotation
|
|
1214
|
+
* @param y Y-axis rotation
|
|
1215
|
+
* @param z Z-axis rotation
|
|
1216
|
+
* @param degrees Whether the values are in degrees (true) or radians (false)
|
|
1217
|
+
*/
|
|
765
1218
|
setWorldRotation(x: number, y: number, z: number, degrees: boolean = true) {
|
|
766
1219
|
threeutils.setWorldRotationXYZ(this.gameObject, x, y, z, degrees);
|
|
767
1220
|
}
|
|
768
1221
|
|
|
769
1222
|
private static _forward: Vector3 = new Vector3();
|
|
770
|
-
/**
|
|
1223
|
+
/**
|
|
1224
|
+
* Gets the forward direction vector (0,0,-1) of this component's GameObject in world space
|
|
1225
|
+
*/
|
|
771
1226
|
public get forward(): Vector3 {
|
|
772
1227
|
return Component._forward.set(0, 0, -1).applyQuaternion(this.worldQuaternion);
|
|
773
1228
|
}
|
|
774
1229
|
private static _right: Vector3 = new Vector3();
|
|
775
|
-
/**
|
|
1230
|
+
/**
|
|
1231
|
+
* Gets the right direction vector (1,0,0) of this component's GameObject in world space
|
|
1232
|
+
*/
|
|
776
1233
|
public get right(): Vector3 {
|
|
777
1234
|
return Component._right.set(1, 0, 0).applyQuaternion(this.worldQuaternion);
|
|
778
1235
|
}
|
|
779
1236
|
private static _up: Vector3 = new Vector3();
|
|
780
|
-
/**
|
|
1237
|
+
/**
|
|
1238
|
+
* Gets the up direction vector (0,1,0) of this component's GameObject in world space
|
|
1239
|
+
*/
|
|
781
1240
|
public get up(): Vector3 {
|
|
782
1241
|
return Component._up.set(0, 1, 0).applyQuaternion(this.worldQuaternion);
|
|
783
1242
|
}
|
|
784
1243
|
|
|
785
|
-
|
|
786
|
-
|
|
787
1244
|
// EventTarget implementation:
|
|
788
1245
|
|
|
1246
|
+
/**
|
|
1247
|
+
* Storage for event listeners registered to this component
|
|
1248
|
+
* @private
|
|
1249
|
+
*/
|
|
789
1250
|
private _eventListeners = new Map<string, EventListener[]>();
|
|
790
1251
|
|
|
1252
|
+
/**
|
|
1253
|
+
* Registers an event listener for the specified event type
|
|
1254
|
+
* @param type The event type to listen for
|
|
1255
|
+
* @param listener The callback function to execute when the event occurs
|
|
1256
|
+
*/
|
|
791
1257
|
addEventListener<T extends Event>(type: string, listener: (evt: T) => any) {
|
|
792
1258
|
this._eventListeners[type] = this._eventListeners[type] || [];
|
|
793
1259
|
this._eventListeners[type].push(listener);
|
|
794
1260
|
}
|
|
795
1261
|
|
|
1262
|
+
/**
|
|
1263
|
+
* Removes a previously registered event listener
|
|
1264
|
+
* @param type The event type the listener was registered for
|
|
1265
|
+
* @param listener The callback function to remove
|
|
1266
|
+
*/
|
|
796
1267
|
removeEventListener<T extends Event>(type: string, listener: (arg: T) => any) {
|
|
797
1268
|
if (!this._eventListeners[type]) return;
|
|
798
1269
|
const index = this._eventListeners[type].indexOf(listener);
|
|
799
1270
|
if (index >= 0) this._eventListeners[type].splice(index, 1);
|
|
800
1271
|
}
|
|
801
1272
|
|
|
1273
|
+
/**
|
|
1274
|
+
* Dispatches an event to all registered listeners
|
|
1275
|
+
* @param evt The event object to dispatch
|
|
1276
|
+
* @returns Always returns false (standard implementation of EventTarget)
|
|
1277
|
+
*/
|
|
802
1278
|
dispatchEvent(evt: Event): boolean {
|
|
803
1279
|
if (!evt || !this._eventListeners[evt.type]) return false;
|
|
804
1280
|
const listeners = this._eventListeners[evt.type];
|