plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
3377b98
— Peter John
2026-11-16T14:19:58+05:30
rules as the screen's checks over round actors, and counter-based randomness (user: "Yes", then "drop the Meets trait": the checks stay in the screen's rules): Orbit Runner's `PlayingScreen.rules(self) -> List<PlayAction>` is `[..self.orbs.filter(|o| self.ship.touches(o)).map(|o| Caught(orb: o.id)), if self.ship.hurt <= 0.0 && self.mines.any(|m| self.ship.touches(m)) then Hit]`, the frame `Next(state: moved, then: if moved.over then Nil else moved.rules())`, and `update`'s `Caught(orb)` scores `Orb.Points` and respawns that orb (two orbs caught in a tick are two `Caught`s, two pickups, the same total; mines on the ship are one `Hit`); `Scored`, the `Caught` fold accumulator, the reversed list and the hand-made `Vec2`s are gone; orbs have a stable `id` (0..2, their animation phase `id * 0.1`, so `phase` is gone); std: `Random.at(seed, tick, salt)` (std/math/Random), a key made with SplitMix64's finaliser (`Random.mix`), and its draws `intAt(i)`, `floatAt(i)`, `intBelowAt(i, n)`, SplitMix64's stream from the key, each a pure function of the key and `i`, so nothing threads a generator (`Rolled` stays for `nextInt` and its users in std); `trait Round[View]` (std/game/Round): `radius()`, and `touches(other: Round)`, whether the circles about the two pivots overlap; `Place.pivot()`, where the origin lands in the parent, whatever the rotation and scale; games: Orbit Runner's screen keeps `seed` and `ticks` (counted in the frame, never reset, so a restart deals a fresh field) where it kept `rng`, `dice(salt)` is `Random.at(seed, ticks, salt)` (orbs by id, mines `OrbCount + count`), `deal` a `Range.until(0, OrbCount).map(..)`, `Orb.spawn(id, dice, near, tex)` and `Mine.spawn(dice, near, tex)` return the actor (`Rolled` gone from the game), `Motion.spawn(dice, near: Vec2, gap, speed)` draws by index (`Spawned` gone), the ship, orbs and mines are `Round`, the hit's ring at `ship.at.pivot()`; `PlayingScreen.start`/`at` take the seed; Balloon Pop's screen keeps `seed` and `ticks`, its frame respawns with a `map` (`Balloon.spawn(id, dice, depth, clock)` returns the balloon; `Spawned` and the fold gone); checksum: Orbit Runner 818414470 to 181767343 (every spawn draws elsewhere); benchmarks/screen_views (minimums of 5, two alternating rounds, before and after): orbit update 11.97/11.99 and 12.29/11.76 ms, paint 130.3/129.9 and 131.0/130.5 ms (noise); in place (`--explain-fusion`): `PlayingScreen.update` 5, `at`, `deal` (the key's parts read into locals, not `self` in the closure), `grown`, `stepped` kept, `PlayingScreen.rules` 1 gone (it returns actions: no spread of the screen left), `Motion.spawn` 1 new, Balloon Pop's `PlaygroundScreen.update` 1 kept; tests: tests/std/math/Random.plum (SplitMix64 reference values, the same key the same draws, uniform along the tick, the salt, the seed and the index, the avalanche of neighbouring ticks, `intBelowAt`), tests/std/game/Round.plum, `Place.pivot`, Orbit Runner's `Caught` (two orbs in a tick, two mines one `Hit`, the rules' edges, an orb's spawn a pure draw), the recorded 600-tick run on seed 43 (42 no longer loses a life in it), Balloon Pop's hue names, game_in_place_test (`rules` out, `Motion.spawn` in); docs: game.md (the screen's fields and frame, randomness by counter, the rules, `Round` and `Place.pivot`, the checksum), std.md (`Random.at`, `Round`), TestRig's sample, the review plan
- benchmarks/screen_views/orbit.plum +4 -4
- compiler/cli/tests/cli/game_in_place_test.rs +5 -3
- docs/game.md +18 -8
- docs/plans/2026-11-13-std-game-review.md +1 -0
- docs/std.md +2 -2
- examples/games/balloonpop/actors/Balloon.plum +5 -9
- examples/games/balloonpop/screens/PlaygroundScreen.plum +7 -14
- examples/games/orbitrunner/OrbitRunner.plum +11 -18
- examples/games/orbitrunner/actors/Mine.plum +5 -4
- examples/games/orbitrunner/actors/Orb.plum +6 -5
- examples/games/orbitrunner/actors/Ship.plum +4 -1
- examples/games/orbitrunner/screens/PlayingScreen.plum +27 -36
- std/game/Place.plum +4 -0
- std/game/Round.plum +19 -0
- std/game/TestRig.plum +1 -1
- std/math/Random.plum +39 -0
- tests/examples/games/balloonpop/screens/PlaygroundScreen.plum +3 -3
- tests/examples/games/orbitrunner/actors/Orb.plum +12 -8
- tests/examples/games/orbitrunner/screens/PlayingScreen.plum +54 -16
- tests/std/game/Place.plum +9 -0
- tests/std/game/Round.plum +35 -0
- tests/std/math/Random.plum +53 -0
benchmarks/screen_views/orbit.plum
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
# calls reach nothing.
|
|
10
10
|
#
|
|
11
11
|
# It prints `us <total> ticks <n> checksum <c>`: the microseconds of every tick (and frame)
|
|
12
|
-
# of the four seeds, the ticks, and the state checksum (the run, the
|
|
12
|
+
# of the four seeds, the ticks, and the state checksum (the run, the tick count, the ship,
|
|
13
13
|
# each orb and each mine at the end of each seed), computed as master's driver does.
|
|
14
14
|
#
|
|
15
15
|
# plum build benchmarks/screen_views/orbit.plum -o orbit
|
|
@@ -35,7 +35,7 @@ import examples/games/orbitrunner/OrbitRunner
|
|
|
35
35
|
import examples/games/orbitrunner/screens/PlayingScreen
|
|
36
36
|
|
|
37
37
|
primitive OrbitBench[App] =
|
|
38
|
-
fun make(seed: Int) -> PlayingScreen = PlayingScreen.at(
|
|
38
|
+
fun make(seed: Int) -> PlayingScreen = PlayingScreen.at(seed, Assets.none(), 0, 3)
|
|
39
39
|
|
|
40
40
|
fun gameOf(seed: Int) -> Game =
|
|
41
41
|
only: Screen := make(seed)
|
|
@@ -45,9 +45,9 @@ primitive OrbitBench[App] =
|
|
|
45
45
|
PlayingScreen as p => p
|
|
46
46
|
_ => make(-1)
|
|
47
47
|
|
|
48
|
-
# The run's state: the run, the
|
|
48
|
+
# The run's state: the run, the tick count, the ship, each orb and each mine.
|
|
49
49
|
fun state(p: PlayingScreen) -> Int =
|
|
50
|
-
head := [p.score, p.best, p.lives, Timing.flag(p.over), p.
|
|
50
|
+
head := [p.score, p.best, p.lives, Timing.flag(p.over), p.ticks, p.ship.at.x.toBits(), p.ship.at.y.toBits(), p.ship.vx.toBits(), p.ship.vy.toBits(), p.orbs.length(), p.mines.length()]
|
|
51
51
|
none: List<Int> := Nil
|
|
52
52
|
orbs := p.orbs.fold(none, |acc, o| Cons(o.at.y.toBits(), Cons(o.at.x.toBits(), acc)))
|
|
53
53
|
mines := p.mines.fold(none, |acc, m| Cons(m.spin.toBits(), Cons(m.at.x.toBits(), acc)))
|
compiler/cli/tests/cli/game_in_place_test.rs
CHANGED
|
@@ -6,8 +6,10 @@
|
|
|
6
6
|
//! Orbit Runner's updates are in place since std's fold copies the screen once as it enters
|
|
7
7
|
//! (`@copy` in `Delivery.deliverTo`): every step of the tick is then unique, so
|
|
8
8
|
//! `PlayingScreen.update` (5: steer, the frame's spread after `stepped`, scored, hit and start
|
|
9
|
-
//! over, whose `best` is now read before the spread), `
|
|
9
|
+
//! over, whose `best` is now read before the spread), `deal`, `grown`, `at` and its derived
|
|
10
|
-
//!
|
|
10
|
+
//! `stepped` need no `$unique` version. Since the rules (2026-11-16) `rules` returns the actions
|
|
11
|
+
//! only (`Caught` for each orb on the ship, `Hit`), so it has no spread of the screen to keep;
|
|
12
|
+
//! the respawn is `update`'s `Caught` arm, and `Motion.spawn`'s spread of the spot is in place. Since the Elm-style screens (2026-11-16) a call
|
|
11
13
|
//! on a receiver of a bounded type parameter (`s.update(a)` with `S: ScreenSlice<A>`) resolves
|
|
12
14
|
//! among the bound's implementers alone, not to every `update` of the program (`Game.update`,
|
|
13
15
|
//! `Viewport.update`). Balloon Pop's `update` has one in place (its `view` keeps `self`, as
|
|
@@ -70,11 +72,11 @@ fn orbitRunnerKeepsItsInPlaceSpreads() {
|
|
|
70
72
|
let mut want = TICK.to_vec();
|
|
71
73
|
want.extend([
|
|
72
74
|
("PlayingScreen.update", 5),
|
|
73
|
-
("PlayingScreen.rules", 1),
|
|
74
75
|
("PlayingScreen.at", 1),
|
|
75
76
|
("PlayingScreen.stepped", 1),
|
|
76
77
|
("PlayingScreen.deal", 1),
|
|
77
78
|
("PlayingScreen.grown", 1),
|
|
79
|
+
("Motion.spawn", 1),
|
|
78
80
|
]);
|
|
79
81
|
assertKept("examples/games/orbitrunner/OrbitRunner.plum", &want);
|
|
80
82
|
}
|
docs/game.md
CHANGED
|
@@ -158,7 +158,7 @@ struct Mine[Actor](at: Motion, spin: Float = 0.0, clock: Float = 0.0, tex: Textu
|
|
|
158
158
|
|
|
159
159
|
```plum
|
|
160
160
|
struct PlayingScreen[ScreenSlice<PlayAction>](
|
|
161
|
-
|
|
161
|
+
seed: Int, ticks: Int, assets: Assets, score: Int, best: Int, lives: Int, over: Bool, clock: Float,
|
|
162
162
|
@stepsWhen(!self.over) ship: Ship, @stepsWhen(!self.over) orbs: List<Orb>, @stepsWhen(!self.over) mines: List<Mine>, rings: List<Ring>) =
|
|
163
163
|
fun name(self) -> Str = "play"
|
|
164
164
|
```
|
|
@@ -180,8 +180,8 @@ and that is what is compiled: the derive is written out before checking, the sam
|
|
|
180
180
|
fun update(self, a: PlayAction) -> Next<PlayingScreen, PlayAction> = match a
|
|
181
181
|
Tick(dt) =>
|
|
182
182
|
stepped := self.stepped(dt)
|
|
183
|
-
moved := PlayingScreen(..stepped, clock: stepped.clock + dt, rings: stepped.rings.filter(|r| !r.done()))
|
|
183
|
+
moved := PlayingScreen(..stepped, ticks: stepped.ticks + 1, clock: stepped.clock + dt, rings: stepped.rings.filter(|r| !r.done()))
|
|
184
|
-
if moved.over then
|
|
184
|
+
Next(state: moved, then: if moved.over then Nil else moved.rules())
|
|
185
185
|
_ => Next(state: self)
|
|
186
186
|
```
|
|
187
187
|
|
|
@@ -200,6 +200,7 @@ What else a view is, it says with a small trait:
|
|
|
200
200
|
| `Actor` (`std/game/View`) | `step(self, dt) -> Self`: its next version `dt` seconds on, which the screen's `stepped` keeps |
|
|
201
201
|
| `Steps` (`std/game/View`) | nothing to write: on an actor the compiler steps its actor fields after its `step` (above); `@stepsWhen(cond)` on a field. A screen needs none |
|
|
202
202
|
| `Sized` (`std/game/Sized`) | `bounds(self) -> Aabb`, the box it covers in its own coordinates. A view with bounds can be hit; one without cannot. `hit(self, p) -> Bool` is inside the bounds, edges included, unless the view says otherwise for an exact shape (`Circle` is hit inside its circle; a balloon on its body, its knot and its string). Culling goes by the bounds too |
|
|
203
|
+
| `Round` (`std/game/Round`) | `radius(self) -> Float`: it is the circle of that radius about its pivot (`Place.pivot`). `touches(self, other: Round) -> Bool`, given, is whether the two circles overlap (closer than the sum of the radii, centre to centre) |
|
|
203
204
|
| `Touch` (`std/game/Touch`) | `touch(self) -> Touchable`: whether it takes presses. `Enabled`: it is hit where it is (when it is `Sized`) and what is under it is; `Disabled`: neither it nor anything under it is hit; `ChildrenOnly`: what is under it is, it is not. A view that says nothing is `ChildrenOnly` |
|
|
204
205
|
|
|
205
206
|
The ready-made kinds are made with their fields named, and their fields have defaults for the usual (see [field defaults](language.md#structs)): a construction names only the others, `Label(x: 16.0, y: 12.0, text: "Score")`. Text and shapes are decoration (`Disabled`). A colour's alpha is the colour's: a box in a half-transparent red paints at half alpha. Each turns with its place: a box and a label in a turned group are drawn turned (`Gfx.drawRectEx`, `Gfx.drawTextEx`). Their `Box.draw`, `Circle.draw`, `Sprite.draw` and `Label.draw` paint the same shapes for your own kinds.
|
|
@@ -227,7 +228,7 @@ The host adds up the time between frames and runs one tick (`Game.update`) for e
|
|
|
227
228
|
|
|
228
229
|
The tick's `Input` is read from the host once per frame, before the ticks, with the window's size (`input.screen`, which the game takes its window from: `Game.update` is `step` with `input.screen`). A frame's key and pointer events go to its first tick, or to the next frame's if it ran none, so a press counts once. Later ticks of the frame see what is held, with no events. A slice gets it through its subscriptions (`OnKeyDown`, `OnHeld`, ..) and its presses; don't read input any other way.
|
|
229
230
|
|
|
230
|
-
Keep randomness in the screen
|
|
231
|
+
Keep randomness in the screen as a seed, taken once in `main` from `io.randomSeed()`, and a tick count, and draw by counter ([`std/math/Random.plum`](../std/math/Random.plum)): `Random.at(seed, tick, salt)` is the key of one moment of one thing (the salt says which: Orbit Runner's orb ids, then a number for each mine), and `dice.floatAt(i)` its `i`th draw, a pure function of the key and `i` (SplitMix64). Nothing is threaded: no generator is handed on from draw to draw or kept in the next screen, so a draw can sit in a `map` (`Range.until(0, n).map(|i| Orb.spawn(i, Random.at(seed, ticks, i), ship, tex))`) and two draws in one tick never wait on each other. Then the same seed and the same inputs give the same game, tick for tick. (`Random.seeded(n)` and `nextFloat`, which return the value and the next generator, are still there for code that would rather thread one.) Orbit Runner's state after 3,600 ticks of each of four seeds under a scripted input has one checksum, run after run (`benchmarks/screen_views/orbit.plum`; it was master's imperative Orbit Runner's until the rules returned their follow-ups as actions).
|
|
231
232
|
|
|
232
233
|
The maths is the same everywhere too. std's maths (`Math.sin`, `Math.sqrt`, ...) is Plum code built from IEEE 754 double operations, which wasm defines exactly, so every browser and the native host get the same bits; a test in [`tests/port/game/replay.plum`](../tests/port/game/replay.plum) pins some of them. The one host value a tick gets is the window's size, on its input, and a replay records it.
|
|
233
234
|
|
|
@@ -391,9 +392,18 @@ The list is a list literal made again each tick, so an item is there only while
|
|
|
391
392
|
|
|
392
393
|
Several subscriptions may answer one event, each in the order listed. Then the transition in progress moves on, and the game does the commands.
|
|
393
394
|
|
|
394
|
-
**What follows from an action** is part of what `update` returns, `Next<S, A>(state: S, then: List<A> = Nil, cmds: List<Cmd> = Nil)`. Each action std delivers is stepped as it is made, and then each action of its `then`, in order, each with its own `then` first; so the next action sees what they did. Orbit Runner's rules return what happened as actions
|
|
395
|
+
**What follows from an action** is part of what `update` returns, `Next<S, A>(state: S, then: List<A> = Nil, cmds: List<Cmd> = Nil)`. Each action std delivers is stepped as it is made, and then each action of its `then`, in order, each with its own `then` first; so the next action sees what they did. Orbit Runner's rules return what happened as actions, one for each thing that met the ship: its frame is `Next(state: moved, then: moved.rules())`, and
|
|
395
396
|
|
|
396
|
-
|
|
397
|
+
```plum
|
|
398
|
+
fun rules(self) -> List<PlayAction> = [
|
|
399
|
+
..self.orbs.filter(|o| self.ship.touches(o)).map(|o| Caught(orb: o.id))
|
|
400
|
+
if self.ship.hurt <= 0.0 && self.mines.any(|m| self.ship.touches(m)) then Hit
|
|
401
|
+
]
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
so two orbs caught in one tick are two `Caught` actions (each scored, and that orb respawned, in `update`), and mines touching the ship are one `Hit`. The delivery is a fold over the slice (`Delivery.deliverTo`, which keeps the actions and the commands in a value, `Trail`), and the kit's `dispatch` runs the same fold (`Delivery.dispatchTo`). The game keeps the screen it had (its screens, and the ones a rewind starts from), so the fold starts from a copy of it (`@copy`, one a tick delivered, [language.md](language.md#copies)): the copy is the fold's alone, and each `update` of the tick updates it in place, Orbit Runner's steering, frame, scoring, hits and start over among them (a sound read from the screen is read before the spread: `sounds := self.assets`). A test calls `update` itself: `s.update(Keyed).then`, `s.update(a).state`. An action enum that lists `Show` (std/Str) has its `toStr` derived, so a test can compare the actions sent as text (`"Steer(1.0, -1.0) Tick(0.016) Caught(0)"`). How a game of screens with actions of different types is typed: `Screen` has `deliver`, and `ScreenSlice<A>` gives it a default of its own that delivers the tick as `A`s ([language.md](language.md#inheritance)).
|
|
405
|
+
|
|
406
|
+
Orbit Runner steers with `h.axis`: `Steer(ax: {h.axis(Key.Left, Key.Right) + h.axis(Key.KeyA, Key.KeyD)}.max(-1.0).min(1.0), ..)`. What the rules need of geometry is std's: an actor that lists `Round` (`std/game/Round`) declares its `radius()`, and is the circle of that radius about its pivot (`Place.pivot`, the point its place turns about, whatever its rotation and scale), so `a.touches(b)` is whether two round actors overlap, with no positions copied out by hand; `Vec2.near(other, dist)` (closer than `dist`) is the test under it. Orbit Runner's ship, orbs and mines are `Round`. An actor that appears somewhere new makes itself: `Orb.spawn(id, dice, near, tex)` and `Mine.spawn(dice, near, tex)` return the actor, placed away from the ship by the draws of `dice` (an orb keeps its `id`, which its `Caught` names and its respawn reuses).
|
|
397
407
|
|
|
398
408
|
## Clicks
|
|
399
409
|
|
|
@@ -444,7 +454,7 @@ Put your rules in code that takes no `Io`, test them from a `primitive XTest[Uni
|
|
|
444
454
|
# tests/examples/games/orbitrunner/screens/PlayingScreen.plum
|
|
445
455
|
primitive PlayingScreenTest[UnitTest] =
|
|
446
456
|
fun thePlayScoresAndStartsOver() =
|
|
447
|
-
t := TestRig.mount(io, PlayingScreen.start(
|
|
457
|
+
t := TestRig.mount(io, PlayingScreen.start(7, Assets.none()))
|
|
448
458
|
u := t.hold(Key.Right).ticks(30)
|
|
449
459
|
assert u.text(u.first<Label>()) == "Score {u.screen.score}"
|
|
450
460
|
v := u.press(Key.Enter).tick()
|
|
@@ -515,7 +525,7 @@ The engine has master's shape at e91c40ce (`View` and `Actor`, `Screen` with its
|
|
|
515
525
|
| std's test helper types private (`_ViewProbe`, `_KitScreen`, ..) | public names (`ViewProbe`, `Stepper`, ..) | a type's name here is the module's: a game's own `Stepper` beside std's compiles, so there is no clash to avoid (checked with a probe) |
|
|
516
526
|
| `std/game` without `Anim` | `std/game/Anim` kept (pure animations of time) | a web-test fixture uses it; no example game does |
|
|
517
527
|
|
|
518
|
-
The same on both: `View` (`place`, `view`, `visible`, `paintSelf`, the shared empty default), the walks (`Views.paint`, `paintCulled`, `drawnIn`, `hitIn`, `worldIn`), `Screen` (`name`, `view`, `hud(window)`, `camera(base)`, `size`, `viewport`, `culls`, `busy`, `background`, `transition`, `frames`; no `enter`, `leave`, `resize`, `draw` or `fresh`), no `restart`, pointer events dropped during a transition, each screen on its own background in one, the ready-made views (Circle by its centre, `Label(align:)`, `Overlay` with a required size hit inside its box, `Sprite.frameAt`), `Gfx.drawRectEx` and `drawTextEx` in std, the browser, the native host and the recorder, the input (`ButtonDown`, `ButtonUp`, `Held.buttons`, the extra `Key`s, the replay's button mask and `B`/`b` events), `Math.near`, `Game.pixels`, `frameDrawn()`, `drawList()`, `camera(i)`, `toWorld`, `toScreen`, `visible(i)`, `travelAsked`, frames on demand in the hosts (`frameIdle`, the browser waiting for input, `PlumGame::idle`), `plum run --dev` (its keys, its line, the page's Cmd key fixes), and `plum compile <folder>`. Orbit Runner's state checksum was master's (925878360) until its rules returned what happened as `then` actions (the mines are checked before more are added for the tick's points, and a restart starts at once), 946756219 then,
|
|
528
|
+
The same on both: `View` (`place`, `view`, `visible`, `paintSelf`, the shared empty default), the walks (`Views.paint`, `paintCulled`, `drawnIn`, `hitIn`, `worldIn`), `Screen` (`name`, `view`, `hud(window)`, `camera(base)`, `size`, `viewport`, `culls`, `busy`, `background`, `transition`, `frames`; no `enter`, `leave`, `resize`, `draw` or `fresh`), no `restart`, pointer events dropped during a transition, each screen on its own background in one, the ready-made views (Circle by its centre, `Label(align:)`, `Overlay` with a required size hit inside its box, `Sprite.frameAt`), `Gfx.drawRectEx` and `drawTextEx` in std, the browser, the native host and the recorder, the input (`ButtonDown`, `ButtonUp`, `Held.buttons`, the extra `Key`s, the replay's button mask and `B`/`b` events), `Math.near`, `Game.pixels`, `frameDrawn()`, `drawList()`, `camera(i)`, `toWorld`, `toScreen`, `visible(i)`, `travelAsked`, frames on demand in the hosts (`frameIdle`, the browser waiting for input, `PlumGame::idle`), `plum run --dev` (its keys, its line, the page's Cmd key fixes), and `plum compile <folder>`. Orbit Runner's state checksum was master's (925878360) until its rules returned what happened as `then` actions (the mines are checked before more are added for the tick's points, and a restart starts at once), 946756219 then, 818414470 since the Elm-style screens (the subscriptions are read once a tick, so the tick Enter restarts on steers nothing), and 181767343 since the rules (counter-based draws, so every spawn lands elsewhere; each orb caught is its own action).
|
|
519
529
|
|
|
520
530
|
Not ported: master's `clockwork` and `menu` games; see the [backlog](backlog.md). This branch keeps `examples/games/clicks.plum` and `two_screens.plum`, which master doesn't have.
|
|
521
531
|
|
docs/plans/2026-11-13-std-game-review.md
CHANGED
|
@@ -41,3 +41,4 @@ Events stay the only source of truth; std keeps held state as a pure fold of the
|
|
|
41
41
|
|
|
42
42
|
## Rules (user 2026-11-16: "Yes", before the benchmark)
|
|
43
43
|
Objects decide their own collisions (option A): `Meets<Other, A>` with `meets(self, other) -> Option<A>` on Orb (Caught(orb: id)) and Mine (Hit); `rules(self) -> List<A>` collects `..xs.filterMap(|x| x.meets(self.ship))`; `update` handles `Caught`; a counter-based `Random.at(seed:, tick:, salt:)` replaces threading `rng` (no Rolled/Caught accumulators, `deal` a plain map); `touches` in std from declared circle shapes; `Place` as a `Vec2` where that removes conversions. Option B (std contact search) not chosen.
|
|
44
|
+
- Done on `sl/rules` (user, mid-task: drop `Meets`, the checks stay in the screen's rules): `rules(self) -> List<PlayAction>` is `[..self.orbs.filter(|o| self.ship.touches(o)).map(|o| Caught(orb: o.id)), if self.ship.hurt <= 0.0 && self.mines.any(|m| self.ship.touches(m)) then Hit]`, the frame `Next(state: moved, then: if moved.over then Nil else moved.rules())`, `update`'s `Caught(orb)` scores and respawns that orb; `Random.at(seed, tick, salt)` with `intAt`/`floatAt`/`intBelowAt(i)` (SplitMix64's mix and stream; std/math/Random), the screens keep `seed` and `ticks` (Orbit Runner and Balloon Pop), `deal` and Balloon Pop's respawn are `map`s, `Rolled` (std's, still used by `nextInt`) and the games' `Caught`/`Spawned` accumulators gone from the games; `trait Round[View]` (std/game/Round: `radius()`, `touches(other: Round)` about `Place.pivot()`), the ship, orbs and mines `Round`; orbs have an `id` (their animation phase is `id * 0.1`). Orbit Runner's checksum 818414470 to 181767343; orbit update 12.0 to 11.8-12.3 ms, paint 130 to 130.5-131 ms (noise); in place: `PlayingScreen.rules` has no spread left (it returns actions), `Motion.spawn` gains one, the rest kept.
|
docs/std.md
CHANGED
|
@@ -257,7 +257,7 @@ primitive Demo[App] =
|
|
|
257
257
|
## Utilities
|
|
258
258
|
|
|
259
259
|
- **`Time`:** `Time.now(io)` and `Duration` values such as `Duration.seconds(5)`; both compare with `==`
|
|
260
|
-
- **`Random`:** `Random.seeded(n)`. Each call returns the value and the next generator, so the same seed always gives the same sequence. `io.randomSeed()` gives a seed from the clock
|
|
260
|
+
- **`Random`:** `Random.seeded(n)`. Each call returns the value and the next generator, so the same seed always gives the same sequence. Or draw by counter, threading nothing: `Random.at(seed, tick, salt)` is a key, and `floatAt(i)`, `intAt(i)` and `intBelowAt(i, n)` its draws, each a pure function of the key and `i` (SplitMix64's mix and stream). `io.randomSeed()` gives a seed from the clock
|
|
261
261
|
- **`Uuid`:** `Uuid.v4(rng)`, `Uuid.fromStr`, `Uuid.isValid`, `==` (case-insensitive: `fromStr` lowers the text)
|
|
262
262
|
- **`Base64`** (`import std/encoding/Base64`): `Base64.encode(data)` and `Base64.decode(text)` (a `Result`; whitespace and missing padding are fine, data after padding and a last group of one character are `Err(InvalidBase64)`), the URL alphabet `Base64.encodeUrl(data)` (no padding unless `pad` is true) and `Base64.decodeUrl(text)`, and the line-wrapped `Base64.encodePem` and `Base64.encodeMime`
|
|
263
263
|
- **`Aabb`:** an axis-aligned box, corners included: `Aabb.of(x, y, w, h)`, `width`, `height`, `center`, `overlaps`, `contains`, `containsPoint`, `union`, `distSq`, `rayEntry`
|
|
@@ -273,7 +273,7 @@ Tests live in the `tests/` mirror of the code (std's in `tests/std/...`), as the
|
|
|
273
273
|
|
|
274
274
|
## Game screens and slices
|
|
275
275
|
|
|
276
|
-
A game (`std/game/Game`) is a list of screens. A screen (`std/game/Screen`) has a name and a `view(self) -> List<View>`, what the frame shows, made again from its fields every frame (`std/game/View`: stateless views, and actors, `Actor[View]`, with `step(self, dt) -> Self`, which a screen steps through its `stepped(dt)`, derived for every screen from its fields holding actors, a `List<View>` among them), and may have a `hud(window)` drawn over it one unit a pixel and a `camera(base)` (`Viewport.lookAt`); a screen with rules is a `ScreenSlice<A>` (`std/game/ScreenSlice`), in the Elm style: its fields are its state, `A` its actions, `update(self, a) -> Next<Self, A>` its one entry point (pure, with no `Io`, so the same actions from the same state give the same state), returning the next screen with what follows: the actions that follow and the commands for the game (`std/game/Cmd`: a sound, `GoTo(name: "play")`), done after the tick. Its `subscriptions(self) -> List<Sub<A>>` say which of each tick's input (`std/game/Input`: its events, and what is held after them, `Held`, their fold) and which frames become its actions: `OnFrame(|dt| ..)`, `OnKeyDown(key:, act:)`, `OnKeyUp`, `OnHeld(|h| ..)`, `OnWheel`, `OnPointerDown`, `OnPointerUp`, `OnPointerMove`, read once a tick; a press on an item that says what it means, a `Presses<A>` such as a `Button` from `std/game/Button`, is that action by default. The frame is an action: the screen steps its actors in it. std keeps the screen each `update` returns. `Vec2.near(other, dist)` (`std/math/Vec2`) is the overlap test of two circles (`a.near(b, ra + rb)`). A run records as a `Replay` (`std/game/Replay`: the seed and a `List<Input>`, one a tick). All of it is in [the game engine](game.md).
|
|
276
|
+
A game (`std/game/Game`) is a list of screens. A screen (`std/game/Screen`) has a name and a `view(self) -> List<View>`, what the frame shows, made again from its fields every frame (`std/game/View`: stateless views, and actors, `Actor[View]`, with `step(self, dt) -> Self`, which a screen steps through its `stepped(dt)`, derived for every screen from its fields holding actors, a `List<View>` among them), and may have a `hud(window)` drawn over it one unit a pixel and a `camera(base)` (`Viewport.lookAt`); a screen with rules is a `ScreenSlice<A>` (`std/game/ScreenSlice`), in the Elm style: its fields are its state, `A` its actions, `update(self, a) -> Next<Self, A>` its one entry point (pure, with no `Io`, so the same actions from the same state give the same state), returning the next screen with what follows: the actions that follow and the commands for the game (`std/game/Cmd`: a sound, `GoTo(name: "play")`), done after the tick. Its `subscriptions(self) -> List<Sub<A>>` say which of each tick's input (`std/game/Input`: its events, and what is held after them, `Held`, their fold) and which frames become its actions: `OnFrame(|dt| ..)`, `OnKeyDown(key:, act:)`, `OnKeyUp`, `OnHeld(|h| ..)`, `OnWheel`, `OnPointerDown`, `OnPointerUp`, `OnPointerMove`, read once a tick; a press on an item that says what it means, a `Presses<A>` such as a `Button` from `std/game/Button`, is that action by default. The frame is an action: the screen steps its actors in it. std keeps the screen each `update` returns. An actor that lists `Round` (`std/game/Round`) declares its `radius()` and is that circle about its pivot (`Place.pivot`, the point its place turns about), and `a.touches(b)` is whether two round actors overlap; under it, `Vec2.near(other, dist)` (`std/math/Vec2`) is the overlap test of two circles (`a.near(b, ra + rb)`). A run records as a `Replay` (`std/game/Replay`: the seed and a `List<Input>`, one a tick). All of it is in [the game engine](game.md).
|
|
277
277
|
|
|
278
278
|
```plum
|
|
279
279
|
import std/Int
|
examples/games/balloonpop/actors/Balloon.plum
CHANGED
|
@@ -31,13 +31,11 @@ struct Balloon[Actor, Sized, Touch, Presses<PopAction>](
|
|
|
31
31
|
const PopTime = 0.6
|
|
32
32
|
const FirstId = 100
|
|
33
33
|
|
|
34
|
-
fun spawn(id: Int,
|
|
34
|
+
fun spawn(id: Int, dice: Random, depth: Float, clock: Float) -> Balloon =
|
|
35
|
-
|
|
35
|
+
Balloon(
|
|
36
|
-
rs := rx.rng.nextFloat()
|
|
37
|
-
rh := rs.rng.nextIntBelow(Hue.All.length())
|
|
38
|
-
|
|
36
|
+
id: id, x: 60.0 + dice.floatAt(0) * {Arena.W - 120.0}, y: Arena.H + Balloon.Radius + depth,
|
|
39
|
-
|
|
37
|
+
speed: Balloon.MinSpeed + dice.floatAt(1) * {Balloon.MaxSpeed - Balloon.MinSpeed}, hue: dice.intBelowAt(2, Hue.All.length()),
|
|
40
|
-
|
|
38
|
+
phase: dice.floatAt(0) * Float.Tau, clock: clock)
|
|
41
39
|
|
|
42
40
|
fun place(self) -> Place = Place.at(self.drawnX(), self.y)
|
|
43
41
|
|
|
@@ -102,5 +100,3 @@ struct Balloon[Actor, Sized, Touch, Presses<PopAction>](
|
|
|
102
100
|
fun spark(i: Int, reach: Float, color: Color) -> Circle =
|
|
103
101
|
a := Float(i) * Float.Tau / 8.0
|
|
104
102
|
Circle(x: Math.cos(a) * reach, y: Math.sin(a) * reach, r: 8.0, color: color)
|
|
105
|
-
|
|
106
|
-
struct Spawned(balloon: Balloon, rng: Random)
|
examples/games/balloonpop/screens/PlaygroundScreen.plum
CHANGED
|
@@ -32,17 +32,15 @@ enum PopAction[Show] =
|
|
|
32
32
|
| Tick(dt: Float)
|
|
33
33
|
|
|
34
34
|
struct PlaygroundScreen[ScreenSlice<PopAction>](
|
|
35
|
-
|
|
35
|
+
seed: Int, ticks: Int, assets: Assets, pops: Int, partyAt: Float, clock: Float, balloons: List<Balloon>) =
|
|
36
36
|
const Count = 6
|
|
37
37
|
const PartyEvery = 10
|
|
38
38
|
const PartyTime = 2.5
|
|
39
39
|
const Confetti = 30
|
|
40
40
|
|
|
41
41
|
fun seeded(seed: Int, assets: Assets) -> PlaygroundScreen =
|
|
42
|
+
balloons := Range.until(0, PlaygroundScreen.Count).map(|i| Balloon.spawn(Balloon.FirstId + i, Random.at(seed, 0, Balloon.FirstId + i), Float(i) * 110.0 - Arena.H * 0.6, 0.0))
|
|
42
|
-
|
|
43
|
+
PlaygroundScreen(seed: seed, ticks: 0, assets: assets, pops: 0, partyAt: -100.0, clock: 0.0, balloons: balloons)
|
|
43
|
-
Range.until(0, PlaygroundScreen.Count).fold(empty, |s, i|
|
|
44
|
-
sp := Balloon.spawn(Balloon.FirstId + i, s.rng, Float(i) * 110.0 - Arena.H * 0.6, 0.0)
|
|
45
|
-
PlaygroundScreen(..s, rng: sp.rng, balloons: s.balloons.append([sp.balloon])))
|
|
46
44
|
|
|
47
45
|
fun name(self) -> Str = "sky"
|
|
48
46
|
|
|
@@ -88,15 +86,10 @@ struct PlaygroundScreen[ScreenSlice<PopAction>](
|
|
|
88
86
|
Tick(dt) =>
|
|
89
87
|
moved := self.stepped(dt)
|
|
90
88
|
clock := moved.clock + dt
|
|
91
|
-
|
|
89
|
+
ticks := moved.ticks + 1
|
|
90
|
+
seed := moved.seed
|
|
91
|
+
balloons := moved.balloons.map(|b| if b.done() then Balloon.spawn(b.id, Random.at(seed, ticks, b.id), 0.0, clock) else b)
|
|
92
|
-
|
|
92
|
+
Next(state: PlaygroundScreen(..moved, ticks: ticks, clock: clock, balloons: balloons))
|
|
93
|
-
next := balloons.fold(risen, |s, b|
|
|
94
|
-
if b.done()
|
|
95
|
-
sp := Balloon.spawn(b.id, s.rng, 0.0, clock)
|
|
96
|
-
PlaygroundScreen(..s, rng: sp.rng, balloons: s.balloons.append([sp.balloon]))
|
|
97
|
-
else
|
|
98
|
-
PlaygroundScreen(..s, balloons: s.balloons.append([b])))
|
|
99
|
-
Next(state: next)
|
|
100
93
|
|
|
101
94
|
fun popped(self, at: Float) -> PlaygroundScreen =
|
|
102
95
|
pops := self.pops + 1
|
examples/games/orbitrunner/OrbitRunner.plum
CHANGED
|
@@ -25,24 +25,19 @@ primitive Sheet =
|
|
|
25
25
|
const Size = 48.0
|
|
26
26
|
|
|
27
27
|
struct Motion(x: Float, y: Float, vx: Float, vy: Float) =
|
|
28
|
-
fun spawn(
|
|
28
|
+
fun spawn(dice: Random, near: Vec2, gap: Float, speed: Float) -> Motion =
|
|
29
|
-
spot := Motion.spot(rng, ax, ay, gap, 20)
|
|
30
|
-
dh := spot.rng.nextFloat()
|
|
31
|
-
dp := dh.rng.nextFloat()
|
|
32
|
-
heading :=
|
|
29
|
+
heading := dice.floatAt(0) * Float.Tau
|
|
33
|
-
pace := speed * {0.4 + 0.6 *
|
|
30
|
+
pace := speed * {0.4 + 0.6 * dice.floatAt(1)}
|
|
34
|
-
|
|
31
|
+
Motion(..Motion.spot(dice, near, gap, 0), vx: Math.cos(heading) * pace, vy: Math.sin(heading) * pace)
|
|
35
32
|
|
|
36
|
-
fun spot(
|
|
33
|
+
fun spot(dice: Random, near: Vec2, gap: Float, n: Int) -> Motion =
|
|
37
34
|
margin := 30.0
|
|
38
|
-
dx := rng.nextFloat()
|
|
39
|
-
dy := dx.rng.nextFloat()
|
|
40
|
-
x := margin +
|
|
35
|
+
x := margin + dice.floatAt(2 + 2 * n) * {Arena.W - 2.0 * margin}
|
|
41
|
-
y := margin +
|
|
36
|
+
y := margin + dice.floatAt(3 + 2 * n) * {Arena.H - 2.0 * margin}
|
|
42
|
-
if
|
|
37
|
+
if n >= 19 || !Vec2(x: x + Sheet.Size / 2.0, y: y + Sheet.Size / 2.0).near(near, gap)
|
|
43
|
-
|
|
38
|
+
Motion(x: x, y: y, vx: 0.0, vy: 0.0)
|
|
44
39
|
else
|
|
45
|
-
Motion.spot(
|
|
40
|
+
Motion.spot(dice, near, gap, n + 1)
|
|
46
41
|
|
|
47
42
|
fun drifted(self, r: Float, dt: Float) -> Motion =
|
|
48
43
|
x0 := self.x + self.vx * dt
|
|
@@ -55,8 +50,6 @@ struct Motion(x: Float, y: Float, vx: Float, vy: Float) =
|
|
|
55
50
|
vx: if bounceX then 0.0 - self.vx else self.vx,
|
|
56
51
|
vy: if bounceY then 0.0 - self.vy else self.vy)
|
|
57
52
|
|
|
58
|
-
struct Spawned(at: Motion, rng: Random)
|
|
59
|
-
|
|
60
53
|
struct Assets(sheet: Texture, pickup: Sound, hit: Sound, over: Sound, music: Sound) =
|
|
61
54
|
const MusicVolume = 0.3
|
|
62
55
|
|
|
@@ -71,5 +64,5 @@ primitive OrbitRunner[App] =
|
|
|
71
64
|
fun main(io: Io) -> Game =
|
|
72
65
|
assets := Assets.load(io)
|
|
73
66
|
assets.music.loop(io, Assets.MusicVolume)
|
|
74
|
-
play: Screen := PlayingScreen.start(
|
|
67
|
+
play: Screen := PlayingScreen.start(io.randomSeed(), assets)
|
|
75
68
|
Game(screens: [play])
|
examples/games/orbitrunner/actors/Mine.plum
CHANGED
|
@@ -8,15 +8,16 @@ import std/math/Random
|
|
|
8
8
|
import std/game/Place
|
|
9
9
|
import std/game/View
|
|
10
10
|
import std/game/Sprite
|
|
11
|
+
import std/game/Round
|
|
11
12
|
import OrbitRunner
|
|
12
13
|
import actors/Ship
|
|
13
14
|
|
|
14
|
-
struct Mine[Actor](at: Motion, spin: Float = 0.0, clock: Float = 0.0, tex: Texture) =
|
|
15
|
+
struct Mine[Actor, Round](at: Motion, spin: Float = 0.0, clock: Float = 0.0, tex: Texture) =
|
|
15
16
|
const Radius = 11.0
|
|
16
17
|
|
|
17
|
-
fun spawn(
|
|
18
|
+
fun spawn(dice: Random, near: Ship, tex: Texture) -> Mine = Mine(at: Motion.spawn(dice, near.at.pivot(), 220.0, 90.0), tex: tex)
|
|
18
|
-
|
|
19
|
+
|
|
19
|
-
|
|
20
|
+
fun radius(self) -> Float = Mine.Radius
|
|
20
21
|
|
|
21
22
|
fun countFor(score: Int) -> Int = {3 + score / 50}.min(8)
|
|
22
23
|
|
examples/games/orbitrunner/actors/Orb.plum
CHANGED
|
@@ -13,21 +13,22 @@ import std/game/Gfx
|
|
|
13
13
|
import std/game/Place
|
|
14
14
|
import std/game/View
|
|
15
15
|
import std/game/Sprite
|
|
16
|
+
import std/game/Round
|
|
16
17
|
import OrbitRunner
|
|
17
18
|
import actors/Ship
|
|
18
19
|
|
|
19
|
-
struct Orb[Actor](
|
|
20
|
+
struct Orb[Actor, Round](id: Int, at: Motion, clock: Float = 0.0, tex: Texture) =
|
|
20
21
|
const Radius = 10.0
|
|
21
22
|
const Points = 10
|
|
22
23
|
|
|
23
|
-
fun spawn(
|
|
24
|
+
fun spawn(id: Int, dice: Random, near: Ship, tex: Texture) -> Orb = Orb(id: id, at: Motion.spawn(dice, near.at.pivot(), 120.0, 40.0), tex: tex)
|
|
25
|
+
|
|
24
|
-
|
|
26
|
+
fun radius(self) -> Float = Orb.Radius
|
|
25
|
-
Rolled(value: Orb(at: sp.at, phase: phase, tex: tex), rng: sp.rng)
|
|
26
27
|
|
|
27
28
|
fun place(self) -> Place = Place.pivoted(self.at.x, self.at.y, Sheet.Size / 2.0, Sheet.Size / 2.0)
|
|
28
29
|
|
|
29
30
|
fun view(self) -> List<View> =
|
|
30
|
-
[Sprite(tex: self.tex, w: Sheet.Size, h: Sheet.Size, srcX: 0.0, srcY: Sheet.Cell, srcW: Sheet.Cell, srcH: Sheet.Cell, frame: Sprite.frameAt(self.clock + self.
|
|
31
|
+
[Sprite(tex: self.tex, w: Sheet.Size, h: Sheet.Size, srcX: 0.0, srcY: Sheet.Cell, srcW: Sheet.Cell, srcH: Sheet.Cell, frame: Sprite.frameAt(self.clock + Float(self.id) * 0.1, 8.0, 4))]
|
|
31
32
|
|
|
32
33
|
fun step(self, dt: Float) -> Orb = Orb(..self, clock: self.clock + dt, at: self.at.drifted(Orb.Radius, dt))
|
|
33
34
|
|
examples/games/orbitrunner/actors/Ship.plum
CHANGED
|
@@ -10,9 +10,10 @@ import std/game/Place
|
|
|
10
10
|
import std/game/View
|
|
11
11
|
import std/game/Circle
|
|
12
12
|
import std/game/Sprite
|
|
13
|
+
import std/game/Round
|
|
13
14
|
import OrbitRunner
|
|
14
15
|
|
|
15
|
-
struct Ship[Actor](tex: Texture, at: Place, vx: Float, vy: Float, ax: Float, ay: Float, hurt: Float, clock: Float) =
|
|
16
|
+
struct Ship[Actor, Round](tex: Texture, at: Place, vx: Float, vy: Float, ax: Float, ay: Float, hurt: Float, clock: Float) =
|
|
16
17
|
const Radius = 10.0
|
|
17
18
|
const Thrust = 900.0
|
|
18
19
|
const Drag = 2.2
|
|
@@ -25,6 +26,8 @@ struct Ship[Actor](tex: Texture, at: Place, vx: Float, vy: Float, ax: Float, ay:
|
|
|
25
26
|
|
|
26
27
|
fun place(self) -> Place = self.at
|
|
27
28
|
|
|
29
|
+
fun radius(self) -> Float = Ship.Radius
|
|
30
|
+
|
|
28
31
|
fun visible(self) -> Bool = self.lit()
|
|
29
32
|
|
|
30
33
|
fun view(self) -> List<View> = [
|
examples/games/orbitrunner/screens/PlayingScreen.plum
CHANGED
|
@@ -32,23 +32,23 @@ import OrbitRunner
|
|
|
32
32
|
enum PlayAction[Show] =
|
|
33
33
|
| Steer(ax: Float, ay: Float)
|
|
34
34
|
| Tick(dt: Float)
|
|
35
|
-
|
|
|
35
|
+
| Caught(orb: Int)
|
|
36
36
|
| Hit
|
|
37
37
|
| StartOver
|
|
38
38
|
|
|
39
39
|
struct PlayingScreen[ScreenSlice<PlayAction>](
|
|
40
|
-
|
|
40
|
+
seed: Int, ticks: Int, assets: Assets, score: Int, best: Int, lives: Int, over: Bool, clock: Float,
|
|
41
41
|
@stepsWhen(!self.over) ship: Ship, @stepsWhen(!self.over) orbs: List<Orb>, @stepsWhen(!self.over) mines: List<Mine>, rings: List<Ring>) =
|
|
42
42
|
const OrbCount = 3
|
|
43
43
|
const StartLives = 3
|
|
44
44
|
|
|
45
|
-
fun start(
|
|
45
|
+
fun start(seed: Int, assets: Assets) -> PlayingScreen =
|
|
46
46
|
PlayingScreen(
|
|
47
|
-
|
|
47
|
+
seed: seed, ticks: 0, assets: assets, score: 0, best: 0, lives: PlayingScreen.StartLives, over: false, clock: 0.0,
|
|
48
48
|
ship: Ship.create(assets.sheet), orbs: Nil, mines: Nil, rings: Nil).deal()
|
|
49
49
|
|
|
50
|
-
fun at(
|
|
50
|
+
fun at(seed: Int, assets: Assets, score: Int, lives: Int) -> PlayingScreen =
|
|
51
|
-
PlayingScreen(..PlayingScreen.start(
|
|
51
|
+
PlayingScreen(..PlayingScreen.start(seed, assets), score: score, lives: lives).grown()
|
|
52
52
|
|
|
53
53
|
fun name(self) -> Str = "play"
|
|
54
54
|
|
|
@@ -88,15 +88,19 @@ struct PlayingScreen[ScreenSlice<PlayAction>](
|
|
|
88
88
|
Steer(ax, ay) => Next(state: PlayingScreen(..self, ship: self.ship.steer(ax, ay)))
|
|
89
89
|
Tick(dt) =>
|
|
90
90
|
stepped := self.stepped(dt)
|
|
91
|
-
moved := PlayingScreen(..stepped, clock: stepped.clock + dt, rings: stepped.rings.filter(|r| !r.done()))
|
|
91
|
+
moved := PlayingScreen(..stepped, ticks: stepped.ticks + 1, clock: stepped.clock + dt, rings: stepped.rings.filter(|r| !r.done()))
|
|
92
|
-
if moved.over then
|
|
92
|
+
Next(state: moved, then: if moved.over then Nil else moved.rules())
|
|
93
|
-
|
|
93
|
+
Caught(orb) =>
|
|
94
94
|
sounds := self.assets
|
|
95
|
+
dice := self.dice(orb)
|
|
96
|
+
ship := self.ship
|
|
97
|
+
orbs := self.orbs.map(|o| if o.id == orb then Orb.spawn(orb, dice, ship, o.tex) else o)
|
|
95
|
-
Next(state: PlayingScreen(..self, score: self.score +
|
|
98
|
+
Next(state: PlayingScreen(..self, score: self.score + Orb.Points, orbs: orbs).grown(), cmds: [Play(sound: sounds.pickup, volume: 0.8)])
|
|
96
99
|
Hit =>
|
|
97
100
|
sounds := self.assets
|
|
98
101
|
lives := self.lives - 1
|
|
102
|
+
c := self.ship.at.pivot()
|
|
99
|
-
ring := Ring.at(
|
|
103
|
+
ring := Ring.at(c.x, c.y)
|
|
100
104
|
over := self.over || lives <= 0
|
|
101
105
|
Next(
|
|
102
106
|
state: PlayingScreen(
|
|
@@ -110,37 +114,24 @@ struct PlayingScreen[ScreenSlice<PlayAction>](
|
|
|
110
114
|
state: PlayingScreen(..self, score: 0, best: best, lives: PlayingScreen.StartLives, over: false).deal(),
|
|
111
115
|
cmds: [Stop(sound: sounds.music), Loop(sound: sounds.music, volume: Assets.MusicVolume)])
|
|
112
116
|
|
|
113
|
-
fun rules(self) ->
|
|
117
|
+
fun rules(self) -> List<PlayAction> = [
|
|
114
|
-
sh := self.ship
|
|
115
|
-
p := Vec2(x: sh.at.x, y: sh.at.y)
|
|
116
|
-
|
|
118
|
+
..self.orbs.filter(|o| self.ship.touches(o)).map(|o| Caught(orb: o.id))
|
|
117
|
-
if p.near(Vec2(x: orb.at.x, y: orb.at.y), Ship.Radius + Orb.Radius)
|
|
118
|
-
o := Orb.spawn(c.rng, sh, orb.phase, orb.tex)
|
|
119
|
-
Caught(orbs: Cons(o.value, c.orbs), rng: o.rng, points: c.points + Orb.Points)
|
|
120
|
-
else
|
|
121
|
-
Caught(..c, orbs: Cons(orb, c.orbs))
|
|
122
|
-
)
|
|
123
|
-
|
|
119
|
+
if self.ship.hurt <= 0.0 && self.mines.any(|m| self.ship.touches(m)) then Hit
|
|
124
|
-
Next(state: PlayingScreen(..self, rng: caught.rng, orbs: caught.orbs.reverse()), then: [
|
|
125
|
-
if caught.points > 0 then Scored(points: caught.points)
|
|
126
|
-
if hit then Hit
|
|
127
|
-
|
|
120
|
+
]
|
|
121
|
+
|
|
122
|
+
fun dice(self, salt: Int) -> Random = Random.at(self.seed, self.ticks, salt)
|
|
128
123
|
|
|
129
124
|
fun deal(self) -> PlayingScreen =
|
|
130
125
|
ship := Ship.create(self.assets.sheet)
|
|
131
126
|
tex := self.assets.sheet
|
|
127
|
+
seed := self.seed
|
|
132
|
-
|
|
128
|
+
ticks := self.ticks
|
|
133
|
-
|
|
129
|
+
orbs := Range.until(0, PlayingScreen.OrbCount).map(|i| Orb.spawn(i, Random.at(seed, ticks, i), ship, tex))
|
|
134
|
-
o := Orb.spawn(r.rng, ship, Float(i) * 0.1, tex)
|
|
135
|
-
Rolled(value: r.value.append([o.value]), rng: o.rng)
|
|
136
|
-
)
|
|
137
|
-
PlayingScreen(..self,
|
|
130
|
+
PlayingScreen(..self, clock: 0.0, ship: ship, orbs: orbs, mines: Nil, rings: Nil).grown()
|
|
138
131
|
|
|
139
132
|
fun grown(self) -> PlayingScreen =
|
|
140
133
|
if self.mines.length() < Mine.countFor(self.score)
|
|
141
|
-
|
|
134
|
+
mine := Mine.spawn(self.dice(PlayingScreen.OrbCount + self.mines.length()), self.ship, self.assets.sheet)
|
|
142
|
-
PlayingScreen(..self,
|
|
135
|
+
PlayingScreen(..self, mines: self.mines.append([mine])).grown()
|
|
143
136
|
else
|
|
144
137
|
self
|
|
145
|
-
|
|
146
|
-
struct Caught(orbs: List<Orb>, rng: Random, points: Int)
|
std/game/Place.plum
CHANGED
|
@@ -72,5 +72,9 @@ struct Place(
|
|
|
72
72
|
# The transform of what is under it, `m` being its parent's: `m` times `local`.
|
|
73
73
|
fun under(self, m: Affine) -> Affine = m.mul(self.local())
|
|
74
74
|
|
|
75
|
+
# Where its origin lands in the parent: the point it turns and scales about, the same
|
|
76
|
+
# whatever its rotation and scale (`Place.pivoted(x, y, ox, oy)`'s is (x + ox, y + oy)).
|
|
77
|
+
fun pivot(self) -> Vec2 = Vec2(x: self.x + self.originX, y: self.y + self.originY)
|
|
78
|
+
|
|
75
79
|
# A point of the parent's coordinates in its own.
|
|
76
80
|
fun toLocal(self, p: Vec2) -> Vec2 = self.local().inverse().apply(p)
|
std/game/Round.plum
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module std
|
|
2
|
+
|
|
3
|
+
import std/Bool
|
|
4
|
+
import std/Float
|
|
5
|
+
import std/math/Vec2
|
|
6
|
+
import std/game/Place
|
|
7
|
+
import std/game/View
|
|
8
|
+
|
|
9
|
+
# A round actor: a circle of `radius()` about its pivot, the point its place turns about
|
|
10
|
+
# (`Place.pivot`). Two round actors of the same parent touch when their circles overlap:
|
|
11
|
+
#
|
|
12
|
+
# struct Orb[Actor, Round](..) =
|
|
13
|
+
# fun radius(self) -> Float = 10.0
|
|
14
|
+
#
|
|
15
|
+
# orb.touches(ship)
|
|
16
|
+
trait Round[View] =
|
|
17
|
+
fun radius(self) -> Float
|
|
18
|
+
|
|
19
|
+
fun touches(self, other: Round) -> Bool = self.place().pivot().near(other.place().pivot(), self.radius() + other.radius())
|
std/game/TestRig.plum
CHANGED
|
@@ -26,7 +26,7 @@ import std/game/Game
|
|
|
26
26
|
# A screen or a whole game run headless, for tests: input in, ticks through the game's own
|
|
27
27
|
# tick, and what came out read back.
|
|
28
28
|
#
|
|
29
|
-
# t := TestRig.mount(io, PlayingScreen.start(
|
|
29
|
+
# t := TestRig.mount(io, PlayingScreen.start(7, Assets.none()))
|
|
30
30
|
# u := t.hold(Key.Right).ticks(30)
|
|
31
31
|
# assert u.screen.score == 10
|
|
32
32
|
# assert u.text(u.first<Label>()) == "Score 10"
|
std/math/Random.plum
CHANGED
|
@@ -9,6 +9,9 @@ import std/Float
|
|
|
9
9
|
# draw returns the value together with the NEXT generator (`Rolled`), so thread
|
|
10
10
|
# the new `rng` forward. For a wall-clock seed use `io.randomSeed()`.
|
|
11
11
|
#
|
|
12
|
+
# A game that would rather not thread a generator draws by counter instead:
|
|
13
|
+
# `Random.at(seed, tick, salt)` and its `floatAt(i)` (below).
|
|
14
|
+
#
|
|
12
15
|
# `Random.seeded(42)` — `seed` holds the generator's CURRENT state (a seed of 0
|
|
13
16
|
# is a fixed point of xorshift, so it is silently replaced by a non-zero
|
|
14
17
|
# constant on first use).
|
|
@@ -45,3 +48,39 @@ struct Random(seed: Int) =
|
|
|
45
48
|
d := self.nextInt()
|
|
46
49
|
bits := {d.value >> 11} & 0x1FFFFFFFFFFFFF
|
|
47
50
|
Rolled(value: Float(bits) / 9007199254740992.0, rng: d.rng)
|
|
51
|
+
|
|
52
|
+
# Counter-based draws: nothing is threaded. `Random.at(seed, tick, salt)` is the key of
|
|
53
|
+
# one moment of one thing (a game's seed, its tick count, the id of what draws), and
|
|
54
|
+
# `intAt(i)`, `floatAt(i)` and `intBelowAt(i, n)` are its draws, each a pure function of
|
|
55
|
+
# the key and `i`: the same key and index always give the same value, and two draws of
|
|
56
|
+
# the same key at different indexes, or of keys that differ in any part, are independent.
|
|
57
|
+
#
|
|
58
|
+
# dice := Random.at(seed, ticks, orb.id)
|
|
59
|
+
# x := dice.floatAt(0)
|
|
60
|
+
# y := dice.floatAt(1)
|
|
61
|
+
#
|
|
62
|
+
# The key is SplitMix64's finaliser (`mix`) applied in turn to the seed, the tick and the
|
|
63
|
+
# salt, each added to the last result; a draw is `mix` of the key plus `i + 1` times the
|
|
64
|
+
# golden ratio's 64 bits (SplitMix64's step), so draws in a row are SplitMix64's stream
|
|
65
|
+
# from the key. `nextInt` and the other draws still work on the key, as a generator.
|
|
66
|
+
fun at(seed: Int, tick: Int, salt: Int) -> Random =
|
|
67
|
+
Random(seed: Random.mix(Random.mix(Random.mix(seed + Random.Golden) + tick) + salt))
|
|
68
|
+
|
|
69
|
+
# SplitMix64's step: 0x9E3779B97F4A7C15 as a signed Int.
|
|
70
|
+
const Golden = -7046029254386353131
|
|
71
|
+
|
|
72
|
+
# SplitMix64's finaliser: every bit of `z` reaches every bit of the result (a bijection on
|
|
73
|
+
# 64 bits). `{x >> k} & mask` is the logical shift (`>>` is arithmetic).
|
|
74
|
+
fun mix(z: Int) -> Int =
|
|
75
|
+
a := {z ^ {{z >> 30} & 0x3FFFFFFFF}} * -4658895280553007687
|
|
76
|
+
b := {a ^ {{a >> 27} & 0x1FFFFFFFFF}} * -7723592293110705685
|
|
77
|
+
b ^ {{b >> 31} & 0x1FFFFFFFF}
|
|
78
|
+
|
|
79
|
+
# The key's `i`th draw: 64 well-mixed bits (may be negative).
|
|
80
|
+
fun intAt(self, i: Int) -> Int = Random.mix(self.seed + {i + 1} * Random.Golden)
|
|
81
|
+
|
|
82
|
+
# The key's `i`th draw as a Float in [0.0, 1.0) with 53 bits of precision.
|
|
83
|
+
fun floatAt(self, i: Int) -> Float = Float({self.intAt(i) >> 11} & 0x1FFFFFFFFFFFFF) / 9007199254740992.0
|
|
84
|
+
|
|
85
|
+
# The key's `i`th draw as an Int in [0, n), or 0 for n <= 0.
|
|
86
|
+
fun intBelowAt(self, i: Int, n: Int) -> Int = if n <= 0 then 0 else {self.intAt(i) & 0x7FFFFFFFFFFFFFFF} % n
|
tests/examples/games/balloonpop/screens/PlaygroundScreen.plum
CHANGED
|
@@ -65,7 +65,7 @@ primitive PlaygroundScreenTest[UnitTest] =
|
|
|
65
65
|
|
|
66
66
|
fun aPoppedBalloonComesBackAtTheBottom() =
|
|
67
67
|
t := PlaygroundScreenHelpers.centred(PlaygroundScreenHelpers.field(io, 3)).clickAt(400.0, 300.0).tick(0.0)
|
|
68
|
-
assert PlaygroundScreenHelpers.lines(t) == "
|
|
68
|
+
assert PlaygroundScreenHelpers.lines(t) == "Blue!, 1"
|
|
69
69
|
u := t.tick(Balloon.PopTime + 0.1)
|
|
70
70
|
assert !u.screen.balloons.at(0).popped()
|
|
71
71
|
assert u.screen.balloons.at(0).y > Arena.H
|
|
@@ -76,7 +76,7 @@ primitive PlaygroundScreenTest[UnitTest] =
|
|
|
76
76
|
u := PlaygroundScreenHelpers.centred(t).clickAt(400.0, 300.0).tick(0.0)
|
|
77
77
|
assert u.screen.pops == 10
|
|
78
78
|
assert u.screen.partying()
|
|
79
|
-
assert PlaygroundScreenHelpers.lines(u) == "
|
|
79
|
+
assert PlaygroundScreenHelpers.lines(u) == "Blue!, Hooray!, 10 balloons!, 10"
|
|
80
80
|
assert !u.tick(PlaygroundScreen.PartyTime + 0.1).screen.partying()
|
|
81
81
|
|
|
82
82
|
# each pop plays the pop sound and the tenth the fanfare too, in the tick of the press
|
|
@@ -203,7 +203,7 @@ primitive PlaygroundScreenHelpers =
|
|
|
203
203
|
Input(events: [if i % 7 == 6 then PointerDown(x: Float({i * 37} % 760 + 20), y: Float({i * 53} % 500 + 50))])
|
|
204
204
|
|
|
205
205
|
fun sameField(a: PlaygroundScreen, b: PlaygroundScreen) -> Bool =
|
|
206
|
-
a.pops == b.pops && a.clock == b.clock && a.partyAt == b.partyAt && a.
|
|
206
|
+
a.pops == b.pops && a.clock == b.clock && a.partyAt == b.partyAt && a.ticks == b.ticks
|
|
207
207
|
&& Range.until(0, a.balloons.length()).all(|i|
|
|
208
208
|
p := a.balloons.at(i)
|
|
209
209
|
q := b.balloons.at(i)
|
tests/examples/games/orbitrunner/actors/Orb.plum
CHANGED
|
@@ -20,17 +20,21 @@ import std/Test
|
|
|
20
20
|
|
|
21
21
|
primitive OrbTest[UnitTest] =
|
|
22
22
|
fun anOrbDriftsAndBouncesOffTheWalls() =
|
|
23
|
-
orb := Orb(at: Motion(x: 795.0, y: 300.0, vx: 100.0, vy: 0.0), tex: Texture.none()).step(0.1)
|
|
23
|
+
orb := Orb(id: 0, at: Motion(x: 795.0, y: 300.0, vx: 100.0, vy: 0.0), tex: Texture.none()).step(0.1)
|
|
24
24
|
assert orb.at.vx == -100.0
|
|
25
25
|
assert orb.at.x == Arena.W - 10.0
|
|
26
26
|
assert orb.clock == 0.1
|
|
27
27
|
|
|
28
|
-
# an orb and a mine spawn themselves away from the ship,
|
|
28
|
+
# an orb and a mine spawn themselves away from the ship, each a pure draw of its key
|
|
29
29
|
fun orbsAndMinesSpawnAwayFromTheShip() =
|
|
30
30
|
ship := Ship.create(Texture.none())
|
|
31
|
+
seeds := Range.until(0, 200)
|
|
32
|
+
assert seeds.all(|k| OrbHelpers.away(Orb.spawn(1, Random.at(k, 3, 1), ship, Texture.none()).at, ship) >= 120.0)
|
|
33
|
+
assert seeds.all(|k| OrbHelpers.away(Mine.spawn(Random.at(k, 3, 4), ship, Texture.none()).at, ship) >= 220.0)
|
|
31
|
-
o := Orb.spawn(Random.
|
|
34
|
+
o := Orb.spawn(2, Random.at(7, 0, 2), ship, Texture.none())
|
|
32
|
-
assert Math.hypot(o.value.at.x - ship.at.x, o.value.at.y - ship.at.y) >= 120.0
|
|
33
|
-
assert o.
|
|
35
|
+
assert o.id == 2 && o.clock == 0.0
|
|
34
|
-
m := Mine.spawn(o.rng, ship, Texture.none())
|
|
35
|
-
assert Math.hypot(m.value.at.x - ship.at.x, m.value.at.y - ship.at.y) >= 220.0
|
|
36
|
-
assert Orb.spawn(Random.
|
|
36
|
+
assert Orb.spawn(2, Random.at(7, 0, 2), ship, Texture.none()).at.x == o.at.x
|
|
37
|
+
assert Orb.spawn(2, Random.at(7, 1, 2), ship, Texture.none()).at.x != o.at.x
|
|
38
|
+
|
|
39
|
+
primitive OrbHelpers =
|
|
40
|
+
fun away(m: Motion, ship: Ship) -> Float = Math.hypot(m.x - ship.at.x, m.y - ship.at.y)
|
tests/examples/games/orbitrunner/screens/PlayingScreen.plum
CHANGED
|
@@ -50,22 +50,56 @@ primitive PlayingScreenTest[UnitTest] =
|
|
|
50
50
|
# a tick is Steer with the keys held, then Tick, whose rules are followed by what happened
|
|
51
51
|
fun aTickIsSteerWithTheKeysHeld() =
|
|
52
52
|
t := PlayingScreenHelpers.orbOnShip(PlayingScreenHelpers.emptied(io, 0, PlayingScreen.StartLives)).hold(Key.Right).hold(Key.KeyW).tick(0.016)
|
|
53
|
-
assert PlayingScreenHelpers.names(t.actions<PlayAction>()) == "Steer(1.0, -1.0) Tick(0.016)
|
|
53
|
+
assert PlayingScreenHelpers.names(t.actions<PlayAction>()) == "Steer(1.0, -1.0) Tick(0.016) Caught(0)"
|
|
54
54
|
|
|
55
|
-
#
|
|
55
|
+
# an orb on the ship answers Caught with its id, which scores, plays the pickup and respawns that orb elsewhere, and the HUD shows it
|
|
56
|
-
fun
|
|
56
|
+
fun anOrbOnTheShipAnswersCaughtWithItsId() =
|
|
57
57
|
t := PlayingScreenHelpers.orbOnShip(PlayingScreenHelpers.emptied(io, 0, PlayingScreen.StartLives)).tick(0.016)
|
|
58
58
|
out := PlayingScreenHelpers.ruledLast(t)
|
|
59
|
-
assert PlayingScreenHelpers.names(out) == "
|
|
59
|
+
assert PlayingScreenHelpers.names(out) == "Caught(0)"
|
|
60
60
|
assert Cmd.names(t.cmds) == "play(-1, 0.8)"
|
|
61
61
|
assert t.screen.score == Orb.Points
|
|
62
62
|
orb := t.screen.orbs.at(0)
|
|
63
63
|
ship := t.screen.ship
|
|
64
64
|
assert Math.hypot(orb.at.x - ship.at.x, orb.at.y - ship.at.y) >= 120.0
|
|
65
65
|
assert orb.at.x < 5000.0
|
|
66
|
+
assert orb.id == 0
|
|
67
|
+
assert t.screen.orbs.map(|o| o.id) == [0, 1, 2]
|
|
66
68
|
assert t.count<Orb>() == PlayingScreen.OrbCount
|
|
67
69
|
assert PlayingScreenHelpers.hud(t) == "Score 10, Best 10"
|
|
68
70
|
|
|
71
|
+
# two orbs caught in one tick are two Caught actions, scoring both, each respawned from its own draw
|
|
72
|
+
fun twoOrbsCaughtInOneTickAreTwoActions() =
|
|
73
|
+
t := PlayingScreenHelpers.orbsOnShip(PlayingScreenHelpers.emptied(io, 0, PlayingScreen.StartLives)).tick(0.016)
|
|
74
|
+
assert PlayingScreenHelpers.names(PlayingScreenHelpers.ruledLast(t)) == "Caught(0) Caught(1)"
|
|
75
|
+
assert Cmd.names(t.cmds) == "play(-1, 0.8) play(-1, 0.8)"
|
|
76
|
+
assert t.screen.score == 2 * Orb.Points
|
|
77
|
+
a := t.screen.orbs.at(0)
|
|
78
|
+
b := t.screen.orbs.at(1)
|
|
79
|
+
assert a.at.x < 5000.0 && b.at.x < 5000.0 && a.at.x != b.at.x
|
|
80
|
+
|
|
81
|
+
# two mines on the ship are one Hit
|
|
82
|
+
fun twoMinesOnTheShipAreOneHit() =
|
|
83
|
+
s := PlayingScreenHelpers.emptied(io, 0, PlayingScreen.StartLives).screen
|
|
84
|
+
on := PlayingScreenHelpers.onShip(s.ship)
|
|
85
|
+
p := PlayingScreen(..s, mines: s.mines.map(|m| Mine(..m, at: on)))
|
|
86
|
+
assert PlayingScreenHelpers.names(p.rules()) == "Hit"
|
|
87
|
+
assert p.update(Hit).state.lives == PlayingScreen.StartLives - 1
|
|
88
|
+
|
|
89
|
+
# the rules: an orb touching the ship is Caught with its id, a mine touching it Hit unless the ship is hurt
|
|
90
|
+
fun theRulesCatchOrbsAndHitTheShip() =
|
|
91
|
+
s := PlayingScreenHelpers.emptied(io, 0, PlayingScreen.StartLives).screen
|
|
92
|
+
ship := s.ship
|
|
93
|
+
orb := Orb(..s.orbs.at(2), at: PlayingScreenHelpers.onShip(ship))
|
|
94
|
+
withOrb := |o: Orb| PlayingScreenHelpers.names(PlayingScreen(..s, orbs: [o]).rules())
|
|
95
|
+
assert withOrb(orb) == "Caught(2)"
|
|
96
|
+
assert withOrb(Orb(..orb, at: Motion(..orb.at, x: ship.at.x + 19.0))) == "Caught(2)"
|
|
97
|
+
assert withOrb(Orb(..orb, at: Motion(..orb.at, x: ship.at.x + 20.0))) == ""
|
|
98
|
+
mined := PlayingScreen(..s, mines: [Mine(..s.mines.at(0), at: PlayingScreenHelpers.onShip(ship))])
|
|
99
|
+
assert PlayingScreenHelpers.names(mined.rules()) == "Hit"
|
|
100
|
+
assert PlayingScreenHelpers.names(PlayingScreen(..mined, ship: ship.hurtFor(1.0)).rules()) == ""
|
|
101
|
+
assert PlayingScreenHelpers.names(s.rules()) == ""
|
|
102
|
+
|
|
69
103
|
# a mine is followed by Hit, which plays the hit, a life icon goes, and the ship blinks while it is hurt
|
|
70
104
|
fun aMineDispatchesHitALifeIconGoes() =
|
|
71
105
|
t := PlayingScreenHelpers.mineOnShip(PlayingScreenHelpers.emptied(io, 0, PlayingScreen.StartLives)).tick(0.016)
|
|
@@ -87,7 +121,7 @@ primitive PlayingScreenTest[UnitTest] =
|
|
|
87
121
|
# a mine the rules push is in the view at once, and moves from the next tick
|
|
88
122
|
fun aMineTheRulesPushIsInThe() =
|
|
89
123
|
t := PlayingScreenHelpers.emptied(io, 0, PlayingScreen.StartLives)
|
|
90
|
-
p := t.screen.update(
|
|
124
|
+
p := PlayingScreen(..t.screen, score: 40).update(Caught(orb: 0)).state
|
|
91
125
|
assert p.mines.length() == 4
|
|
92
126
|
late := p.mines.at(3)
|
|
93
127
|
u := PlayingScreenHelpers.poked(t, p)
|
|
@@ -130,7 +164,7 @@ primitive PlayingScreenTest[UnitTest] =
|
|
|
130
164
|
p := steered.update(Tick(dt: 0.1)).state
|
|
131
165
|
assert p.ship.at.x > Arena.W / 2.0
|
|
132
166
|
assert p.ship.at.x == t.hold(Key.Right).tick(0.1).screen.ship.at.x
|
|
133
|
-
q := p.update(
|
|
167
|
+
q := p.update(Caught(orb: 1)).state.update(Caught(orb: 2)).state
|
|
134
168
|
assert q.score == 20
|
|
135
169
|
assert PlayingScreenHelpers.hud(PlayingScreenHelpers.poked(t, q)) == "Score 20, Best 20"
|
|
136
170
|
|
|
@@ -209,7 +243,7 @@ primitive PlayingScreenTest[UnitTest] =
|
|
|
209
243
|
|
|
210
244
|
# the reducer scores, takes lives, ends the game and starts over, and the view follows
|
|
211
245
|
fun theReducerScoresTakesLivesEndsTheGame() =
|
|
212
|
-
t := PlayingScreenHelpers.emptied(io, 0, PlayingScreen.StartLives).dispatch(
|
|
246
|
+
t := PlayingScreenHelpers.emptied(io, 0, PlayingScreen.StartLives).dispatch(Caught(orb: 0)).dispatch(Caught(orb: 1)).dispatch(Caught(orb: 2))
|
|
213
247
|
assert t.screen.score == 30
|
|
214
248
|
assert t.screen.best == 0
|
|
215
249
|
assert PlayingScreenHelpers.hud(t) == "Score 30, Best 30"
|
|
@@ -231,7 +265,7 @@ primitive PlayingScreenTest[UnitTest] =
|
|
|
231
265
|
assert w.count<Overlay>() == 0
|
|
232
266
|
assert w.shown(w.screen.ship)
|
|
233
267
|
assert PlayingScreenHelpers.hud(w) == "Score 0, Best 30"
|
|
234
|
-
x := w.dispatch(
|
|
268
|
+
x := Range.until(0, 5).fold(w, |r, i| r.dispatch(Caught(orb: i % 3))).dispatch(StartOver)
|
|
235
269
|
assert x.screen.best == 50
|
|
236
270
|
assert x.screen.score == 0
|
|
237
271
|
|
|
@@ -297,7 +331,7 @@ primitive PlayingScreenTest[UnitTest] =
|
|
|
297
331
|
assert v.shown(v.screen.ship)
|
|
298
332
|
assert PlayingScreenHelpers.icons(v) == PlayingScreen.StartLives
|
|
299
333
|
|
|
300
|
-
# starting over deals a fresh field for the fresh run
|
|
334
|
+
# starting over deals a fresh field for the fresh run: the tick count goes on, so the draws differ
|
|
301
335
|
fun startingOverDealsAFreshFieldForThe() =
|
|
302
336
|
t := PlayingScreenHelpers.mineOnShip(PlayingScreenHelpers.mounted(io, 42, 100, 1)).tick()
|
|
303
337
|
assert t.screen.over
|
|
@@ -316,17 +350,17 @@ primitive PlayingScreenTest[UnitTest] =
|
|
|
316
350
|
|
|
317
351
|
# a recorded run of 600 ticks replays to the same score and positions
|
|
318
352
|
fun aRecordedRunOf600TicksReplaysTo() =
|
|
319
|
-
a := PlayingScreenHelpers.framesUntil(PlayingScreenHelpers.mounted(io,
|
|
353
|
+
a := PlayingScreenHelpers.framesUntil(PlayingScreenHelpers.mounted(io, 43, 0, PlayingScreen.StartLives).record(43), 0, 600)
|
|
320
354
|
r := a.recorded()
|
|
321
355
|
assert r.length() == a.game.ticks
|
|
322
356
|
assert a.screen.score > 0
|
|
323
357
|
assert a.screen.lives < PlayingScreen.StartLives
|
|
324
358
|
back := Replay.decode(r.encode()).unwrap()
|
|
325
|
-
assert back.seed ==
|
|
359
|
+
assert back.seed == 43
|
|
326
360
|
b := PlayingScreenHelpers.mounted(io, back.seed, 0, PlayingScreen.StartLives).replay(back)
|
|
327
361
|
assert PlayingScreenHelpers.sameRun(b.screen, a.screen)
|
|
328
362
|
assert b.screen.score == a.screen.score
|
|
329
|
-
c := PlayingScreenHelpers.mounted(io,
|
|
363
|
+
c := PlayingScreenHelpers.mounted(io, 44, 0, PlayingScreen.StartLives).replay(back)
|
|
330
364
|
assert !PlayingScreenHelpers.sameRun(c.screen, a.screen)
|
|
331
365
|
|
|
332
366
|
# a recorded run through game over and a restart replays to the same state
|
|
@@ -342,7 +376,7 @@ primitive PlayingScreenTest[UnitTest] =
|
|
|
342
376
|
|
|
343
377
|
# a run through game over and a restart, rewound to its middle and played on again, reaches the same end
|
|
344
378
|
fun aRunThroughGameOverAndARestart() =
|
|
345
|
-
a := PlayingScreenHelpers.counted(Run(rig: TestRig.mountWith(io, PlayingScreen.at(
|
|
379
|
+
a := PlayingScreenHelpers.counted(Run(rig: TestRig.mountWith(io, PlayingScreen.at(42, Assets.none(), 0, 1)), overs: 0, restarts: 0), 0, 900, Ticker.Tick)
|
|
346
380
|
b := PlayingScreenHelpers.counted(Run(rig: PlayingScreenHelpers.mounted(io, 42, 0, 1), overs: 0, restarts: 0), 0, 900, Ticker.Tick)
|
|
347
381
|
assert a.restarts > 0
|
|
348
382
|
assert PlayingScreenHelpers.sameRun(a.rig.screen, b.rig.screen)
|
|
@@ -372,12 +406,12 @@ primitive PlayingScreenHelpers =
|
|
|
372
406
|
fun names(out: List<PlayAction>) -> Str = out.fold("", |s, a| if s == "" then a.toStr() else "{s} {a.toStr()}")
|
|
373
407
|
|
|
374
408
|
fun mounted(io: Io, seed: Int, score: Int, lives: Int) -> TestRig<PlayingScreen> =
|
|
375
|
-
TestRig.mount(io, PlayingScreen.at(
|
|
409
|
+
TestRig.mount(io, PlayingScreen.at(seed, Assets.none(), score, lives))
|
|
376
410
|
|
|
377
411
|
fun cleared(s: PlayingScreen) -> PlayingScreen = PlayingScreen(..s, orbs: s.orbs.map(|o| Orb(..o, at: away())), mines: s.mines.map(|m| Mine(..m, at: away())))
|
|
378
412
|
|
|
379
413
|
fun emptied(io: Io, score: Int, lives: Int) -> TestRig<PlayingScreen> =
|
|
380
|
-
TestRig.mount(io, cleared(PlayingScreen.at(
|
|
414
|
+
TestRig.mount(io, cleared(PlayingScreen.at(7, Assets.none(), score, lives)))
|
|
381
415
|
|
|
382
416
|
fun poked(t: TestRig<PlayingScreen>, s: PlayingScreen) -> TestRig<PlayingScreen> =
|
|
383
417
|
only: Screen := s
|
|
@@ -387,6 +421,10 @@ primitive PlayingScreenHelpers =
|
|
|
387
421
|
s := t.screen
|
|
388
422
|
poked(t, PlayingScreen(..s, orbs: Cons(Orb(..s.orbs.at(0), at: onShip(s.ship)), rest(s.orbs))))
|
|
389
423
|
|
|
424
|
+
fun orbsOnShip(t: TestRig<PlayingScreen>) -> TestRig<PlayingScreen> =
|
|
425
|
+
s := t.screen
|
|
426
|
+
poked(t, PlayingScreen(..s, orbs: s.orbs.map(|o| if o.id < 2 then Orb(..o, at: onShip(s.ship)) else o)))
|
|
427
|
+
|
|
390
428
|
fun mineOnShip(t: TestRig<PlayingScreen>) -> TestRig<PlayingScreen> =
|
|
391
429
|
s := t.screen
|
|
392
430
|
poked(t, PlayingScreen(..s, mines: Cons(Mine(..s.mines.at(0), at: onShip(s.ship)), rest(s.mines))))
|
|
@@ -410,7 +448,7 @@ primitive PlayingScreenHelpers =
|
|
|
410
448
|
fun hud(t: TestRig<PlayingScreen>) -> Str = "{t.text(t.find<Label>().at(0))}, {t.text(t.find<Label>().at(1))}"
|
|
411
449
|
|
|
412
450
|
fun sameRun(a: PlayingScreen, b: PlayingScreen) -> Bool =
|
|
413
|
-
a.score == b.score && a.best == b.best && a.lives == b.lives && a.over == b.over && a.
|
|
451
|
+
a.score == b.score && a.best == b.best && a.lives == b.lives && a.over == b.over && a.ticks == b.ticks
|
|
414
452
|
&& a.ship.at.x == b.ship.at.x && a.ship.at.y == b.ship.at.y && a.ship.vx == b.ship.vx && a.ship.vy == b.ship.vy
|
|
415
453
|
&& a.ship.hurt == b.ship.hurt && a.ship.clock == b.ship.clock
|
|
416
454
|
&& a.orbs.length() == b.orbs.length() && a.mines.length() == b.mines.length()
|
tests/std/game/Place.plum
CHANGED
|
@@ -6,6 +6,15 @@ import std/game/Place
|
|
|
6
6
|
import std/Test
|
|
7
7
|
|
|
8
8
|
primitive PlaceTest[UnitTest] =
|
|
9
|
+
# the pivot is where the origin lands in the parent, whatever the rotation and scale
|
|
10
|
+
fun thePivotIsWhereTheOriginLands() =
|
|
11
|
+
p := Place.pivoted(10.0, 20.0, 24.0, 24.0)
|
|
12
|
+
assert p.pivot() == Vec2(x: 34.0, y: 44.0)
|
|
13
|
+
assert p.rotateTo(1.2).scaleTo(3.0, 0.5).pivot() == p.pivot()
|
|
14
|
+
q := p.rotateTo(1.2).scaleTo(3.0, 0.5).local().apply(Vec2(x: 24.0, y: 24.0))
|
|
15
|
+
assert Math.near(q.x, 34.0, 0.0001) && Math.near(q.y, 44.0, 0.0001)
|
|
16
|
+
assert Place.at(5.0, 6.0).pivot() == Vec2(x: 5.0, y: 6.0)
|
|
17
|
+
|
|
9
18
|
# local translates, then turns and scales about the origin
|
|
10
19
|
fun localTranslatesThenTurnsAndScalesAboutThe() =
|
|
11
20
|
b := Place.turned(10.0, 20.0, 2.0, 0.0).pivotAt(1.0, 1.0)
|
tests/std/game/Round.plum
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import std/Bool
|
|
2
|
+
import std/Float
|
|
3
|
+
import std/collections/List
|
|
4
|
+
import std/math/Vec2
|
|
5
|
+
import std/game/Place
|
|
6
|
+
import std/game/View
|
|
7
|
+
import std/game/Round
|
|
8
|
+
import std/Test
|
|
9
|
+
|
|
10
|
+
struct Ball[Round](x: Float, y: Float, r: Float) =
|
|
11
|
+
fun place(self) -> Place = Place.pivoted(self.x, self.y, 8.0, 8.0)
|
|
12
|
+
|
|
13
|
+
fun radius(self) -> Float = self.r
|
|
14
|
+
|
|
15
|
+
struct Dot[Round](x: Float, y: Float) =
|
|
16
|
+
fun place(self) -> Place = Place.at(self.x, self.y).rotateTo(2.0)
|
|
17
|
+
|
|
18
|
+
fun radius(self) -> Float = 1.0
|
|
19
|
+
|
|
20
|
+
primitive RoundTest[UnitTest] =
|
|
21
|
+
# two round actors touch when their circles about their pivots overlap, not when they only meet
|
|
22
|
+
fun twoRoundActorsTouchWhenTheirCirclesOverlap() =
|
|
23
|
+
a := Ball(x: 0.0, y: 0.0, r: 5.0)
|
|
24
|
+
assert a.touches(Ball(x: 9.0, y: 0.0, r: 5.0))
|
|
25
|
+
assert !a.touches(Ball(x: 10.0, y: 0.0, r: 5.0))
|
|
26
|
+
assert a.touches(Ball(x: 6.0, y: 6.0, r: 4.0))
|
|
27
|
+
assert !a.touches(Ball(x: 7.0, y: 7.0, r: 4.0))
|
|
28
|
+
assert a.touches(a)
|
|
29
|
+
|
|
30
|
+
# each is measured from its own pivot: a ball's is its origin's place, a dot's its position whatever its rotation
|
|
31
|
+
fun eachIsMeasuredFromItsOwnPivot() =
|
|
32
|
+
b := Ball(x: 0.0, y: 0.0, r: 2.0)
|
|
33
|
+
assert b.touches(Dot(x: 10.5, y: 8.0))
|
|
34
|
+
assert !b.touches(Dot(x: 11.5, y: 8.0))
|
|
35
|
+
assert Dot(x: 10.5, y: 8.0).touches(b)
|
tests/std/math/Random.plum
CHANGED
|
@@ -51,3 +51,56 @@ primitive RandomTest[UnitTest] =
|
|
|
51
51
|
fun seed0StillProducesAVaryingStream() =
|
|
52
52
|
d := Random.seeded(0).nextInt()
|
|
53
53
|
assert d.rng.nextInt().value != d.value
|
|
54
|
+
|
|
55
|
+
# a key's draws are SplitMix64's stream from the key (0xE220A8397B1DCDAF is SplitMix64's first output from 0)
|
|
56
|
+
fun aKeysDrawsAreSplitMix64sStream() =
|
|
57
|
+
assert Random(seed: 0).intAt(0) == -2152535657050944081
|
|
58
|
+
assert Random.mix(1) == 6238072747940578789
|
|
59
|
+
k := Random.at(42, 7, 3)
|
|
60
|
+
assert k.seed == -3073514834297413759
|
|
61
|
+
assert k.intAt(0) == -1500995302339472769
|
|
62
|
+
assert k.intAt(1) == 4282093252367899588
|
|
63
|
+
|
|
64
|
+
# the same key draws the same values; a key differing in its seed, tick or salt draws others
|
|
65
|
+
fun theSameKeyDrawsTheSameValues() =
|
|
66
|
+
k := Random.at(9, 100, 2)
|
|
67
|
+
assert k.floatAt(0) == Random.at(9, 100, 2).floatAt(0)
|
|
68
|
+
assert k.intAt(5) == k.intAt(5)
|
|
69
|
+
assert k.intAt(0) != k.intAt(1)
|
|
70
|
+
assert k.intAt(0) != Random.at(10, 100, 2).intAt(0)
|
|
71
|
+
assert k.intAt(0) != Random.at(9, 101, 2).intAt(0)
|
|
72
|
+
assert k.intAt(0) != Random.at(9, 100, 3).intAt(0)
|
|
73
|
+
assert Random.at(0, 0, 0).intAt(0) != 0
|
|
74
|
+
assert Random.at(0, 1, 0).seed != Random.at(0, 0, 1).seed
|
|
75
|
+
|
|
76
|
+
# floats along the tick, the salt and the index are uniform in [0, 1): a mean near 0.5 and ten even buckets
|
|
77
|
+
fun floatsAlongEachAxisAreUniform() =
|
|
78
|
+
assert RandomHelpers.uniform(|i| Random.at(1, i, 0).floatAt(0))
|
|
79
|
+
assert RandomHelpers.uniform(|i| Random.at(1, 0, i).floatAt(0))
|
|
80
|
+
assert RandomHelpers.uniform(|i| Random.at(i, 0, 0).floatAt(0))
|
|
81
|
+
assert RandomHelpers.uniform(|i| Random.at(1, 0, 0).floatAt(i))
|
|
82
|
+
|
|
83
|
+
# neighbouring ticks differ in about half their bits (the avalanche of the mix)
|
|
84
|
+
fun neighbouringTicksDifferInAboutHalfTheirBits() =
|
|
85
|
+
flips := Range.until(0, 2000).fold(0, |n, t| n + {Random.at(5, t, 1).intAt(0) ^ Random.at(5, t + 1, 1).intAt(0)}.popCount())
|
|
86
|
+
assert flips > 2000 * 31
|
|
87
|
+
assert flips < 2000 * 33
|
|
88
|
+
lows := Range.until(0, 2000).fold(0, |n, t| n + {Random.at(5, t, 1).intAt(0) & 1})
|
|
89
|
+
assert lows > 900 && lows < 1100
|
|
90
|
+
|
|
91
|
+
fun intBelowAtStaysInRangeAndCoversIt() =
|
|
92
|
+
seen := Range.until(0, 1000).fold(0, |bits, t|
|
|
93
|
+
d := Random.at(3, t, 0).intBelowAt(0, 10)
|
|
94
|
+
assert d >= 0 && d < 10
|
|
95
|
+
bits | {1 << d})
|
|
96
|
+
assert seen == 1023
|
|
97
|
+
assert Random.at(3, 0, 0).intBelowAt(0, 0) == 0
|
|
98
|
+
assert Random.at(3, 0, 0).intBelowAt(0, -4) == 0
|
|
99
|
+
|
|
100
|
+
primitive RandomHelpers =
|
|
101
|
+
# 10,000 draws: the mean within 0.01 of 0.5, each tenth of [0, 1) holding 1000 within 150 (5 sigma)
|
|
102
|
+
fun uniform(draw: fn(Int) -> Float) -> Bool =
|
|
103
|
+
counts := Range.until(0, 10).map(|b| Range.until(0, 10000).fold(0, |n, i| if Int(draw(i) * 10.0) == b then n + 1 else n))
|
|
104
|
+
sum := Range.until(0, 10000).fold(0.0, |s, i| s + draw(i))
|
|
105
|
+
inRange := Range.until(0, 10000).all(|i| draw(i) >= 0.0 && draw(i) < 1.0)
|
|
106
|
+
inRange && sum > 4900.0 && sum < 5100.0 && counts.all(|c| c > 850 && c < 1150)
|