feat(lang): 0.R1 - actions and reducers
action Name { fields } is a typed record; reducer State on Action(s: mut State, a: Action) { ... }
in the module that owns the state takes exactly that state and the action (a second state is
refused); dispatch Action { fields } queues one from anywhere, the queue supplied by the runtime.
The queue is drained at the end of every phase of the frame loop, after every phase of ludic.base's
core_tick_all, and by drain_actions(): in dispatch order, each action's reducers in the order of
their states' names, an action a reducer dispatches queued behind, a queue still growing after 64
rounds stopped with the action named. Examples actions/pack, phases, runaway; rejects for a second
state, a reducer on a non-action and an unknown dispatch; ludic.base's actions_test; LANGUAGE.md.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3a87d02696
commit
808c4a6f7a
19 changed files with 415 additions and 1 deletions
|
|
@ -63,6 +63,7 @@ export function core_tick_all(base_st: mut BaseState, t: Tick) -> void {
|
|||
for i in 0 .. len(all) {
|
||||
if all[i].phase == ph and all[i].tick != null { all[i].tick(t) }
|
||||
}
|
||||
drain_actions() # 0.R: the phase's actions, reduced before the next
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
30
packages/ludic.base/tests/actions_test.ludic
Normal file
30
packages/ludic.base/tests/actions_test.ludic
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# actions_test.ludic - 0.R: the runner drains the action queue after every phase, so what an input
|
||||
# system dispatches is reduced before the simulation reads it, the same tick
|
||||
import "ludic.base"
|
||||
program ActionsTest {
|
||||
numbers float
|
||||
state Door {
|
||||
open: bool = false
|
||||
seen: string = ""
|
||||
}
|
||||
action Knock { times: int }
|
||||
reducer Door on Knock(d: mut Door, a: Knock) {
|
||||
if a.times >= 2 { d.open = true }
|
||||
}
|
||||
function input_tick(t: Tick) -> void { dispatch Knock { times: 2 } }
|
||||
function sim_tick(d: mut Door, t: Tick) -> void {
|
||||
if d.open { d.seen = d.seen + "o" } else { d.seen = d.seen + "c" }
|
||||
}
|
||||
test "an input system's action is reduced before the simulation's phase" (b: mut BaseState, d: Door) {
|
||||
core_clear(b)
|
||||
let i = system_new("input", PH_INPUT)
|
||||
i.tick = fn input_tick
|
||||
core_add(b, i)
|
||||
let s = system_new("sim", PH_SIMULATE)
|
||||
s.tick = fn sim_tick
|
||||
core_add(b, s)
|
||||
core_tick_all(b, tick_new(0.016, 1, 0.0))
|
||||
expect(d.open)
|
||||
expect(d.seen == "o")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue