machine_world.bend
bendvy-ecs-core@0.1.0.0 · 33 lines · 0 definitions
1import Base
2import bendvy-ecs-core@0.1.0.0/clock_state.bend as Clock
3import bendvy-ecs-core@0.1.0.0/deferred_command.bend as Deferred
4import entity_allocator.bend as Alloc
5import machine.bend as M
6import world.bend as W
7
8# Closed resource lenses adapt machine slots to the existing actual ECS world.
9# No component store or affine resource is projected to Data or copied.
10def take_join(~S: Data,~K: Data,~V: Data,~C: Type,~R: Type,~E: Data,
11 +ns: U32,allocator:Alloc.Allocator,
12 store: C,split: R & M.Slot<S,K,V>,+events: List<&2,E>,commands:List<&1,Deferred.Command<W.World<S,C,R,E>>>,
13 +registrations: List<&2,W.RegistrationMeta>,+nextSystem: U32,+clock:Clock.State
14) -> W.World<S,C,R,E> & M.Slot<S,K,V>:
15 (resources,slot)=split
16 (W.World{ns,allocator,store,resources,events,commands,registrations,nextSystem,clock},slot)
17
18def take(~S: Data,~K: Data,~V: Data,~C: Type,~R: Type,~E: Data,
19 ~take_resource: R -> R & M.Slot<S,K,V>,world: W.World<S,C,R,E>) -> W.World<S,C,R,E> & M.Slot<S,K,V>:
20 match world:
21 case W.World{ns,allocator,store,resources,events,commands,registrations,nextSystem,clock}:
22 take_join(~S,~K,~V,~C,~R,~E,ns,allocator,store,take_resource(resources),events,commands,registrations,nextSystem,clock)
23
24def put(~S: Data,~K: Data,~V: Data,~C: Type,~R: Type,~E: Data,
25 ~put_resource: R -> M.Slot<S,K,V> -> R,world: W.World<S,C,R,E>,slot: M.Slot<S,K,V>) -> W.World<S,C,R,E>:
26 match world:
27 case W.World{ns,allocator,store,resources,events,commands,registrations,nextSystem,clock}:
28 W.World{ns,allocator,store,put_resource(resources,slot),events,commands,registrations,nextSystem,clock}
29
30# ApplyDeferred only drains structural commands. Machine commits require the
31# separate marker; callers must not conflate the two public operations.
32def structural_flush(~S: Data,~C: Type,~R: Type,~E: Data,world: W.World<S,C,R,E>) -> W.World<S,C,R,E>:
33 W.barrier(~S,~C,~R,~E,world)