@typeonce/ritmo 0.6.0 → 0.7.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.
Files changed (125) hide show
  1. package/README.md +2 -0
  2. package/dist/Brand.d.ts +9 -4
  3. package/dist/Brand.d.ts.map +1 -1
  4. package/dist/Brand.js +9 -4
  5. package/dist/Brand.js.map +1 -1
  6. package/dist/Clock.d.ts +186 -0
  7. package/dist/Clock.d.ts.map +1 -0
  8. package/dist/Clock.js +111 -0
  9. package/dist/Clock.js.map +1 -0
  10. package/dist/Command.d.ts +157 -33
  11. package/dist/Command.d.ts.map +1 -1
  12. package/dist/Command.js +49 -15
  13. package/dist/Command.js.map +1 -1
  14. package/dist/Debug.d.ts +104 -12
  15. package/dist/Debug.d.ts.map +1 -1
  16. package/dist/Debug.js.map +1 -1
  17. package/dist/Decode.d.ts +1 -1
  18. package/dist/Decode.js +1 -1
  19. package/dist/Descriptor.d.ts +31 -21
  20. package/dist/Descriptor.d.ts.map +1 -1
  21. package/dist/Descriptor.js +21 -12
  22. package/dist/Descriptor.js.map +1 -1
  23. package/dist/Entity.d.ts +14 -5
  24. package/dist/Entity.d.ts.map +1 -1
  25. package/dist/Entity.js +2 -2
  26. package/dist/Entity.js.map +1 -1
  27. package/dist/EntityScope.d.ts +6 -0
  28. package/dist/EntityScope.d.ts.map +1 -1
  29. package/dist/EntityScope.js +6 -0
  30. package/dist/EntityScope.js.map +1 -1
  31. package/dist/HostClock.d.ts +49 -0
  32. package/dist/HostClock.d.ts.map +1 -0
  33. package/dist/HostClock.js +46 -0
  34. package/dist/HostClock.js.map +1 -0
  35. package/dist/Inspector.d.ts +7 -1
  36. package/dist/Inspector.d.ts.map +1 -1
  37. package/dist/Inspector.js.map +1 -1
  38. package/dist/Machine.d.ts +12 -3
  39. package/dist/Machine.d.ts.map +1 -1
  40. package/dist/Machine.js +4 -1
  41. package/dist/Machine.js.map +1 -1
  42. package/dist/Query.d.ts +28 -24
  43. package/dist/Query.d.ts.map +1 -1
  44. package/dist/Query.js +0 -24
  45. package/dist/Query.js.map +1 -1
  46. package/dist/Runtime.d.ts +36 -16
  47. package/dist/Runtime.d.ts.map +1 -1
  48. package/dist/Runtime.js +230 -67
  49. package/dist/Runtime.js.map +1 -1
  50. package/dist/Schedule.d.ts +217 -10
  51. package/dist/Schedule.d.ts.map +1 -1
  52. package/dist/Schedule.js +5 -0
  53. package/dist/Schedule.js.map +1 -1
  54. package/dist/Schema.d.ts +61 -25
  55. package/dist/Schema.d.ts.map +1 -1
  56. package/dist/Schema.js +0 -8
  57. package/dist/Schema.js.map +1 -1
  58. package/dist/System.d.ts +149 -52
  59. package/dist/System.d.ts.map +1 -1
  60. package/dist/System.js +52 -6
  61. package/dist/System.js.map +1 -1
  62. package/dist/index.d.ts +8 -7
  63. package/dist/index.d.ts.map +1 -1
  64. package/dist/index.js +8 -7
  65. package/dist/index.js.map +1 -1
  66. package/dist/internal/cells.d.ts +3 -3
  67. package/dist/internal/cells.d.ts.map +1 -1
  68. package/dist/internal/cells.js +9 -11
  69. package/dist/internal/cells.js.map +1 -1
  70. package/dist/internal/debug.d.ts +6 -0
  71. package/dist/internal/debug.d.ts.map +1 -1
  72. package/dist/internal/debug.js +95 -1
  73. package/dist/internal/debug.js.map +1 -1
  74. package/dist/internal/game.d.ts.map +1 -1
  75. package/dist/internal/game.js +9 -2
  76. package/dist/internal/game.js.map +1 -1
  77. package/dist/internal/order.d.ts +10 -0
  78. package/dist/internal/order.d.ts.map +1 -0
  79. package/dist/internal/order.js +120 -0
  80. package/dist/internal/order.js.map +1 -0
  81. package/dist/internal/queries.js +2 -2
  82. package/dist/internal/queries.js.map +1 -1
  83. package/package.json +1 -1
  84. package/src/Brand.ts +9 -4
  85. package/src/Clock.ts +311 -0
  86. package/src/Command.ts +271 -73
  87. package/src/Debug.ts +111 -10
  88. package/src/Decode.ts +1 -1
  89. package/src/Descriptor.ts +37 -27
  90. package/src/Entity.ts +15 -5
  91. package/src/EntityScope.ts +6 -0
  92. package/src/HostClock.ts +64 -0
  93. package/src/Inspector.ts +7 -2
  94. package/src/Machine.ts +13 -3
  95. package/src/Query.ts +41 -33
  96. package/src/Runtime.ts +273 -91
  97. package/src/Schedule.ts +380 -13
  98. package/src/Schema.ts +70 -41
  99. package/src/System.ts +174 -88
  100. package/src/index.ts +8 -7
  101. package/src/internal/cells.ts +10 -12
  102. package/src/internal/debug.ts +104 -4
  103. package/src/internal/game.ts +11 -3
  104. package/src/internal/order.ts +140 -0
  105. package/src/internal/queries.ts +3 -3
  106. package/dist/Definition.d.ts +0 -99
  107. package/dist/Definition.d.ts.map +0 -1
  108. package/dist/Definition.js +0 -68
  109. package/dist/Definition.js.map +0 -1
  110. package/dist/Feature.d.ts +0 -106
  111. package/dist/Feature.d.ts.map +0 -1
  112. package/dist/Feature.js +0 -87
  113. package/dist/Feature.js.map +0 -1
  114. package/dist/Fx.d.ts +0 -128
  115. package/dist/Fx.d.ts.map +0 -1
  116. package/dist/Fx.js +0 -112
  117. package/dist/Fx.js.map +0 -1
  118. package/dist/internal/definition.d.ts +0 -17
  119. package/dist/internal/definition.d.ts.map +0 -1
  120. package/dist/internal/definition.js +0 -25
  121. package/dist/internal/definition.js.map +0 -1
  122. package/src/Definition.ts +0 -106
  123. package/src/Feature.ts +0 -241
  124. package/src/Fx.ts +0 -176
  125. package/src/internal/definition.ts +0 -56
