Skip to content

packages/core/src/features/physics/lib/2d/runtime/world-component.ts

Read as Markdown

This is the source snapshot used to build these API details. View this revision on GitHub.

Back to reference

1 import type { PibblComponent } from "@pibbl/core";
2 import {
3   pibblInternalCurrentSimulationOwner,
4   pibblInternalCurrentWorldTransform,
5   setCurrentComponentAfterRender,
6   useInternalHookSlot,
7 } from "@pibbl/core/internal";
8 import type {
9   PibblPhysicsWorld2DProps,
10   PibblPhysicsWorldHandle2D,
11 } from "../types.js";
12 import { resolveSignalFields } from "../../shared/signal-inputs.js";
13 import type { PibblResolvedPhysicsWorld2DProps } from "./world-props.js";
14 import { pushPhysicsWorld2D, requireCurrentPhysicsWorld2D } from "./context.js";
15 import { PhysicsWorld2DController } from "./controller.js";
16 
17 const renderPhysicsWorld2D: PibblComponent<PibblPhysicsWorld2DProps> = (props) => {
18   const resolvedProps = resolveSignalFields<PibblResolvedPhysicsWorld2DProps>(
19     props,
20     WORLD_STRUCTURAL_FIELDS,
21   );
22   const owner = pibblInternalCurrentSimulationOwner();
23   const slot = useInternalHookSlot("physicsWorld2DController", (teardowns) => {
24     const controller = new PhysicsWorld2DController(owner, resolvedProps);
25     teardowns.add(() => controller.dispose());
26     return controller;
27   });
28   const controller = slot.value;
29   controller.beginRender(resolvedProps, pibblInternalCurrentWorldTransform());
30   const popWorld = pushPhysicsWorld2D(controller);
31   try {
32     setCurrentComponentAfterRender(popWorld);
33   } catch (error) {
34     popWorld();
35     throw error;
36   }
37   return props.children;
38 };
39 
40 const WORLD_STRUCTURAL_FIELDS = new Set<PropertyKey>(["children"]);
41 
42 /**
43  * Owns one transparent, component-scoped 2D physics world.
44  *
45  * @see {@link PibblComponent}
46  * @see {@link PibblPhysicsWorld2DProps}
47  */
48 export const PhysicsWorld2D = renderPhysicsWorld2D;
49 
50 /**
51  * Returns the nearest 2D physics world while a descendant is evaluating.
52  *
53  * @returns A stable world handle that can be attached to a physics world. See
54  * {@link PibblPhysicsWorldHandle2D} .
55  *
56  * @see {@link PibblPhysicsWorldHandle2D}
57  */
58 export function pibblPhysicsWorld2D(): PibblPhysicsWorldHandle2D {
59   return requireCurrentPhysicsWorld2D().handle;
60 }
61 

Documentation built with @pibbl/core 0.0.2, revision 2dccb19. ALPHA — NOT FOR PRODUCTION USE.