plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
c0a9ec9
— Peter John
2026-11-10T22:12:15+05:30
Game: a screen that returns itself with another key stays the current screen
- std/game/Game.plum +78 -19
std/game/Game.plum
CHANGED
|
@@ -252,14 +252,18 @@ struct Game<S: Reducer<M>, M>(
|
|
|
252
252
|
fun tick(self, io: Io, input: Input, dt: Float, view: Vec2) -> Game<S, M> =
|
|
253
253
|
ctx: Ctx<M> := Ctx.of(input, view)
|
|
254
254
|
key := self.currentKey()
|
|
255
|
+
name := self.current
|
|
255
256
|
routed := routeAll(self.liveAt(key), ctx, input.events, 0, self.pointer)
|
|
256
257
|
stepped := routed.step(ctx, dt)
|
|
258
|
+
# The screen's key after each stage, in case the stage returned the screen with another.
|
|
259
|
+
stepKey := keyNow<S, M>(stepped.root.kids(), key, name)
|
|
257
|
-
ruled := onScreen<S, M>(stepped,
|
|
260
|
+
ruled := onScreen<S, M>(stepped, stepKey, |s, t| s.rules(t, ctx, self.store.state, dt))
|
|
258
261
|
applied := ruled.apply(ctx.out)
|
|
259
262
|
ctx.keepOnly(true)
|
|
263
|
+
now := keyNow<S, M>(applied.root.kids(), stepKey, name)
|
|
260
|
-
back := self.tree.withRoot(spliced(self.tree.root, applied.root, key))
|
|
264
|
+
back := self.tree.withRoot(spliced(self.tree.root, applied.root, key, now))
|
|
261
|
-
begin: Turn<S, M> := Turn(tree: back, store: self.store, restart: false, exit: false)
|
|
265
|
+
begin: Turn<S, M> := Turn(tree: back, store: self.store, key: now, restart: false, exit: false)
|
|
262
|
-
turn := ctx.out.fold(begin, |tu, c| tu.take(c,
|
|
266
|
+
turn := ctx.out.fold(begin, |tu, c| tu.take(c, name))
|
|
263
267
|
flushed := turn.store.flush(io)
|
|
264
268
|
pointer := lastPointer(input.events, self.pointer)
|
|
265
269
|
if turn.restart && !turn.exit
|
|
@@ -269,7 +273,7 @@ struct Game<S: Reducer<M>, M>(
|
|
|
269
273
|
..self, tree: self.first, store: Store.of(self.initial), current: again, screenKey: keyOf<S, M>(self.first, again), unknown: "", shift: none,
|
|
270
274
|
ticks: self.ticks + 1, pointer: pointer, view: view)
|
|
271
275
|
else
|
|
272
|
-
ran := Game(..self, tree: turn.tree, store: flushed, shift: advanced(self.shift, dt), exited: turn.exit, ticks: self.ticks + 1, pointer: pointer, view: view)
|
|
276
|
+
ran := Game(..self, tree: turn.tree, store: flushed, screenKey: turn.key, shift: advanced(self.shift, dt), exited: turn.exit, ticks: self.ticks + 1, pointer: pointer, view: view)
|
|
273
277
|
# Only a message changes the state, so a tick that sent none names the same screen.
|
|
274
278
|
if @sameRef(turn.store, self.store) then ran else ran.switched(io)
|
|
275
279
|
|
|
@@ -294,9 +298,10 @@ struct Game<S: Reducer<M>, M>(
|
|
|
294
298
|
st := self.store.state
|
|
295
299
|
whole := self.tree
|
|
296
300
|
entered := onScreen<S, M>(self.tree, key, |s, t| s.enter(t, st, whole))
|
|
301
|
+
now := keyNow<S, M>(entered.root.kids(), key, to)
|
|
297
|
-
how := transitionTo<S, M>(entered,
|
|
302
|
+
how := transitionTo<S, M>(entered, now, self.current)
|
|
298
303
|
shift: Option<Shift> := if how.secs() > 0.0 then Some(Shift(from: self.current, how: how, t: 0.0)) else None
|
|
299
|
-
Game(..self, tree: entered, current: to, screenKey:
|
|
304
|
+
Game(..self, tree: entered, current: to, screenKey: now, unknown: "", shift: shift)
|
|
300
305
|
|
|
301
306
|
# Draws a frame: the current screen's `draw` (by default the tree's) of what is shown.
|
|
302
307
|
fun draw(self, io: Io) =
|
|
@@ -343,6 +348,26 @@ fun keyAmong<S, M>(kids: RoseTree<Actor<M>>, name: Str) -> Int = match kids
|
|
|
343
348
|
RoseEmpty => NoScreen
|
|
344
349
|
RoseNode(a, _, n) => if nameOf<S, M>(a) == name then a.key() else keyAmong<S, M>(n, name)
|
|
345
350
|
|
|
351
|
+
# The key now of the screen named `name` among the root's children `kids` (a live root's
|
|
352
|
+
# screen may be under its `ShiftFrame`), which was `key`: `key` itself while a child still
|
|
353
|
+
# has it (one walk over the screens' keys, no names compared), else the key of the child
|
|
354
|
+
# named `name`. So a screen whose `step`, `input`, `rules`, `react` or `enter` returned it
|
|
355
|
+
# with another key is still the current screen. `NoScreen` if it is gone.
|
|
356
|
+
fun keyNow<S, M>(kids: RoseTree<Actor<M>>, key: Int, name: Str) -> Int = match kids
|
|
357
|
+
RoseEmpty => NoScreen
|
|
358
|
+
RoseNode(a, k, _) =>
|
|
359
|
+
if a.key() == ShiftFrameId
|
|
360
|
+
keyNow<S, M>(k, key, name)
|
|
361
|
+
else if hasKey(kids, key)
|
|
362
|
+
key
|
|
363
|
+
else
|
|
364
|
+
keyAmong<S, M>(kids, name)
|
|
365
|
+
|
|
366
|
+
# Whether an actor of the chain `kids` (not their subtrees) has the key `key`.
|
|
367
|
+
fun hasKey<M>(kids: RoseTree<Actor<M>>, key: Int) -> Bool = match kids
|
|
368
|
+
RoseEmpty => false
|
|
369
|
+
RoseNode(a, _, n) => a.key() == key || hasKey(n, key)
|
|
370
|
+
|
|
346
371
|
# The name `asked` stands for in `tree`: itself, or for "" (a `Reducer`'s default) the
|
|
347
372
|
# first screen's.
|
|
348
373
|
fun chosen<S, M>(tree: SceneTree<M>, asked: Str) -> Str =
|
|
@@ -396,17 +421,17 @@ fun framed<M>(n: RoseTree<Actor<M>>, f: ShiftFrame<M>) -> RoseTree<Actor<M>> =
|
|
|
396
421
|
empty: RoseTree<Actor<M>> := RoseEmpty
|
|
397
422
|
RoseNode(value: frame, firstChild: n, nextSibling: empty)
|
|
398
423
|
|
|
399
|
-
# The whole tree `whole` after the tick changed the root and the screen
|
|
424
|
+
# The whole tree `whole` after the tick changed the root and the screen in `live` (the root
|
|
400
|
-
#
|
|
425
|
+
# over that screen, as `Game.live` makes it), which was keyed `was` and is now keyed
|
|
401
|
-
# in its place among the others (or dropped, if
|
|
426
|
+
# `key`: the new root, the screen put back in its place among the others (or dropped, if
|
|
402
|
-
# under the root added after them.
|
|
427
|
+
# it was killed), and anything spawned under the root added after them.
|
|
403
|
-
fun spliced<M>(whole: RoseTree<Actor<M>>, live: RoseTree<Actor<M>>, key: Int) -> RoseTree<Actor<M>> =
|
|
428
|
+
fun spliced<M>(whole: RoseTree<Actor<M>>, live: RoseTree<Actor<M>>, was: Int, key: Int) -> RoseTree<Actor<M>> =
|
|
404
429
|
match whole
|
|
405
430
|
RoseEmpty => whole
|
|
406
431
|
RoseNode(r0, k, n0) => match live
|
|
407
432
|
RoseEmpty => live
|
|
408
433
|
RoseNode(r, lk, _) =>
|
|
409
|
-
put := replaceKid(k,
|
|
434
|
+
put := replaceKid(k, was, liveScreen(lk, key))
|
|
410
435
|
extras := liveExtras(lk, key)
|
|
411
436
|
kids := if extras.isEmpty() then put else put.concat(extras)
|
|
412
437
|
if @sameRef(r, r0) && @sameRef(kids, k) && n0.isEmpty()
|
|
@@ -489,15 +514,17 @@ fun routeAll<M>(t: SceneTree<M>, ctx: Ctx<M>, evs: Slice<InputEvent>, i: Int, at
|
|
|
489
514
|
fun lastPointer(evs: Slice<InputEvent>, at: Vec2) -> Vec2 =
|
|
490
515
|
evs.fold(at, |p, e| e.position().unwrapOr(p))
|
|
491
516
|
|
|
492
|
-
# A tick's game commands taken so far: the tree and store after the messages,
|
|
517
|
+
# A tick's game commands taken so far: the tree and store after the messages, the current
|
|
518
|
+
# screen's key (a `react` may return the screen with another), and whether a `Restart` or
|
|
493
|
-
#
|
|
519
|
+
# an `Exit` came. `take` takes one more command; `name` is the current screen's name.
|
|
494
|
-
struct Turn<S: Reducer<M>, M>(tree: SceneTree<M>, store: Store<S, M>, restart: Bool, exit: Bool) =
|
|
520
|
+
struct Turn<S: Reducer<M>, M>(tree: SceneTree<M>, store: Store<S, M>, key: Int, restart: Bool, exit: Bool) =
|
|
495
|
-
fun take(self, c: Cmd<M>,
|
|
521
|
+
fun take(self, c: Cmd<M>, name: Str) -> Turn<S, M> = match c
|
|
496
522
|
Send(m) =>
|
|
497
523
|
store := self.store.dispatch(m)
|
|
498
524
|
st := store.state
|
|
499
|
-
tree := onScreen<S, M>(self.tree,
|
|
525
|
+
tree := onScreen<S, M>(self.tree, self.key, |s, t| s.react(t, st, m))
|
|
526
|
+
key := if @sameRef(tree, self.tree) then self.key else keyNow<S, M>(tree.root.kids(), self.key, name)
|
|
500
|
-
Turn(..self, tree: tree, store: store)
|
|
527
|
+
Turn(..self, tree: tree, store: store, key: key)
|
|
501
528
|
Restart => Turn(..self, restart: true)
|
|
502
529
|
Exit => Turn(..self, exit: true)
|
|
503
530
|
_ => self
|
|
@@ -643,6 +670,19 @@ struct GOnlyScreen[Screen<GOne, GMsg>](id: Int) =
|
|
|
643
670
|
fun gboxes(n: Int, acc: List<RoseTree<Actor<GMsg>>>) -> List<RoseTree<Actor<GMsg>>> =
|
|
644
671
|
if n == 0 then acc else gboxes(n - 1, Cons(head: RoseTree.leaf(Box(id: 100 + n, x: 0.0, y: 0.0, w: 1.0, h: 1.0, color: Color.Red)), tail: acc))
|
|
645
672
|
|
|
673
|
+
# A screen that takes another key each time it reacts to a message, and each time its
|
|
674
|
+
# rules run when `byRules`.
|
|
675
|
+
struct GRekeyScreen[Screen<GOne, GMsg>](id: Int, byRules: Bool) =
|
|
676
|
+
fun key(self) -> Int = self.id
|
|
677
|
+
|
|
678
|
+
fun place(self) -> Place = place.at(5.0, 0.0)
|
|
679
|
+
|
|
680
|
+
fun rules(self, t: SceneTree<GMsg>, ctx: Ctx<GMsg>, state: GOne, dt: Float) -> SceneTree<GMsg> =
|
|
681
|
+
if self.byRules then t.withTop(GRekeyScreen(..self, id: self.id + 1)) else t
|
|
682
|
+
|
|
683
|
+
fun react(self, t: SceneTree<GMsg>, state: GOne, msg: GMsg) -> SceneTree<GMsg> =
|
|
684
|
+
if self.byRules then t else t.withTop(GRekeyScreen(..self, id: self.id + 1))
|
|
685
|
+
|
|
646
686
|
GameTestView = Vec2(x: 800.0, y: 600.0)
|
|
647
687
|
|
|
648
688
|
fun ginput(events: List<InputEvent>) -> Input =
|
|
@@ -905,3 +945,22 @@ test "a hidden screen is skipped: not stepped, drawn or hit, and left as it was"
|
|
|
905
945
|
after := g5.tree.subtree(GamePlayKey).unwrap()
|
|
906
946
|
assert after.root.kids() == before.root.kids()
|
|
907
947
|
assert after.root.kids().rest().rest().top().unwrap() == before.root.kids().rest().rest().top().unwrap()
|
|
948
|
+
|
|
949
|
+
test "a screen replaced by one with the same name and another key stays the current screen"
|
|
950
|
+
log := Slice<Str>()
|
|
951
|
+
g := Game.start(SceneTree.ofScreens([RoseTree.of(GRekeyScreen(id: 500, byRules: false), gprobes(log, 1, false))]), GOne(n: 0))
|
|
952
|
+
g1 := g.step(io, ginput(Nil), 0.1, GameTestView)
|
|
953
|
+
# two messages (the probes' steps), each reacted to: the screen is now keyed 502
|
|
954
|
+
assert g1.state().n == 2
|
|
955
|
+
assert g1.currentKey() == 502
|
|
956
|
+
assert g1.screenTree().count() == 3 && g1.shown().count() == 4
|
|
957
|
+
g2 := g1.step(io, ginput(Nil), 0.1, GameTestView)
|
|
958
|
+
assert gexact(log, "step 1") == 2 && g2.state().n == 4 && g2.currentKey() == 504
|
|
959
|
+
# rekeyed by its rules, on a tick with messages and on one with none
|
|
960
|
+
r := Game.start(SceneTree.ofScreens([RoseTree.of(GRekeyScreen(id: 500, byRules: true), gprobes(log, 1, false))]), GOne(n: 0))
|
|
961
|
+
r2 := r.step(io, ginput(Nil), 0.1, GameTestView).step(io, ginput(Nil), 0.1, GameTestView)
|
|
962
|
+
assert r2.currentKey() == 502 && r2.state().n == 4 && r2.screenTree().count() == 3
|
|
963
|
+
alone: RoseTree<Actor<GMsg>> := RoseTree.leaf(GRekeyScreen(id: 500, byRules: true))
|
|
964
|
+
q := Game.start(SceneTree.ofScreens([alone]), GOne(n: 0))
|
|
965
|
+
q2 := q.step(io, ginput(Nil), 0.1, GameTestView).step(io, ginput(Nil), 0.1, GameTestView)
|
|
966
|
+
assert q2.currentKey() == 502 && q2.screenTree().count() == 1
|