JFLib

game/src/world.rs#

Source: lib/game/src/world.rs

world -- entities with a generation and their components, one column per component type

  Entity            an index and a generation: an id of a despawned entity never means the entity
                    that gets its index next (every use of it is refused or finds nothing)
  Entities          the allocator: spawn takes the index freed last (a free list), despawn raises the
                    generation; a slot whose generations are spent is never handed out again
  World             the entities and a column (SparseSet) per component type, found by the type;
                    spawn / despawn / insert / remove / get / get_mut / column / column_mut / pair_mut;
                    begin_tick: the interpolation's bookkeeping of Pose (pose.rs)

No archetypes and no scheduler (docs/GAME.md, G7b: until a measurement asks for them).  A system is a
function over the world (tick.rs) that takes the columns it needs: one column is a slice of values in
the order of iteration, a join walks the smaller column and looks the entity up in the other.  The
world keeps no unordered container: columns are found by their TypeId in a sorted list (a lookup,
nothing iterates in that order), entities and values iterate in index and column order.
  • struct Entity an entity: an index (its slot) and a generation (how often the slot was handed out before). Plain data: the world decides whether it lives

impl Entity

  • const NONE: Entity = Entity { index: u32::MAX, generation: u32::MAX } no entity: never alive, in no column

  • const fn index(self) -> u32 the slot of the entity

  • const fn generation(self) -> u32 how often its slot was handed out before it

  • const fn to_bits(self) -> u64 generation in the high, index in the low 32 bits (for a message or a file)

  • const fn from_bits(bits: u64) -> Entity the entity of to_bits

  • struct Entities the allocator of entities (World holds one)

impl Entities

  • const fn new() -> Entities no entities

  • fn spawn(&mut self) -> Entity a new entity: the slot freed last, with its next generation, else a new slot

  • fn despawn(&mut self, e: Entity) -> bool e dies, its slot is free for the next generation (false: e was not alive)

  • fn alive(&self, e: Entity) -> bool e lives: its slot is taken and has e's generation

  • fn at(&self, index: u32) -> Option<Entity> the entity alive in slot index

  • fn len(&self) -> u32 entities alive

  • fn is_empty(&self) -> bool none alive

  • fn slots(&self) -> u32 slots so far (alive, free, spent)

  • fn iter(&self) -> impl Iterator<Item = Entity> + '_ the live entities in ascending order of their index

  • trait Bundle a bundle of components for World::spawn_with: a tuple of one to six values of different types

    • fn insert_into(self, world: &mut World, e: Entity) every value to e (World::insert)
  • struct World entities and their components (see the module header)

impl World

  • fn new() -> World an empty world
  • fn entities(&self) -> &Entities the allocator: which entities live
  • fn spawn(&mut self) -> Entity a new entity without components
  • fn spawn_with<B: Bundle>(&mut self, bundle: B) -> Entity a new entity with the components of a tuple: world.spawn_with((Pose::at(p), Health(100)))
  • fn despawn(&mut self, e: Entity) -> bool e dies with all its components (false: e was not alive: a stale id changes nothing)
  • fn alive(&self, e: Entity) -> bool e lives
  • fn len(&self) -> u32 entities alive
  • fn is_empty(&self) -> bool none alive
  • fn register<T: 'static>(&mut self) make the column of T now (a column is otherwise made by its first insert or column_mut)
  • fn column<T: 'static>(&self) -> Option<&SparseSet<T>> the column of T (None: nothing ever asked for it)
  • fn column_mut<T: 'static>(&mut self) -> &mut SparseSet<T> the column of T for writing, made if there is none. Its values may be written; insert values through World::insert, which refuses dead entities (the column alone does not know them)
  • fn pair_mut<A: 'static, B: 'static>(&mut self) -> (&mut SparseSet<A>, &mut SparseSet<B>) the columns of two different types for writing at once (a system that writes one by the other). Panics when A and B are the same type
  • fn insert<T: 'static>(&mut self, e: Entity, value: T) -> bool e gets the component (replacing one of the same type); false: e is not alive, nothing changes
  • fn remove<T: 'static>(&mut self, e: Entity) -> Option<T> e's component of type T goes; the value
  • fn get<T: 'static>(&self, e: Entity) -> Option<&T> e's component of type T
  • fn get_mut<T: 'static>(&mut self, e: Entity) -> Option<Mut<'_, T>> e's component of type T for writing (a write through it takes a new version)
  • fn has<T: 'static>(&self, e: Entity) -> bool e has a component of type T
  • fn columns(&self) -> Vec<(&'static str, usize)> (type name, values) of every column, in the order they were made (what a world holds: tests, an inspector)
  • fn sort_columns(&mut self) every column in ascending entity order (SparseSet::sort): the canonical order of iteration, the same for a world rebuilt from a snapshot as for the one it came from
  • fn begin_tick(&mut self) the start of a tick, before its systems (Game::step calls it): the tick count goes up, and every Pose written in the tick before gets that state as its previous one (prev = now), the state the next frames interpolate from. Only those poses are touched: a scan of one version per pose
  • fn tick(&self) -> u64 ticks begun so far (begin_tick)
  • fn pose_mark(&self) -> u64 the Pose column's version when the systems of the latest tick began: poses with a newer version moved in that tick and are between two states (the bridge to render3d writes them every frame)

Source: docs/API.md in the JFLib repository.