feat(ecs): Light2D/Occluder/Ambient as auto-consumed components (#47)
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 18s
ci / build-and-test (push) Successful in 1m18s
commit-lint / conventional-commits (push) Successful in 4s
docs / build-and-deploy (push) Successful in 19s

The ECS-native shape the 2D lighting follow-up asked for, built on the
engine-owned-system hook from #43. A torch is just an entity carrying
Light2D, a wall an entity carrying Occluder, and one Ambient entity sets
the night tint — the engine runs the whole deterministic light pass at the
end of the Render phase (ambient modulate -> carve occluder shadows ->
accumulate additive radial lights) and presents. No Light.* calls wired.

- runtime/native/systems_light.ludic: esys_light2d, consuming the components
  through the by-name reflection ABI and reusing the #4 accumulation core
  (light_ambient / light_occlude / light_point). Reads position from a
  Position component when present, else the light's own x/y.
- Split from systems.ludic so a SpriteAnim/Motion-only game never links the
  light pass; spliced (with light.ludic) only when Light2D/Occluder declared.
- emit_main now boots rt_init like the auto-loop/test runner, so an
  entry-driven game that renders has its framebuffer allocated (headless:
  allocate only, no window, byte-identical stdout for non-rendering games).

Worked example + regression: examples/library/light_ecs.ludic (32 1 32 1).
Full suite 74 passed, self-host C-free fixpoint intact, no golden drift.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-08-31 15:49:50 +03:00
parent b0143337d9
commit 3679ce1797
8 changed files with 14660 additions and 14404 deletions

View file

@ -0,0 +1,113 @@
# ============================================================================
# systems_light.ludic — the engine-owned 2D lighting render system (#47).
#
# This is the ECS-native shape the lighting follow-up asked for: a torch is just
# an entity carrying `Light2D` (and optionally `Position`), a wall is an entity
# carrying `Occluder`, and the engine runs the whole light pass for you at the
# end of the Render phase — no `Light.point` / `Light.occlude` calls wired by
# hand. It reuses the deterministic accumulation core shipped in #4 (light.ludic:
# light_ambient / light_occlude / light_point), consuming components through the
# by-name reflection ABI instead of imperative calls.
#
# Split from systems.ludic because it links against the light pass (light.ludic):
# a game that uses SpriteAnim/Motion but no lights must not pull light_point in,
# so esys_light2d lives in its own file, spliced only when Light2D / Occluder is
# declared (parse.ludic). It is inserted into the Render phase after the game's
# own draw handlers (emit_engine_systems_for_phase), so the scene is already on
# the framebuffer when the light pass runs; it finishes by presenting the frame,
# so an ECS-lit game leaves Screen.show to the engine.
#
# Component contracts:
# property Ambient { color: int } # optional, one entity
# property Light2D { x, y, radius, color, intensity } # intensity is 0..100 %
# property Occluder { x, y, w, h }
# A Light2D / Occluder reads its position from a `Position { x, y }` component on
# the same entity when the entity carries one, else from its own x / y fields —
# so "Position + Light2D" and a self-positioned Light2D both work.
# ============================================================================
# read entity `e`'s world x: a `Position { x }` component wins when present, else
# the component's own x field (fx) on prop `p`. Same idea for y below.
function esys_pos_x(e: int, p: int, fx: int) -> int {
let pp = World.prop_id("Position")
if pp >= 0 {
if World.has(e, pp) != 0 {
let f = World.field_id(pp, "x")
if f >= 0 { return World.get(e, pp, f) }
}
}
if fx >= 0 { return World.get(e, p, fx) }
return 0
}
function esys_pos_y(e: int, p: int, fy: int) -> int {
let pp = World.prop_id("Position")
if pp >= 0 {
if World.has(e, pp) != 0 {
let f = World.field_id(pp, "y")
if f >= 0 { return World.get(e, pp, f) }
}
}
if fy >= 0 { return World.get(e, p, fy) }
return 0
}
function esys_light2d() -> void {
let pl = World.prop_id("Light2D")
if pl < 0 { return }
# 1. ambient — a single `Ambient { color }` entity modulates the whole scene
# toward its tint (night / cave). Absent: the scene keeps its drawn brightness.
let pa = World.prop_id("Ambient")
if pa >= 0 {
let af = World.field_id(pa, "color")
let ae = World.query_next(pa, 0)
if ae >= 0 {
if af >= 0 { light_ambient(World.get(ae, pa, af)) }
}
}
# 2. occluders — re-register every `Occluder` as a shadow caster this frame.
light_clear_occluders()
let po = World.prop_id("Occluder")
if po >= 0 {
let oxf = World.field_id(po, "x")
let oyf = World.field_id(po, "y")
let owf = World.field_id(po, "w")
let ohf = World.field_id(po, "h")
var oe = World.query_next(po, 0)
while oe >= 0 {
let ox = esys_pos_x(oe, po, oxf)
let oy = esys_pos_y(oe, po, oyf)
var ow = 0
var oh = 0
if owf >= 0 { ow = World.get(oe, po, owf) }
if ohf >= 0 { oh = World.get(oe, po, ohf) }
light_occlude(ox, oy, ow, oh)
oe = World.query_next(po, oe + 1)
}
}
# 3. lights — additively accumulate each `Light2D` as a radial glow, cut by the
# occluders registered above (hard shadows).
let lxf = World.field_id(pl, "x")
let lyf = World.field_id(pl, "y")
let lrf = World.field_id(pl, "radius")
let lcf = World.field_id(pl, "color")
let lif = World.field_id(pl, "intensity")
var e = World.query_next(pl, 0)
while e >= 0 {
let x = esys_pos_x(e, pl, lxf)
let y = esys_pos_y(e, pl, lyf)
var radius = 0
if lrf >= 0 { radius = World.get(e, pl, lrf) }
var color = 16777215 # 0xFFFFFF (white) default
if lcf >= 0 { color = World.get(e, pl, lcf) }
var energy = fixed(1) # full intensity default
if lif >= 0 { energy = fixed(World.get(e, pl, lif)) / fixed(100) }
light_point(x, y, radius, color, energy)
e = World.query_next(pl, e + 1)
}
# 4. present — the engine owns the flip for an ECS-lit frame.
Screen.show()
}