package/src/Schema.ts CHANGED
@@ -13,8 +13,11 @@
13
13
  * 3. bind one `Game` with `Schema.bind(...)`
14
14
  * 4. build a runtime from that bound `Game`
15
15
  *
16
- * `Schema.Feature` composes fragments and their schedules before binding; see
17
- * the `feature` module.
16
+ * There is no plugin or feature object. A reusable slice of a game is a
17
+ * fragment (its descriptors) plus plain functions `(Game, options) => System |
18
+ * Schedule` that build its systems from the bound `Game`, like
19
+ * `RenderSync.system(Game, ...)` and `InputCapture.system(Game, ...)`. List
20
+ * every fragment in one `Schema.bind(...)` call.
18
21
  *
19
22
  * @module Schema
20
23
  * @docGroup core
@@ -65,8 +68,6 @@ import type * as Runtime from "./Runtime.ts"
65
68
  import type * as Schedule from "./Schedule.ts"
66
69
  import type * as System from "./System.ts"
67
70
 
68
- export { Feature } from "./Feature.ts"
69
-
70
71
  /**
71
72
  * A mapping from schema keys to nominal descriptors.
72
73
  */
@@ -246,20 +247,6 @@ export const fragment = <
246
247
  relations: definition.relations ?? {}
247
248
  }) as SchemaDefinition<Components, Resources, Events, Relations>
