ludic/runtime/native/tiled_spawn.ludic
Orkuncakilkaya b8c71d2a8e
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 21s
ci / build-and-test (push) Successful in 1m57s
commit-lint / conventional-commits (push) Successful in 3s
docs / build-and-deploy (push) Successful in 27s
feat(stdlib): Tiled P4 — objects, custom props/types, templates, opt-in spawn (#72)
- Object layers: all shapes (rectangle/ellipse/point/polygon/polyline/text) and
  custom properties parse and are queryable — Tiled.object_count / Tiled.object /
  Tiled.object_shape, and Tiled.prop / prop_int / prop_type over any object /
  tile / layer / map.
- Custom types: Tiled.load_types reads an objecttypes.xml project custom-type
  table so a class property resolves its default; enum values resolve as strings.
- Templates: Tiled.template reads a .tx/.tj, and Tiled.load merges each object's
  `template` reference under the instance's overrides (field inheritance).
- Opt-in spawning: Tiled.spawn / Tiled.spawn_layer map an object's class +
  properties onto Ludic components through the reflection ABI (Position from x/y,
  each property to a like-named field). Off by default — no gameplay coupling in
  the core load. Split into tiled_spawn.ludic, spliced only for a Tiled *game*
  (the world table exists only under has_ecs), so a plain map-reading tool never
  links against the reflection ABI.

Proven by library/tiled_p4.ludic (18 assertions) incl. the design's named
orthogonal-outside.tmx object shapes. x test: 95 passed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-01 15:08:06 +03:00

55 lines
2.4 KiB
Text

# ============================================================================
# tiled_spawn.ludic — Tiled P4 (#72) opt-in entity spawning. Split from
# tiled.ludic because it stands on the reflection ABI (world_spawn / world_set /
# world_field_id …), which the compiler emits only for a game (has_ecs); so this
# fragment is spliced only when a Tiled program is itself a game. A plain tool
# that reads maps never references the world table.
# ============================================================================
# ---- opt-in entity spawning ------------------------------------------------
# set an int field named `fname` on entity e wherever it exists (any prop the
# entity carries) — the by-name bridge from object properties to components.
function tiled_apply_field(e: int, fname: pointer, v: int) -> void {
let np = world_prop_count()
var pi = 0
while pi < np {
if world_has(e, pi) == 1 {
let fid = world_field_id(pi, fname)
if fid >= 0 { world_set(e, pi, fid, v) }
}
pi = pi + 1
}
}
# spawn a Ludic entity of `model_name` from an object: its Position (if the model
# has one) takes the object's x/y, and each object custom property sets a like-
# named int/bool component field. Returns the entity, or -1 if the model is
# unknown. Opt-in — the core load never calls this.
function tiled_spawn_object(m: Tmap, obj: Val, model_name: pointer) -> int {
let mid = world_model_id(model_name)
if mid < 0 { return 0 - 1 }
let e = world_spawn(mid)
let pp = world_prop_id("Position")
if pp >= 0 and world_has(e, pp) == 1 {
let fx = world_field_id(pp, "x")
let fy = world_field_id(pp, "y")
if fx >= 0 { world_set(e, pp, fx, value_as_int(value_get(obj, "x"))) }
if fy >= 0 { world_set(e, pp, fy, value_as_int(value_get(obj, "y"))) }
}
let props = value_get(obj, "properties")
var i = 0
while i < value_count(props) {
let p = value_at(props, i)
tiled_apply_field(e, value_as_str(value_get(p, "name")), tiled_prop_int(obj, value_as_str(value_get(p, "name"))))
i = i + 1
}
return e
}
# spawn every object on `layer` as `model_name` (opt-in). Returns the count.
function tiled_spawn_layer(m: Tmap, layer: int, model_name: pointer) -> int {
var n = 0
let c = tmap_object_count(m, layer)
var i = 0
while i < c { if tiled_spawn_object(m, tmap_object(m, layer, i), model_name) >= 0 { n = n + 1 }; i = i + 1 }
return n
}