ludic/packages/ludic.things/store.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

112 lines
3.9 KiB
Text

# store.ludic - the Things, and the only verbs that make, remove, move, show and hide one; each
# placing and removing is a fact, and the port is told at once so the game can draw it
export state ThingsState {
th_list: []Thing = null
th_uid: int = 0
th_facts: Queue<ThingFact> = null
th_marked: int = -1
}
export function things_facts(base_st: mut BaseState, things_st: mut ThingsState) -> Queue<ThingFact> {
if things_st.th_facts == null { things_st.th_facts = queue_new(base_st, "things.facts") }
return things_st.th_facts
}
function th_fact(base_st: mut BaseState, things_st: mut ThingsState, what: int, t: Thing) -> void {
let f = new ThingFact
f.what = what
f.uid = t.uid
f.kind = t.kind
f.id = t.id
f.owner = t.owner
f.x = t.x
f.z = t.z
q_push(base_st, things_facts(base_st, things_st), f)
}
# whether a world has been set out (things_clear) since the program began
export function things_ready(things_st: ThingsState) -> bool { return things_st.th_list != null }
# every Thing, in the order placed; the list is the package's, never to be pushed to
export function things_all(things_st: mut ThingsState) -> []Thing {
if things_st.th_list == null { things_st.th_list = new []Thing }
return things_st.th_list
}
export function things_count(things_st: mut ThingsState) -> int { return len(things_all(things_st)) }
# a new world: every Thing forgotten, quietly (the game has already dropped its drawings)
export function things_clear(things_st: mut ThingsState) -> void { things_st.th_list = new []Thing }
export function thing_spawn(base_st: mut BaseState, things_st: mut ThingsState, kind: int, look: string, x: float, y: float, z: float, yaw: float, id: int) -> Thing {
return th_place(base_st, things_st, new Thing, kind, look, x, y, z, yaw, id)
}
function th_place(base_st: mut BaseState, things_st: mut ThingsState, t: Thing, kind: int, look: string, x: float, y: float, z: float, yaw: float, id: int) -> Thing {
t.kind = kind
t.look = look
t.x = x
t.y = y
t.z = z
t.yaw = yaw
t.id = id
things_st.th_uid += 1
t.uid = things_st.th_uid
push(things_all(things_st), t)
ThingsWorld.placed(t)
th_fact(base_st, things_st, THING_PLACED, t)
return t
}
# a Thing on the ground (the port's), set into it by `sink`, used from `reach`
export function thing_put(base_st: mut BaseState, things_st: mut ThingsState, kind: int, look: string, x: float, z: float, yaw: float, sink: float, reach: float, id: int) -> Thing {
let t = new Thing
t.reach = reach
return th_place(base_st, things_st, t, kind, look, x, ThingsWorld.ground(x, z) - sink, z, yaw, id)
}
# gone for good
export function thing_remove(base_st: mut BaseState, things_st: mut ThingsState, t: Thing) -> void {
if t == null { return }
things_st.th_marked = t.uid
things_drop(base_st, things_st, fn th_is_marked)
things_st.th_marked = -1
}
function th_is_marked(things_st: ThingsState, t: Thing) -> bool { return t.uid == things_st.th_marked }
# every Thing `gone` says so is removed for good
export function things_drop(base_st: mut BaseState, things_st: mut ThingsState, gone: fn(Thing) -> bool) -> void {
let keep = new []Thing
let all = things_all(things_st)
for i in 0 .. len(all) {
let t = all[i]
if gone(t) {
ThingsWorld.removed(t)
th_fact(base_st, things_st, THING_REMOVED, t)
} else {
push(keep, t)
}
}
things_st.th_list = keep
}
export function thing_move(t: Thing, x: float, z: float, yaw: float) -> void {
thing_move_to(t, x, ThingsWorld.ground(x, z), z, yaw)
}
export function thing_move_to(t: Thing, x: float, y: float, z: float, yaw: float) -> void {
t.x = x
t.y = y
t.z = z
t.yaw = yaw
ThingsWorld.moved(t)
}
export function thing_set_active(t: Thing, on: bool) -> void {
if t == null { return }
t.active = on
ThingsWorld.shown(t)
}
export function thing_hide(t: Thing) -> void { thing_set_active(t, false) }
export function thing_show(t: Thing) -> void { thing_set_active(t, true) }