248
249
 
249
- /**
250
- * Merges two schema fragments into a larger closed schema.
251
- *
252
- * Duplicate keys and descriptor names are rejected at the type level and at
253
- * runtime.
254
- */
255
- export const merge = <
256
- A extends Schema.Any,
257
- B extends Schema.Any
258
- >(
259
- left: A,
260
- right: B & ValidateFragments<[A, B]>
261
- ): MergeSchemaDefinitions<A, B> => mergeSchemas(left, right) as MergeSchemaDefinitions<A, B>
262
-
263
250
  /**
264
251
  * Composes one or more fragments and returns one bound `Game` surface.
265
252
  *
@@ -322,6 +309,7 @@ interface BoundSystemAccess<S extends Schema.Any, Root> extends System.SystemAcc
322
309
  readonly pauseWhen?: ReadonlyArray<Machine.Condition<Root>>
323
310
  readonly transitions?: Record<string, Machine.TransitionRead<Schema.BoundStateMachine<Root>>>
324
311
  readonly local?: () => unknown
312
+ readonly commands?: Command.CommandAccess<ComponentDescriptor<S>, ComponentDescriptor<S>, ComponentDescriptor<S>, ComponentDescriptor<S>, EntityScope.EntityScope<string, Root>, RelationDescriptor<S>>
325
313
  }
326
314
 
327
315
  interface BoundCheckAccess<S extends Schema.Any, Root> extends Condition.CheckAccessInput {
@@ -341,10 +329,10 @@ interface BoundInspectorAccess<S extends Schema.Any, Root> extends Inspector.Ins
341
329
  }
342
330
 
343
331
  type BoundScheduleEntry<S extends Schema.Any, Root> =
344
- | Schema.BoundSystem<S, Root, any, any, any>
332
+ | Schema.BoundSystem<S, Root, any, any>
345
333
  | Schedule.ApplyDeferredStep
346
334
  | Schedule.ApplyStateTransitionsStep<any, Root>
347
- | Schema.BoundSchedule<S, Root, any, any>
335
+ | Schedule.ScheduleDefinition<S, any, Root, any, any, Schedule.AnyScheduleOrder>
348
336
 
349
337
  type BoundTransitionEntry<S extends Schema.Any, Root> =
350
338
  Exclude<BoundScheduleEntry<S, Root>, Schedule.ApplyStateTransitionsStep<any, Root>>
@@ -359,7 +347,8 @@ type BoundTransitionScheduleResult<
359
347
  M,
360
348
  Schedule.CompositionExactRequirements<Entries>,
361
349
  Root,
362
- Schedule.CompositionFailure<Entries>
350
+ Schedule.CompositionFailure<Entries>,
351
+ Schedule.CompositionOrder<Entries>
363
352
  >
364
353
 
365
354
  type BoundTransitionBundleInput<S extends Schema.Any, Root> =
@@ -376,7 +365,8 @@ type BoundTransitionBundleResult<
376
365
  Schedule.TransitionBundleRequirements<Schedule.FlattenTransitionEntries<Entries>>,
377
366
  Root,
378
367
  Schedule.TransitionBundleRequirements<Schedule.FlattenTransitionEntries<Entries>>,
379
- Schedule.TransitionBundleFailure<Schedule.FlattenTransitionEntries<Entries>>
368
+ Schedule.TransitionBundleFailure<Schedule.FlattenTransitionEntries<Entries>>,
369
+ Schedule.CompositionOrder<Entries>
380
370
  >
381
371
 
382
372
  /**
@@ -413,11 +403,10 @@ export namespace Schema {
413
403
  S extends Any,
414
404
  Root,
415
405
  Spec extends System.AnySystemSpec = System.AnySystemSpec,
416
- A = void,
417
406
  E = never,
418
407
  Name extends string = string,
419
408
  Needs extends Requirement.Requirement = Requirement.Requirement
420
- > = System.SystemDefinition<Spec & { readonly schema: S }, A, E, Root, Name, Needs>
409
+ > = System.SystemDefinition<Spec & { readonly schema: S }, E, Root, Name, Needs>
421
410
 
422
411
  /** A schema-bound read-only world projection. */
