plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


d8ce9bc — Peter John 2026-11-13T12:04:32+05:30
docs: game.md on master's View and Actor (the stepping through a screen's stepped, the hud, steps and camera, Circle by its centre, Label align, turned shapes, the other buttons and keys, Input.axis, transitions dropping presses, the kit's hud and dispatch), Beside master rewritten against e91c40ce; std.md; the backlog (what still differs: the stepping walk's work, the window size on the input, Ticker, real font metrics)
Files changed (3) hide show
  1. docs/backlog.md +7 -4
  2. docs/game.md +131 -112
  3. docs/std.md +1 -1
docs/backlog.md CHANGED
@@ -4,15 +4,18 @@ Open work on `simpler-language`, rewritten 2026-11-11. Only what is still open i
4
4
 
5
5
  ## Screen views (branch `sl/view`, 2026-11-12)
6
6
 
7
- The retained scene tree was replaced by master's screen view in functional form: a `Game(screens: ..)` of `Screen`s, a `ScreenSlice<A>` per screen with rules (master's `Slice<A>`), a `view` each frame that the game draws and hit-tests itself, `Control` for effects, undo and time travel by re-simulation, and the headless kit (`docs/game.md`, "Beside master", has the table of differences). `SceneTree`, `Ctx`, `Clickable`, `Store` and `ui` are gone, with the examples that used them (`orb_run`, `kids/balloon_pop`, now `examples/games/orbitrunner` and `balloonpop`), `benchmarks/scene_trees` and two web-test stress fixtures (their results stay in `docs/benchmarks` and `compiler/game/web-test/BENCHMARK.md`). Orbit Runner's state checksum over four seeds of 3,600 ticks is master's (925878360). Open:
7
+ The retained scene tree was replaced by master's screen view in functional form: a `Game(screens: ..)` of `Screen`s, a `ScreenSlice<A>` per screen with rules (master's `Slice<A>`), a `view` each frame that the game draws and hit-tests itself, `Control` for effects, undo and time travel by re-simulation, and the headless kit (`docs/game.md`, "Beside master", has the table of differences). `SceneTree`, `Ctx`, `Clickable`, `Store` and `ui` are gone, with the examples that used them (`orb_run`, `kids/balloon_pop`, now `examples/games/orbitrunner` and `balloonpop`), `benchmarks/scene_trees` and two web-test stress fixtures (their results stay in `docs/benchmarks` and `compiler/game/web-test/BENCHMARK.md`). Orbit Runner's state checksum over four seeds of 3,600 ticks is master's (925878360). On 2026-11-13 it was brought up to master at e91c40ce (stages 24 and 25 and the review after 24): `View` and `Actor[View]`, actors stepped by the game through the screen's `stepped(dt)`, `Screen.hud`, `steps` and `camera`, one walk over `view`, turned boxes and text, the other mouse buttons and keys, the per-screen backgrounds and dropped pointer events of a transition (the "Beside master" table has what still differs). Open:
8
8
 
9
- - **A `match` on a nullary variant inside a closure in a function body matches every value.** `xs.fold(0, |s, e| match e` with an arm `B => s + 1` counts every element; the same match in a named function, or in a closure in a test block, is right, and a variant with fields (`A(n) =>`) is right. std/game works around it (the matches are in methods: `Game.travelled`, `Game.redone`, `headless.isRestart`). Repro: `fun count(xs: List<E>) -> Int = xs.fold(0, |s, e| match e` / `B => s + 1` / `_ => s)` with `enum E = A(n: Int) | B | C`, `count([C])` gives 1.
10
- - **Pass re-check failures met on the way** (fixed in std, not in the compiler): fusion devirtualising `s.frames().onDemand()` on a `Screen` with one implementer mistypes the fused call (std matches on `frames()` instead); trmc on `List.map` with a coercion (`xs.map(|m| m)` into a `List<Actor>`) fails its re-check (`actor.all(xs)` lifts through a typed helper); dead-code removal is reverted when it removes every type a downcast to a trait could match (`Container as c`; `declared` became an `Actor` method).
9
+ - **A `match` on a nullary variant inside a closure in a function body matches every value.** `xs.fold(0, |s, e| match e` with an arm `B => s + 1` counts every element; the same match in a named function, or in a closure in a test block, is right, and a variant with fields (`A(n) =>`) is right. std/game works around it (the matches are in methods: `Game.travelled`, `Game.redone`). Repro: `fun count(xs: List<E>) -> Int = xs.fold(0, |s, e| match e` / `B => s + 1` / `_ => s)` with `enum E = A(n: Int) | B | C`, `count([C])` gives 1.
10
+ - **Pass re-check failures met on the way** (fixed in std, not in the compiler): fusion devirtualising `s.frames().onDemand()` on a `Screen` with one implementer mistypes the fused call (std matches on `frames()` instead); trmc on `List.map` with a coercion (`xs.map(|m| m)` into a `List<View>`) fails its re-check (`view.all(xs)` lifts through a typed helper); dead-code removal is reverted when it removes every type a downcast to a trait could match (it met `Container as c`, which master has since folded into `view` too).
11
11
  - **Frames on demand in the hosts**: `Game.idle()` works, but no host is told (master's `frameIdle` extern, and the browser skipping the draw and waiting for input).
12
12
  - **Development keys** (`plum run --dev`, master's `remakeGame` host call): a game offers undo only through its own effects.
13
13
  - **`plum run <folder>`** (master runs a game's folder; here the main file is named).
14
14
  - **Master's other games** (`clockwork`, `menu`) are not ported.
15
- - **The view's cost**: each frame makes the view's list and items. `benchmarks/screen_views` (`orbit.plum`, `scale.plum`, master's drivers) times it; not yet run against master.
15
+ - **The view's cost**: each frame makes the view's list and items. `benchmarks/screen_views` (`orbit.plum`, `scale.plum`, master's drivers) times it; not yet run against master. Master's stepping walk makes the view once more a tick and each actor's view; this branch's `stepped` makes neither, so the two `update` timings are not the same work (`scale.plum`'s header says so).
16
+ - **Stepping without the screen naming its actors**: the game cannot step an actor found in a view, as the screen holding it is a value; each screen maps its own fields in `stepped`. A lens per field, or a view walk that rebuilds the screen, would let std do it; not designed.
17
+ - **Real font metrics** for a `Label`'s width, which its bounds and `align` go by (0.6 of the size a letter), as on master.
18
+ - **The window's size on the tick's input** (master's `input.screen`) and master's `Ticker` name for `Clock`: not ported.
16
19
 
17
20
  ## Decided, not built
18
21
 
docs/game.md CHANGED
@@ -31,21 +31,21 @@ examples/games/orbitrunner/
31
31
 
32
32
  ## A minimal game
33
33
 
34
- A game is a `Game` (`std/game/Game`): a list of screens. A screen implements the `Screen` trait. Its state is plain fields, and its `view` says what the frame shows: a list of actors, made again from those fields every time the frame is drawn. A screen with rules is a slice of the game ([The slice](#the-slice), below): an enum of its actions, the input mapped to them, and a reducer that returns the next screen. If `main` returns a `Game`, `plum run` runs it as a game. Any other `main` is a command line program.
34
+ A game is a `Game` (`std/game/Game`): a list of screens. A screen implements the `Screen` trait. Its state is plain fields, and its `view` says what the frame shows: a list of views, made again from those fields every time the frame is drawn. A screen with rules is a slice of the game ([The slice](#the-slice), below): an enum of its actions, the input mapped to them, and a reducer that returns the next screen. If `main` returns a `Game`, `plum run` runs it as a game. Any other `main` is a command line program.
35
35
 
36
- This game counts the clicks on a box and rolls a ball while Right is held, and ends on Escape. The box says it takes presses (`touch: Enabled`; a ready-made box is decoration otherwise), and the screen keeps it in a field, so a press on it comes to the screen as that box. It is [`examples/games/clicks.plum`](../examples/games/clicks.plum):
36
+ This game counts the clicks on a box, rolls a ball while Right is held, and ends on Escape. The box says it takes presses (`touch: Enabled`; a ready-made box is decoration otherwise), and the screen keeps it in a field, so a press on it comes to the screen as that box. The ball is an `Actor`: it has a `step(dt)`, and the game steps it each tick through the screen's `stepped`. It is [`examples/games/clicks.plum`](../examples/games/clicks.plum):
37
37
 
38
38
  ```plum
39
- enum TapAction = Tapped | Push(dx: Float) | Tick(dt: Float) | Quit
39
+ enum TapAction = Tapped | Push(dx: Float) | Quit
40
40
 
41
41
  struct Ball[Actor](x: Float, y: Float, vx: Float) =
42
42
  fun place(self) -> Place = place.at(self.x, self.y)
43
43
 
44
- fun view(self) -> List<Actor> = [Circle.of(0.0, 0.0, 10.0, Color.Yellow)]
44
+ fun view(self) -> List<View> = [Circle.of(0.0, 0.0, 10.0, Color.Yellow)]
45
45
 
46
46
  fun push(self, vx: Float) -> Ball = Ball(..self, vx: vx)
47
47
 
48
- fun update(self, dt: Float) -> Ball = Ball(..self, x: self.x + self.vx * dt)
48
+ fun step(self, dt: Float) -> Ball = Ball(..self, x: self.x + self.vx * dt)
49
49
 
50
50
  struct TapScreen[ScreenSlice<TapAction>](taps: Int, box: Box, ball: Ball) =
51
51
  fun start() -> TapScreen =
@@ -53,9 +53,11 @@ struct TapScreen[ScreenSlice<TapAction>](taps: Int, box: Box, ball: Ball) =
53
53
 
54
54
  fun name(self) -> Str = "tap"
55
55
 
56
- fun view(self) -> List<Actor> = [self.box, self.ball, Label.of(20.0, 20.0, "Taps: {self.taps}", 24.0, Color.White)]
56
+ fun view(self) -> List<View> = [self.box, self.ball, Label.of(20.0, 20.0, "Taps: {self.taps}", 24.0, Color.White)]
57
57
 
58
+ fun stepped(self, dt: Float) -> TapScreen = TapScreen(..self, ball: self.ball.step(dt))
59
+
58
- fun onPress(self, target: Actor, at: Vec2) -> Option<TapAction> = if @sameRef(target, self.box) then Some(Tapped) else None
60
+ fun onPress(self, target: View, at: Vec2) -> Option<TapAction> = if @sameRef(target, self.box) then Some(Tapped) else None
59
61
 
60
62
  fun onKey(self, e: InputEvent) -> Option<TapAction> = match e
61
63
  KeyDown(k) => if k == Key.Escape then Some(Quit) else None
@@ -63,12 +65,9 @@ struct TapScreen[ScreenSlice<TapAction>](taps: Int, box: Box, ball: Ball) =
63
65
 
64
66
  fun held(self, input: Input) -> Option<TapAction> = Some(Push(dx: if input.isKeyDown(Key.Right) then 100.0 else 0.0))
65
67
 
66
- fun tick(self, dt: Float) -> Option<TapAction> = Some(Tick(dt: dt))
67
-
68
68
  fun reduce(self, a: TapAction) -> TapScreen = match a
69
69
  Tapped => TapScreen(..self, taps: self.taps + 1)
70
70
  Push(dx) => TapScreen(..self, ball: self.ball.push(dx))
71
- Tick(dt) => TapScreen(..self, ball: self.ball.update(dt))
72
71
  Quit => self
73
72
 
74
73
  fun effect(self, io: Io, a: TapAction, game: Control) = match a
@@ -80,25 +79,23 @@ fun main(io: Io) -> Game =
80
79
  Game(screens: [only])
81
80
  ```
82
81
 
83
- `main` runs once, after your assets have loaded. Then the game runs in ticks of 1/60 s: each tick is delivered to the shown screen as actions, and its reducer returns the next screen, which the game keeps; once per frame the game draws it, which only reads it: it clears the screen with the screen's `background()` (black by default) and draws the screen's view. Nothing is bound to anything and nothing is kept between frames: the label shows `self.taps` because the view reads it each time.
84
-
85
- Something that moves on its own moves in the reducer, on the tick action (`tick(dt)` above, `Tick(dt) => .. self.ball.update(dt)`): the reducer calls the actor's own method, which returns the actor's next version.
82
+ `main` runs once, after your assets have loaded. Then the game runs in ticks of 1/60 s: each tick is delivered to the shown screen as actions, its actors are stepped, and the game keeps the next screen they return; once per frame the game draws it, which only reads it: it clears the screen with the screen's `background()` (black by default) and draws the screen's view. Nothing is bound to anything and nothing is kept between frames: the label shows `self.taps` because the view reads it each time.
86
83
 
87
84
  ## A screen's view
88
85
 
89
- A screen's `view(self) -> List<Actor>` is what the frame shows: a list of actors, placed in the world's coordinates, made from the screen's fields. Orbit Runner's play:
86
+ A screen's `view(self) -> List<View>` is what the frame shows: a list of views, placed in the world's coordinates, made from the screen's fields. Orbit Runner's play:
90
87
 
91
88
  ```plum
92
- fun view(self) -> List<Actor> =
89
+ fun view(self) -> List<View> =
93
- back: List<Actor> := [Box.of(0.0, 0.0, Arena.W, Arena.H, Color.rgb(0.07, 0.08, 0.16))]
90
+ back: List<View> := [Box.of(0.0, 0.0, Arena.W, Arena.H, Color.rgb(0.07, 0.08, 0.16))]
94
- mines := actor.all(self.mines)
91
+ mines := view.all(self.mines)
95
- orbs := actor.all(self.orbs)
92
+ orbs := view.all(self.orbs)
96
- ship: List<Actor> := if self.over then Nil else [self.ship]
93
+ ship: List<View> := if self.over then Nil else [self.ship]
97
- rings := actor.all(self.rings)
94
+ rings := view.all(self.rings)
98
- hud: List<Actor> := [
95
+ hud: List<View> := [
99
96
  Label.of(16.0, 12.0, "Score {self.score}", 28.0, Color.White),
100
97
  Label.of(16.0, 44.0, "Best {self.best.max(self.score)}", 20.0, Color.Yellow)]
101
- over: List<Actor> := if self.over
98
+ over: List<View> := if self.over
102
99
  [Overlay.of(Arena.W, Arena.H, [
103
100
  Label.of(300.0, 258.0, "Game over", 48.0, Color.Red),
104
101
  Label.of(270.0, 316.0, "Press Enter to play again", 24.0, Color.White)])]
@@ -107,89 +104,107 @@ fun view(self) -> List<Actor> =
107
104
  back.append(starsFrom(0, self.clock)).append(mines).append(orbs).append(ship).append(rings).append(hud).append(lifeIcons(0, self.lives, self.assets.sheet)).append(over)
108
105
  ```
109
106
 
110
- What shows only sometimes is left out (the ship once the run is over, the game over panel until it is), what repeats is a list made by a function or a fold, and a list the screen holds is appended, lifted to a `List<Actor>` by `actor.all` (a `List<Mine>` is not a `List<Actor>`, see [Coercion](language.md#coercion)). Later items draw over earlier ones. (Master writes the same view as one list literal with `if`, `for` and `..` elements, which this dialect's list literals don't have.)
107
+ What shows only sometimes is left out (the ship once the run is over, the game over panel until it is), what repeats is a list made by a function or a fold, and a list the screen holds is appended, lifted to a `List<View>` by `view.all` (a `List<Mine>` is not a `List<View>`, see [Coercion](language.md#coercion)). Later items draw over earlier ones. (Master writes the same view as one list literal with `if`, `for` and `..` elements, which this dialect's list literals don't have.)
111
108
 
112
- - **Drawn, not live.** The view is made again every time the frame is drawn, and its items keep nothing from one frame to the next: they are not stepped. So each takes plain values read from the fields (a `Label`'s `text`, a `Sprite`'s `frame`). A `Group` or an `Overlay` in a view draws the children it is made with (an actor's `declared(self) -> List<Actor>`, empty by default), and an actor with a view of its own draws that, so views nest.
113
- - **Two kinds of item.** An actor the screen holds in a field and names in its view (the ship, each mine of `self.mines`) is the very object in the field when the frame is drawn: a press on it reaches the screen as that object ([Clicks](#clicks)), and `@sameRef(target, self.box)` tells which. An item made inside `view` (the HUD's labels, the stars, the game over panel) is a new object every frame, made from the fields.
114
- - **The state rule.** `view` only reads; the state changes in the reducer and in what it calls. A screen is a value, so this holds by construction: there is nothing for the draw to change.
109
+ - **Drawn, not live.** The view is made again every time it is walked (to draw, to hit-test a press), and an item made inside it keeps nothing from one walk to the next. So each takes plain values read from the fields (a `Label`'s `text`, a `Sprite`'s `frame`). A `Group` or an `Overlay` in a view has the children it is made with as its own `view`, and an actor with a view of its own draws that, so views nest.
110
+ - **Two kinds of item.** An actor the screen holds in a field and names in its view (the ship, each mine of `self.mines`) is the very object in the field when the frame is drawn: a press on it reaches the screen as that object ([Clicks](#clicks)), and `@sameRef(target, self.box)` tells which. The game steps it through the screen's `stepped`, which returns the screen with the actor's next version. An item made inside `view` (the score label, the stars, the game over panel) is a new object every frame, made from the fields.
111
+ - **What is there, what shows.** An item that is not there is left out (the ship once the run is over). An actor that is there but hides itself for a moment says so with `visible()` (the ship blinking after a hit): it is not drawn or hit, but the screen still steps it.
112
+ - **The state rule.** `view` only reads; the state changes in the actors' `step`, the screen's `stepped` and the reducer. A screen is a value, so this holds by construction: there is nothing for the draw to change.
115
113
 
116
114
  What else a screen says about its frame, each with a default:
117
115
 
118
116
  | `Screen`'s method | Default | What it does |
119
117
  |---|---|---|
120
- | `view(self) -> List<Actor>` | empty | what the frame shows (above) |
118
+ | `view(self) -> List<View>` | empty | what the frame shows (above) |
119
+ | `hud(self, window: Vec2) -> List<View>` | empty | what is drawn over the view, one unit a pixel of the window (`window` pixels): Balloon Pop's score box. Hit before the view |
120
+ | `steps(self) -> Bool` | true | false steps none of the screen's actors this tick: Orbit Runner's run over |
121
+ | `stepped(self, dt) -> Self` | the screen as it is | the screen with each actor it holds stepped `dt` seconds on ([Views and actors](#views-and-actors)) |
122
+ | `camera(self, base: Viewport) -> Viewport` | `base` | the camera it is seen through, given the one its `size()` and `viewport()` make: `base.lookAt(x, y, zoom)` follows a point or zooms. Presses, `toWorld`, culling and the draw go through it |
121
123
  | `size(self) -> Vec2` | 0 by 0 | the world the view is placed in, in its own units: Orbit Runner's arena, 800 by 600. 0 by 0 is the screen's own pixels |
122
124
  | `viewport(self) -> Scaling` | `Fit` | how that world is put on the screen (`std/game/Viewport`): `Fit` letterboxes it, `Extend` grows it to fill the screen, `Fill`, `Stretch`, `Pixels` |
123
125
  | `background(self) -> Color` | black | what the frame is cleared with before the view is drawn |
124
- | `culls(self) -> Bool` | false | true skips each `Sized` item (and what is under it) whose bounds miss what the screen shows |
126
+ | `culls(self) -> Bool` | false | true skips each `Sized` item (and what is under it) whose bounds miss what the camera sees |
125
127
  | `busy(self) -> Bool` | false | true asks an `OnDemand` screen's next tick ([Frames on demand](#frames-on-demand)) |
126
128
  | `frames(self) -> Frames` | `Always` | `OnDemand` for a screen that only changes when something happens |
127
- | `draw(self, io, m, alpha, area)` | the view | draws the frame through `m` (the world to the screen's pixels); define it to draw more (a HUD with `gfx`), not to clear |
128
129
 
129
- The pointer is mapped through the same viewport, so a press arrives in the world's coordinates: a screen has no fit or resize code of its own (`resize(self, w, h) -> Self` is there for a screen that wants the size, as a layout).
130
+ The game works out each screen's camera from its `size()`, its `viewport()` and the window's size in pixels (`Game.pixels`), then the screen's `camera(base)` (`screen.cameraOf`). The pointer is mapped through the same camera, so a press arrives in the world's coordinates: a screen has no fit or resize code of its own. A screen that follows its player returns `base.lookAt(self.player.at.x, self.player.at.y, 1.0)` (`std/game/Viewport`: that world point at the window's centre, `zoom` times as close, culling to what is on the window).
131
+
132
+ ### Views and actors
130
133
 
131
- ### Actors
134
+ What a screen draws is made of views: a view is a struct that implements `View` (`std/game/View`), stateless, saying what to draw. An actor is a view that acts: it implements `Actor`, whose supertrait is `View`, and has state that advances each tick.
135
+
136
+ ```
137
+ trait View = # what to draw
138
+ fun place(self) -> Place
139
+ fun view(self) -> List<View> # default: empty
140
+ fun visible(self) -> Bool # default: true
141
+ fun paintSelf(self, io: Io, m: Affine, alpha: Float) # default: nothing
142
+
143
+ trait Actor[View] = # acts: state that advances each tick
144
+ fun step(self, dt: Float) -> Self
145
+ ```
132
146
 
133
- An actor is a struct of the game's own that implements `Actor` (`std/game/Actor`): `place(self) -> Place` (`std/game/Place`: position, rotation, scale, the origin it turns about and its `alpha`), and what it looks like, its own `view` of ready-made items placed in its coordinates. Its other fields are plain and public. It moves in a method of its own that returns its next version, which the screen's reducer calls each tick (`update(self, dt, frozen) -> Mine` in the example games):
147
+ `place` is its `Place` (`std/game/Place`: position, rotation, scale, the origin it turns about and its `alpha`); `view` is what is under it, ready-made items placed in its coordinates; `visible` hides it, with what is under it, from the draw and from presses; `paintSelf` is what it draws itself, before its view, for what no ready-made view draws (an additive glow, a line): it draws with `gfx` through `m`, its own coordinates to the world's, at `alpha` (Orbit Runner's `Orb`, Balloon Pop's `Balloon`, whose string is a line). Its other fields are plain and public. `step` returns the actor's next version, `dt` seconds on. The ready-made views (`Group`, `Box`, `Circle`, `Sprite`, `Label`, `Overlay`) are stateless and never stepped.
134
148
 
135
149
  ```plum
136
150
  struct Mine[Actor](at: Place, vx: Float, vy: Float, clock: Float, tex: Texture) =
137
- Radius = 11.0
151
+ fun place(self) -> Place = self.at
138
152
 
139
- fun create(start: Motion, tex: Texture) -> Mine =
153
+ fun view(self) -> List<View> =
140
- Mine(at: place.pivoted(start.x, start.y, Sheet.Size / 2.0, Sheet.Size / 2.0), vx: start.vx, vy: start.vy, clock: 0.0, tex: tex)
154
+ [Sprite.cell(0.0, 0.0, self.tex, Sheet.Size, Sheet.Size, 0.0, 2.0 * Sheet.Cell, Sheet.Cell, Sheet.Cell, Sprite.frameAt(self.clock, 4.0, 4))]
141
155
 
142
- fun place(self) -> Place = self.at
156
+ fun step(self, dt: Float) -> Mine =
157
+ moved := Mine(..self, clock: self.clock + dt).put(self.motion().drifted(Mine.Radius, dt))
158
+ Mine(..moved, at: moved.at.rotateBy(2.0 * dt))
159
+ ```
143
160
 
144
- fun view(self) -> List<Actor> =
145
- [Sprite.cell(0.0, 0.0, self.tex, Sheet.Size, Sheet.Size, 0.0, 2.0 * Sheet.Cell, Sheet.Cell, Sheet.Cell, Sheet.frame(self.clock, 4, 4.0))]
161
+ **Stepping.** Each tick the shown screen gets, between `held` and its tick action, the game has it step its actors: it calls the screen's `stepped(dt)` and keeps the screen it returns, unless the screen's `steps()` is false. On master the game walks the view and the hud and steps each `Actor` it finds in place; here a screen is a value, and an actor found in a view can't be put back into the field that holds it, so the screen says where its actors are, in one line that maps its fields (`view.stepAll` steps each actor of a list, `view.stepIn` each `Actor` of a `List<View>`, the others as they are):
146
162
 
163
+ ```plum
164
+ fun steps(self) -> Bool = !self.over
165
+
147
- fun update(self, dt: Float, frozen: Bool = false) -> Mine =
166
+ fun stepped(self, dt: Float) -> PlayingScreen =
148
- clock := self.clock + dt
149
- if frozen
150
- Mine(..self, clock: clock)
151
- else
152
- moved := Mine(..self, clock: clock).put(self.motion().drifted(Mine.Radius, dt))
167
+ PlayingScreen(..self, mines: view.stepAll(self.mines, dt), orbs: view.stepAll(self.orbs, dt), ship: self.ship.step(dt), rings: view.stepAll(self.rings, dt))
153
- Mine(..moved, at: moved.at.rotateBy(2.0 * dt))
154
168
  ```
155
169
 
156
- and the screen's tick moves what it holds, then runs the rules:
170
+ That is the only line a game writes for stepping, one per screen that holds actors; the default returns the screen as it is. A plain screen (no slice) is stepped the same way; an `OnDemand` screen asleep steps nothing, as it gets no tick; a replay, a rewind and the kit's `tick` go through the same tick. An actor the screen leaves out of `stepped` is not stepped, whether it is in the view or not, and one stepped there moves whether it is visible or not. So the tick action only runs the rules on what has moved:
157
171
 
158
172
  ```plum
159
173
  fun advance(self, dt: Float) -> PlayingScreen =
160
- over := self.over
161
- moved := PlayingScreen(
162
- ..self, clock: self.clock + dt, mines: self.mines.map(|m| m.update(dt, over)), orbs: self.orbs.map(|o| o.update(dt, over)),
174
+ rings := if self.over then view.stepAll(self.rings, dt) else self.rings
163
- ship: self.ship.update(dt, over), rings: self.rings.map(|r| r.update(dt)).filter(|r| !r.done()))
175
+ moved := PlayingScreen(..self, clock: self.clock + dt, rings: rings.filter(|r| !r.done()))
164
- ruled := if over then moved else moved.rules()
176
+ ruled := if self.over then moved else moved.rules()
165
177
  if ruled.again then PlayingScreen(..ruled, again: false).dispatch(StartOver) else ruled
166
178
  ```
167
179
 
168
- An actor that draws something no ready-made actor does (an additive glow, a line) overrides `paint(self, io, m, alpha)` and draws with `gfx` through `m`; `actor.paintView(io, self.view(), m, alpha)` then draws its view as well (Orbit Runner's `Orb`, Balloon Pop's `Balloon`, whose string is a line). `Actor.plum` also has helpers for drawing in an actor's own coordinates (`actor.rectPaint`, `circlePaint`, `linePaint`, `spritePaint`, `textPaint`).
180
+ Once Orbit Runner's run is over the screen steps nothing, so the mines and orbs stand still under the game over panel, while the stars, which read the screen's clock, keep twinkling, and the ship is left out of the view. What should move while the rest is held, the reducer still steps by hand: here the ripple of the last hit grows on under the panel.
169
181
 
170
- What else an actor is, it says with a small trait:
182
+ What else a view is, it says with a small trait:
171
183
 
172
184
  | Trait | What it says |
173
185
  |---|---|
174
- | `Sized` (`std/game/Sized`) | `bounds(self) -> Aabb`, the box it covers in its own coordinates. An actor with bounds can be hit; one without cannot. `hit(self, p) -> Bool` is inside the bounds, edges included, unless the actor 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 |
175
- | `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. An actor that says nothing is `ChildrenOnly` |
186
+ | `Actor` (`std/game/View`) | `step(self, dt) -> Self`: its next version `dt` seconds on, which the screen's `stepped` keeps |
187
+ | `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 |
188
+ | `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` |
176
189
 
177
- The ready-made kinds take every field (struct fields have no defaults in this dialect), and each has a static that fills in the usual ones:
190
+ The ready-made kinds take every field (struct fields have no defaults in this dialect), and each has a static that fills in the usual ones. 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.drawBox`, `circle.drawDisc`, `sprite.drawSpriteFrame` and `label.drawLabel` paint the same shapes for your own kinds.
178
191
 
179
192
  | Kind | Fields | Usual |
180
193
  |---|---|---|
181
- | `Group` | `x, y, touch, children` | `Group.of(x, y, children)`, `ChildrenOnly` |
182
- | `Box` | `x, y, w, h, color, touch` | `Box.of(x, y, w, h, color)`, decoration (`Disabled`) |
183
- | `Circle` (`std/game/Circle`) | `x, y, r, color, touch` (x, y the top-left of its bounding box) | `Circle.of(x, y, r, color)` |
184
- | `Sprite` (`std/game/Sprite`) | `x, y, tex, w, h, srcX, srcY, srcW, srcH, frame, tint, touch`; `frame` picks the cell along a strip of `srcW`-wide cells from (`srcX`, `srcY`) | `Sprite.cell(x, y, tex, w, h, srcX, srcY, srcW, srcH, frame)` |
185
- | `Label` (`std/game/Label`) | `x, y, text, size, color, touch` | `Label.of(x, y, text, size, color)` |
186
- | `Overlay` (`std/game/Overlay`) | `x, y, w, h, color, touch, open, children`: a panel that dims what is under it and takes every press on it | `Overlay.of(w, h, children)`: black at 0.6 alpha, `Enabled`, open |
194
+ | `Group` | `x, y, touch, children`: a parent that paints nothing; its children are its `view` | `Group.of(x, y, children)`, `ChildrenOnly` |
195
+ | `Box` | `x, y, w, h, color, touch` (x, y its top-left) | `Box.of(x, y, w, h, color)`, decoration (`Disabled`) |
196
+ | `Circle` | `x, y, r, color, touch` (x, y its centre: its bounds are -r to r) | `Circle.of(x, y, r, color)` |
197
+ | `Sprite` | `x, y, tex, w, h, srcX, srcY, srcW, srcH, frame, tint, touch`; `frame` picks the cell along a strip of `srcW`-wide cells from (`srcX`, `srcY`), and `Sprite.frameAt(clock, fps, count)` is the frame of a looping strip at a clock | `Sprite.cell(x, y, tex, w, h, srcX, srcY, srcW, srcH, frame)` |
198
+ | `Label` | `x, y, text, size, color, align, touch`; `align` puts the text's start (`Left`), middle (`Centre`) or end (`Right`) on `x`, by the width estimate of 0.6 of the size a letter | `Label.of(x, y, text, size, color, align = Left)` |
199
+ | `Overlay` | `x, y, w, h, color, touch, children`: a panel that dims what is under it and takes every press inside it, left out of the view while it should not show | `Overlay.of(w, h, children)`: black at 0.6 alpha, `Enabled` |
187
200
 
188
- A colour's alpha is the colour's: a box in a half-transparent red paints at half alpha.
201
+ A kind of your own that takes children has a `children: List<View>` field and returns it from `view`.
189
202
 
190
203
  ### How std draws a view
191
204
 
192
- `Game.draw` asks the shown screen for its viewport (its `size()` fitted to the screen by its `viewport()`, `std/game/Viewport`), clears with its `background()` and calls its `draw` with the viewport's transform, which by default draws the view (`actor.paintView`): each shown item through its own place, at its alpha times its parents', its own `paint` (so its own view), then its declared children. There is no tree between the screen and the draw: the view is walked as it is made. `actor.viewDrawn` lists what a view draws, each item with its transform and alpha (the test kit reads it), and `actor.viewHit` finds what a press is on ([Clicks](#clicks)).
205
+ The game draws the shown screen once per frame (`Game.draw`): it clears with the screen's `background()` and draws the screen's `view()`, made for the frame, through its camera's transform (the world to the window's pixels) with `view.paintView`: each visible item through its place, its `paintSelf` and then its own view, multiplying the alphas down. A screen that `culls()` is drawn with `view.paintCulled`, which skips each `Sized` item whose bounds miss what the camera shows, with what is under it. Then the screen's `hud(window)` is drawn one unit a pixel. During a transition both screens are drawn this way, each on its own background, moved and faded. `Game.drawList()` lists what a frame draws, each item with its transform and alpha (`Drawn`), culled as the frame is, the hud's after the view's; std/game/headless reads it.
206
+
207
+ Nothing is kept between frames, and there is no tree: the game keeps the screens and their state, the camera's inputs (each screen's `size()`, `viewport()` and `camera`, the window's size) and the transition, and works out the rest when it needs it. One walk goes over a view for everything (draw, cull, list, hit test, find): an item, then its `view()`. The walks are the functions of `std/game/View` (`paintView`, `paintCulled`, `drawnIn`, `hitIn`, `worldIn`), with `stepAll` and `stepIn` for a screen's `stepped`.
193
208
 
194
209
  ## Fixed timestep and determinism
195
210
 
@@ -243,17 +258,17 @@ fun main(io: Io) -> Game =
243
258
 
244
259
  A move that can't be made (no history, a full one, a tick out of reach) leaves the game as it was with `moved` false. After a move the game plays on from there: the ticks undone stay as the future, for `redo`, until a user action cuts them.
245
260
 
246
- **What is recorded.** Each tick's `Input`, the screen's size, the tick's length, whether it was a user action, and what its effects asked of the game. Not actions: an action may hold an actor (`onPress(target, ..)`), and the recorded input is mapped again into fresh actions against the state made again.
261
+ **What is recorded.** Each tick's `Input`, the window's size (`pixels`), the tick's length, whether it was a user action, and what its effects asked of the game. Not actions: an action may hold an actor (`onPress(target, ..)`), and the recorded input is mapped again into fresh actions against the state made again.
247
262
 
248
- **What a rewind does with effects.** It runs none: no sound plays. What the effects of a tick asked of the screens (`goTo`, `restart`) is done again at the same point; an `exit` is not, nor an undo, a redo or a rewind.
263
+ **What a rewind does with effects.** It runs none: no sound plays. What the effects of a tick asked of the screens (`goTo`) is done again at the same point; an `exit` is not, nor an undo, a redo or a rewind.
249
264
 
250
- **A user action** is a tick in which the shown slice screen's mapping gave an action for one of the tick's events (`onKey`, `onPointer`, `onPress`), or `held` gave an action while what is held differs from the tick before. The `tick` action is not one, nor an unchanged `held`, nor what a reducer dispatches. A tick whose effects asked for an undo, a redo or a rewind is not one either. `undo` lands just before the last user action; again, just before the one before.
265
+ **A user action** is a tick in which the shown slice screen's mapping gave an action for one of the tick's events (`onKey`, `onPointer`, `onPress`), or `held` gave an action while what is held (the keys and the mouse buttons) differs from that screen's tick before. The `tick` action is not one, nor an unchanged `held`, nor what a reducer dispatches. A tick whose effects asked for an undo, a redo or a rewind is not one either. `undo` lands just before the last user action; again, just before the one before.
251
266
 
252
267
  **Offering undo in a game.** An effect asks through its `Control`: `game.undo()`, `game.redo()`, `game.rewindTo(tick)`, done once the tick is over. Balloon Pop maps Z and Backspace to `TakeBack`, whose effect is `game.undo()`: the last press is taken back, and the balloon it popped is whole again.
253
268
 
254
269
  **What it costs.** A rewind back costs the ticks it plays: `rewindTo(k)` plays `k` ticks from the start, with no drawing and no effects. A move forward plays only the ticks ahead. The history is a list of ticks; `timeTravel(cap)` bounds it (`History.Cap` is 30 minutes of ticks). Once full, the history lets its ticks go and every move is refused from then on; the game plays on.
255
270
 
256
- **Not here yet.** Master's development keys (`plum run --dev`: pause, step, undo and redo from the keyboard, with a line on the screen) need the host's dev mode and `remakeGame`, which this branch's hosts don't have; a game offers undo through its own effects.
271
+ **Not here yet.** Master's development keys (`plum run --dev`: pause, step, undo and redo from the keyboard, with a line on the screen) need the host's dev mode and `remakeGame`, which this branch's hosts don't have; a game offers undo through its own effects. The keys they use are `Key`s here too (`Key.BracketLeft`, `Key.Backslash`, `Key.BracketRight`, the right-hand modifiers, `Key.LeftAlt`, `Key.LeftSuper`), so a game reads them as any other key.
257
272
 
258
273
  ## What a game can use
259
274
 
@@ -264,8 +279,8 @@ Three modules talk to the host, files of `std/game`. Each function takes `io` fi
264
279
  | Group | Functions |
265
280
  |---|---|
266
281
  | Screen | `screenWidth(io)`, `screenHeight(io)`, `frameTime(io)`, `clear(io, color)` |
267
- | Shapes | `drawRect(io, x, y, w, h, c)`, `drawCircle(io, x, y, r, c)`, `drawLine(io, x1, y1, x2, y2, thickness, c)` |
282
+ | Shapes | `drawRect(io, x, y, w, h, c)`, `drawRectEx(io, x, y, w, h, rotation, c)` (turned clockwise about its centre), `drawCircle(io, x, y, r, c)`, `drawLine(io, x1, y1, x2, y2, thickness, c)` |
268
- | Text | `drawText(io, s, x, y, size, c)` |
283
+ | Text | `drawText(io, s, x, y, size, c)`, `drawTextEx(io, s, x, y, size, rotation, c)` (turned clockwise about its top-left) |
269
284
  | Images | `loadTexture(io, name)` gives a `Texture` with `draw(io, x, y)`, `width(io)`, `height(io)`, `setSmooth(io, smooth)`; `drawTextureEx(io, t.id, sx, sy, sw, sh, x, y, w, h, rotation, flipX, tint)` |
270
285
  | Camera | `setCamera(io, x, y, scale)`, `resetCamera(io)` |
271
286
  | Draw into an image | `createTexture(io, w, h)`, `t.renderTarget(io)`, `renderToScreen(io)` |
@@ -276,10 +291,11 @@ Three modules talk to the host, files of `std/game`. Each function takes `io` fi
276
291
 
277
292
  | Method | What it gives you |
278
293
  |---|---|
279
- | `isKeyDown(k)`, `isKeyPressed(k)` | key state, with `Key.Space`, `Key.KeyA`, `Key.Up`, `Key.Enter`, ... |
280
- | `pointer()` | a `Pointer` with `pos`, `down`, `pressed`, `released`, for the mouse or one finger |
281
- | `isMouseDown()`, `isMousePressed()`, `mousePos()`, `mouseWheel()` | mouse state |
282
- | `events` | this tick's `InputEvent`s: `PointerDown`, `PointerUp`, `PointerMove`, `KeyDown`, `KeyUp` |
294
+ | `isKeyDown(k)`, `isKeyPressed(k)` | key state, with `Key.Space`, `Key.KeyA`, `Key.Up`, `Key.Enter`, `Key.BracketLeft`, `Key.RightShift`, `Key.LeftAlt`, `Key.LeftSuper` (Cmd, or the Windows key), ... |
295
+ | `axis(low, high)` | -1, 0 or 1: the key `high` held against the key `low`, 0 when both or neither are (`input.axis(Key.Left, Key.Right)`) |
296
+ | `isMouseDown(b)`, `isMousePressed(b)`, `isMouseReleased(b)` | a mouse button held, gone down or up this tick: `Mouse.Left` (also a finger) unless given, `Mouse.Right`, `Mouse.Middle` |
297
+ | `pos`, `wheel` | where the pointer is, in window pixels, and how far the wheel turned |
298
+ | `events` | this tick's `InputEvent`s: `PointerDown`, `PointerUp`, `PointerMove` (the left button or a finger), `ButtonDown(button, x, y)`, `ButtonUp(..)` (the right and middle buttons), `KeyDown`, `KeyUp` |
283
299
 
284
300
  **`std/game/audio`:** `loadSound(io, name)` gives a `Sound` with `play(io, volume)`, `loop(io, volume)`, `stop(io)` and `setVolume(io, volume)`.
285
301
 
@@ -289,9 +305,9 @@ A missing file stops the game and names the file, so typos show up straight away
289
305
 
290
306
  A game with more than one screen (a title, a level select, the play) gives each its own `Screen`, with its own name, its own state and its own view. What sits over a view (a game over, a pause, a dialog) is not a screen: it is part of the view of the screen it sits on, drawn while the state says so, as Orbit Runner's game over `Overlay`. The keys still go to the screen's mapping, so Enter can play again while the game over shows.
291
307
 
292
- The first screen of the list shows first. A screen never gets the game: it changes screen through its effects (below). A screen says how it comes in with `transition(self, from: Str) -> Transition`: `Cut` (the default), `Fade(secs: 0.5)` or `Slide(from: FromRight, secs: 0.4)`. While a transition runs, the game draws both screens, the one going out and then the one coming in, each moved and faded. Only the screen coming in gets ticks.
308
+ The first screen of the list shows first. A screen never gets the game: it changes screen through its effects (below). A screen says how it comes in with `transition(self, from: Str) -> Transition`: `Cut` (the default), `Fade(secs: 0.5)` or `Slide(from: FromRight, secs: 0.4)`. While a transition runs, the game draws both screens, the one going out and then the one coming in, each on its own `background()`, moved and faded. Only the screen coming in gets ticks, and its pointer events are dropped until the transition is over (a press on a screen still sliding in lands nowhere); keys still come.
293
309
 
294
- A screen's other hooks each return its next version: `enter(self, from) -> Self` when the game goes to it, `leave(self, to) -> Self` when it goes from it, `resize(self, w, h) -> Self` when the screen's size is first known or changes. A screen left and shown again is as it was left; a screen whose `fresh()` is true comes back as the game was made with it. [`examples/games/two_screens.plum`](../examples/games/two_screens.plum) is a title that fades into a play, which counts its runs in `enter`.
310
+ A screen left and shown again is as it was left: its state is its fields, which the game keeps. A screen has no `update`, and no hooks for going to it, leaving it or a resize: a slice screen gets its ticks as actions, a plain `Screen` (no rules) gets none and draws what it was made with, its actors stepped, and starting a run over is the reducer's (Orbit Runner's `StartOver` deals the new run into the screen's fields). [`examples/games/two_screens.plum`](../examples/games/two_screens.plum) is a title that fades into a play.
295
311
 
296
312
  ## Frames on demand
297
313
 
@@ -307,7 +323,7 @@ fun frames(self) -> Frames = OnDemand
307
323
  - the tick before left the screen `busy()`, ran an effect that asked the game for something, or a transition is in progress;
308
324
  - the screen was just entered, or the screen's size changed.
309
325
 
310
- It then ticks as an `Always` screen until it goes quiet again. A tick it sleeps through is still a tick of the run: a recording keeps its input, and whether the screen sleeps is worked out from that input and the screen's state alone, so a replay sleeps through the same ticks. `Game.delivered` and `Game.skipped` count the ticks delivered and slept through. `Game.idle()` is true when the shown screen is `OnDemand`, no transition runs and no tick has run since the last frame drawn (`drawn()`, which a test calls).
326
+ It then ticks as an `Always` screen until it goes quiet again. A tick it sleeps through is still a tick of the run: a recording keeps its input, and whether the screen sleeps is worked out from that input and the screen's state alone, so a replay sleeps through the same ticks. `Game.delivered` and `Game.skipped` count the ticks delivered and slept through. `Game.idle()` is true when the shown screen is `OnDemand`, no transition runs and no tick has run since the last frame drawn (`frameDrawn()`, which a test calls).
311
327
 
312
328
  **The host.** On master the frame entry point tells the host when a frame is idle (`frameIdle`) and the browser skips the draw and waits for input. This branch's hosts have no such call yet: they draw every frame, and only the ticks sleep.
313
329
 
@@ -322,32 +338,33 @@ A screen with rules is a `ScreenSlice<A>` (`std/game/ScreenSlice`), as in Redux
322
338
  | `effect(self, io, a: A, game: Control)` | nothing | what `a` does after the tick (a sound, a change of screen); it only reads the slice |
323
339
  | `onKey(self, e) -> Option<A>` | None | the action for a key event |
324
340
  | `onPointer(self, e, at: Vec2) -> Option<A>` | None | the action for a pointer event, `at` its point in the world |
325
- | `onPress(self, target: Actor, at: Vec2) -> Option<A>` | None | the action for a press on an item that takes presses ([Clicks](#clicks)) |
341
+ | `onPress(self, target: View, at: Vec2) -> Option<A>` | None | the action for a press on an item that takes presses ([Clicks](#clicks)) |
326
342
  | `held(self, input) -> Option<A>` | None | the action for what is held, every tick |
327
343
  | `tick(self, dt) -> Option<A>` | None | the tick action |
328
344
 
329
- **Dispatch** reduces at once and queues. Each action std delivers is reduced as it is made, so the next mapping and the next action see what it did, and it is queued for its effect after the tick. A reducer dispatches more with `self.dispatch(a)`, which returns the slice after it (Orbit Runner's rules: `scored := if caught.points > 0 then collected.dispatch(Scored(points: caught.points)) else collected`): reduced at once, and queued before the action that dispatched it. Called outside a tick (a test calling `reduce` itself), `dispatch` only reduces. 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)).
345
+ **Dispatch** reduces at once and queues (the queue of the dispatch running sits in one slot of `std/game/Effects`, set around a screen's delivery and the kit's `dispatch`). Each action std delivers is reduced as it is made, so the next mapping and the next action see what it did, and it is queued for its effect after the tick. A reducer dispatches more with `self.dispatch(a)`, which returns the slice after it (Orbit Runner's rules: `scored := if caught.points > 0 then collected.dispatch(Scored(points: caught.points)) else collected`): reduced at once, and queued before the action that dispatched it. Called outside a tick (a test calling `reduce` itself), `dispatch` only reduces. 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)).
330
346
 
331
347
  ## Input and the tick as actions
332
348
 
333
349
  Each tick, std delivers to the shown screen's slice, in this order:
334
350
 
335
- 1. **Each of the tick's events**, in the order they came: a key through `onKey(e)`; a press (a `PointerDown`) on an item that takes presses through `onPress(target, at)`, and if that gives None, through `onPointer` as any pointer event is; any other pointer event through `onPointer(e, at)`, with `at` the event's point in the world.
351
+ 1. **Each of the tick's events**, in the order they came: a key through `onKey(e)`; a press (a `PointerDown`) on an item that takes presses through `onPress(target, at)`, and if that gives None, through `onPointer` as any pointer event is; any other pointer event through `onPointer(e, at)`, with `at` the event's point in the world. While a transition runs, the pointer events are dropped.
336
352
  2. **`held(input)`**: an action for what is held, every tick, events or none (the steering keys).
353
+ 3. **The step**: the screen's `stepped(dt)`, unless `steps()` is false ([Views and actors](#views-and-actors)).
337
- 3. **`tick(dt)`**: the tick action.
354
+ 4. **`tick(dt)`**: the tick action, which sees the actors moved and runs the rules.
338
355
 
339
- A mapping that gives None sends nothing. Then the transition in progress moves on, and the effects run. std moves nothing of a slice screen: what moves, its reducer moves, on the tick action.
356
+ A mapping that gives None sends nothing. Then the transition in progress moves on, and the effects run.
340
357
 
341
- The input comes before the tick, so a rule that must see the tick's motion first notes the action and acts on it in the tick: Orbit Runner's `PlayAgain` notes that the run is to start over (`again`), and its `Tick` dispatches `StartOver` once the actors have moved.
358
+ The input comes before the tick, so a rule that must see the tick's motion first notes the action and acts on it in the tick: Orbit Runner's `PlayAgain` notes that the run is to start over (`again`), and its `Tick` dispatches `StartOver` once the actors have moved. Orbit Runner steers with `input.axis`: `Steer(ax: {input.axis(Key.Left, Key.Right) + input.axis(Key.KeyA, Key.KeyD)}.max(-1.0).min(1.0), ..)`.
342
359
 
343
360
  ## Clicks
344
361
 
345
- A press is hit-tested by std against what the shown screen's view draws: the press is on the top-most shown item under it that is `Sized` with `touch` `Enabled` and whose `hit(p)` says the point (in its own coordinates) is on it: what is drawn later before what is drawn earlier, what is under an item (the children of a `Group` or an `Overlay`, then its own view) before the item, through every turn and scale on the way. A `Disabled` item takes everything under it out of the walk. The view is made again for the press, from the screen as the tick has left it so far. [`tests/port/game/routing.plum`](../tests/port/game/routing.plum) checks the walk against a brute-force reference on 300 random nested views.
362
+ A press is hit-tested by std against what the shown screen's `hud` draws, then its view: the press is on the top-most visible item under it that is `Sized` with `touch` `Enabled` and whose `hit(p)` says the point (in its own coordinates) is on it: what is drawn later before what is drawn earlier, what is under an item (its `view`: the children of a `Group` or an `Overlay`) before the item, through every turn and scale on the way. A `Disabled` item takes everything under it out of the walk. The view is made again for the press, from the screen as the tick has left it so far. [`tests/port/game/routing.plum`](../tests/port/game/routing.plum) checks the walk against a brute-force reference on 300 random nested views.
346
363
 
347
364
  The item hit becomes the slice's action through `onPress(target, at)`. Balloon Pop's balloons are such actors, each `Sized` with its exact shape and `Enabled` while whole:
348
365
 
349
366
  ```plum
350
- fun onPress(self, target: Actor, at: Vec2) -> Option<PopAction> = match target
367
+ fun onPress(self, target: View, at: Vec2) -> Option<PopAction> = match target
351
368
  Balloon as b => Some(Pop(balloon: b))
352
369
  _ => None
353
370
  ```
@@ -356,7 +373,7 @@ and `Pop(b)` bursts the balloon of `self.balloons` with `b`'s id. A balloon that
356
373
 
357
374
  ## Effects that outlive a tick
358
375
 
359
- A flash, a ripple, a slide: something that runs for a while after what started it is plain state too. The reducer adds it, ages it on each tick, and drops it once it is done; the view draws it at its age. Orbit Runner's ripple where the ship is hit is a `Ring`, an actor with its age: `Hit` appends one to `rings` where the ship is; each `Tick` ages them and keeps those not done; the view appends them.
376
+ A flash, a ripple, a slide: something that runs for a while after what started it is plain state too. The reducer adds it, the step ages it, the tick action drops it once it is done; the view draws it at its age. Orbit Runner's ripple where the ship is hit is a `Ring`, an actor with its age: `Hit` appends one to `rings` where the ship is; the screen's `stepped` ages them; each `Tick` keeps those not done; the view appends them. A value that goes from one number to another over a while is a `Tween` (`std/game/Tween`, `Tween.of(from, to, secs, ease)`), which the screen advances on its tick action and reads in its view.
360
377
 
361
378
  ## Effects and navigation
362
379
 
@@ -364,8 +381,7 @@ After the tick, the game runs `effect(io, a, game)` for every action dispatched
364
381
 
365
382
  | `Control` | What the game does |
366
383
  |---|---|
367
- | `game.goTo("over")` | shows the screen of that name from the next tick on: the shown screen `leave`s, the other (as the game was made with it, if it is `fresh`) `enter`s and comes in by its `transition(from)`. A name no screen has stops the program and lists the screens. |
368
- | `game.restart()` | every screen back as the game was made with it, and the first one shown, at once. (On master a restart builds the trees again and a view screen keeps its state; here a screen's state is the screen, so starting over is going back to it.) |
384
+ | `game.goTo("over")` | shows the screen of that name from the next tick on, as it was left, coming in by its `transition(from)`. A name no screen has stops the program and lists the screens. |
369
385
  | `game.exit()` | no tick runs after this one. |
370
386
  | `game.undo()`, `game.redo()`, `game.rewindTo(tick)` | once the tick is over, a move through the game's history ([Undo and time travel](#undo-and-time-travel)); nothing if the game keeps none |
371
387
 
@@ -381,7 +397,7 @@ While `plum run` serves a game, it watches your code and assets, the game's own
381
397
 
382
398
  ## Testing a game
383
399
 
384
- Put your rules in code that takes no `Io`, with `test` blocks, and `plum test` runs them with no browser. An actor's own methods are tested by calling them (`ship.update(0.1)`); a reducer the same way (`s.reduce(a)`); a screen, or a whole game, with the kit below. To check a game actually renders, the browser test tool runs it in headless Chromium. See [its README](../compiler/game/web-test/README.md).
400
+ Put your rules in code that takes no `Io`, with `test` blocks, and `plum test` runs them with no browser. An actor's own methods are tested by calling them (`ship.step(0.1)`); a reducer the same way (`s.reduce(a)`); a screen, or a whole game, with the kit below. To check a game actually renders, the browser test tool runs it in headless Chromium. See [its README](../compiler/game/web-test/README.md).
385
401
 
386
402
  ### Testing a screen
387
403
 
@@ -396,15 +412,15 @@ test "the play scores and starts over"
396
412
  assert v.count<Overlay>() == 0
397
413
  ```
398
414
 
399
- `headless.mount(io, screen)` makes a game of that one screen; `headless.play(io, game)` takes a whole game. Either sizes the view to 800 by 600 (`t.resize(w, h)` changes it). The rig, a `TestRig<S>`, is generic over the screen's type, so `t.screen` is the screen itself as the last tick left it (for `play`, the first screen, a `Screen`).
415
+ `headless.mount(io, screen)` makes a game of that one screen; `headless.play(io, game)` takes a whole game. Either sizes the window to 800 by 600 pixels (`t.resize(w, h)` changes it). The rig, a `TestRig<S>`, is generic over the screen's type, so `t.screen` is the screen itself as the last tick left it (for `play`, the first screen, a `Screen`).
400
416
 
401
417
  | | |
402
418
  |---|---|
403
- | Input, for the next tick | `press(key)` (down and up within the tick: one key down for the mapping, not held after), `hold(key)`, `release(key)`, `click(actor)` (a press and release at the actor's centre, its bounds' if it is `Sized`, in screen pixels through the viewport, so it goes through the real hit test: a panel in front gets it), `clickAt(x, y)`, `pointerMove(x, y)`, `send(e)`, `next(held, events)` |
404
- | Time | `tick()` (the game's own tick: mapping, reducer, transition, effects and what they ask), `ticks(n)` (`n` ticks, the input going to the first), `tick(dt)` for a tick of another length, `frame(input, frameDt)` for the host's frame with the `Clock` turning wall time into ticks |
405
- | An action | `dispatch(a)`: reduced into the screen as its reducer would dispatch it, then its effects |
406
- | Reading | `find<T>()` (the items of type `T` the view draws, in the order drawn, an item before what is under it), `first<T>()` (none stops the test: "headless: first: no actor of the type asked for is in the view"), `count<T>()`, `text(label)`, `shown(actor)` (the view draws it), `hit(x, y)` (what a press at that screen pixel lands on), `worldOf(actor)` (its transform), `toWorld(x, y)` (a screen pixel in the world), `drawn()` (what the frame draws, culled, each item with its transform and alpha) |
407
- | What came out of the last `tick` or `ticks` | `effects<A>()` (the actions of type `A` whose effects ran, in order), `restarted()`, `wentTo()` (an `Option`), `exited()`; `showing()` names the screen shown |
419
+ | Input, for the next tick | `press(key)` (down and up within the tick: one key down for the mapping, not held after), `hold(key)`, `release(key)`, `click(actor)` (a press and release at the actor's centre, its bounds' if it is `Sized`, in screen pixels through the camera, or one unit a pixel for an item of the hud, so it goes through the real hit test: a panel in front gets it), `clickAt(x, y)`, `pointerMove(x, y)`, `send(e)`, `next(held, events)` |
420
+ | Time | `tick()` (the game's own tick: mapping, the step, reducer, transition, effects and what they ask), `ticks(n)` (`n` ticks, the input going to the first), `tick(dt)` for a tick of another length, `frame(input, frameDt)` for the host's frame with the `Clock` turning wall time into ticks |
421
+ | An action | `dispatch(a)`: reduced into the screen as its reducer would dispatch it, then what ends a tick: its effects with what they ask, an undo, a redo or a rewind done at once |
422
+ | Reading, the view's then the hud's | `find<T>()` (the items of type `T` the view and the hud draw, in the order drawn, an item before what is under it), `first<T>()` (none stops the test: "headless: first: no actor of the type asked for is in the view"), `count<T>()`, `text(label)`, `shown(actor)` (the view or the hud draws it), `hit(x, y)` (what a press at that screen pixel lands on, the hud first), `worldOf(actor)` (its transform), `toWorld(x, y)` (a screen pixel in the world, through the camera), `drawn()` (what the frame draws, `Game.drawList()`) |
423
+ | What came out of the last `tick` or `ticks` | `effects<A>()` (the actions of type `A` whose effects ran, in order), `wentTo()` (an `Option`), `exited()`; `showing()` names the screen shown |
408
424
  | A whole run | `record(seed)`, `recorded()`, `replay(r)`, and `game` (its counters) |
409
425
  | Time travel | `mountWith(io, screen)` and `playWith(io, game)` keep the history; then `rewindTo(k)`, `undo()`, `redo()`, `stepBack()`, `stepForward()`, `pause()`, `resume()`, with `moved()`, and `t.screen` is the one made again |
410
426
 
@@ -421,36 +437,39 @@ test "while the game over panel shows, Enter or R plays again: the run starts ov
421
437
  assert !v.screen.over
422
438
  ```
423
439
 
424
- `std/game/headless` is tested by [`tests/port/game/screens.plum`](../tests/port/game/screens.plum) (presses through turned groups and overlays, dispatch order, navigation, transitions, frames on demand, record and replay, undo and rewind).
440
+ `std/game/headless` is tested by [`tests/port/game/screens.plum`](../tests/port/game/screens.plum) (presses through turned groups and overlays, dispatch order, navigation, transitions and the presses they drop, frames on demand, record and replay, undo and rewind, the stepping and `steps()`, the hud, a camera that follows). `compiler/game/tests/host_test.rs` checks what the native host draws: each scaling's placing and culling, a slide's offsets and backgrounds on both screens, a turned box and label and the hud.
425
441
 
426
442
  ## Beside master
427
443
 
428
- The engine has master's shape (`Screen`, a slice per screen, `Game(screens: ..)`, `Control`, the view and its hit test, the kit), in the functional dialect. Where the signatures differ:
444
+ The engine has master's shape at e91c40ce (`View` and `Actor`, `Screen` with its `hud`, `steps` and `camera`, a slice per screen, `Game(screens: ..)`, `Control`, the walks over a view, the kit), in the functional dialect. Where the signatures or the behaviour differ:
429
445
 
430
446
  | Master | Here | Why |
431
447
  |---|---|---|
448
+ | `trait Actor[View]` with `step(&self, dt)` | `step(self, dt) -> Self` | a value is not changed in place: `step` returns the actor's next version |
449
+ | the game walks the view and the hud and steps every `Actor` it finds, nested ones included (`view.stepIn` from `Delivery.stepScreen`) | the game calls the screen's `stepped(dt) -> Self` (default: the screen as it is), which maps the fields that hold actors (`view.stepAll`, `view.stepIn`); `steps()` pauses it as on master | an actor found in a view can't be put back into the field of the value that holds it, so the screen says where its actors are, one line per screen. An actor held under another is stepped by its holder's `step`. The walk also makes no view to look under an actor, so `benchmarks/screen_views/scale.plum`'s `update view` makes 10,000 fewer one-`Sprite` lists a tick than master's |
432
450
  | `trait Slice<A>[Screen]` | `trait ScreenSlice<A>[Screen]` | `Slice` is std's growable array here, and a trait can't share a type's name |
433
- | `fun reduce(&self, a: A)` | `fun reduce(self, a: A) -> Self` | a value is not changed in place: the reducer returns the next screen |
451
+ | `fun reduce(&self, a: A)`, `fun dispatch(&self, a: A)` | `fun reduce(self, a: A) -> Self`, `fun dispatch(self, a: A) -> Self` | the reducer returns the next screen |
434
- | `fun dispatch(&self, a: A)` | `fun dispatch(self, a: A) -> Self` | the same; it returns the slice after `a` |
435
452
  | `onKey(..) -> A?` and the other mappings | `-> Option<A>` | no `T?` in this dialect |
436
- | `build(&self, io) -> SceneTree`, `draw(self, tree, io)` | no `build`; `draw(self, io, m, alpha, area)` | no tree: the game draws the view through the screen's viewport |
437
- | `enter(&self, tree, from)`, `leave(&self, tree, to)`, `resize(&self, tree, w, h)` | `enter(self, from) -> Self`, `leave(self, to) -> Self`, `resize(self, w, h) -> Self` | no tree; each returns the next screen |
438
- | `deliver(&self, tree, input, dt)`, `wants(self, input)` | `deliver(self, cam: Viewport, input, dt) -> Delivered`, `wants(self, input)` | std's; returns the next screen and the tick's effects |
439
- | `name(self) -> Str` (and Orbit Runner's `"play"`) | the same | |
440
- | `Actor`: `place`, `view`, `paint`, `step(&self, ctx, dt)`, `shown` | `place`, `view`, `paint`, `shown`; no `step` | an actor moves by its own methods returning its next version, called by the reducer (master's example games do this too, with `update(&self, dt)`) |
441
- | actor fields with defaults (`Box(w:, h:)`) | every field given, or a static (`Box.of`, `Label.of`, `Sprite.cell`, `Overlay.of`) | no struct field defaults in this dialect |
442
- | `Overlay(open: true, ..)` takes presses as an `InputHandler` | `Overlay` is `Sized` and `Enabled`, hit anywhere in it | no tree input phases |
443
- | `Game` mutated by `update(&self, ..)` | `update(self, io, input, dt) -> Game`, `step(self, io, input, dt, view) -> Game` | the game is a value; `step` takes the screen size, which the host's `update` reads |
453
+ | `deliver(&self, d: Delivery)`, the delivery noting `acted` | `deliver(self, d: Delivery) -> Delivered` (the next screen, the tick's effects, whether it was a user action) | the delivery returns what it made |
454
+ | `Effects`, one queue of `Queued<A>` holding each action with its slice, set in one slot around a delivery | one slot too (`effects.startDispatch`), holding the delivery's queue of its own action type; after the tick each action is held with the slice as the tick left it (`Queued<A>`), and `effects.sent<A>(list)` reads them | an effect must see the slice the tick left, which is a new value, not the one the action was reduced into |
455
+ | `Viewport` updated in place (`v.update(w, h)`), `lookAt(x, y, zoom = 1.0)` | `Viewport.of(..).update(w, h) -> Viewport`, the same `lookAt` and `moved` | a value |
456
+ | the frame sets the host's camera (`setCamera`) for a camera with one scale, and puts a `Stretch` camera into the transform | every camera goes into the transform the view is drawn through, in window pixels, and culling maps its area through it | this branch's draw has always gone through the transform; the host sees no `setCamera`/`resetCamera` in a frame, so the recorder's calls differ (`compiler/game/tests/host_test.rs` checks the pixels) |
457
+ | the window's size on each tick's input (`input.screen`, `Input.sized`) | `Game.update(io, input, dt)` reads it from `gfx`; `Game.step(io, input, dt, pixels)` takes it (a test's) | not ported: the replay records the size in its header, as before |
458
+ | the fixed-timestep driver is `Ticker` (`Ticker.Tick`, `game.ticker.ticks`) | `Clock` (`Clock.Tick`), `game.ticks`, the rig's `t.frame(..)` | named before master's rename; not ported |
459
+ | actor and view fields with defaults (`Box(w:, h:)`, `Held(buttons: [])`) | every field given, or a static (`Box.of`, `Label.of(.., align = Left)`, `Sprite.cell`, `Overlay.of`, `Tween.of`, `Held.keysOnly`) | no struct field defaults in this dialect |
460
+ | `Tween` advanced in place | `tween.advance(dt) -> Tween` | a value |
461
+ | `Game` mutated by `update(&self, ..)` | `update(self, io, input, dt) -> Game`, `step(self, io, input, dt, pixels) -> Game` | the game is a value |
444
462
  | `Control`: methods on `&self` | methods that push onto its `asked` slice | an effect returns nothing; this is the one thing a tick writes in place |
445
- | `game.restart()` builds the trees again | every screen back as the game was made with it | a screen's state is the screen |
446
463
  | `timeTravel(make: fn(Io) -> Game, cap)` | `timeTravel(cap) -> Game` | the game keeps the screens it was made with |
447
464
  | `rewindTo`, `undo`, `redo`, .. `-> Bool` | `-> Game`, with `game.moved` | the move returns the moved game |
448
465
  | the kit's `t.press(k)` (in place) | `u := t.press(k)` | a rig is a value |
449
- | a view literal's `if`, `for`, `..` elements | `append`, `map`, functions | not in this dialect's list literals |
450
- | `ScreenView`, the one-actor tree that draws a view | none | the game draws and hit-tests the view itself |
451
- | `Container` trait, `declared(self)` | `Actor.declared(self) -> List<Actor>`, empty by default | spares a type test per item drawn, and keeps every optimizer pass in a game with no container kind (the downcast had to stay valid with none) |
466
+ | a view literal's `if`, `for`, `..` elements | `append`, `map`, functions, `view.all` | not in this dialect's list literals |
467
+ | 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) |
468
+ | `std/game` without `Anim` | `std/game/Anim` kept (pure animations of time) | a web-test fixture uses it; no example game does |
469
+
470
+ The same on both: `View` (`place`, `view`, `visible`, `paintSelf`, the shared empty default), the walks (`paintView`, `paintCulled`, `drawnIn`, `hitIn`, `worldIn`), `Screen` (`name`, `view`, `hud(window)`, `steps`, `camera(base)`, `size`, `viewport`, `culls`, `busy`, `background`, `transition`, `frames`; no `enter`, `leave`, `resize`, `draw` or `fresh`), `Control` (`goTo`, `exit`, `undo`, `redo`, `rewindTo`; 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, `Input.axis`, `isMouseDown(b)`, `isMousePressed(b)`, `isMouseReleased(b)`, the replay's button mask and `B`/`b` events), `math.near`, `Game.pixels`, `frameDrawn()`, `drawList()`, `camera(i)`, `toWorld`, `toScreen`, `visible(i)`, `travelAsked`, and Orbit Runner's state checksum (925878360).
452
471
 
453
- Not ported yet: master's frames-on-demand host call (`frameIdle`) and its development keys (`plum run --dev`, `remakeGame`), and `plum run` taking a game's folder; see the [backlog](backlog.md).
472
+ Not ported: master's frames-on-demand host call (`frameIdle`), its development keys (`plum run --dev`, `remakeGame`, `TravelKeys`), `plum run` taking a game's folder, and its `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.
454
473
 
455
474
  ## Publishing
456
475
 
docs/std.md CHANGED
@@ -256,7 +256,7 @@ fun main(io: Io) =
256
256
 
257
257
  ## Game screens and slices
258
258
 
259
- A game (`std/game/Game`) is a list of screens. A screen (`std/game/Screen`) has a name and a `view(self) -> List<Actor>`, what the frame shows, made again from its fields every frame; a screen with rules is a `ScreenSlice<A>` (`std/game/ScreenSlice`): its fields are its state, `A` its actions, `reduce(self, a) -> Self` its reducer (pure, with no `Io`, so the same actions from the same state give the same state) and `effect(self, io, a, game)` what an action does after the tick (a sound, `game.goTo("play")`). std maps each tick's input to the screen's actions (`onKey`, `onPress`, `onPointer`, `held`, `tick`) and keeps the screen each `reduce` returns. All of it is in [the game engine](game.md).
259
+ 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 the game steps each tick through the screen's `stepped(dt)`), 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`): its fields are its state, `A` its actions, `reduce(self, a) -> Self` its reducer (pure, with no `Io`, so the same actions from the same state give the same state) and `effect(self, io, a, game)` what an action does after the tick (a sound, `game.goTo("play")`). std maps each tick's input to the screen's actions (`onKey`, `onPress`, `onPointer`, `held`, `tick`) and keeps the screen each `reduce` returns. All of it is in [the game engine](game.md).
260
260
 
261
261
  ```plum
262
262
  import std/Int