Files
ecs/ecs/observers.sakura
T
2026-10-30 09:55:40 -05:00

115 lines
3.8 KiB
Plaintext

#! Contains observers: functions a world runs as an entity is given a
#! component, or loses one, as a game keeps what follows from it up to date.
module ecs;
#! What an observer runs on: an entity given a component it lacked, or losing
#! one, as it's taken or the entity despawned.
pub enum change: u8 {
added,
removed,
}
#! An observer a world runs, which `unobserve` takes out.
pub struct observer {
val index: u64;
}
# An observer: the component it watches, the change it runs on, and what it
# runs, or nothing once taken out.
struct watch {
var id: u32;
var on: change;
var run: def(&mut world, entity): void;
var live: bool;
}
#! Runs a function each time an entity is given a component it lacked, or
#! loses one, given the world and the entity: as it's given, once the value is
#! there, or as it loses it, while the value still is, so it reads it. Several
#! observe a component in the order they were added.
#!
#! What it changes that would move an entity, as a component given or taken, a
#! spawn, or a despawn, is made once the change it observed is done, as the
#! world defers it, and runs observers in turn. A component replaced, or
#! written, runs none. Observers run on the world's thread, once what threads
#! sharing it deferred is made.
#!
#! ```sakura
#! _ = w.observe::<health>(change::removed, def(w, e) { ... });
#! ```
#! @remarks A lambda's captures must outlast the world, which keeps it.
pub def world::observe<T>(&mut this, on: change, run: def(&mut world, entity): void): observer {
this.refuse_while_shared("be given observers");
if this.parallel {
fault("A function `par_each` calls can't give the world observers.");
}
val id := this.id_of(info_of::<T>());
this.watches.append(watch{ id = id, on = on, run = run, live = true });
while this.watched.length <= id as u64 {
this.watched.append(0 as u8);
}
this.watched[id as u64] |= bit_of(on);
return observer{ index = this.watches.length - 1 };
}
#! Takes out an observer, which runs no more.
#! @returns Whether it ran till now.
pub def world::unobserve(&mut this, o: observer): bool {
this.refuse_while_shared("have observers taken");
return false if o.index >= this.watches.length || !this.watches[o.index].live;
val w := &mut this.watches[o.index];
w.live = false;
var still: u8 = 0;
for i in 0..this.watches.length {
val each := &this.watches[i];
if each.live && each.id == w.id {
still |= bit_of(each.on);
}
}
this.watched[w.id as u64] = still;
return true;
}
def bit_of(on: change): u8 {
return on == change::added ? 1 as u8 : 2 as u8;
}
# Whether any observer watches a component, for a change.
def world::watches_for(&this, id: u32, on: change): bool {
return id < this.watched.length && (this.watched[id as u64] & bit_of(on)) != 0;
}
# Runs the observers of a change to an entity's component, the world
# deferring what they change till the change they observed is done.
def world::notify(&mut this, e: entity, id: u32, on: change): void {
return if !this.watches_for(id, on);
this.deferring++;
for i in 0..this.watches.length {
val each: watch = this.watches[i];
if each.live && each.id == id && each.on == on {
val run := each.run;
run(this, e);
}
}
this.deferring--;
}
# Runs the observers of an entity given each component of a table, as it's
# spawned with them.
def world::notify_all(&mut this, e: entity, table: u32, on: change): void {
return if this.watches.length == 0;
val count := this.tables[table as u64].ids.length;
for i in 0..count {
this.notify(e, this.tables[table as u64].ids[i], on);
}
}
# Makes what observers deferred, once a change is done, where no pass goes
# over the world.
def world::settle(&mut this): void {
if this.deferring == 0 && this.pending.length > 0 {
this.flush();
}
}