423
412
  export type BoundInspector<
@@ -437,7 +426,7 @@ export namespace Schema {
437
426
  Root,
438
427
  Needs extends Requirement.Requirement = Requirement.Requirement,
439
428
  Failure extends System.SystemFailure = never
440
- > = Schedule.ScheduleDefinition<S, Needs, Root, Needs, Failure>
429
+ > = Schedule.ScheduleDefinition<S, Needs, Root, Needs, Failure, Schedule.ScheduleOrder>
441
430
 
442
431
  /**
443
432
  * A schema-bound finite-state machine.
@@ -453,16 +442,18 @@ export namespace Schema {
453
442
  Root,
454
443
  M extends BoundStateMachine<Root> = BoundStateMachine<Root>,
455
444
  Needs extends Requirement.Requirement = Requirement.Requirement,
456
- Failure extends System.SystemFailure = never
457
- > = Machine.TransitionScheduleDefinition<S, M, Needs, Root, Failure>
445
+ Failure extends System.SystemFailure = never,
446
+ Order extends Schedule.AnyScheduleOrder = Schedule.AnyScheduleOrder
447
+ > = Machine.TransitionScheduleDefinition<S, M, Needs, Root, Failure, Order>
458
448
 
459
449
  export type BoundTransitionBundle<
460
450
  S extends Any,
461
451
  Root,
462
452
  Entries extends ReadonlyArray<BoundTransitionSchedule<S, Root, any, any, any>> = ReadonlyArray<BoundTransitionSchedule<S, Root, any, any, any>>,
463
453
  Needs extends Requirement.Requirement = Requirement.Requirement,
464
- Failure extends System.SystemFailure = never
465
- > = Schedule.TransitionBundleDefinition<S, Entries, Needs, Root, Needs, Failure>
454
+ Failure extends System.SystemFailure = never,
455
+ Order extends Schedule.AnyScheduleOrder = Schedule.AnyScheduleOrder
456
+ > = Schedule.TransitionBundleDefinition<S, Entries, Needs, Root, Needs, Failure, Order>
466
457
 
467
458
  /**
468
459
  * A schema-bound runtime branded to one bound schema root.
@@ -480,7 +471,12 @@ export namespace Schema {
480
471
  */
481
472
  export interface Game<S extends Any, Root = S> {
482
473
  readonly schema: S
483
- /** Creates a typed ownership scope for scene or level entities. */
474
+ /**
475
+ * Creates a typed ownership scope for scene or level entities: a flat
476
+ * group despawned together with `commands.despawnScope(...)`. Use
477
+ * `Descriptor.Hierarchy` instead for a parent-child tree with ordered
478
+ * children, traversal, and recursive despawn.
479
+ */
484
480
  readonly EntityScope: <const Name extends string>(name: Name) => EntityScope.EntityScope<Name, Root>
485
481
  /** Declares a reusable, requirement-checked read-only world projection. */
486
482
  readonly Inspector: <
@@ -521,7 +517,8 @@ export namespace Schema {
521
517
  readonly withoutRelations?: WithoutRelations
522
518
  readonly withRelated?: WithRelated
523
519
  readonly withoutRelated?: WithoutRelated
524
- }): QueryModule.QuerySpec<Selection, With, Without, Filters, WithRelations, WithoutRelations, WithRelated, WithoutRelated, Root>
520
+ // Uninferred: an omitted list is `[]`, never inferred from the spec it is written in (`queries: { ... }`).
521
+ }): QueryModule.QuerySpec<Selection, QueryModule.Uninferred<With, ReadonlyArray<ComponentDescriptor<S>>>, QueryModule.Uninferred<Without, ReadonlyArray<ComponentDescriptor<S>>>, QueryModule.Uninferred<Filters, ReadonlyArray<QueryModule.Filter<ComponentDescriptor<S>>>>, QueryModule.Uninferred<WithRelations, ReadonlyArray<Relation.Relation.Any>>, QueryModule.Uninferred<WithoutRelations, ReadonlyArray<Relation.Relation.Any>>, QueryModule.Uninferred<WithRelated, ReadonlyArray<Relation.Relation.Any>>, QueryModule.Uninferred<WithoutRelated, ReadonlyArray<Relation.Relation.Any>>, Root>
525
522
  read: <D extends ComponentDescriptor<S>>(descriptor: D) => QueryModule.ReadAccess<D>
