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 Entityan 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 columnconst fn index(self) -> u32the slot of the entityconst fn generation(self) -> u32how often its slot was handed out before itconst fn to_bits(self) -> u64generation in the high, index in the low 32 bits (for a message or a file)const fn from_bits(bits: u64) -> Entitythe entity of to_bitsstruct Entitiesthe allocator of entities (World holds one)
impl Entities
const fn new() -> Entitiesno entitiesfn spawn(&mut self) -> Entitya new entity: the slot freed last, with its next generation, else a new slotfn despawn(&mut self, e: Entity) -> boole dies, its slot is free for the next generation (false: e was not alive)fn alive(&self, e: Entity) -> boole lives: its slot is taken and has e's generationfn at(&self, index: u32) -> Option<Entity>the entity alive in slotindexfn len(&self) -> u32entities alivefn is_empty(&self) -> boolnone alivefn slots(&self) -> u32slots so far (alive, free, spent)fn iter(&self) -> impl Iterator<Item = Entity> + '_the live entities in ascending order of their indextrait Bundlea bundle of components for World::spawn_with: a tuple of one to six values of different typesfn insert_into(self, world: &mut World, e: Entity)every value to e (World::insert)
struct Worldentities and their components (see the module header)
impl World
fn new() -> Worldan empty worldfn entities(&self) -> &Entitiesthe allocator: which entities livefn spawn(&mut self) -> Entitya new entity without componentsfn spawn_with<B: Bundle>(&mut self, bundle: B) -> Entitya new entity with the components of a tuple:world.spawn_with((Pose::at(p), Health(100)))fn despawn(&mut self, e: Entity) -> boole dies with all its components (false: e was not alive: a stale id changes nothing)fn alive(&self, e: Entity) -> boole livesfn len(&self) -> u32entities alivefn is_empty(&self) -> boolnone alivefn 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 typefn insert<T: 'static>(&mut self, e: Entity, value: T) -> boole gets the component (replacing one of the same type); false: e is not alive, nothing changesfn remove<T: 'static>(&mut self, e: Entity) -> Option<T>e's component of type T goes; the valuefn get<T: 'static>(&self, e: Entity) -> Option<&T>e's component of type Tfn 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) -> boole has a component of type Tfn 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 fromfn 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 posefn tick(&self) -> u64ticks begun so far (begin_tick)fn pose_mark(&self) -> u64the 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)