events.bend
bendvy-ecs-core@0.1.0.4 · 48 lines · 8 definitions
1import Base
2import bendvy-ecs-core@0.1.0.4/world.bend as W
3
4
5
6
7
8
9
10type Reader<S: Data> is Type:
11 Reader{namespace: U32,offset: Nat}
12
13
14
15type Observed<S: Data,C: Type,R: Type,E: Data> is Type:
16 Read{world: W.World<S,C,R,E>,reader: Reader<S>,events: List<&2,E>}
17 ForeignReader{world: W.World<S,C,R,E>,reader: Reader<S>}
18
19def create_join(~S: Data,~C: Type,~R: Type,~E: Data,named: W.World<S,C,R,E> & U32) -> W.World<S,C,R,E> & Reader<S>:
20 (world,namespace) = named
21 (world,Reader{namespace,0n})
22
23
24
25
26def create(~S: Data,~C: Type,~R: Type,~E: Data,world: W.World<S,C,R,E>) -> W.World<S,C,R,E> & Reader<S>:
27 create_join(~S,~C,~R,~E,W.namespace(~S,~C,~R,~E,world))
28
29def read_events(~S: Data,~C: Type,~R: Type,~E: Data,+namespace: U32,+offset: Nat,observed: W.World<S,C,R,E> & List<&2,E>) -> Observed<S,C,R,E>:
30 (world,+events) = observed
31 Read{world,Reader{namespace,List.length(&2,E,events)},List.drop(&2,E,events,offset)}
32
33def read_checked(~S: Data,~C: Type,~R: Type,~E: Data,allowed: Bool,world: W.World<S,C,R,E>,reader: Reader<S>) -> Observed<S,C,R,E>:
34 match allowed reader:
35 case False{} reader: ForeignReader{world,reader}
36 case True{} Reader{+namespace,+offset}: read_events(~S,~C,~R,~E,namespace,offset,W.events_view(~S,~C,~R,~E,world))
37
38def read_named(~S: Data,~C: Type,~R: Type,~E: Data,named: W.World<S,C,R,E> & U32,reader: Reader<S>) -> Observed<S,C,R,E>:
39 match named reader:
40 case (world,actual) Reader{+namespace,+offset}: read_checked(~S,~C,~R,~E,U32.is_eq(actual,namespace),world,Reader{namespace,offset})
41
42
43
44
45
46
47def read(~S: Data,~C: Type,~R: Type,~E: Data,world: W.World<S,C,R,E>,reader: Reader<S>) -> Observed<S,C,R,E>:
48 read_named(~S,~C,~R,~E,W.namespace(~S,~C,~R,~E,world),reader)