ecs/system
System value + conflict detection.
Since: E2 (initial); I3 (cap-binding rewrite)
System
(defstruct System [])
a runnable bundle (reads-mask, writes-mask, run-fn).
Since: E2
make-system
(make-system [reads-mask : int writes-mask : int run-fn ptr<void>] :)
bundle a top-level defn with its read/write masks.
| reads-mask | OR of component-id bits the system reads | |
| writes-mask | OR of component-id bits the system writes | |
| run-fn | ptr<void> to a top-level defn (signature `[w : int] : nil`) |
A System value.
(def physics
(make-system
(bit-or (cid-bit POS-CID) (cid-bit VEL-CID)) ;; reads
(cid-bit POS-CID) ;; writes
physics-impl)) ;; defn ptr
Since: E2
system-reads-mask
(system-reads-mask [s : System] :)
accessor for the System's reads bitmask.
Since: E2
system-writes-mask
(system-writes-mask [s : System] :)
accessor for the System's writes bitmask.
Since: E2
system-run-fn
(system-run-fn [s : System] :)
accessor for the System's run-fn pointer.
Since: E2
system-run!
(system-run! [s : System w : int] :)
invoke the System's run-fn against a world handle.
| s | System value | |
| w | world handle (an :int that the system's run-fn understands) |
Since: E2
system-conflicts?
(system-conflicts? [a : System b : System] :)
true if two systems cannot run concurrently.
| a | System value | |
| b | System value |
true if either system's writes-mask overlaps the other's reads-or-writes mask; false if both systems can run in parallel.
(system-conflicts? physics render) ;; physics writes Pos, render reads Pos -> true
Since: E2
cid-bit
(cid-bit [cid : int] :)
pack a component ID (0..62) into its single-bit mask.
(def POS-CID 0) (def VEL-CID 1) (def physics-reads (bit-or (cid-bit POS-CID) (cid-bit VEL-CID)))
Since: E2
defsystem--mask
(defsystem--mask [comps])
internal helper. Build the bit-or'd cid-bit
defsystem--with-write-caps
(defsystem--with-write-caps [body comps])
internal helper. Wrap `body` in a
defsystem--with-read-caps
(defsystem--with-read-caps [body comps])
internal helper. Wrap `body` in a
defsystem--consume-write-caps
(defsystem--consume-write-caps [comps])
internal helper. Emit `(use-cap!
defsystem
(defsystem [name reads-vec writes-vec body])
declare a System and its implementation in one form.
(def Pos-cid 0)
(def Vel-cid 1)
(defsystem physics
[Pos Vel] ;; :reads -- binds Pos-read-cap, Vel-read-cap
[Pos] ;; :writes -- binds Pos-write-cap (linear)
(println "physics tick"))
;; physics-impl and physics both exist at top level.
Since: E2 (initial); I3 (cap-binding rewrite)
defsystem-world
(defsystem-world [name world-name reads-vec writes-vec body])
declare a System whose body reaches its world at
| name | the System value's name (also prefixes -impl / -fn) | |
| world-name | the world type the body is typed against | |
| reads-vec | vector of component names the system reads | |
| writes-vec | vector of component names the system writes | |
| body | the system body; `w` is bound at `world-name` |
Nothing at runtime -- expands to three top-level declarations. Required imports at the call site: (import ecs/cap :refer [WriteCap ReadCap make-write-cap make-read-cap use-cap!]) (import ecs/xworld :refer [defworld-box-helpers])
(defworld GameWorld [Pos Vel])
(defcomponent-accessors GameWorld Pos)
(defcomponent-cids [Pos Vel])
(defworld-box-helpers GameWorld)
(defsystem-world physics GameWorld
[Pos] ;; :reads
[Pos] ;; :writes
(set-Pos! Pos-write-cap w (slot-new 0) (make-struct Pos 42 0)))
(system-run! physics (box-GameWorld world))
Since: GEN-V1