plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
78e191a
— Peter John
2026-11-09T21:26:04+05:30
Game tick: no sine for unturned places, one walk per pointer event, no empty walk sets
- std/game/Game.plum +20 -12
- std/game/scene/Ctx.plum +17 -6
- std/game/scene/Place.plum +15 -3
- std/game/scene/SceneTree.plum +50 -28
std/game/Game.plum
CHANGED
|
@@ -218,11 +218,14 @@ struct Game[S: Reducer[M], M](
|
|
|
218
218
|
|
|
219
219
|
# The root over the screen that is stepped and gets input: the current one, under its
|
|
220
220
|
# `ShiftFrame` during a transition.
|
|
221
|
+
fun live(self) -> SceneTree[M] = self.liveAt(self.currentKey())
|
|
222
|
+
|
|
223
|
+
# `live`, with `key` the current screen's key.
|
|
221
|
-
fun
|
|
224
|
+
fun liveAt(self, key: Int) -> SceneTree[M] =
|
|
222
225
|
match self.tree.root
|
|
223
226
|
FRoseEmpty => self.tree
|
|
224
227
|
FRoseNode(r, k, _) =>
|
|
225
|
-
now := screenNode(k,
|
|
228
|
+
now := screenNode(k, key)
|
|
226
229
|
kids := match self.shift
|
|
227
230
|
Some(s) => framed(now, s.frame(true, self.view))
|
|
228
231
|
None => now
|
|
@@ -241,7 +244,7 @@ struct Game[S: Reducer[M], M](
|
|
|
241
244
|
fun tick(self, io: Io, input: Input, dt: Float, view: Vec2) -> Game[S, M] =
|
|
242
245
|
ctx: Ctx[M] := Ctx.of(input, view)
|
|
243
246
|
key := self.currentKey()
|
|
244
|
-
routed := routeAll(self.
|
|
247
|
+
routed := routeAll(self.liveAt(key), ctx, input.events, 0, self.pointer)
|
|
245
248
|
stepped := routed.step(ctx, dt)
|
|
246
249
|
ruled := onScreen[S, M](stepped, key, |s, t| s.rules(t, ctx, self.store.state, dt))
|
|
247
250
|
applied := ruled.apply(ctx.out)
|
|
@@ -265,10 +268,15 @@ struct Game[S: Reducer[M], M](
|
|
|
265
268
|
# screen, and is reported once (`unknown`, and a line on the host's output).
|
|
266
269
|
fun switched(self, io: Io) -> Game[S, M] =
|
|
267
270
|
to := chosen[S, M](self.tree, self.store.state.screen())
|
|
268
|
-
key := keyOf[S, M](self.tree, to)
|
|
269
271
|
if to == self.current
|
|
270
|
-
Game(..self, unknown: "")
|
|
272
|
+
if self.unknown == "" then self else Game(..self, unknown: "")
|
|
273
|
+
else
|
|
274
|
+
self.switchTo(io, to)
|
|
275
|
+
|
|
276
|
+
# `switched` to the screen named `to`, another one than the current.
|
|
277
|
+
fun switchTo(self, io: Io, to: Str) -> Game[S, M] =
|
|
278
|
+
key := keyOf[S, M](self.tree, to)
|
|
271
|
-
|
|
279
|
+
if key == NoScreen
|
|
272
280
|
if to != self.unknown
|
|
273
281
|
io.printLn("game: the state asks for the screen '{to}', and no screen has that name: staying on '{self.current}'")
|
|
274
282
|
Game(..self, unknown: to)
|
|
@@ -318,12 +326,12 @@ fun nameOf[S, M](a: Actor[M]) -> Str = match a
|
|
|
318
326
|
_ => ""
|
|
319
327
|
|
|
320
328
|
# The key of the screen of `tree` named `name`, `NoScreen` if there is none.
|
|
329
|
+
fun keyOf[S, M](tree: SceneTree[M], name: Str) -> Int = keyAmong[S, M](tree.root.kids(), name)
|
|
330
|
+
|
|
331
|
+
# The key of the first of the chain `kids` named `name`, `NoScreen` if there is none.
|
|
321
|
-
fun
|
|
332
|
+
fun keyAmong[S, M](kids: FRoseTree[Actor[M]], name: Str) -> Int = match kids
|
|
322
|
-
match tree.root.kids().chain().find(|n| match n.top()
|
|
323
|
-
Some(a) => nameOf[S, M](a) == name
|
|
324
|
-
None => false)
|
|
325
|
-
Some(n) => n.top().map(|a| a.key()).unwrapOr(NoScreen)
|
|
326
|
-
|
|
333
|
+
FRoseEmpty => NoScreen
|
|
334
|
+
FRoseNode(a, _, n) => if nameOf[S, M](a) == name then a.key() else keyAmong[S, M](n, name)
|
|
327
335
|
|
|
328
336
|
# The name `asked` stands for in `tree`: itself, or for "" (a `Reducer`'s default) the
|
|
329
337
|
# first screen's.
|
std/game/scene/Ctx.plum
CHANGED
|
@@ -92,12 +92,23 @@ struct Ctx[M](up: Affine, alpha: Float, input: Input, view: Vec2, out: Slice[Cmd
|
|
|
92
92
|
|
|
93
93
|
# Keeps in `out` only the game's commands (`game` true) or only the tree's, in order.
|
|
94
94
|
fun keepOnly(self, game: Bool) =
|
|
95
|
+
if !allGame(self.out, game, 0)
|
|
95
|
-
|
|
96
|
+
kept := Slice[Cmd[M]]()
|
|
96
|
-
|
|
97
|
+
for c := range self.out
|
|
97
|
-
|
|
98
|
+
if c.isGame() == game
|
|
98
|
-
|
|
99
|
+
kept.push(c)
|
|
99
|
-
|
|
100
|
+
self.out.clear()
|
|
100
|
-
|
|
101
|
+
self.out.add(kept)
|
|
102
|
+
|
|
103
|
+
# Whether each command of `cmds` from index `i` on is the game's (`game`) or each the
|
|
104
|
+
# tree's (not `game`).
|
|
105
|
+
fun allGame[M](cmds: Slice[Cmd[M]], game: Bool, i: Int) -> Bool =
|
|
106
|
+
if i >= cmds.length()
|
|
107
|
+
true
|
|
108
|
+
else if cmds.at(i).isGame() != game
|
|
109
|
+
false
|
|
110
|
+
else
|
|
111
|
+
allGame(cmds, game, i + 1)
|
|
101
112
|
|
|
102
113
|
fun cmdName[M](c: Cmd[M]) -> Str = match c
|
|
103
114
|
Spawn(_, _) => "Spawn"
|
std/game/scene/Place.plum
CHANGED
|
@@ -22,10 +22,12 @@ struct Place(
|
|
|
22
22
|
originX: Float, originY: Float) =
|
|
23
23
|
|
|
24
24
|
# The transform into the parent's coordinates: translate to the position, rotate and
|
|
25
|
-
# scale about the origin.
|
|
25
|
+
# scale about the origin. An unturned place (most of them) skips the sine and cosine:
|
|
26
|
+
# `math.cos(0.0)` is exactly 1 and `math.sin(±0.0)` exactly 0, so it is the same matrix.
|
|
26
27
|
fun local(self) -> Affine =
|
|
28
|
+
turned := self.rotation != 0.0
|
|
27
|
-
cs := math.cos(self.rotation)
|
|
29
|
+
cs := if turned then math.cos(self.rotation) else 1.0
|
|
28
|
-
sn := math.sin(self.rotation)
|
|
30
|
+
sn := if turned then math.sin(self.rotation) else 0.0
|
|
29
31
|
a := cs * self.scaleX
|
|
30
32
|
b := sn * self.scaleX
|
|
31
33
|
c := 0.0 - sn * self.scaleY
|
|
@@ -63,6 +65,16 @@ test "local translates, then turns and scales about the origin"
|
|
|
63
65
|
q := b.local().apply(Vec2(x: 2.0, y: 1.0))
|
|
64
66
|
assert q.x == 13.0 && q.y == 21.0
|
|
65
67
|
|
|
68
|
+
test "an unturned place is the same matrix as turning by 0"
|
|
69
|
+
p := Place(x: 3.0, y: -4.0, scaleX: -2.0, scaleY: 0.5, rotation: 0.0, originX: 1.5, originY: -2.0)
|
|
70
|
+
m := p.local()
|
|
71
|
+
assert m.a == math.cos(0.0) * -2.0 && m.b == math.sin(0.0) * -2.0
|
|
72
|
+
assert m.c == 0.0 - math.sin(0.0) * 0.5 && m.d == math.cos(0.0) * 0.5
|
|
73
|
+
assert 1.0 / m.b == 1.0 / {math.sin(0.0) * -2.0}
|
|
74
|
+
assert Place(..p, rotation: -0.0).local() == m
|
|
75
|
+
q := Place(..p, rotation: 0.25).local()
|
|
76
|
+
assert q.a == math.cos(0.25) * -2.0 && q.b == math.sin(0.25) * -2.0
|
|
77
|
+
|
|
66
78
|
test "the copies change one thing"
|
|
67
79
|
p := at(1.0, 2.0)
|
|
68
80
|
assert p.scaleX == 1.0 && p.rotation == 0.0 && p.originX == 0.0
|
std/game/scene/SceneTree.plum
CHANGED
|
@@ -156,11 +156,9 @@ struct SceneTree[M](root: FRoseTree[Actor[M]], camX: Float, camY: Float, camScal
|
|
|
156
156
|
# `step` gives it). With no actor under the pointer the tree is returned as it was.
|
|
157
157
|
fun input(self, ctx: Ctx[M], e: InputEvent) -> SceneTree[M] = match e.position()
|
|
158
158
|
None => self
|
|
159
|
-
Some(p) =>
|
|
160
|
-
if hitChain(self.root, p).isNone()
|
|
161
|
-
self
|
|
162
|
-
else
|
|
163
|
-
|
|
159
|
+
Some(p) => match inputChain(self.root, ctx, p, e)
|
|
160
|
+
Some(r) => SceneTree(..self, root: r)
|
|
161
|
+
None => self
|
|
164
162
|
|
|
165
163
|
# What `draw` would draw, in order: each shown actor with its tree transform and alpha,
|
|
166
164
|
# a parent before its children, siblings first to last.
|
|
@@ -218,13 +216,21 @@ struct TreeScan(mut top: Int, mut ok: Bool) =
|
|
|
218
216
|
# each parent id, in the order pushed. The walk asks about every actor, so beside the set
|
|
219
217
|
# and the map it keeps a bitset of each (one bit per id up to the batch's largest): most
|
|
220
218
|
# answers are a shift and a mask, and only an actor with spawns does a hash lookup. Ids
|
|
221
|
-
# below 0 or from 2^22 up make that batch use the set and the map alone.
|
|
219
|
+
# below 0 or from 2^22 up make that batch use the set and the map alone. A batch with no
|
|
220
|
+
# `Kill` and no `Spawn` (most ticks) has neither: it builds nothing.
|
|
222
221
|
struct TreeWalk[M](
|
|
223
|
-
kills: Set[Int], spawns: Map[Int, Slice[FRoseTree[Actor[M]]]],
|
|
222
|
+
kills: Option[Set[Int]], spawns: Option[Map[Int, Slice[FRoseTree[Actor[M]]]]],
|
|
224
223
|
hasKills: Bool, hasSpawns: Bool,
|
|
225
224
|
bitsOk: Bool, killBits: Array[Int], spawnBits: Array[Int]) =
|
|
226
225
|
|
|
227
226
|
fun of(cmds: Slice[Cmd[M]]) -> TreeWalk[M] =
|
|
227
|
+
if gameOnly(cmds, 0)
|
|
228
|
+
TreeWalk(kills: None, spawns: None, hasKills: false, hasSpawns: false, bitsOk: true, killBits: Array[Int](), spawnBits: Array[Int]())
|
|
229
|
+
else
|
|
230
|
+
TreeWalk.ofTree(cmds)
|
|
231
|
+
|
|
232
|
+
# `of` for a batch with a `Kill` or a `Spawn`.
|
|
233
|
+
fun ofTree(cmds: Slice[Cmd[M]]) -> TreeWalk[M] =
|
|
228
234
|
kills := Set[Int]()
|
|
229
235
|
spawns: Map[Int, Slice[FRoseTree[Actor[M]]]] := Map.empty()
|
|
230
236
|
scan := TreeScan(top: 0, ok: true)
|
|
@@ -252,7 +258,7 @@ struct TreeWalk[M](
|
|
|
252
258
|
Spawn(parent, _) => bitSet(spawnBits, parent)
|
|
253
259
|
_ => Unit
|
|
254
260
|
TreeWalk(
|
|
255
|
-
kills: kills, spawns: spawns,
|
|
261
|
+
kills: Some(kills), spawns: Some(spawns),
|
|
256
262
|
hasKills: kills.length() > 0, hasSpawns: spawns.length() > 0,
|
|
257
263
|
bitsOk: scan.ok, killBits: killBits, spawnBits: spawnBits)
|
|
258
264
|
|
|
@@ -263,17 +269,31 @@ struct TreeWalk[M](
|
|
|
263
269
|
else if self.bitsOk
|
|
264
270
|
bitHas(self.killBits, id)
|
|
265
271
|
else
|
|
272
|
+
match self.kills
|
|
266
|
-
|
|
273
|
+
Some(k) => k.has(id)
|
|
274
|
+
None => false
|
|
267
275
|
|
|
268
276
|
# The nodes to add under the actor `id`, as a chain in the order pushed (empty if none).
|
|
269
277
|
fun spawnsUnder(self, id: Int) -> FRoseTree[Actor[M]] =
|
|
270
278
|
if !self.hasSpawns || {self.bitsOk && !bitHas(self.spawnBits, id)}
|
|
271
279
|
FRoseEmpty
|
|
272
280
|
else
|
|
273
|
-
match self.spawns
|
|
281
|
+
match self.spawns
|
|
282
|
+
Some(m) => match m.get(id)
|
|
274
|
-
|
|
283
|
+
Some(xs) => chainFrom(xs, 0)
|
|
284
|
+
None => FRoseEmpty
|
|
275
285
|
None => FRoseEmpty
|
|
276
286
|
|
|
287
|
+
# Whether the commands of `cmds` from index `i` on are all the game's (no `Kill`, no
|
|
288
|
+
# `Spawn`).
|
|
289
|
+
fun gameOnly[M](cmds: Slice[Cmd[M]], i: Int) -> Bool =
|
|
290
|
+
if i >= cmds.length()
|
|
291
|
+
true
|
|
292
|
+
else if !cmds.at(i).isGame()
|
|
293
|
+
false
|
|
294
|
+
else
|
|
295
|
+
gameOnly(cmds, i + 1)
|
|
296
|
+
|
|
277
297
|
# The nodes of `ns` from index `i` on (each with its subtree, not its siblings), linked
|
|
278
298
|
# into one chain, in order: `FRoseTree.chainOf` for a `Slice`.
|
|
279
299
|
fun chainFrom[T](ns: Slice[FRoseTree[T]], i: Int) -> FRoseTree[T] =
|
|
@@ -348,24 +368,26 @@ fun drawChain[M](t: FRoseTree[Actor[M]], io: Io, parent: Affine, alpha: Float) =
|
|
|
348
368
|
|
|
349
369
|
# The chain `t` after the pointer event `e` at `p` (in the chain's parent's coordinates),
|
|
350
370
|
# found as `hitChain` finds it: the hit actor's `input`, then its parents' on the way
|
|
351
|
-
# back up.
|
|
371
|
+
# back up. None if no actor of the chain is hit (the chain is then unchanged), so one walk
|
|
352
|
-
|
|
372
|
+
# both finds the actor and routes the event, and a miss builds nothing.
|
|
353
|
-
|
|
354
|
-
fun inputChain[M](t: FRoseTree[Actor[M]], ctx: Ctx[M], p: Vec2, e: InputEvent) ->
|
|
373
|
+
fun inputChain[M](t: FRoseTree[Actor[M]], ctx: Ctx[M], p: Vec2, e: InputEvent) -> Option[FRoseTree[Actor[M]]] = match t
|
|
355
|
-
FRoseEmpty => TreeRouted(t: t, hit: false)
|
|
356
|
-
|
|
374
|
+
FRoseEmpty => None
|
|
357
|
-
|
|
375
|
+
FRoseNode(a, k, n) => match inputChain(n, ctx, p, e)
|
|
358
|
-
if rn.hit || !isShown(a)
|
|
359
|
-
|
|
376
|
+
Some(n2) => Some(FRoseNode(value: a, firstChild: k, nextSibling: n2))
|
|
377
|
+
None =>
|
|
378
|
+
if !isShown(a)
|
|
360
|
-
|
|
379
|
+
None
|
|
361
|
-
local := a.place().local()
|
|
362
|
-
q := local.inverse().apply(p)
|
|
363
|
-
rk := inputChain(k, ctx.under(local, a.opacity()), q, e)
|
|
364
|
-
if rk.hit || a.hit(q)
|
|
365
|
-
na := a.input(ctx, InputEvent.pointerAt(e, q))
|
|
366
|
-
TreeRouted(t: FRoseNode(value: na, firstChild: rk.t, nextSibling: rn.t), hit: true)
|
|
367
380
|
else
|
|
381
|
+
local := a.place().local()
|
|
382
|
+
q := local.inverse().apply(p)
|
|
383
|
+
rk := if k.isEmpty() then None else inputChain(k, ctx.under(local, a.opacity()), q, e)
|
|
384
|
+
match rk
|
|
368
|
-
|
|
385
|
+
Some(k2) => Some(FRoseNode(value: a.input(ctx, InputEvent.pointerAt(e, q)), firstChild: k2, nextSibling: n))
|
|
386
|
+
None =>
|
|
387
|
+
if a.hit(q)
|
|
388
|
+
Some(FRoseNode(value: a.input(ctx, InputEvent.pointerAt(e, q)), firstChild: k, nextSibling: n))
|
|
389
|
+
else
|
|
390
|
+
None
|
|
369
391
|
|
|
370
392
|
# The topmost shown actor under `p` (a point in the chain's parent's coordinates) that
|
|
371
393
|
# says it is hit: later siblings first, and in each, its children before itself.
|