ludic/examples/library/prefabs.ludic
Orkuncakilkaya 19fcf60599 wip(0.S3): packages and examples migrated again from their pre-0.S sources in one run
ludic migrate state packages <every example program> packages/ludic.lab/example/plate.ludic
  1804 vars into 126 states, 64 into lets; 23498 edits in 460 files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 15:31:34 +03:00

27 lines
1.4 KiB
Text

# prefabs.ludic — prefabs: a model with preset fields, chained (`Grunt: Foe: Creature`),
# spawned by name with overrides, `spawn` as an expression, `Prefab.spawn` / `spawn_at`
# by a runtime name, and `self()` inside @OnSpawn. Deterministic; prints:
# 230159 30 0 7 44 -1 2
program Prefabs {
import "ludic.core/components.ludic"
property Enemy { kind: int = 0, hp: int = 10, team: int = 0 }
model Creature { Position, Enemy }
prefab Foe: Creature { Enemy { team: 2, hp: 1 }, Position { y: 9 } }
prefab Grunt: Foe { Enemy { kind: 1, hp: 30 } }
state PrefabsState {
last: int = -1
}
@OnSpawn(Creature) handler Remember(prefabs_st: mut PrefabsState) { prefabs_st.last = self() }
entry (prefabs_st: PrefabsState) {
let g = spawn Grunt { Position { x: 5 } } # Foe's presets, Grunt's, then this
print(Enemy.of(g).team * 100000 + Enemy.of(g).hp * 1000 + Enemy.of(g).kind * 100 + Position.of(g).x * 10 + Position.of(g).y)
let n = Prefab.spawn(name: "Grunt")
print(Enemy.of(n).hp)
print(g) # the first entity
let placed = Prefab.spawn_at(name: "Foe", at: IVec2.make(7, 44))
print(Position.of(placed).x)
print(Position.of(placed).y)
print(Prefab.spawn(name: "Nope")) # no such prefab
print(prefabs_st.last) # self() in the hook: the last spawn
}
}