Skip to content

WorldNode

hhh edited this page Apr 11, 2022 · 3 revisions

WorldNode

/**
 * Emits before main loop.
 * (stoppable &cancelable;
 * Invoke `event.cancel` to skip main loop.)
 */
type WorldBeforeUpdateEvent = Event<'beforeUpdate', null>;

/**
 * Emits after main loop. (stoppable)
 */
type WorldAfterUpdateEvent = Event<'afterUpdate', null>;

/**
 * Type map of events on world nodes.
 */
interface WorldNodeEvents extends CanvasNodeEvents {
    beforeUpdate: WorldBeforeUpdateEvent;
    afterUpdate: WorldAfterUpdateEvent;
}

/**
 * Type of options for {@link WorldNode}.
 */
type WorldNodeOptions<Events extends WorldNodeEvents> = (CanvasNodeOptions<Events> & Partial<{
    /**
     * The duration of a frame. (ms)
     * @default 10
     */
    frameDuration: number;
    /**
     * The maximum count of frames that an update can have.
     * @default 3
     */
    maxFrameCount: number;
    /**
     * Default value of `renderRoot` & `eventRoot`.
     * @default this.getRoot()
     */
    root: CanvasRoot<any> | null;
    /**
     * The root node that should be automatically rerendered.
     * (Set this to `null` to disable automatic rerender.)
     * @default options.root
     */
    renderRoot: CanvasRoot<any> | null;
    /**
     * The root node to which event listeners are attached.
     * (Set this to `null` to disable default event handling.)
     * @default options.root
     */
    eventRoot: CanvasRoot<any> | null;
    /**
     * The collision checker to use.
     * (Set this to `null` to disable collision checking.)
     * @default Collision.Checkers.SAT
     */
    collisionChecker: CollisionChecker | null;
    /**
     * Wether to enable built-in drag handling.
     * @default false
     */
    draggable: boolean;
}>);

/**
 * Class of physical world nodes
 * that contain and update body nodes.
 * (Note that inside a world node,
 * the positions of body nodes
 * are represented by their `offset`,
 * relative to the world node.)
 */
class WorldNode<Events extends WorldNodeEvents = WorldNodeEvents> extends CanvasNode<Events> {

    /**
     * Constructor of {@link WorldNode}.
     */
    constructor(options?: WorldNodeOptions<Events>);

    /**
     * @override CanvasNode.tag
     * @default 'world'
     */
    readonly tag: string;

    /**
     * The root node to which event listeners are attached.
     * (Set this to `null` to disable default event handling.)
     * @default options.root
     */
    readonly eventRoot: CanvasRoot<any> | null;

    /**
     * The internal pointer constraint.
     * (The `bodyA` is initialized with an anchor body
     * which automatically follows the pointer,
     * and the `bodyB` will be the object being dragged.)
     */
    readonly pointerConstraint: ConstraintNode;

    /**
     * @override CanvasNode.penetrable
     * @default true
     */
    penetrable: boolean;

    /**
     * The duration of a frame. (ms)
     * @default 10
     */
    frameDuration: number;

    /**
     * The maximum count of frames that an update can have.
     * @default 3
     */
    maxFrameCount: number;

    /**
     * The root node that should be automatically rerendered.
     * (Set this to `null` to disable automatic rerender.)
     * @default options.root
     */
    renderRoot: CanvasRoot<any> | null;

    /**
     * The collision checker to use.
     * (Set this to `null` to disable collision checking.)
     * @default Collision.Checkers.SAT
     */
    collisionChecker: CollisionChecker | null;

    /**
     * Wether to enable built-in drag handling.
     * @default false
     */
    draggable: boolean;

    /**
     * @override CanvasNode.noChildUpdate
     * @default true
     */
    protected noChildUpdate: boolean;

    /**
     * Whether the world is active now.
     */
    get active(): boolean;

    /**
     * Activate the world node.
     */
    activate(): void;

    /**
     * Deactivate the world node.
     */
    deactivate(): void;

    /**
     * Attach event listeners.
     * (This will be automatically invoked in constructor.)
     */
    attachListeners(): void;

    /**
     * Detach event listeners.
     */
    detachListeners(): void;

    /**
     * @override CanvasNode.updateLayout
     */
    protected updateLayout(timeStamp: number): void;

    /**
     * @override CanvasNode.afterUpdate
     */
    protected afterUpdate(timeStamp: number): void;
}
Clone this wiki locally