@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.
68 lines (67 loc) • 2.87 kB
TypeScript
import { Behaviour, GameObject } from "./Component.js";
import { type IPointerEventHandler, PointerEventData } from "./ui/PointerEvents.js";
/**
* The [Duplicatable](https://engine.needle.tools/docs/api/Duplicatable) component creates clones of a GameObject when clicked/tapped/dragged.
* Perfect for spawning objects, creating drag-and-drop inventories, or multiplayer object creation.
*
* 
*
* **How it works:**
* - When the user clicks on this object, it creates a clone of the assigned `object`
* - The clone is automatically set up with {@link DragControls} so users can drag it
* - If networking is enabled, clones are synced via {@link SyncedTransform}
* - Rate limiting prevents spam (controlled by `limitCount`)
*
* **Setup tips:**
* - Assign `object` to a template object (it will be hidden and used as source)
* - If `object` is not assigned, the component's own GameObject is used as template
* - Add an {@link ObjectRaycaster} to enable pointer detection (added automatically if missing)
*
* @example Basic duplicatable button
* ```ts
* const duplicatable = spawnButton.addComponent(Duplicatable);
* duplicatable.object = templateObject; // Object to clone
* duplicatable.parent = spawnContainer; // Where to place clones
* duplicatable.limitCount = 10; // Max 10 per second
* ```
*
* @summary Duplicates a GameObject on pointer events
* @category Interactivity
* @group Components
* @see {@link DragControls} for dragging the duplicated objects
* @see {@link SyncedTransform} for networking support
* @see {@link GameObject.instantiateSynced} for the underlying instantiation
* @link https://engine.needle.tools/samples/collaborative-sandbox/
*/
export declare class Duplicatable extends Behaviour implements IPointerEventHandler {
/**
* Parent object for spawned duplicates.
* If not set, duplicates are parented to this GameObject's parent.
*/
parent: GameObject | null;
/**
* Template object to duplicate. This object will be hidden and used as the source for clones.
* If not assigned, this GameObject itself is used as the template.
*/
object: GameObject | null;
/**
* Maximum duplications allowed per second to prevent spam.
* The counter decreases by 1 each second.
* @default 60
*/
limitCount: number;
private _currentCount;
private _startPosition;
private _startQuaternion;
start(): void;
onEnable(): void;
private _forwardPointerEvents;
onPointerEnter(args: PointerEventData): void;
onPointerExit(args: PointerEventData): void;
/** @internal */
onPointerDown(args: PointerEventData): void;
/** @internal */
onPointerUp(args: PointerEventData): void;
private cloneLimitIntervalFn;
private handleDuplication;
}