Lifecycle hooks: @OnStart/@OnQuit, @OnDespawn, @OnAttach (like @OnSpawn)
Adds the rest of the game/entity/property lifecycle as @-hooks, each firing at one timeline moment and reducing to ordinary code: - @OnStart / @OnQuit (program): @OnStart aliases the Start phase; @OnQuit runs in the frame loop's done: block, after the loop stops and before teardown. - @OnDespawn(Model) (entity): a destructor. Despawn doesn't statically know the entity's model, so hooks compile to @on_despawn_<Model>(entity) functions and emit_despawn dispatches on @L_kind. Symmetric with @OnSpawn. - @OnAttach(Property): fires in emit_init_component once a property is attached and seeded, with the property bound by name. Registries + parsing mirror @OnSpawn. examples/lifecycle.ludic narrates the whole timeline (output 1 700 50 950 2). Behaviour-preserving (goldens byte-identical, despawn users unaffected when no hooks registered); test.sh 16/16, fixpoint holds. Deferred: @OnDetach (per-property runtime dispatch) and scene @OnEnter/@OnExit (need scene support in the compiler). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
28abd3a2c3
commit
6e11212cf2
8 changed files with 3993 additions and 3258 deletions
28
examples/lifecycle.ludic
Normal file
28
examples/lifecycle.ludic
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# lifecycle.ludic — a game's whole lifecycle as @-hooks. Each fires at one point
|
||||
# on the timeline and reduces to ordinary code, so nothing is hidden and the
|
||||
# data-oriented model is untouched. Running it prints: 1 700 50 950 2 — one line
|
||||
# per hook, in the order the timeline reaches them.
|
||||
#
|
||||
# boot ── @OnStart ──▶ spawn ── @OnAttach, @OnSpawn ──▶ … ── @OnDespawn ──▶ quit ── @OnQuit
|
||||
program Life {
|
||||
property Health { hp: int = 0, max: int = 100 }
|
||||
property Sprite { id: int = 0 }
|
||||
model Enemy { Health, Sprite }
|
||||
|
||||
# ---- program lifecycle -----------------------------------------------------
|
||||
@OnStart handler Boot { print_int(1) } # once, at boot
|
||||
@OnQuit handler Bye { print_int(2) } # once, at shutdown
|
||||
|
||||
# ---- property / entity lifecycle -------------------------------------------
|
||||
@OnAttach(Sprite) handler Load { print_int(Sprite.id + 700) } # when a Sprite is attached
|
||||
@OnSpawn(Enemy) handler Init { Health.hp = Health.max } # constructor
|
||||
@OnDespawn(Enemy) handler Clean { print_int(Health.hp + 900) } # destructor (dispatched by kind)
|
||||
|
||||
handler Seed phase Start { spawn Enemy { Health { max: 50 } } }
|
||||
|
||||
handler Run phase Render {
|
||||
for (h) in query [Health] { print_int(h.hp) } # 50 — Init set hp to max
|
||||
for (e) in query [Health] { despawn self() } # Clean fires: 50 + 900 = 950
|
||||
quit()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue