@typeonce/ritmo-render 0.14.0 → 0.16.0
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 +72 -0
- package/README.md +31 -2
- package/dist/NodeRegistry.d.ts +69 -19
- package/dist/NodeRegistry.d.ts.map +1 -1
- package/dist/NodeRegistry.js +113 -16
- package/dist/NodeRegistry.js.map +1 -1
- package/dist/RenderSync.d.ts +49 -4
- package/dist/RenderSync.d.ts.map +1 -1
- package/dist/RenderSync.js +71 -4
- package/dist/RenderSync.js.map +1 -1
- package/package.json +2 -2
- package/src/NodeRegistry.ts +142 -23
- package/src/RenderSync.ts +124 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,77 @@
|
|
|
1
1
|
# @typeonce/ritmo-render
|
|
2
2
|
|
|
3
|
+
## 0.16.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 9b0379e: **Every JSDoc `@example` now compiles against the current API.** Examples are type-checked in CI as a game would import them, so they no longer cite exports that do not exist or call them with the wrong arguments. What changed in meaning:
|
|
8
|
+
|
|
9
|
+
- `Game.Runtime.make(...)` with a constructed resource (a `Clock.resource`, a `Descriptor.ConstructedResource`) returns a `Result` that may fail: the examples check `ok` instead of reading `.value`.
|
|
10
|
+
- A captured input resource is typed from its source: `Descriptor.TransientResource<InputCapture.SnapshotOf<typeof KeyboardInput>>()`, not `Keyboard.Snapshot<...>`.
|
|
11
|
+
- `Netcode.seats(...)` returns a `Result`: check it instead of `.value!`.
|
|
12
|
+
- `Lint.Target<typeof Game.schema, typeof Root>` takes the schema root second.
|
|
13
|
+
- `NodeRegistry.make` infers the node type from `attach`'s parameter, with no explicit generic.
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [9b0379e]
|
|
16
|
+
- Updated dependencies [be9af72]
|
|
17
|
+
- Updated dependencies [9b0379e]
|
|
18
|
+
- Updated dependencies [9290521]
|
|
19
|
+
- Updated dependencies [9290521]
|
|
20
|
+
- Updated dependencies [1b55d21]
|
|
21
|
+
- @typeonce/ritmo@0.16.0
|
|
22
|
+
|
|
23
|
+
## 0.15.0
|
|
24
|
+
|
|
25
|
+
### Minor Changes
|
|
26
|
+
|
|
27
|
+
- b58d75b: **Child entities' nodes attach under their parent's node.** `RenderSync.system(Game, { ..., hierarchy: Attached })` takes a `Descriptor.Hierarchy` of the game: a child's node is added under its parent's node instead of the root, so the renderer composes the transforms and `apply` sets the child's local transform. A child moves when its parent changes, is removed, or gains or loses its node, in the same run; parents move before their children. Despawning a parent despawns its children, so their nodes go with it. A parent that only loses its renderable hands its children's nodes back to the root.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
const { relation: Attached } = Descriptor.Hierarchy(
|
|
31
|
+
"Game/Attached",
|
|
32
|
+
"Game/Attachments"
|
|
33
|
+
);
|
|
34
|
+
const Render = RenderSync.system(Game, {
|
|
35
|
+
name: "Game/Render",
|
|
36
|
+
renderable: Body,
|
|
37
|
+
transform: Position,
|
|
38
|
+
registry: Sprites,
|
|
39
|
+
hierarchy: Attached,
|
|
40
|
+
create,
|
|
41
|
+
apply,
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
- A `Descriptor.Relation` as `hierarchy` does not compile (`hierarchy must be a Descriptor.Hierarchy relation`), and neither does a relation missing from the schema (`hierarchy must be a relation of the game's schema`).
|
|
46
|
+
- `NodeRegistry` keeps the node tree: `ensure(entity, create, parent?)` creates a node under a parent's node, `reparent(entity, parent)` moves one (and refuses a loop), and `parents` maps each nested node's entity to its parent's. `remove` moves a removed node's children back to the root before detaching it, and `clear` detaches children before their parents.
|
|
47
|
+
- `PixiNodeRegistry.make(container)` adds a node with `(parent ?? container).addChild(node)`. Its nodes must be able to hold children (`addChild` and `removeChild`, as every Pixi display object has); otherwise it does not compile (`each node must be a container`).
|
|
48
|
+
- **Breaking: `RegistryOptions.attach` receives the parent node**, `(node, parent: Node | undefined)`, `undefined` meaning the root, and is also called to move an attached node, so it must take the node out of its current parent. A one-parameter `attach` still compiles but puts every node on the root, so a game passing `hierarchy` sees children unattached. Update a custom registry:
|
|
49
|
+
|
|
50
|
+
```diff
|
|
51
|
+
NodeRegistry.make<Mesh>({
|
|
52
|
+
- attach: (mesh) => scene.add(mesh),
|
|
53
|
+
- detach: (mesh) => scene.remove(mesh)
|
|
54
|
+
+ attach: (mesh, parent) => (parent ?? scene).add(mesh),
|
|
55
|
+
+ detach: (mesh) => mesh.removeFromParent()
|
|
56
|
+
})
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
- **Breaking: a hand-written `NodeRegistry<Node>` object needs `reparent` and `parents`**, and `ensure` takes an optional `parent`. Build registries with `NodeRegistry.make` instead of implementing the interface.
|
|
60
|
+
- **Breaking: `PixiNodeRegistry.make`'s nodes must have `addChild` and `removeChild`.** Pixi display objects already do; a test double adds the two methods.
|
|
61
|
+
|
|
62
|
+
### Patch Changes
|
|
63
|
+
|
|
64
|
+
- Updated dependencies [3a195b7]
|
|
65
|
+
- Updated dependencies [d01a735]
|
|
66
|
+
- Updated dependencies [45458dc]
|
|
67
|
+
- Updated dependencies [c75daff]
|
|
68
|
+
- Updated dependencies [b77b17d]
|
|
69
|
+
- Updated dependencies [c20f20b]
|
|
70
|
+
- Updated dependencies [01959dd]
|
|
71
|
+
- Updated dependencies [637fa1f]
|
|
72
|
+
- Updated dependencies [44e9b42]
|
|
73
|
+
- @typeonce/ritmo@0.15.0
|
|
74
|
+
|
|
3
75
|
## 0.14.0
|
|
4
76
|
|
|
5
77
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -26,13 +26,42 @@ const Render = RenderSync.system(Game, {
|
|
|
26
26
|
const runtime = Game.Runtime.make({
|
|
27
27
|
services: Game.Runtime.services(
|
|
28
28
|
Game.Runtime.service(Meshes, NodeRegistry.make<Mesh>({
|
|
29
|
-
attach: (mesh) => scene.add(mesh),
|
|
30
|
-
detach: (mesh) =>
|
|
29
|
+
attach: (mesh, parent) => (parent ?? scene).add(mesh),
|
|
30
|
+
detach: (mesh) => mesh.removeFromParent()
|
|
31
31
|
}))
|
|
32
32
|
)
|
|
33
33
|
}).value
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
`attach` receives the node to add and the node to add it under (`undefined` for the root). It is also called to move an attached node to another parent, so it must take the node out of the parent it is in, as Three.js's `add` and Pixi's `addChild` do. Declare both parameters: a one-parameter `attach` still compiles but puts every node on the root.
|
|
37
|
+
|
|
38
|
+
## Hierarchies
|
|
39
|
+
|
|
40
|
+
A part that moves with its entity (a turret on a tank, a name over a head) is a child entity in a `Descriptor.Hierarchy`. Pass the hierarchy to `RenderSync.system` and each child's node is attached under its parent's node, so the renderer composes the transforms:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
const { relation: Attached } = Descriptor.Hierarchy("Game/Attached", "Game/Attachments")
|
|
44
|
+
|
|
45
|
+
const Render = RenderSync.system(Game, {
|
|
46
|
+
name: "Game/Render",
|
|
47
|
+
renderable: Renderable,
|
|
48
|
+
transform: Position, // a child's Position is relative to its parent
|
|
49
|
+
registry: Meshes,
|
|
50
|
+
hierarchy: Attached,
|
|
51
|
+
create: ({ renderable }) => new Mesh(geometry, materials[renderable.material]),
|
|
52
|
+
apply: (mesh, { transform }) => mesh.position.set(transform.x, 0, transform.y)
|
|
53
|
+
})
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
- `apply` receives the child's own components: its transform is local to the parent's node. Ritmo does no world-transform math.
|
|
57
|
+
- A child whose parent has no node in the registry sits at the root, and moves under the parent's node when the parent gets one. Only the direct parent counts.
|
|
58
|
+
- A child's node moves when its parent changes (`commands.relate`) or is removed (`commands.unrelate`), in the same run.
|
|
59
|
+
- Despawning a parent despawns its children, so their nodes are destroyed with it. A parent that only loses its renderable hands its children's nodes back to the root.
|
|
60
|
+
- A plain `Descriptor.Relation` does not compile as `hierarchy`.
|
|
61
|
+
- Each run checks every renderable entity that has a parent, so the cost grows with the number of children. A game without `hierarchy` pays nothing.
|
|
62
|
+
|
|
63
|
+
The registry keeps the tree: `ensure(entity, create, parent)` creates a node under a parent's node, `reparent(entity, parent)` moves one, and `parents` maps each nested node's entity to its parent's. Removing a parent's node first moves the nodes under it back to the root, so an entity's node is destroyed only when that entity's is removed.
|
|
64
|
+
|
|
36
65
|
For Pixi, [`@typeonce/ritmo-pixi`](https://github.com/typeonce-dev/ritmo/blob/main/packages/pixi/README.md) provides `PixiNodeRegistry.make(container)`, an implementation of the same registry contract. The host provides it with `Game.Runtime.service`; render systems keep depending on the generic service descriptor.
|
|
37
66
|
|
|
38
67
|
Released together with [`@typeonce/ritmo`](https://github.com/typeonce-dev/ritmo/blob/main/packages/core/README.md), always with the same version.
|
package/dist/NodeRegistry.d.ts
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Hosts mirror ECS entities into renderer objects (sprites, containers,
|
|
5
5
|
* meshes, DOM elements). The registry owns the bookkeeping every game
|
|
6
|
-
* repeats: one node per entity, attaching new nodes to the scene
|
|
7
|
-
* and destroying them when the entity
|
|
8
|
-
* and teardown. It does not import or
|
|
9
|
-
* detaching mean is passed to `make`.
|
|
6
|
+
* repeats: one node per entity, attaching new nodes to the scene (or under
|
|
7
|
+
* another entity's node), detaching and destroying them when the entity
|
|
8
|
+
* loses its renderable or is despawned, and teardown. It does not import or
|
|
9
|
+
* assume any renderer: what attaching and detaching mean is passed to `make`.
|
|
10
10
|
*
|
|
11
11
|
* Nodes are keyed by the entity's numeric value, so an `EntityId`, a query
|
|
12
12
|
* match's `entity.id`, or a stored `Handle` all address the same node.
|
|
@@ -16,15 +16,32 @@
|
|
|
16
16
|
*
|
|
17
17
|
* @example
|
|
18
18
|
* ```ts
|
|
19
|
-
*
|
|
20
|
-
*
|
|
19
|
+
* // Three.js, reduced to what the registry touches:
|
|
20
|
+
* interface Geometry { dispose(): void }
|
|
21
|
+
* declare class Mesh {
|
|
22
|
+
* constructor(geometry: Geometry, material: object)
|
|
23
|
+
* readonly geometry: Geometry
|
|
24
|
+
* add(child: Mesh): void
|
|
25
|
+
* removeFromParent(): void
|
|
26
|
+
* }
|
|
27
|
+
* declare const scene: { add(mesh: Mesh): void }
|
|
28
|
+
* declare const geometry: Geometry
|
|
29
|
+
* declare const turretGeometry: Geometry
|
|
30
|
+
* declare const material: object
|
|
31
|
+
* declare const tank: Entity.EntityId<typeof Game.schema>
|
|
32
|
+
* declare const turret: Entity.EntityId<typeof Game.schema>
|
|
33
|
+
* declare const despawned: Entity.EntityId<typeof Game.schema>
|
|
34
|
+
* // ---cut---
|
|
35
|
+
* const meshes = NodeRegistry.make({
|
|
36
|
+
* attach: (mesh: Mesh, parent) => (parent ?? scene).add(mesh),
|
|
21
37
|
* detach: (mesh) => {
|
|
22
|
-
*
|
|
38
|
+
* mesh.removeFromParent()
|
|
23
39
|
* mesh.geometry.dispose()
|
|
24
40
|
* }
|
|
25
41
|
* })
|
|
26
|
-
* meshes.ensure(
|
|
27
|
-
* meshes.
|
|
42
|
+
* meshes.ensure(tank, () => new Mesh(geometry, material))
|
|
43
|
+
* meshes.ensure(turret, () => new Mesh(turretGeometry, material), tank) // under the tank's node
|
|
44
|
+
* meshes.remove(despawned)
|
|
28
45
|
* meshes.clear()
|
|
29
46
|
* ```
|
|
30
47
|
*/
|
|
@@ -38,6 +55,12 @@ export type EntityKey = number;
|
|
|
38
55
|
* as `PixiNodeRegistry.make`). Provide it as a runtime service and hand that
|
|
39
56
|
* service to `RenderSync.system`, which fills and empties it; host code reads
|
|
40
57
|
* it with `get`.
|
|
58
|
+
*
|
|
59
|
+
* A node sits at the root of the scene or under another entity's node (its
|
|
60
|
+
* parent): `RenderSync.system`'s `hierarchy` option keeps that in step with
|
|
61
|
+
* a `Descriptor.Hierarchy`. Removing a parent's node first moves the nodes
|
|
62
|
+
* attached under it back to the root, so an entity's node is only ever
|
|
63
|
+
* destroyed by removing that entity.
|
|
41
64
|
*/
|
|
42
65
|
export interface NodeRegistry<Node> {
|
|
43
66
|
/** Number of live nodes. */
|
|
@@ -45,15 +68,31 @@ export interface NodeRegistry<Node> {
|
|
|
45
68
|
/** The entity's node, or `undefined` when it has none. Use `ensure` to create one. */
|
|
46
69
|
get(entity: EntityKey): Node | undefined;
|
|
47
70
|
/**
|
|
48
|
-
* Returns the entity's node, creating and attaching it first when missing
|
|
71
|
+
* Returns the entity's node, creating and attaching it first when missing:
|
|
72
|
+
* under `parent`'s node when `parent` is given and has one, else at the
|
|
73
|
+
* root. An existing node is returned where it is; use `reparent` to move it.
|
|
74
|
+
*/
|
|
75
|
+
ensure(entity: EntityKey, create: () => Node, parent?: EntityKey): Node;
|
|
76
|
+
/**
|
|
77
|
+
* Moves the entity's existing node under `parent`'s node, or to the root
|
|
78
|
+
* when `parent` is `undefined` or has no node. Does nothing when the node
|
|
79
|
+
* is already there. Returns `false`, moving nothing, when the entity has
|
|
80
|
+
* no node or `parent`'s node is the entity's own or attached under it (a
|
|
81
|
+
* loop); `true` otherwise.
|
|
82
|
+
*/
|
|
83
|
+
reparent(entity: EntityKey, parent: EntityKey | undefined): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Each node attached under another entity's node: the entity's key to its
|
|
86
|
+
* parent's key. Nodes at the root are absent.
|
|
49
87
|
*/
|
|
50
|
-
|
|
88
|
+
readonly parents: ReadonlyMap<EntityKey, EntityKey>;
|
|
51
89
|
/**
|
|
52
|
-
* Detaches and destroys the entity's node
|
|
90
|
+
* Detaches and destroys the entity's node, after moving the nodes attached
|
|
91
|
+
* under it to the root. Returns whether one existed.
|
|
53
92
|
*/
|
|
54
93
|
remove(entity: EntityKey): boolean;
|
|
55
94
|
/**
|
|
56
|
-
* Detaches and destroys every node.
|
|
95
|
+
* Detaches and destroys every node, children before their parents.
|
|
57
96
|
*/
|
|
58
97
|
clear(): void;
|
|
59
98
|
/** The live nodes with their entity keys. */
|
|
@@ -61,16 +100,27 @@ export interface NodeRegistry<Node> {
|
|
|
61
100
|
}
|
|
62
101
|
/** What attaching and detaching a node mean for one renderer; see `make`. */
|
|
63
102
|
export interface RegistryOptions<Node> {
|
|
64
|
-
/**
|
|
65
|
-
|
|
66
|
-
|
|
103
|
+
/**
|
|
104
|
+
* Adds a node to the scene: under `parent` (another entity's node), or at
|
|
105
|
+
* the root when `parent` is `undefined`. Called for new nodes and to move
|
|
106
|
+
* an attached node to a new parent, so it must also take a node out of the
|
|
107
|
+
* parent it is in: Pixi's `addChild` and Three.js's `add` do.
|
|
108
|
+
*
|
|
109
|
+
* Declare both parameters. A one-parameter `attach` still compiles but
|
|
110
|
+
* ignores `parent`, so every node lands on the root.
|
|
111
|
+
*/
|
|
112
|
+
readonly attach: (node: Node, parent: Node | undefined) => void;
|
|
113
|
+
/**
|
|
114
|
+
* Removes a node from the scene, from the root or the parent node it is
|
|
115
|
+
* attached under, and releases it.
|
|
116
|
+
*/
|
|
67
117
|
readonly detach: (node: Node) => void;
|
|
68
118
|
}
|
|
69
119
|
/**
|
|
70
120
|
* Creates a registry with explicit attach/detach behavior: `attach` adds a
|
|
71
|
-
*
|
|
72
|
-
* packages build on this (for example `PixiNodeRegistry.make`
|
|
73
|
-
* `@typeonce/ritmo-pixi`).
|
|
121
|
+
* node under its parent node or at the root, `detach` removes and releases
|
|
122
|
+
* it. Renderer packages build on this (for example `PixiNodeRegistry.make`
|
|
123
|
+
* in `@typeonce/ritmo-pixi`).
|
|
74
124
|
*/
|
|
75
125
|
export declare const make: <Node>(options: RegistryOptions<Node>) => NodeRegistry<Node>;
|
|
76
126
|
//# sourceMappingURL=NodeRegistry.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeRegistry.d.ts","sourceRoot":"","sources":["../src/NodeRegistry.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"NodeRegistry.d.ts","sourceRoot":"","sources":["../src/NodeRegistry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,10 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Hosts mirror ECS entities into renderer objects (sprites, containers,
|
|
5
5
|
* meshes, DOM elements). The registry owns the bookkeeping every game
|
|
6
|
-
* repeats: one node per entity, attaching new nodes to the scene
|
|
7
|
-
* and destroying them when the entity
|
|
8
|
-
* and teardown. It does not import or
|
|
9
|
-
* detaching mean is passed to `make`.
|
|
6
|
+
* repeats: one node per entity, attaching new nodes to the scene (or under
|
|
7
|
+
* another entity's node), detaching and destroying them when the entity
|
|
8
|
+
* loses its renderable or is despawned, and teardown. It does not import or
|
|
9
|
+
* assume any renderer: what attaching and detaching mean is passed to `make`.
|
|
10
10
|
*
|
|
11
11
|
* Nodes are keyed by the entity's numeric value, so an `EntityId`, a query
|
|
12
12
|
* match's `entity.id`, or a stored `Handle` all address the same node.
|
|
@@ -16,57 +16,154 @@
|
|
|
16
16
|
*
|
|
17
17
|
* @example
|
|
18
18
|
* ```ts
|
|
19
|
-
*
|
|
20
|
-
*
|
|
19
|
+
* // Three.js, reduced to what the registry touches:
|
|
20
|
+
* interface Geometry { dispose(): void }
|
|
21
|
+
* declare class Mesh {
|
|
22
|
+
* constructor(geometry: Geometry, material: object)
|
|
23
|
+
* readonly geometry: Geometry
|
|
24
|
+
* add(child: Mesh): void
|
|
25
|
+
* removeFromParent(): void
|
|
26
|
+
* }
|
|
27
|
+
* declare const scene: { add(mesh: Mesh): void }
|
|
28
|
+
* declare const geometry: Geometry
|
|
29
|
+
* declare const turretGeometry: Geometry
|
|
30
|
+
* declare const material: object
|
|
31
|
+
* declare const tank: Entity.EntityId<typeof Game.schema>
|
|
32
|
+
* declare const turret: Entity.EntityId<typeof Game.schema>
|
|
33
|
+
* declare const despawned: Entity.EntityId<typeof Game.schema>
|
|
34
|
+
* // ---cut---
|
|
35
|
+
* const meshes = NodeRegistry.make({
|
|
36
|
+
* attach: (mesh: Mesh, parent) => (parent ?? scene).add(mesh),
|
|
21
37
|
* detach: (mesh) => {
|
|
22
|
-
*
|
|
38
|
+
* mesh.removeFromParent()
|
|
23
39
|
* mesh.geometry.dispose()
|
|
24
40
|
* }
|
|
25
41
|
* })
|
|
26
|
-
* meshes.ensure(
|
|
27
|
-
* meshes.
|
|
42
|
+
* meshes.ensure(tank, () => new Mesh(geometry, material))
|
|
43
|
+
* meshes.ensure(turret, () => new Mesh(turretGeometry, material), tank) // under the tank's node
|
|
44
|
+
* meshes.remove(despawned)
|
|
28
45
|
* meshes.clear()
|
|
29
46
|
* ```
|
|
30
47
|
*/
|
|
31
48
|
/**
|
|
32
49
|
* Creates a registry with explicit attach/detach behavior: `attach` adds a
|
|
33
|
-
*
|
|
34
|
-
* packages build on this (for example `PixiNodeRegistry.make`
|
|
35
|
-
* `@typeonce/ritmo-pixi`).
|
|
50
|
+
* node under its parent node or at the root, `detach` removes and releases
|
|
51
|
+
* it. Renderer packages build on this (for example `PixiNodeRegistry.make`
|
|
52
|
+
* in `@typeonce/ritmo-pixi`).
|
|
36
53
|
*/
|
|
37
54
|
export const make = (options) => {
|
|
38
55
|
const nodes = new Map();
|
|
56
|
+
// The tree: each nested node's parent, and each parent's nested children.
|
|
57
|
+
const parents = new Map();
|
|
58
|
+
const children = new Map();
|
|
59
|
+
const link = (entity, parent) => {
|
|
60
|
+
parents.set(entity, parent);
|
|
61
|
+
const siblings = children.get(parent);
|
|
62
|
+
if (siblings === undefined)
|
|
63
|
+
children.set(parent, new Set([entity]));
|
|
64
|
+
else
|
|
65
|
+
siblings.add(entity);
|
|
66
|
+
};
|
|
67
|
+
const unlink = (entity) => {
|
|
68
|
+
const parent = parents.get(entity);
|
|
69
|
+
if (parent === undefined)
|
|
70
|
+
return;
|
|
71
|
+
parents.delete(entity);
|
|
72
|
+
const siblings = children.get(parent);
|
|
73
|
+
siblings?.delete(entity);
|
|
74
|
+
if (siblings?.size === 0)
|
|
75
|
+
children.delete(parent);
|
|
76
|
+
};
|
|
77
|
+
// Whether `entity`'s node is `ancestor`'s node or attached somewhere under it.
|
|
78
|
+
const nestedUnder = (entity, ancestor) => {
|
|
79
|
+
for (let current = entity; current !== undefined; current = parents.get(current)) {
|
|
80
|
+
if (current === ancestor)
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
};
|
|
85
|
+
// Moves every node nested under `entity`'s node to the root.
|
|
86
|
+
const release = (entity) => {
|
|
87
|
+
const nested = children.get(entity);
|
|
88
|
+
if (nested === undefined)
|
|
89
|
+
return;
|
|
90
|
+
children.delete(entity);
|
|
91
|
+
for (const child of nested) {
|
|
92
|
+
parents.delete(child);
|
|
93
|
+
const node = nodes.get(child);
|
|
94
|
+
if (node !== undefined)
|
|
95
|
+
options.attach(node, undefined);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
// Detaches a node after the nodes nested under it, depth first.
|
|
99
|
+
const detachTree = (entity) => {
|
|
100
|
+
const nested = children.get(entity);
|
|
101
|
+
if (nested !== undefined)
|
|
102
|
+
for (const child of nested)
|
|
103
|
+
detachTree(child);
|
|
104
|
+
const node = nodes.get(entity);
|
|
105
|
+
if (node !== undefined)
|
|
106
|
+
options.detach(node);
|
|
107
|
+
};
|
|
39
108
|
return {
|
|
40
109
|
get size() {
|
|
41
110
|
return nodes.size;
|
|
42
111
|
},
|
|
112
|
+
parents,
|
|
43
113
|
get(entity) {
|
|
44
114
|
return nodes.get(entity);
|
|
45
115
|
},
|
|
46
|
-
ensure(entity, create) {
|
|
116
|
+
ensure(entity, create, parent) {
|
|
47
117
|
const existing = nodes.get(entity);
|
|
48
118
|
if (existing !== undefined) {
|
|
49
119
|
return existing;
|
|
50
120
|
}
|
|
51
121
|
const node = create();
|
|
122
|
+
const parentNode = parent === undefined ? undefined : nodes.get(parent);
|
|
52
123
|
nodes.set(entity, node);
|
|
53
|
-
|
|
124
|
+
if (parent !== undefined && parentNode !== undefined)
|
|
125
|
+
link(entity, parent);
|
|
126
|
+
options.attach(node, parentNode);
|
|
54
127
|
return node;
|
|
55
128
|
},
|
|
129
|
+
reparent(entity, parent) {
|
|
130
|
+
const node = nodes.get(entity);
|
|
131
|
+
if (node === undefined) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
const parentNode = parent === undefined ? undefined : nodes.get(parent);
|
|
135
|
+
const next = parentNode === undefined ? undefined : parent;
|
|
136
|
+
if (parents.get(entity) === next) {
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
if (next !== undefined && nestedUnder(next, entity)) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
unlink(entity);
|
|
143
|
+
if (next !== undefined)
|
|
144
|
+
link(entity, next);
|
|
145
|
+
options.attach(node, parentNode);
|
|
146
|
+
return true;
|
|
147
|
+
},
|
|
56
148
|
remove(entity) {
|
|
57
149
|
const node = nodes.get(entity);
|
|
58
150
|
if (node === undefined) {
|
|
59
151
|
return false;
|
|
60
152
|
}
|
|
153
|
+
release(entity);
|
|
154
|
+
unlink(entity);
|
|
61
155
|
nodes.delete(entity);
|
|
62
156
|
options.detach(node);
|
|
63
157
|
return true;
|
|
64
158
|
},
|
|
65
159
|
clear() {
|
|
66
|
-
for (const
|
|
67
|
-
|
|
160
|
+
for (const entity of nodes.keys()) {
|
|
161
|
+
if (!parents.has(entity))
|
|
162
|
+
detachTree(entity);
|
|
68
163
|
}
|
|
69
164
|
nodes.clear();
|
|
165
|
+
parents.clear();
|
|
166
|
+
children.clear();
|
|
70
167
|
},
|
|
71
168
|
entries() {
|
|
72
169
|
return nodes.entries();
|
package/dist/NodeRegistry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeRegistry.js","sourceRoot":"","sources":["../src/NodeRegistry.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"NodeRegistry.js","sourceRoot":"","sources":["../src/NodeRegistry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AA4EH;;;;;GAK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ACd,CAAC;YACD,MAAM,CAAC,MAAM,CAAC,CAAA;YACd,IAAI,IAAI,KAAK,SAAS;gBAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;YAC1C,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,CAAA;YAChC,OAAO,IAAI,CAAA;QACb,CAAC;QACD,MAAM,CAAC,MAAM;YACX,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;YAC9B,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gBACvB,OAAO,KAAK,CAAA;YACd,CAAC;YACD,OAAO,CAAC,MAAM,CAAC,CAAA;YACf,MAAM,CAAC,MAAM,CAAC,CAAA;YACd,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;YACpB,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;YACpB,OAAO,IAAI,CAAA;QACb,CAAC;QACD,KAAK;YACH,KAAK,MAAM,MAAM,IAAI,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;gBAClC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC;oBAAE,UAAU,CAAC,MAAM,CAAC,CAAA;YAC9C,CAAC;YACD,KAAK,CAAC,KAAK,EAAE,CAAA;YACb,OAAO,CAAC,KAAK,EAAE,CAAA;YACf,QAAQ,CAAC,KAAK,EAAE,CAAA;QAClB,CAAC;QACD,OAAO;YACL,OAAO,KAAK,CAAC,OAAO,EAAE,CAAA;QACxB,CAAC;KACF,CAAA;AACH,CAAC,CAAA"}
|
package/dist/RenderSync.d.ts
CHANGED
|
@@ -26,6 +26,12 @@
|
|
|
26
26
|
* services declared in `services`. Those resources and services become
|
|
27
27
|
* runtime requirements like the registry itself.
|
|
28
28
|
*
|
|
29
|
+
* With `hierarchy` (a `Descriptor.Hierarchy` of the game), a child's node is
|
|
30
|
+
* attached under its parent's node instead of the root, and moved whenever
|
|
31
|
+
* its parent changes, so the renderer composes the transforms: `apply` sets
|
|
32
|
+
* the child's own (local) transform, relative to the parent's node, and no
|
|
33
|
+
* world-transform math happens in the system.
|
|
34
|
+
*
|
|
29
35
|
* Its spec is erased, so it does not count as a writer for
|
|
30
36
|
* `Game.System.thisTick(...)` order checks.
|
|
31
37
|
*
|
|
@@ -34,6 +40,10 @@
|
|
|
34
40
|
*
|
|
35
41
|
* @example
|
|
36
42
|
* ```ts
|
|
43
|
+
* import { Sprite, type Texture } from "pixi.js"
|
|
44
|
+
*
|
|
45
|
+
* const Assets = Descriptor.Service<{ readonly textures: Readonly<Record<string, Texture>> }>()("Game/Assets")
|
|
46
|
+
* // ---cut---
|
|
37
47
|
* const Sprites = Descriptor.Service<NodeRegistry.NodeRegistry<Sprite>>()("Game/Sprites")
|
|
38
48
|
*
|
|
39
49
|
* const Render = RenderSync.system(Game, {
|
|
@@ -46,18 +56,26 @@
|
|
|
46
56
|
* apply: (sprite, { transform }) => sprite.position.set(transform.x, transform.y)
|
|
47
57
|
* })
|
|
48
58
|
*
|
|
49
|
-
* const update = Game.Schedule(
|
|
59
|
+
* const update = Game.Schedule(Move, Game.Schedule.applyDeferred(), Render)
|
|
50
60
|
* ```
|
|
51
61
|
*/
|
|
52
62
|
import type { Descriptor } from "@typeonce/ritmo/Descriptor";
|
|
53
63
|
import type { Condition, ConditionNeedsFromConditions } from "@typeonce/ritmo/Machine";
|
|
54
64
|
import type * as Entity from "@typeonce/ritmo/Entity";
|
|
65
|
+
import type { Relation } from "@typeonce/ritmo/Relation";
|
|
55
66
|
import type { OptionalReadAccess, OptionalReadCell, ReadAccess, ReadCell, ReadonlyValue } from "@typeonce/ritmo/Query";
|
|
56
67
|
import type { Schema } from "@typeonce/ritmo/Schema";
|
|
57
68
|
import type { NodeRegistry } from "./NodeRegistry.ts";
|
|
58
69
|
type RegistryService = Descriptor<"service", string, NodeRegistry<any>>;
|
|
59
70
|
type ServiceDescriptor = Descriptor<"service", string, any>;
|
|
60
71
|
type ResourceDescriptor = Descriptor<"resource", string, any>;
|
|
72
|
+
/**
|
|
73
|
+
* The `hierarchy` option: a `Descriptor.Hierarchy` relation of the game's
|
|
74
|
+
* schema, or a message naming the fix for anything else.
|
|
75
|
+
*/
|
|
76
|
+
export type HierarchyOption<S extends Schema.Any, H> = [
|
|
77
|
+
H
|
|
78
|
+
] extends [undefined] ? H : [H] extends [Schema.RelationDescriptor<S>] ? [H] extends [Relation.Hierarchy] ? H : "hierarchy must be a Descriptor.Hierarchy relation: a Descriptor.Relation does not make a parent's node hold its children" : "hierarchy must be a relation of the game's schema: add it to Schema.fragment({ relations })";
|
|
61
79
|
/**
|
|
62
80
|
* The node type held by a registry service.
|
|
63
81
|
*/
|
|
@@ -89,7 +107,7 @@ export interface NodeContext<S extends Schema.Any, Root, Renderable extends Sche
|
|
|
89
107
|
readonly services: ServiceValues<Services>;
|
|
90
108
|
}
|
|
91
109
|
/** The options of `system`. */
|
|
92
|
-
export interface Options<S extends Schema.Any, Root, Renderable extends Schema.ComponentDescriptor<S>, Transform extends Schema.ComponentDescriptor<S> | undefined, Registry extends RegistryService, Select extends Selection<S>, Services extends Readonly<Record<string, ServiceDescriptor>>, Resources extends Readonly<Record<string, Schema.ResourceDescriptor<S>>> = {}, When extends ReadonlyArray<Condition> = readonly []> {
|
|
110
|
+
export interface Options<S extends Schema.Any, Root, Renderable extends Schema.ComponentDescriptor<S>, Transform extends Schema.ComponentDescriptor<S> | undefined, Registry extends RegistryService, Select extends Selection<S>, Services extends Readonly<Record<string, ServiceDescriptor>>, Resources extends Readonly<Record<string, Schema.ResourceDescriptor<S>>> = {}, When extends ReadonlyArray<Condition> = readonly [], Hierarchy extends Relation.Any | undefined = undefined> {
|
|
93
111
|
/** The system name. */
|
|
94
112
|
readonly name: string;
|
|
95
113
|
/** The component whose presence means "this entity has a node". */
|
|
@@ -122,6 +140,21 @@ export interface Options<S extends Schema.Any, Root, Renderable extends Schema.C
|
|
|
122
140
|
* seen when the system runs again.
|
|
123
141
|
*/
|
|
124
142
|
readonly when?: When;
|
|
143
|
+
/**
|
|
144
|
+
* A `Descriptor.Hierarchy` of the game whose tree the nodes follow: a
|
|
145
|
+
* child's node is attached under its parent's node (`attach(node, parent)`)
|
|
146
|
+
* when the parent has a node in this registry, and at the root otherwise,
|
|
147
|
+
* and is moved when the parent changes, is removed, or gains or loses its
|
|
148
|
+
* node. Only the direct parent counts: a child of a parent without a node
|
|
149
|
+
* sits at the root, not under a grandparent. `apply` then receives the
|
|
150
|
+
* child's own components, which the renderer composes with its parent's
|
|
151
|
+
* node: a child's transform is local to its parent. Despawning a parent
|
|
152
|
+
* despawns its children (hierarchies despawn together), so their nodes go
|
|
153
|
+
* with it; a parent that only loses its node hands its children's nodes
|
|
154
|
+
* back to the root. Each run checks every renderable entity that has a
|
|
155
|
+
* parent, so the cost grows with the number of children, not of changes.
|
|
156
|
+
*/
|
|
157
|
+
readonly hierarchy?: HierarchyOption<S, Hierarchy>;
|
|
125
158
|
/** Builds the node for a new renderable entity. */
|
|
126
159
|
readonly create: (context: NodeContext<S, Root, Renderable, Transform, Select, Services, Resources>) => NodeOf<Registry>;
|
|
127
160
|
/**
|
|
@@ -140,7 +173,7 @@ export type RenderSystem<S extends Schema.Any, Root, Needs extends ServiceDescri
|
|
|
140
173
|
* applies, and destroys that component's nodes. To place nodes between fixed
|
|
141
174
|
* steps every frame, add `interpolate` on the same registry after it.
|
|
142
175
|
*/
|
|
143
|
-
export declare const system: <S extends Schema.Any, Root, const Renderable extends Schema.ComponentDescriptor<S>, const Registry extends RegistryService, const Select extends Selection<S> = {}, const Services extends Readonly<Record<string, ServiceDescriptor>> = {}, const Resources extends Readonly<Record<string, Schema.ResourceDescriptor<S>>> = {}, const When extends ReadonlyArray<Condition<Root>> = readonly [], const Transform extends Schema.ComponentDescriptor<S> | undefined = undefined>(Game: Schema.Game<S, Root>, options: Options<S, Root, Renderable, Transform, Registry, Select, Services, Resources, When>) => RenderSystem<S, Root, Registry | Services[keyof Services] | Resources[keyof Resources] | ConditionNeedsFromConditions<When>>;
|
|
176
|
+
export declare const system: <S extends Schema.Any, Root, const Renderable extends Schema.ComponentDescriptor<S>, const Registry extends RegistryService, const Select extends Selection<S> = {}, const Services extends Readonly<Record<string, ServiceDescriptor>> = {}, const Resources extends Readonly<Record<string, Schema.ResourceDescriptor<S>>> = {}, const When extends ReadonlyArray<Condition<Root>> = readonly [], const Transform extends Schema.ComponentDescriptor<S> | undefined = undefined, const Hierarchy extends Relation.Any | undefined = undefined>(Game: Schema.Game<S, Root>, options: Options<S, Root, Renderable, Transform, Registry, Select, Services, Resources, When, Hierarchy>) => RenderSystem<S, Root, Registry | Services[keyof Services] | Resources[keyof Resources] | ConditionNeedsFromConditions<When>>;
|
|
144
177
|
/** A 2D position, the value `interpolate` blends when no `blend` is given. */
|
|
145
178
|
export interface Planar {
|
|
146
179
|
readonly x: number;
|
|
@@ -221,6 +254,18 @@ interface InterpolateBase<S extends Schema.Any, Root, Previous extends Schema.Co
|
|
|
221
254
|
*
|
|
222
255
|
* @example
|
|
223
256
|
* ```ts
|
|
257
|
+
* import type { Sprite } from "pixi.js"
|
|
258
|
+
*
|
|
259
|
+
* interface Mesh { readonly position: { set(x: number, y: number, z: number): void } }
|
|
260
|
+
* const PrevPosition = Descriptor.Component<{ x: number; y: number }>()("Game/PrevPosition")
|
|
261
|
+
* const Position3 = Descriptor.Component<{ x: number; y: number; z: number }>()("Game/Position3")
|
|
262
|
+
* const PrevPosition3 = Descriptor.Component<{ x: number; y: number; z: number }>()("Game/PrevPosition3")
|
|
263
|
+
* const Game = Schema.bind(Schema.fragment({ components: { Position, PrevPosition, Position3, PrevPosition3 } }))
|
|
264
|
+
* const Sprites = Descriptor.Service<NodeRegistry.NodeRegistry<Sprite>>()("Game/Sprites")
|
|
265
|
+
* const Meshes = Descriptor.Service<NodeRegistry.NodeRegistry<Mesh>>()("Game/Meshes")
|
|
266
|
+
* // The loop's render alpha, provided by the host each frame:
|
|
267
|
+
* const RenderClock = Descriptor.Service<{ readonly alpha: number }>()("Game/RenderClock")
|
|
268
|
+
* // ---cut---
|
|
224
269
|
* const Interpolate = RenderSync.interpolate(Game, {
|
|
225
270
|
* name: "Game/Interpolate",
|
|
226
271
|
* registry: Sprites,
|
|
@@ -229,7 +274,7 @@ interface InterpolateBase<S extends Schema.Any, Root, Previous extends Schema.Co
|
|
|
229
274
|
* clock: RenderClock,
|
|
230
275
|
* place: (sprite, { x, y }) => sprite.position.set(x, y)
|
|
231
276
|
* })
|
|
232
|
-
* const render = Game.Schedule(
|
|
277
|
+
* const render = Game.Schedule(Interpolate) // after the RenderSync.system that creates the nodes
|
|
233
278
|
*
|
|
234
279
|
* // 3D: blend every coordinate.
|
|
235
280
|
* RenderSync.interpolate(Game, {
|
package/dist/RenderSync.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RenderSync.d.ts","sourceRoot":"","sources":["../src/RenderSync.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"RenderSync.d.ts","sourceRoot":"","sources":["../src/RenderSync.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAA;AAC5D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B,EAAE,MAAM,yBAAyB,CAAA;AACtF,OAAO,KAAK,KAAK,MAAM,MAAM,wBAAwB,CAAA;AACrD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAA;AACxD,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,UAAU,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AACtH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AACpD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAErD,KAAK,eAA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;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,IAAI,CAAA;CAC7G;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,eAAO,MAAM,WAAW,GACtB,CAAC,SAAS,MAAM,CAAC,GAAG,EACpB,IAAI,EACJ,KAAK,CAAC,QAAQ,SAAS,MAAM,CAAC,mBAAmB,CAAC,CAAC,CAAC,EACpD,KAAK,CAAC,OAAO,SAAS,MAAM,CAAC,mBAAmB,CAAC,CAAC,CAAC,EACnD,KAAK,CAAC,QAAQ,SAAS,eAAe,EACtC,KAAK,CAAC,KAAK,SAAS,iBAAiB,EACrC,KAAK,CAAC,IAAI,SAAS,aAAa,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,GAAG,SAAS,EAAE,EAC/D,MAAM,GAAG,OAAO,QAEV,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,IAAI,CAAC,WACjB,kBAAkB,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,CAAC,KACrF,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,GAAG,KAAK,GAAG,4BAA4B,CAAC,IAAI,CAAC,CAkC7E,CAAA"}
|
package/dist/RenderSync.js
CHANGED
|
@@ -10,10 +10,12 @@ export const system = (Game, options) => {
|
|
|
10
10
|
const Query = Game.Query;
|
|
11
11
|
const anyOf = Game.Query.anyOf;
|
|
12
12
|
const transform = options.transform;
|
|
13
|
+
const hierarchy = options.hierarchy;
|
|
13
14
|
const selection = {
|
|
14
15
|
...options.select,
|
|
15
16
|
__renderable: Game.Query.read(options.renderable),
|
|
16
|
-
...(transform === undefined ? {} : { __transform: Game.Query.read(transform) })
|
|
17
|
+
...(transform === undefined ? {} : { __transform: Game.Query.read(transform) }),
|
|
18
|
+
...(hierarchy === undefined ? {} : { __parent: Game.Query.optionalRelation(hierarchy) })
|
|
17
19
|
};
|
|
18
20
|
const services = { __registry: Game.System.service(options.registry) };
|
|
19
21
|
for (const [key, descriptor] of Object.entries(options.services ?? {})) {
|
|
@@ -60,13 +62,18 @@ export const system = (Game, options) => {
|
|
|
60
62
|
selection: { __renderable: Game.Query.read(options.renderable) },
|
|
61
63
|
with: required.filter((descriptor) => descriptor !== options.renderable)
|
|
62
64
|
});
|
|
65
|
+
// Every renderable entity with a parent: checked each run against where
|
|
66
|
+
// its node is attached. There is no per-reader filter for relation edges.
|
|
67
|
+
if (hierarchy !== undefined) {
|
|
68
|
+
queries["parented"] = Query({ selection: { __parent: Game.Query.readRelation(hierarchy) }, with: [options.renderable] });
|
|
69
|
+
}
|
|
63
70
|
return System(options.name, {
|
|
64
71
|
queries,
|
|
65
72
|
removed: removedReads,
|
|
66
73
|
resources,
|
|
67
74
|
services,
|
|
68
75
|
when: options.when ?? []
|
|
69
|
-
}, ({ queries, removed, resources, services }) => {
|
|
76
|
+
}, ({ queries, removed, resources, services, lookup }) => {
|
|
70
77
|
const registry = services.__registry;
|
|
71
78
|
// Destroy first: an entity that lost a required component has no node,
|
|
72
79
|
// and one that lost it and got it back since the last run gets a fresh one.
|
|
@@ -92,10 +99,58 @@ export const system = (Game, options) => {
|
|
|
92
99
|
}
|
|
93
100
|
for (const { entity, data } of queries["sync"].each()) {
|
|
94
101
|
const context = contextOf(entity, data, services, resources);
|
|
95
|
-
|
|
102
|
+
// A child met before its parent's node exists starts at the root; the
|
|
103
|
+
// pass below moves it under the parent in this same run.
|
|
104
|
+
const parent = hierarchy === undefined || !data["__parent"].present ? undefined : data["__parent"].get();
|
|
105
|
+
options.apply(registry.ensure(entity.id, () => options.create(context), parent), context);
|
|
96
106
|
}
|
|
107
|
+
if (hierarchy !== undefined)
|
|
108
|
+
reattach(registry, queries["parented"], lookup, hierarchy);
|
|
97
109
|
});
|
|
98
110
|
};
|
|
111
|
+
// Moves every node whose parent changed since it was attached: the entity
|
|
112
|
+
// gained, lost, or changed its parent, or the parent gained or lost its node.
|
|
113
|
+
// There is no per-reader filter for relation edges, so every parented entity
|
|
114
|
+
// is checked each run. Leaving parents go first, then the rest in depth
|
|
115
|
+
// order (parents before children), so a node never moves under a node that
|
|
116
|
+
// is still inside it and each move happens once.
|
|
117
|
+
const reattach = (registry, parented, lookup, hierarchy) => {
|
|
118
|
+
let moves;
|
|
119
|
+
// Nested nodes whose entity still has a parent; any other nested node lost it.
|
|
120
|
+
let stillParented = 0;
|
|
121
|
+
for (const { entity, data } of parented.each()) {
|
|
122
|
+
const attached = registry.parents.get(entity.id);
|
|
123
|
+
if (attached !== undefined)
|
|
124
|
+
stillParented++;
|
|
125
|
+
const parent = data.__parent.get();
|
|
126
|
+
if (attached === parent)
|
|
127
|
+
continue;
|
|
128
|
+
const wanted = registry.get(parent) === undefined ? undefined : parent;
|
|
129
|
+
if (attached === wanted || registry.get(entity.id) === undefined)
|
|
130
|
+
continue;
|
|
131
|
+
(moves ??= []).push({ entity: entity.id, parent: wanted, depth: 0 });
|
|
132
|
+
}
|
|
133
|
+
if (stillParented < registry.parents.size) {
|
|
134
|
+
const toRoot = [];
|
|
135
|
+
for (const [entity] of registry.parents) {
|
|
136
|
+
if (!lookup.parent(entity, hierarchy).ok)
|
|
137
|
+
toRoot.push(entity);
|
|
138
|
+
}
|
|
139
|
+
for (const entity of toRoot)
|
|
140
|
+
registry.reparent(entity, undefined);
|
|
141
|
+
}
|
|
142
|
+
if (moves === undefined)
|
|
143
|
+
return;
|
|
144
|
+
if (moves.length > 1) {
|
|
145
|
+
for (const move of moves) {
|
|
146
|
+
const ancestors = lookup.ancestors(move.entity, hierarchy);
|
|
147
|
+
move.depth = ancestors.ok ? ancestors.value.length : 0;
|
|
148
|
+
}
|
|
149
|
+
moves.sort((a, b) => a.depth - b.depth);
|
|
150
|
+
}
|
|
151
|
+
for (const move of moves)
|
|
152
|
+
registry.reparent(move.entity, move.parent);
|
|
153
|
+
};
|
|
99
154
|
/**
|
|
100
155
|
* Builds the system that draws moving entities between fixed steps.
|
|
101
156
|
*
|
|
@@ -112,6 +167,18 @@ export const system = (Game, options) => {
|
|
|
112
167
|
*
|
|
113
168
|
* @example
|
|
114
169
|
* ```ts
|
|
170
|
+
* import type { Sprite } from "pixi.js"
|
|
171
|
+
*
|
|
172
|
+
* interface Mesh { readonly position: { set(x: number, y: number, z: number): void } }
|
|
173
|
+
* const PrevPosition = Descriptor.Component<{ x: number; y: number }>()("Game/PrevPosition")
|
|
174
|
+
* const Position3 = Descriptor.Component<{ x: number; y: number; z: number }>()("Game/Position3")
|
|
175
|
+
* const PrevPosition3 = Descriptor.Component<{ x: number; y: number; z: number }>()("Game/PrevPosition3")
|
|
176
|
+
* const Game = Schema.bind(Schema.fragment({ components: { Position, PrevPosition, Position3, PrevPosition3 } }))
|
|
177
|
+
* const Sprites = Descriptor.Service<NodeRegistry.NodeRegistry<Sprite>>()("Game/Sprites")
|
|
178
|
+
* const Meshes = Descriptor.Service<NodeRegistry.NodeRegistry<Mesh>>()("Game/Meshes")
|
|
179
|
+
* // The loop's render alpha, provided by the host each frame:
|
|
180
|
+
* const RenderClock = Descriptor.Service<{ readonly alpha: number }>()("Game/RenderClock")
|
|
181
|
+
* // ---cut---
|
|
115
182
|
* const Interpolate = RenderSync.interpolate(Game, {
|
|
116
183
|
* name: "Game/Interpolate",
|
|
117
184
|
* registry: Sprites,
|
|
@@ -120,7 +187,7 @@ export const system = (Game, options) => {
|
|
|
120
187
|
* clock: RenderClock,
|
|
121
188
|
* place: (sprite, { x, y }) => sprite.position.set(x, y)
|
|
122
189
|
* })
|
|
123
|
-
* const render = Game.Schedule(
|
|
190
|
+
* const render = Game.Schedule(Interpolate) // after the RenderSync.system that creates the nodes
|
|
124
191
|
*
|
|
125
192
|
* // 3D: blend every coordinate.
|
|
126
193
|
* RenderSync.interpolate(Game, {
|
package/dist/RenderSync.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RenderSync.js","sourceRoot":"","sources":["../src/RenderSync.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"RenderSync.js","sourceRoot":"","sources":["../src/RenderSync.ts"],"names":[],"mappings":"AAsNA;;;;GAIG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CAYpB,IAA0B,EAC1B,OAAwG,EACsB,EAAE;IAGhI,wEAAwE;IACxE,0EAA0E;IAC1E,MAAM,MAAM,GAAG,IAAI,CAAC,MAAyF,CAAA;IAC7G,MAAM,KAAK,GAAG,IAAI,CAAC,KAA4C,CAAA;IAC/D,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAiE,CAAA;IAC1F,MAAM,SAAS,GAAG,OAAO,CAAC,SAAsD,CAAA;IAChF,MAAM,SAAS,GAAG,OAAO,CAAC,SAAqD,CAAA;IAC/E,MAAM,SAAS,GAAG;QAChB,GAAG,OAAO,CAAC,MAAM;QACjB,YAAY,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC;QACjD,GAAG,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;QAC/E,GAAG,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,SAAS,CAAC,EAAE,CAAC;KACzF,CAAA;IACD,MAAM,QAAQ,GAA4B,EAAE,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAA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E,CAAC;gBACxB,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,KAAK,CAAC,EAAE,MAAM,CAAC,EAAE,CAAC,CAAA;gBAC7E,SAAQ;YACV,CAAC;YACD,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAY,CAAA;YAC1C,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,EAAY,CAAA;YACvC,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,KAAK,EAAoB,EAAE,MAAM,CAAC,EAAE,CAAC,CAAA;QACxH,CAAC;IACH,CAAC,CAAiF,CAAA;AACpF,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typeonce/ritmo-render",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "Renderer-agnostic entity-to-node mirroring for Ritmo",
|
|
5
5
|
"author": "Sandro Maglione",
|
|
6
6
|
"license": "MIT",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"provenance": true
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@typeonce/ritmo": "^0.
|
|
49
|
+
"@typeonce/ritmo": "^0.16.0"
|
|
50
50
|
},
|
|
51
51
|
"engines": {
|
|
52
52
|
"node": ">=20"
|
package/src/NodeRegistry.ts
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Hosts mirror ECS entities into renderer objects (sprites, containers,
|
|
5
5
|
* meshes, DOM elements). The registry owns the bookkeeping every game
|
|
6
|
-
* repeats: one node per entity, attaching new nodes to the scene
|
|
7
|
-
* and destroying them when the entity
|
|
8
|
-
* and teardown. It does not import or
|
|
9
|
-
* detaching mean is passed to `make`.
|
|
6
|
+
* repeats: one node per entity, attaching new nodes to the scene (or under
|
|
7
|
+
* another entity's node), detaching and destroying them when the entity
|
|
8
|
+
* loses its renderable or is despawned, and teardown. It does not import or
|
|
9
|
+
* assume any renderer: what attaching and detaching mean is passed to `make`.
|
|
10
10
|
*
|
|
11
11
|
* Nodes are keyed by the entity's numeric value, so an `EntityId`, a query
|
|
12
12
|
* match's `entity.id`, or a stored `Handle` all address the same node.
|
|
@@ -16,15 +16,32 @@
|
|
|
16
16
|
*
|
|
17
17
|
* @example
|
|
18
18
|
* ```ts
|
|
19
|
-
*
|
|
20
|
-
*
|
|
19
|
+
* // Three.js, reduced to what the registry touches:
|
|
20
|
+
* interface Geometry { dispose(): void }
|
|
21
|
+
* declare class Mesh {
|
|
22
|
+
* constructor(geometry: Geometry, material: object)
|
|
23
|
+
* readonly geometry: Geometry
|
|
24
|
+
* add(child: Mesh): void
|
|
25
|
+
* removeFromParent(): void
|
|
26
|
+
* }
|
|
27
|
+
* declare const scene: { add(mesh: Mesh): void }
|
|
28
|
+
* declare const geometry: Geometry
|
|
29
|
+
* declare const turretGeometry: Geometry
|
|
30
|
+
* declare const material: object
|
|
31
|
+
* declare const tank: Entity.EntityId<typeof Game.schema>
|
|
32
|
+
* declare const turret: Entity.EntityId<typeof Game.schema>
|
|
33
|
+
* declare const despawned: Entity.EntityId<typeof Game.schema>
|
|
34
|
+
* // ---cut---
|
|
35
|
+
* const meshes = NodeRegistry.make({
|
|
36
|
+
* attach: (mesh: Mesh, parent) => (parent ?? scene).add(mesh),
|
|
21
37
|
* detach: (mesh) => {
|
|
22
|
-
*
|
|
38
|
+
* mesh.removeFromParent()
|
|
23
39
|
* mesh.geometry.dispose()
|
|
24
40
|
* }
|
|
25
41
|
* })
|
|
26
|
-
* meshes.ensure(
|
|
27
|
-
* meshes.
|
|
42
|
+
* meshes.ensure(tank, () => new Mesh(geometry, material))
|
|
43
|
+
* meshes.ensure(turret, () => new Mesh(turretGeometry, material), tank) // under the tank's node
|
|
44
|
+
* meshes.remove(despawned)
|
|
28
45
|
* meshes.clear()
|
|
29
46
|
* ```
|
|
30
47
|
*/
|
|
@@ -40,6 +57,12 @@ export type EntityKey = number
|
|
|
40
57
|
* as `PixiNodeRegistry.make`). Provide it as a runtime service and hand that
|
|
41
58
|
* service to `RenderSync.system`, which fills and empties it; host code reads
|
|
42
59
|
* it with `get`.
|
|
60
|
+
*
|
|
61
|
+
* A node sits at the root of the scene or under another entity's node (its
|
|
62
|
+
* parent): `RenderSync.system`'s `hierarchy` option keeps that in step with
|
|
63
|
+
* a `Descriptor.Hierarchy`. Removing a parent's node first moves the nodes
|
|
64
|
+
* attached under it back to the root, so an entity's node is only ever
|
|
65
|
+
* destroyed by removing that entity.
|
|
43
66
|
*/
|
|
44
67
|
export interface NodeRegistry<Node> {
|
|
45
68
|
/** Number of live nodes. */
|
|
@@ -47,15 +70,31 @@ export interface NodeRegistry<Node> {
|
|
|
47
70
|
/** The entity's node, or `undefined` when it has none. Use `ensure` to create one. */
|
|
48
71
|
get(entity: EntityKey): Node | undefined
|
|
49
72
|
/**
|
|
50
|
-
* Returns the entity's node, creating and attaching it first when missing
|
|
73
|
+
* Returns the entity's node, creating and attaching it first when missing:
|
|
74
|
+
* under `parent`'s node when `parent` is given and has one, else at the
|
|
75
|
+
* root. An existing node is returned where it is; use `reparent` to move it.
|
|
76
|
+
*/
|
|
77
|
+
ensure(entity: EntityKey, create: () => Node, parent?: EntityKey): Node
|
|
78
|
+
/**
|
|
79
|
+
* Moves the entity's existing node under `parent`'s node, or to the root
|
|
80
|
+
* when `parent` is `undefined` or has no node. Does nothing when the node
|
|
81
|
+
* is already there. Returns `false`, moving nothing, when the entity has
|
|
82
|
+
* no node or `parent`'s node is the entity's own or attached under it (a
|
|
83
|
+
* loop); `true` otherwise.
|
|
51
84
|
*/
|
|
52
|
-
|
|
85
|
+
reparent(entity: EntityKey, parent: EntityKey | undefined): boolean
|
|
53
86
|
/**
|
|
54
|
-
*
|
|
87
|
+
* Each node attached under another entity's node: the entity's key to its
|
|
88
|
+
* parent's key. Nodes at the root are absent.
|
|
89
|
+
*/
|
|
90
|
+
readonly parents: ReadonlyMap<EntityKey, EntityKey>
|
|
91
|
+
/**
|
|
92
|
+
* Detaches and destroys the entity's node, after moving the nodes attached
|
|
93
|
+
* under it to the root. Returns whether one existed.
|
|
55
94
|
*/
|
|
56
95
|
remove(entity: EntityKey): boolean
|
|
57
96
|
/**
|
|
58
|
-
* Detaches and destroys every node.
|
|
97
|
+
* Detaches and destroys every node, children before their parents.
|
|
59
98
|
*/
|
|
60
99
|
clear(): void
|
|
61
100
|
/** The live nodes with their entity keys. */
|
|
@@ -64,51 +103,131 @@ export interface NodeRegistry<Node> {
|
|
|
64
103
|
|
|
65
104
|
/** What attaching and detaching a node mean for one renderer; see `make`. */
|
|
66
105
|
export interface RegistryOptions<Node> {
|
|
67
|
-
/**
|
|
68
|
-
|
|
69
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Adds a node to the scene: under `parent` (another entity's node), or at
|
|
108
|
+
* the root when `parent` is `undefined`. Called for new nodes and to move
|
|
109
|
+
* an attached node to a new parent, so it must also take a node out of the
|
|
110
|
+
* parent it is in: Pixi's `addChild` and Three.js's `add` do.
|
|
111
|
+
*
|
|
112
|
+
* Declare both parameters. A one-parameter `attach` still compiles but
|
|
113
|
+
* ignores `parent`, so every node lands on the root.
|
|
114
|
+
*/
|
|
115
|
+
readonly attach: (node: Node, parent: Node | undefined) => void
|
|
116
|
+
/**
|
|
117
|
+
* Removes a node from the scene, from the root or the parent node it is
|
|
118
|
+
* attached under, and releases it.
|
|
119
|
+
*/
|
|
70
120
|
readonly detach: (node: Node) => void
|
|
71
121
|
}
|
|
72
122
|
|
|
73
123
|
/**
|
|
74
124
|
* Creates a registry with explicit attach/detach behavior: `attach` adds a
|
|
75
|
-
*
|
|
76
|
-
* packages build on this (for example `PixiNodeRegistry.make`
|
|
77
|
-
* `@typeonce/ritmo-pixi`).
|
|
125
|
+
* node under its parent node or at the root, `detach` removes and releases
|
|
126
|
+
* it. Renderer packages build on this (for example `PixiNodeRegistry.make`
|
|
127
|
+
* in `@typeonce/ritmo-pixi`).
|
|
78
128
|
*/
|
|
79
129
|
export const make = <Node>(options: RegistryOptions<Node>): NodeRegistry<Node> => {
|
|
80
130
|
const nodes = new Map<number, Node>()
|
|
131
|
+
// The tree: each nested node's parent, and each parent's nested children.
|
|
132
|
+
const parents = new Map<number, number>()
|
|
133
|
+
const children = new Map<number, Set<number>>()
|
|
134
|
+
|
|
135
|
+
const link = (entity: number, parent: number): void => {
|
|
136
|
+
parents.set(entity, parent)
|
|
137
|
+
const siblings = children.get(parent)
|
|
138
|
+
if (siblings === undefined) children.set(parent, new Set([entity]))
|
|
139
|
+
else siblings.add(entity)
|
|
140
|
+
}
|
|
141
|
+
const unlink = (entity: number): void => {
|
|
142
|
+
const parent = parents.get(entity)
|
|
143
|
+
if (parent === undefined) return
|
|
144
|
+
parents.delete(entity)
|
|
145
|
+
const siblings = children.get(parent)
|
|
146
|
+
siblings?.delete(entity)
|
|
147
|
+
if (siblings?.size === 0) children.delete(parent)
|
|
148
|
+
}
|
|
149
|
+
// Whether `entity`'s node is `ancestor`'s node or attached somewhere under it.
|
|
150
|
+
const nestedUnder = (entity: number, ancestor: number): boolean => {
|
|
151
|
+
for (let current: number | undefined = entity; current !== undefined; current = parents.get(current)) {
|
|
152
|
+
if (current === ancestor) return true
|
|
153
|
+
}
|
|
154
|
+
return false
|
|
155
|
+
}
|
|
156
|
+
// Moves every node nested under `entity`'s node to the root.
|
|
157
|
+
const release = (entity: number): void => {
|
|
158
|
+
const nested = children.get(entity)
|
|
159
|
+
if (nested === undefined) return
|
|
160
|
+
children.delete(entity)
|
|
161
|
+
for (const child of nested) {
|
|
162
|
+
parents.delete(child)
|
|
163
|
+
const node = nodes.get(child)
|
|
164
|
+
if (node !== undefined) options.attach(node, undefined)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// Detaches a node after the nodes nested under it, depth first.
|
|
168
|
+
const detachTree = (entity: number): void => {
|
|
169
|
+
const nested = children.get(entity)
|
|
170
|
+
if (nested !== undefined) for (const child of nested) detachTree(child)
|
|
171
|
+
const node = nodes.get(entity)
|
|
172
|
+
if (node !== undefined) options.detach(node)
|
|
173
|
+
}
|
|
174
|
+
|
|
81
175
|
return {
|
|
82
176
|
get size() {
|
|
83
177
|
return nodes.size
|
|
84
178
|
},
|
|
179
|
+
parents,
|
|
85
180
|
get(entity) {
|
|
86
181
|
return nodes.get(entity)
|
|
87
182
|
},
|
|
88
|
-
ensure(entity, create) {
|
|
183
|
+
ensure(entity, create, parent) {
|
|
89
184
|
const existing = nodes.get(entity)
|
|
90
185
|
if (existing !== undefined) {
|
|
91
186
|
return existing
|
|
92
187
|
}
|
|
93
188
|
const node = create()
|
|
189
|
+
const parentNode = parent === undefined ? undefined : nodes.get(parent)
|
|
94
190
|
nodes.set(entity, node)
|
|
95
|
-
|
|
191
|
+
if (parent !== undefined && parentNode !== undefined) link(entity, parent)
|
|
192
|
+
options.attach(node, parentNode)
|
|
96
193
|
return node
|
|
97
194
|
},
|
|
195
|
+
reparent(entity, parent) {
|
|
196
|
+
const node = nodes.get(entity)
|
|
197
|
+
if (node === undefined) {
|
|
198
|
+
return false
|
|
199
|
+
}
|
|
200
|
+
const parentNode = parent === undefined ? undefined : nodes.get(parent)
|
|
201
|
+
const next = parentNode === undefined ? undefined : parent
|
|
202
|
+
if (parents.get(entity) === next) {
|
|
203
|
+
return true
|
|
204
|
+
}
|
|
205
|
+
if (next !== undefined && nestedUnder(next, entity)) {
|
|
206
|
+
return false
|
|
207
|
+
}
|
|
208
|
+
unlink(entity)
|
|
209
|
+
if (next !== undefined) link(entity, next)
|
|
210
|
+
options.attach(node, parentNode)
|
|
211
|
+
return true
|
|
212
|
+
},
|
|
98
213
|
remove(entity) {
|
|
99
214
|
const node = nodes.get(entity)
|
|
100
215
|
if (node === undefined) {
|
|
101
216
|
return false
|
|
102
217
|
}
|
|
218
|
+
release(entity)
|
|
219
|
+
unlink(entity)
|
|
103
220
|
nodes.delete(entity)
|
|
104
221
|
options.detach(node)
|
|
105
222
|
return true
|
|
106
223
|
},
|
|
107
224
|
clear() {
|
|
108
|
-
for (const
|
|
109
|
-
|
|
225
|
+
for (const entity of nodes.keys()) {
|
|
226
|
+
if (!parents.has(entity)) detachTree(entity)
|
|
110
227
|
}
|
|
111
228
|
nodes.clear()
|
|
229
|
+
parents.clear()
|
|
230
|
+
children.clear()
|
|
112
231
|
},
|
|
113
232
|
entries() {
|
|
114
233
|
return nodes.entries()
|
package/src/RenderSync.ts
CHANGED
|
@@ -26,6 +26,12 @@
|
|
|
26
26
|
* services declared in `services`. Those resources and services become
|
|
27
27
|
* runtime requirements like the registry itself.
|
|
28
28
|
*
|
|
29
|
+
* With `hierarchy` (a `Descriptor.Hierarchy` of the game), a child's node is
|
|
30
|
+
* attached under its parent's node instead of the root, and moved whenever
|
|
31
|
+
* its parent changes, so the renderer composes the transforms: `apply` sets
|
|
32
|
+
* the child's own (local) transform, relative to the parent's node, and no
|
|
33
|
+
* world-transform math happens in the system.
|
|
34
|
+
*
|
|
29
35
|
* Its spec is erased, so it does not count as a writer for
|
|
30
36
|
* `Game.System.thisTick(...)` order checks.
|
|
31
37
|
*
|
|
@@ -34,6 +40,10 @@
|
|
|
34
40
|
*
|
|
35
41
|
* @example
|
|
36
42
|
* ```ts
|
|
43
|
+
* import { Sprite, type Texture } from "pixi.js"
|
|
44
|
+
*
|
|
45
|
+
* const Assets = Descriptor.Service<{ readonly textures: Readonly<Record<string, Texture>> }>()("Game/Assets")
|
|
46
|
+
* // ---cut---
|
|
37
47
|
* const Sprites = Descriptor.Service<NodeRegistry.NodeRegistry<Sprite>>()("Game/Sprites")
|
|
38
48
|
*
|
|
39
49
|
* const Render = RenderSync.system(Game, {
|
|
@@ -46,12 +56,13 @@
|
|
|
46
56
|
* apply: (sprite, { transform }) => sprite.position.set(transform.x, transform.y)
|
|
47
57
|
* })
|
|
48
58
|
*
|
|
49
|
-
* const update = Game.Schedule(
|
|
59
|
+
* const update = Game.Schedule(Move, Game.Schedule.applyDeferred(), Render)
|
|
50
60
|
* ```
|
|
51
61
|
*/
|
|
52
62
|
import type { Descriptor } from "@typeonce/ritmo/Descriptor"
|
|
53
63
|
import type { Condition, ConditionNeedsFromConditions } from "@typeonce/ritmo/Machine"
|
|
54
64
|
import type * as Entity from "@typeonce/ritmo/Entity"
|
|
65
|
+
import type { Relation } from "@typeonce/ritmo/Relation"
|
|
55
66
|
import type { OptionalReadAccess, OptionalReadCell, ReadAccess, ReadCell, ReadonlyValue } from "@typeonce/ritmo/Query"
|
|
56
67
|
import type { Schema } from "@typeonce/ritmo/Schema"
|
|
57
68
|
import type { NodeRegistry } from "./NodeRegistry.ts"
|
|
@@ -60,6 +71,18 @@ type RegistryService = Descriptor<"service", string, NodeRegistry<any>>
|
|
|
60
71
|
type ServiceDescriptor = Descriptor<"service", string, any>
|
|
61
72
|
type ResourceDescriptor = Descriptor<"resource", string, any>
|
|
62
73
|
|
|
74
|
+
/**
|
|
75
|
+
* The `hierarchy` option: a `Descriptor.Hierarchy` relation of the game's
|
|
76
|
+
* schema, or a message naming the fix for anything else.
|
|
77
|
+
*/
|
|
78
|
+
export type HierarchyOption<S extends Schema.Any, H> =
|
|
79
|
+
[H] extends [undefined] ? H
|
|
80
|
+
: [H] extends [Schema.RelationDescriptor<S>]
|
|
81
|
+
? [H] extends [Relation.Hierarchy]
|
|
82
|
+
? H
|
|
83
|
+
: "hierarchy must be a Descriptor.Hierarchy relation: a Descriptor.Relation does not make a parent's node hold its children"
|
|
84
|
+
: "hierarchy must be a relation of the game's schema: add it to Schema.fragment({ relations })"
|
|
85
|
+
|
|
63
86
|
/**
|
|
64
87
|
* The node type held by a registry service.
|
|
65
88
|
*/
|
|
@@ -123,7 +146,8 @@ export interface Options<
|
|
|
123
146
|
Select extends Selection<S>,
|
|
124
147
|
Services extends Readonly<Record<string, ServiceDescriptor>>,
|
|
125
148
|
Resources extends Readonly<Record<string, Schema.ResourceDescriptor<S>>> = {},
|
|
126
|
-
When extends ReadonlyArray<Condition> = readonly []
|
|
149
|
+
When extends ReadonlyArray<Condition> = readonly [],
|
|
150
|
+
Hierarchy extends Relation.Any | undefined = undefined
|
|
127
151
|
> {
|
|
128
152
|
/** The system name. */
|
|
129
153
|
readonly name: string
|
|
@@ -157,6 +181,21 @@ export interface Options<
|
|
|
157
181
|
* seen when the system runs again.
|
|
158
182
|
*/
|
|
159
183
|
readonly when?: When
|
|
184
|
+
/**
|
|
185
|
+
* A `Descriptor.Hierarchy` of the game whose tree the nodes follow: a
|
|
186
|
+
* child's node is attached under its parent's node (`attach(node, parent)`)
|
|
187
|
+
* when the parent has a node in this registry, and at the root otherwise,
|
|
188
|
+
* and is moved when the parent changes, is removed, or gains or loses its
|
|
189
|
+
* node. Only the direct parent counts: a child of a parent without a node
|
|
190
|
+
* sits at the root, not under a grandparent. `apply` then receives the
|
|
191
|
+
* child's own components, which the renderer composes with its parent's
|
|
192
|
+
* node: a child's transform is local to its parent. Despawning a parent
|
|
193
|
+
* despawns its children (hierarchies despawn together), so their nodes go
|
|
194
|
+
* with it; a parent that only loses its node hands its children's nodes
|
|
195
|
+
* back to the root. Each run checks every renderable entity that has a
|
|
196
|
+
* parent, so the cost grows with the number of children, not of changes.
|
|
197
|
+
*/
|
|
198
|
+
readonly hierarchy?: HierarchyOption<S, Hierarchy>
|
|
160
199
|
/** Builds the node for a new renderable entity. */
|
|
161
200
|
readonly create: (context: NodeContext<S, Root, Renderable, Transform, Select, Services, Resources>) => NodeOf<Registry>
|
|
162
201
|
/**
|
|
@@ -187,10 +226,11 @@ export const system = <
|
|
|
187
226
|
const Services extends Readonly<Record<string, ServiceDescriptor>> = {},
|
|
188
227
|
const Resources extends Readonly<Record<string, Schema.ResourceDescriptor<S>>> = {},
|
|
189
228
|
const When extends ReadonlyArray<Condition<Root>> = readonly [],
|
|
190
|
-
const Transform extends Schema.ComponentDescriptor<S> | undefined = undefined
|
|
229
|
+
const Transform extends Schema.ComponentDescriptor<S> | undefined = undefined,
|
|
230
|
+
const Hierarchy extends Relation.Any | undefined = undefined
|
|
191
231
|
>(
|
|
192
232
|
Game: Schema.Game<S, Root>,
|
|
193
|
-
options: Options<S, Root, Renderable, Transform, Registry, Select, Services, Resources, When>
|
|
233
|
+
options: Options<S, Root, Renderable, Transform, Registry, Select, Services, Resources, When, Hierarchy>
|
|
194
234
|
): RenderSystem<S, Root, Registry | Services[keyof Services] | Resources[keyof Resources] | ConditionNeedsFromConditions<When>> => {
|
|
195
235
|
type Node = NodeOf<Registry>
|
|
196
236
|
type Context = NodeContext<S, Root, Renderable, Transform, Select, Services, Resources>
|
|
@@ -200,10 +240,12 @@ export const system = <
|
|
|
200
240
|
const Query = Game.Query as unknown as (spec: object) => object
|
|
201
241
|
const anyOf = Game.Query.anyOf as unknown as (...filters: ReadonlyArray<object>) => object
|
|
202
242
|
const transform = options.transform as Schema.ComponentDescriptor<S> | undefined
|
|
243
|
+
const hierarchy = options.hierarchy as Schema.RelationDescriptor<S> | undefined
|
|
203
244
|
const selection = {
|
|
204
245
|
...options.select,
|
|
205
246
|
__renderable: Game.Query.read(options.renderable),
|
|
206
|
-
...(transform === undefined ? {} : { __transform: Game.Query.read(transform) })
|
|
247
|
+
...(transform === undefined ? {} : { __transform: Game.Query.read(transform) }),
|
|
248
|
+
...(hierarchy === undefined ? {} : { __parent: Game.Query.optionalRelation(hierarchy) })
|
|
207
249
|
}
|
|
208
250
|
const services: Record<string, unknown> = { __registry: Game.System.service(options.registry) }
|
|
209
251
|
for (const [key, descriptor] of Object.entries(options.services ?? {})) {
|
|
@@ -258,6 +300,11 @@ export const system = <
|
|
|
258
300
|
selection: { __renderable: Game.Query.read(options.renderable) },
|
|
259
301
|
with: required.filter((descriptor) => descriptor !== options.renderable)
|
|
260
302
|
})
|
|
303
|
+
// Every renderable entity with a parent: checked each run against where
|
|
304
|
+
// its node is attached. There is no per-reader filter for relation edges.
|
|
305
|
+
if (hierarchy !== undefined) {
|
|
306
|
+
queries["parented"] = Query({ selection: { __parent: Game.Query.readRelation(hierarchy) }, with: [options.renderable] })
|
|
307
|
+
}
|
|
261
308
|
|
|
262
309
|
return System(options.name, {
|
|
263
310
|
queries,
|
|
@@ -265,7 +312,7 @@ export const system = <
|
|
|
265
312
|
resources,
|
|
266
313
|
services,
|
|
267
314
|
when: options.when ?? []
|
|
268
|
-
}, ({ queries, removed, resources, services }) => {
|
|
315
|
+
}, ({ queries, removed, resources, services, lookup }) => {
|
|
269
316
|
const registry = services.__registry as NodeRegistry<Node>
|
|
270
317
|
// Destroy first: an entity that lost a required component has no node,
|
|
271
318
|
// and one that lost it and got it back since the last run gets a fresh one.
|
|
@@ -288,11 +335,68 @@ export const system = <
|
|
|
288
335
|
|
|
289
336
|
for (const { entity, data } of queries["sync"].each()) {
|
|
290
337
|
const context = contextOf(entity, data, services, resources)
|
|
291
|
-
|
|
338
|
+
// A child met before its parent's node exists starts at the root; the
|
|
339
|
+
// pass below moves it under the parent in this same run.
|
|
340
|
+
const parent: number | undefined = hierarchy === undefined || !data["__parent"].present ? undefined : data["__parent"].get()
|
|
341
|
+
options.apply(registry.ensure(entity.id, () => options.create(context), parent), context)
|
|
292
342
|
}
|
|
343
|
+
|
|
344
|
+
if (hierarchy !== undefined) reattach(registry as NodeRegistry<unknown>, queries["parented"], lookup, hierarchy)
|
|
293
345
|
}) as RenderSystem<S, Root, Registry | Services[keyof Services] | Resources[keyof Resources] | ConditionNeedsFromConditions<When>>
|
|
294
346
|
}
|
|
295
347
|
|
|
348
|
+
// The lookups `reattach` uses, with the generic ids erased.
|
|
349
|
+
interface HierarchyLookup {
|
|
350
|
+
parent(entity: number, relation: unknown): { readonly ok: boolean }
|
|
351
|
+
ancestors(entity: number, relation: unknown): { readonly ok: true; readonly value: ReadonlyArray<number> } | { readonly ok: false }
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
interface ParentedQuery {
|
|
355
|
+
each(): Iterable<{ readonly entity: { readonly id: number }; readonly data: { readonly __parent: { get(): number } } }>
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Moves every node whose parent changed since it was attached: the entity
|
|
359
|
+
// gained, lost, or changed its parent, or the parent gained or lost its node.
|
|
360
|
+
// There is no per-reader filter for relation edges, so every parented entity
|
|
361
|
+
// is checked each run. Leaving parents go first, then the rest in depth
|
|
362
|
+
// order (parents before children), so a node never moves under a node that
|
|
363
|
+
// is still inside it and each move happens once.
|
|
364
|
+
const reattach = (
|
|
365
|
+
registry: NodeRegistry<unknown>,
|
|
366
|
+
parented: ParentedQuery,
|
|
367
|
+
lookup: HierarchyLookup,
|
|
368
|
+
hierarchy: unknown
|
|
369
|
+
): void => {
|
|
370
|
+
let moves: Array<{ readonly entity: number; readonly parent: number | undefined; depth: number }> | undefined
|
|
371
|
+
// Nested nodes whose entity still has a parent; any other nested node lost it.
|
|
372
|
+
let stillParented = 0
|
|
373
|
+
for (const { entity, data } of parented.each()) {
|
|
374
|
+
const attached = registry.parents.get(entity.id)
|
|
375
|
+
if (attached !== undefined) stillParented++
|
|
376
|
+
const parent = data.__parent.get()
|
|
377
|
+
if (attached === parent) continue
|
|
378
|
+
const wanted = registry.get(parent) === undefined ? undefined : parent
|
|
379
|
+
if (attached === wanted || registry.get(entity.id) === undefined) continue
|
|
380
|
+
;(moves ??= []).push({ entity: entity.id, parent: wanted, depth: 0 })
|
|
381
|
+
}
|
|
382
|
+
if (stillParented < registry.parents.size) {
|
|
383
|
+
const toRoot: Array<number> = []
|
|
384
|
+
for (const [entity] of registry.parents) {
|
|
385
|
+
if (!lookup.parent(entity, hierarchy).ok) toRoot.push(entity)
|
|
386
|
+
}
|
|
387
|
+
for (const entity of toRoot) registry.reparent(entity, undefined)
|
|
388
|
+
}
|
|
389
|
+
if (moves === undefined) return
|
|
390
|
+
if (moves.length > 1) {
|
|
391
|
+
for (const move of moves) {
|
|
392
|
+
const ancestors = lookup.ancestors(move.entity, hierarchy)
|
|
393
|
+
move.depth = ancestors.ok ? ancestors.value.length : 0
|
|
394
|
+
}
|
|
395
|
+
moves.sort((a, b) => a.depth - b.depth)
|
|
396
|
+
}
|
|
397
|
+
for (const move of moves) registry.reparent(move.entity, move.parent)
|
|
398
|
+
}
|
|
399
|
+
|
|
296
400
|
/** A 2D position, the value `interpolate` blends when no `blend` is given. */
|
|
297
401
|
export interface Planar {
|
|
298
402
|
readonly x: number
|
|
@@ -398,6 +502,18 @@ interface InterpolateBase<
|
|
|
398
502
|
*
|
|
399
503
|
* @example
|
|
400
504
|
* ```ts
|
|
505
|
+
* import type { Sprite } from "pixi.js"
|
|
506
|
+
*
|
|
507
|
+
* interface Mesh { readonly position: { set(x: number, y: number, z: number): void } }
|
|
508
|
+
* const PrevPosition = Descriptor.Component<{ x: number; y: number }>()("Game/PrevPosition")
|
|
509
|
+
* const Position3 = Descriptor.Component<{ x: number; y: number; z: number }>()("Game/Position3")
|
|
510
|
+
* const PrevPosition3 = Descriptor.Component<{ x: number; y: number; z: number }>()("Game/PrevPosition3")
|
|
511
|
+
* const Game = Schema.bind(Schema.fragment({ components: { Position, PrevPosition, Position3, PrevPosition3 } }))
|
|
512
|
+
* const Sprites = Descriptor.Service<NodeRegistry.NodeRegistry<Sprite>>()("Game/Sprites")
|
|
513
|
+
* const Meshes = Descriptor.Service<NodeRegistry.NodeRegistry<Mesh>>()("Game/Meshes")
|
|
514
|
+
* // The loop's render alpha, provided by the host each frame:
|
|
515
|
+
* const RenderClock = Descriptor.Service<{ readonly alpha: number }>()("Game/RenderClock")
|
|
516
|
+
* // ---cut---
|
|
401
517
|
* const Interpolate = RenderSync.interpolate(Game, {
|
|
402
518
|
* name: "Game/Interpolate",
|
|
403
519
|
* registry: Sprites,
|
|
@@ -406,7 +522,7 @@ interface InterpolateBase<
|
|
|
406
522
|
* clock: RenderClock,
|
|
407
523
|
* place: (sprite, { x, y }) => sprite.position.set(x, y)
|
|
408
524
|
* })
|
|
409
|
-
* const render = Game.Schedule(
|
|
525
|
+
* const render = Game.Schedule(Interpolate) // after the RenderSync.system that creates the nodes
|
|
410
526
|
*
|
|
411
527
|
* // 3D: blend every coordinate.
|
|
412
528
|
* RenderSync.interpolate(Game, {
|