526
523
  write: <D extends ComponentDescriptor<S>>(descriptor: D) => QueryModule.WriteAccess<D>
527
524
  optional: <D extends ComponentDescriptor<S>>(descriptor: D) => QueryModule.OptionalReadAccess<D>
@@ -537,16 +534,15 @@ export namespace Schema {
537
534
  spawn: <const Entries extends ReadonlyArray<Command.SpawnEntry<S>> = readonly []>(
538
535
  ...entries: Entries
539
536
  ) => Command.DraftFor<S, Entries, {}, Root>
540
- /** Adds entries to a draft. */
541
- insert: <P extends Entity.ComponentProof, const Entries extends ReadonlyArray<Command.SpawnEntry<S>>>(
537
+ /**
538
+ * Adds entries to a draft (a value not yet spawned). Use
539
+ * `commands.insert` for an entity that already exists.
540
+ */
541
+ extend: <P extends Entity.ComponentProof, const Entries extends ReadonlyArray<Command.SpawnEntry<S>>>(
542
542
  draft: Entity.EntityDraft<S, P, Root>,
543
543
  ...entries: Entries
544
544
  ) => Command.DraftFor<S, Entries, P, Root>
545
545
  entry: <D extends ComponentDescriptor<S>>(descriptor: D, value: Descriptor.Value<D>) => Command.Entry<D>
546
- entryResult: <D extends ComponentDescriptor<S>, E>(
547
- descriptor: D,
548
- result: Result.Result<Descriptor.Value<D>, E>
549
- ) => Result.Result<Command.Entry<D>, E>
550
546
  entryRaw: <D extends Extract<ComponentDescriptor<S>, ConstructedDescriptor<"component", string, any, any, any>>>(
551
547
  descriptor: D,
552
548
  raw: Descriptor.Raw<D>
@@ -557,6 +553,12 @@ export namespace Schema {
557
553
  target: Entity.EntityId<S, Root>
558
554
  ) => Entity.EntityDraft<S, P, Root>
559
555
  }
556
+ /**
557
+ * Declares one game-wide mode (menu, playing, paused): queued with
558
+ * `nextState`, committed at `applyStateTransitions(...)`, gating systems
559
+ * with `Game.Condition.inState(...)`. Use `Descriptor.EntityState(...)`
560
+ * instead for a state each entity has on its own.
561
+ */
560
562
  readonly StateMachine: {
561
563
  <
562
564
  const Name extends string,
@@ -584,17 +586,24 @@ export namespace Schema {
584
586
  <
585
587
  const Name extends string,
586
588
  const Access extends BoundSystemAccess<S, Root>,
587
- A = void,
588
589
  E = never
589
590
  >(
590
591
  name: Name,
591
592
  spec: System.ExactAccess<Access>,
592
- run: System.SystemRun<System.SystemSpec<S, Access, Root>, A, E>
593
- ): BoundSystem<S, Root, System.SystemSpec<S, Access, Root>, A, E, Name, System.SystemAccessNeeds<Access>>
593
+ run: System.SystemRun<System.SystemSpec<S, Access, Root>, E>
594
+ ): BoundSystem<S, Root, System.SystemSpec<S, Access, Root>, E, Name, System.SystemAccessNeeds<Access>>
594
595
  readResource: <D extends ResourceDescriptor<S>>(descriptor: D) => System.ResourceRead<D>
595
596
  writeResource: <D extends ResourceDescriptor<S>>(descriptor: D) => System.ResourceWrite<D>
596
597
  readEvent: <D extends EventDescriptor<S>>(descriptor: D) => System.EventRead<D>
597
598
  writeEvent: <D extends EventDescriptor<S>>(descriptor: D) => System.EventWrite<D>
599
+ /**
600
+ * Requires every writer of this event or resource to run earlier in the
601
+ * same `runtime.tick(...)` call; checked at compile time by `tick`.
602
+ * Accepts `readEvent(...)`, `readResource(...)`, or a query.
603
+ */
604
+ thisTick: <Read extends System.EventRead<EventDescriptor<S>> | System.ResourceRead<ResourceDescriptor<S>> | Query.Any<Root>>(
605
+ read: Read
606
+ ) => Read & System.ThisTick
598
607
  service: typeof System.service
599
608
  machine: <M extends BoundStateMachine<Root>>(machine: M) => Machine.MachineRead<M>
600
609
  nextState: <M extends BoundStateMachine<Root>>(machine: M) => Machine.NextMachineWrite<M>
@@ -603,6 +612,23 @@ export namespace Schema {
603
612
  readRemoved: <D extends ComponentDescriptor<S>>(descriptor: D) => System.RemovedRead<D>
604
613
  readDespawned: () => System.DespawnedRead
605
614
  readRelationFailures: <R extends RelationDescriptor<S>>(relation: R) => System.RelationFailureRead<R>
615
+ /**
616
+ * Declares the commands a system may queue, for its spec's `commands`:
617
+ * `Game.System.commands(Game.System.spawns(Enemy, Position), Game.System.despawns(Enemy))`.
618
+ */
619
+ commands: typeof Command.commands
620
+ /** Allows spawning drafts whose components are all among these; see `Command.spawns`. */
621
+ spawns: <const Components extends ReadonlyArray<ComponentDescriptor<S>>>(...components: Components) => Command.Permission<"spawns", Components[number]>
622
+ /** Allows `insert` (add or replace at the next `applyDeferred()`) of these; see `Command.inserts`. */
623
+ inserts: <const Components extends ReadonlyArray<ComponentDescriptor<S>>>(...components: Components) => Command.Permission<"inserts", Components[number]>
624
+ /** Allows `remove` of these. */
625
+ removes: <const Components extends ReadonlyArray<ComponentDescriptor<S>>>(...components: Components) => Command.Permission<"removes", Components[number]>
626
+ /** Allows `despawn` of entities a query proves have one of these; see `Command.despawns`. */
627
+ despawns: <const Components extends ReadonlyArray<ComponentDescriptor<S>>>(...components: Components) => Command.Permission<"despawns", Components[number]>
628
+ /** Allows `despawnScope` of this scope. */
629
+ despawnsScope: <const Scope extends EntityScope.EntityScope<string, Root>>(scope: Scope) => Command.Permission<"despawnsScope", Scope>
630
+ /** Allows `relate`, `unrelate`, and `reorderChildren` of these relations. */
631
+ relates: <const Relations extends ReadonlyArray<RelationDescriptor<S>>>(...relations: Relations) => Command.Permission<"relates", Relations[number]>
606
632
  }
607
633
  readonly Schedule: {
608
634
  /**
@@ -633,7 +659,9 @@ export namespace Schema {
633
659
  /**
634
660
  * Builds a schedule whose systems run only while every condition
635
661
  * passes (in addition to their own `when`). Nested schedules are
636
- * gated too; marker steps still run.
662
+ * gated too; marker steps still run. Gated systems drop the events
663
+ * published while gated; use `pauseWhen` to keep them. For one system,
664
+ * use `when` in its spec.
637
665
  */
638
666
  when: <
639
667
  const Conditions extends readonly [Machine.Condition<Root>, ...Array<Machine.Condition<Root>>],
@@ -647,6 +675,7 @@ export namespace Schema {
647
675
  * fails: unlike `when`, a paused system keeps its place in the event
648
676
  * streams it reads and sees everything published meanwhile once it
649
677
  * runs again. For freezes whose events stay valid (hit-stop, pause).
678
+ * For one system, use `pauseWhen` in its spec.
650
679
  */
651
680
  pauseWhen: <
652
681
  const Conditions extends readonly [Machine.Condition<Root>, ...Array<Machine.Condition<Root>>],