@needle-tools/engine
Version:
Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development with great integrations into editors like Unity or Blender - and can be deployed onto any device! It is flexible, extensible and networking and XR are built-in.
77 lines • 3.08 kB
JavaScript
import { getParam } from "../../engine/engine_utils.js";
import { Behaviour } from "../Component.js";
export const debug = getParam("debugavatar");
/**
* Marks a GameObject as being controlled or owned by a player in networked XR sessions.
* This is used internally by the networking system to identify player-controlled objects.
*
* **Note:** This is an internal marker class. For most use cases, use the {@link Avatar} component instead.
*
* @summary Internal marker for player-controlled objects in networked sessions
* @category XR
* @category Networking
* @group Components
* @see {@link Avatar} for avatar setup and configuration
*/
// We might be updating this system in the future to a centralized API (PlayerView)
// but since currently quite a few core components rely on it, we're keeping it for now
export class AvatarMarker extends Behaviour {
/**
* Get an avatar marker by index from the global list of avatar markers.
* @param index The index in the instances array
* @returns The AvatarMarker at the specified index, or null if index is out of bounds
*/
static getAvatar(index) {
if (index >= 0 && index < AvatarMarker.instances.length)
return AvatarMarker.instances[index];
return null;
}
/** Global list of all active AvatarMarker instances */
static instances = [];
/**
* Subscribe to avatar marker creation events.
* @param cb Callback function called when a new avatar marker is created
* @returns The callback function (for removal)
*/
static onAvatarMarkerCreated(cb) {
AvatarMarker._onNewAvatarMarkerAdded.push(cb);
return cb;
}
/**
* Subscribe to avatar marker destruction events.
* @param cb Callback function called when an avatar marker is destroyed
* @returns The callback function (for removal)
*/
static onAvatarMarkerDestroyed(cb) {
AvatarMarker._onAvatarMarkerDestroyed.push(cb);
return cb;
}
static _onNewAvatarMarkerAdded = [];
static _onAvatarMarkerDestroyed = [];
/** The network connection ID of the player who owns this avatar */
connectionId;
/** Reference to the avatar GameObject with optional XR flags */
avatar;
/** @internal */
awake() {
AvatarMarker.instances.push(this);
if (debug)
console.log(this);
for (const cb of AvatarMarker._onNewAvatarMarkerAdded)
cb({ avatarMarker: this, gameObject: this.gameObject });
}
/** @internal */
onDestroy() {
AvatarMarker.instances.splice(AvatarMarker.instances.indexOf(this), 1);
for (const cb of AvatarMarker._onAvatarMarkerDestroyed)
cb({ avatarMarker: this, gameObject: this.gameObject });
}
/**
* Check if this avatar marker represents the local player.
* @returns True if this avatar belongs to the local player, false otherwise
*/
isLocalAvatar() {
return this.connectionId === this.context.connection.connectionId;
}
}
//# sourceMappingURL=WebXRAvatar.js.map