plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
83aab43
— Peter John
2026-11-09T20:07:45+05:30
Screens in one tree: the state names the screen, with transitions
- compiler/cli/src/program.rs +1 -1
- compiler/cli/tests/cli/game_entry_test.rs +4 -4
- compiler/cli/tests/cli/game_web_test.rs +1 -1
- compiler/core/src/game_entry.rs +3 -3
- compiler/driver/tests/driver_tests.rs +12 -9
- compiler/game/src/host.rs +1 -1
- compiler/game/tests/fixtures/assets_fixture.plum +9 -5
- compiler/game/tests/fixtures/host_fixture.plum +9 -5
- compiler/game/tests/fixtures/main_game_fixture.plum +9 -5
- compiler/game/tests/fixtures/main_missing_fixture.plum +9 -5
- compiler/game/tests/fixtures/state_draw_keeps.plum +9 -5
- compiler/game/tests/fixtures/state_in_place.plum +9 -5
- compiler/game/tests/fixtures/state_main_alias.plum +9 -5
- compiler/game/tests/fixtures/state_reset.plum +9 -5
- compiler/game/tests/fixtures/target_fixture.plum +9 -5
- compiler/game/tests/host_test.rs +4 -4
- compiler/game/web-test/fixtures/interleave.plum +9 -5
- compiler/game/web-test/fixtures/main_game.plum +9 -5
- compiler/game/web-test/fixtures/rendertarget.plum +9 -5
- compiler/game/web-test/fixtures/stress.plum +9 -5
- compiler/game/web-test/fixtures/stress_scene_port.plum +10 -6
- compiler/game/web-test/fixtures/stress_state_view.plum +32 -23
- compiler/game/web-test/fixtures/stress_world.plum +28 -21
- compiler/game/web-test/fixtures/text_sizes.plum +9 -5
- docs/game.md +97 -25
- docs/plans/2026-11-09-actor-trait.md +78 -72
- docs/std.md +2 -2
- examples/games/clicks.plum +14 -12
- examples/games/kids/balloon_pop.plum +49 -51
- examples/games/orb_run.plum +157 -120
- examples/games/two_screens.plum +88 -0
- std/game/Game.plum +655 -137
- std/game/Store.plum +6 -0
- std/game/scene/Ctx.plum +1 -1
- std/game/scene/SceneTree.plum +29 -0
- tests/port/game/determinism.plum +8 -5
- tests/port/game/replay.plum +8 -5
compiler/cli/src/program.rs
CHANGED
|
@@ -73,7 +73,7 @@ pub fn tryCompileProgram(
|
|
|
73
73
|
render(&ds, true)
|
|
74
74
|
})?;
|
|
75
75
|
if !out.isGame && hasLegacyGameExports(&ast) {
|
|
76
|
-
return Err(Failure { codegen: false, text: format!("{}: the `export fun init/update/draw` game entry is gone; a game's `main` returns `Game[S, M]`, made by `Game.start(
|
|
76
|
+
return Err(Failure { codegen: false, text: format!("{}: the `export fun init/update/draw` game entry is gone; a game's `main` returns `Game[S, M]`, made by `Game.start(tree, state)` (std/game/Game)", file.display()) });
|
|
77
77
|
}
|
|
78
78
|
Ok(Program { isGame: out.isGame, wasm: out.wasm, resultMain })
|
|
79
79
|
}
|
compiler/cli/tests/cli/game_entry_test.rs
CHANGED
|
@@ -65,12 +65,12 @@ const HEAD: &str = "module app\nimport std/Int\nimport std/Float\nimport std/Str
|
|
|
65
65
|
|
|
66
66
|
/// A whole game: one screen with one actor over a state that counts its messages.
|
|
67
67
|
const GAME: &str = "module app\nimport std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/game/input\n\
|
|
68
|
-
import std/game/Store\nimport std/game/Game\nimport std/game/scene/Place\nimport std/game/scene/Actor\nimport std/game/scene/Ctx\nimport std/game/scene/SceneTree\n\n\
|
|
68
|
+
import std/game/Store\nimport std/game/Game\nimport std/game/scene/Place\nimport std/game/scene/Actor\nimport std/game/scene/Ctx\nimport std/game/scene/SceneTree\nimport std/collections/functional/FRoseTree\n\n\
|
|
69
69
|
struct Count(n: Int) is Reducer[Int] =\n fun update(self, msg: Int) -> Count = Count(n: self.n + msg)\n\n\
|
|
70
70
|
struct Ticker(id: Int) is Actor[Int] =\n fun key(self) -> Int = self.id\n fun place(self) -> Place = place.at(0.0, 0.0)\n \
|
|
71
71
|
fun step(self, ctx: Ctx[Int], dt: Float) -> Ticker =\n ctx.send(1)\n self\n\n\
|
|
72
|
-
struct
|
|
73
|
-
fun main(io: Io) -> Game[Count, Int] =\n
|
|
72
|
+
struct OnlyScreen(id: Int) is Screen[Count, Int] =\n fun key(self) -> Int = self.id\n fun place(self) -> Place = place.at(0.0, 0.0)\n\n\
|
|
73
|
+
fun main(io: Io) -> Game[Count, Int] =\n kid: FRoseTree[Actor[Int]] := FRoseTree.leaf(Ticker(id: 0))\n only: FRoseTree[Actor[Int]] := FRoseTree.of(OnlyScreen(id: -2), [kid])\n Game.start(SceneTree.ofScreens([only]), Count(n: 0))\n";
|
|
74
74
|
|
|
75
75
|
#[test]
|
|
76
76
|
fn aMainReturningAGameIsAGame() {
|
|
@@ -94,7 +94,7 @@ fn theOldUpdateAndDrawShapeNamesGameStart() {
|
|
|
94
94
|
let (ok, err) = compileSource("oldShape", &src);
|
|
95
95
|
assert!(!ok, "{err}");
|
|
96
96
|
assert!(err.contains("`Counter` has `update` and `draw`, but a game is no longer a type of its own"), "{err}");
|
|
97
|
-
assert!(err.contains("Game.start(
|
|
97
|
+
assert!(err.contains("Game.start(tree, state)"), "{err}");
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
|
compiler/cli/tests/cli/game_web_test.rs
CHANGED
|
@@ -172,7 +172,7 @@ fn oldInitEntryIsRejectedWithAPointer() {
|
|
|
172
172
|
let out = Command::new(plumBin()).current_dir(root()).args([cmd]).arg(&file).args(["--lib-path", "."]).output().unwrap();
|
|
173
173
|
let err = String::from_utf8_lossy(&out.stderr);
|
|
174
174
|
assert!(!out.status.success(), "{cmd} should fail");
|
|
175
|
-
assert!(err.contains("Game.start(
|
|
175
|
+
assert!(err.contains("Game.start(tree, state)"), "{cmd}: {err}");
|
|
176
176
|
}
|
|
177
177
|
let _ = std::fs::remove_dir_all(&scratch);
|
|
178
178
|
}
|
compiler/core/src/game_entry.rs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! A game is a `main` returning `Game[S, M]` (std/game/Game), made by `Game.start(
|
|
1
|
+
//! A game is a `main` returning `Game[S, M]` (std/game/Game), made by `Game.start(tree, state)`:
|
|
2
2
|
//! there is no other kind of game. This lowering (run before checking) renames the game's `main`
|
|
3
3
|
//! to `gameUserMain` and appends the state the host holds, `GameRun(game, clock)` with the
|
|
4
4
|
//! `Clock` of std/game/input, a `main` that builds it, and the two frame entry points the host
|
|
@@ -53,7 +53,7 @@ pub fn gameType(source: &Source) -> Result<Option<Type>, Diagnostic> {
|
|
|
53
53
|
if declared && hasMethod(source, ty, "update") && hasMethod(source, ty, "draw") {
|
|
54
54
|
return Err(at(format!(
|
|
55
55
|
"`{ty}` has `update` and `draw`, but a game is no longer a type of its own: \
|
|
56
|
-
a game's `main` returns `Game[S, M]`, made by `Game.start(
|
|
56
|
+
a game's `main` returns `Game[S, M]`, made by `Game.start(tree, state)` (import std/game/Game; see docs/game.md)"
|
|
57
57
|
)));
|
|
58
58
|
}
|
|
59
59
|
Ok(None)
|
|
@@ -147,7 +147,7 @@ mod tests {
|
|
|
147
147
|
fn theOldUpdateAndDrawShapeIsAnErrorNamingGameStart() {
|
|
148
148
|
for update in ["fun update(self, io: Io, input: Input, dt: Float) -> G =", "fun update(self, io: Io, dt: Float) -> G ="] {
|
|
149
149
|
let e = gameType(&oldShape(update)).expect_err("old shape");
|
|
150
|
-
assert!(e.message.contains("`G` has `update` and `draw`") && e.message.contains("Game.start(
|
|
150
|
+
assert!(e.message.contains("`G` has `update` and `draw`") && e.message.contains("Game.start(tree, state)"), "{}", e.message);
|
|
151
151
|
}
|
|
152
152
|
}
|
|
153
153
|
|
compiler/driver/tests/driver_tests.rs
CHANGED
|
@@ -215,25 +215,28 @@ fn aSharedReceiverOfAnotherTypeDoesNotProveItsOwnMethod() {
|
|
|
215
215
|
|
|
216
216
|
// ---- a game: the host is the caller the proof cannot see ----
|
|
217
217
|
|
|
218
|
-
/// A game (std/game/Game) of one screen, `
|
|
218
|
+
/// A game (std/game/Game) of one screen, `PaneScreen`, holding one actor, `Stress`; `step`, `draw`
|
|
219
219
|
/// and `main` are the bodies of the actor's step, the screen's draw and the game's start.
|
|
220
220
|
fn gameSource(name: &str, main: &str, step: &str, draw: &str) -> Source {
|
|
221
221
|
let text = format!(
|
|
222
222
|
"module {name}\nimport std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/game/input\n\
|
|
223
223
|
import std/game/Store\nimport std/game/Game\nimport std/game/scene/Place\nimport std/game/scene/Actor\n\
|
|
224
|
-
import std/game/scene/Ctx\nimport std/game/scene/SceneTree\n\
|
|
224
|
+
import std/game/scene/Ctx\nimport std/game/scene/SceneTree\nimport std/collections/functional/FRoseTree\n\
|
|
225
225
|
Start = Stress(label: \"s\", t: 0)\n\
|
|
226
226
|
struct Quiet(n: Int) is Reducer[Int] =\n fun update(self, msg: Int) -> Quiet = self\n\
|
|
227
227
|
struct Stress(label: Str, t: Int) is Actor[Int] =\n \
|
|
228
228
|
fun key(self) -> Int = 1\n \
|
|
229
229
|
fun place(self) -> Place = place.at(0.0, 0.0)\n \
|
|
230
230
|
fun step(self, ctx: Ctx[Int], dt: Float) -> Stress =\n {step}\n\
|
|
231
|
-
struct
|
|
231
|
+
struct PaneScreen(id: Int, n: Int) is Screen[Quiet, Int] =\n \
|
|
232
|
-
fun
|
|
232
|
+
fun key(self) -> Int = self.id\n \
|
|
233
|
-
fun
|
|
233
|
+
fun place(self) -> Place = place.at(0.0, 0.0)\n \
|
|
234
|
-
fun later(self) ->
|
|
234
|
+
fun later(self) -> PaneScreen = PaneScreen(..self, n: 99)\n \
|
|
235
|
-
fun draw(self, io: Io) =\n {draw}\n\
|
|
235
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) =\n {draw}\n\
|
|
236
|
+
fun main(io: Io) -> Game[Quiet, Int] =\n \
|
|
237
|
+
kid: FRoseTree[Actor[Int]] := FRoseTree.leaf({main})\n \
|
|
238
|
+
pane: FRoseTree[Actor[Int]] := FRoseTree.of(PaneScreen(id: -2, n: 0), [kid])\n \
|
|
236
|
-
|
|
239
|
+
Game.start(SceneTree.ofScreens([pane]), Quiet(n: 0))\n"
|
|
237
240
|
);
|
|
238
241
|
let mut src = load(name, &text);
|
|
239
242
|
assert!(plum_core::game_entry::lowerGame(&mut src).expect("lowers"), "{name} is a game");
|
|
@@ -265,7 +268,7 @@ fn gameStepReturningAConstantCopies() {
|
|
|
265
268
|
fn gameDrawNeverWritesTheScreenInPlace() {
|
|
266
269
|
let draw = "x := self.later()\n io.printLn(\"{x.n}\")";
|
|
267
270
|
let info = check(&gameSource("g_draw", FRESH_ACTOR, SPREAD_T, draw), &PassOptions::default()).expect("checks");
|
|
268
|
-
assert!(!spreadInPlace(&info, "
|
|
271
|
+
assert!(!spreadInPlace(&info, "PaneScreen", "later"));
|
|
269
272
|
}
|
|
270
273
|
|
|
271
274
|
/// Purity keys a method by the receiver's type: `Orb.bump` writes a field of its argument, so it
|
compiler/game/src/host.rs
CHANGED
|
@@ -263,7 +263,7 @@ impl<B: Backend> PlumGame<B> {
|
|
|
263
263
|
for name in ["main", "update", "draw"] {
|
|
264
264
|
if module.get_export(name).is_none() {
|
|
265
265
|
let old = if name == "main" && module.get_export("init").is_some() {
|
|
266
|
-
" (the `export fun init` entry is gone: write `fun main(io: Io) -> Game[S, M]` returning `Game.start(
|
|
266
|
+
" (the `export fun init` entry is gone: write `fun main(io: Io) -> Game[S, M]` returning `Game.start(tree, state)`)"
|
|
267
267
|
} else {
|
|
268
268
|
""
|
|
269
269
|
};
|
compiler/game/tests/fixtures/assets_fixture.plum
CHANGED
|
@@ -12,6 +12,7 @@ import std/game/input
|
|
|
12
12
|
import std/game/Affine
|
|
13
13
|
import std/game/Store
|
|
14
14
|
import std/game/Game
|
|
15
|
+
import std/collections/functional/FRoseTree
|
|
15
16
|
import std/game/scene/Place
|
|
16
17
|
import std/game/scene/Actor
|
|
17
18
|
import std/game/scene/Ctx
|
|
@@ -41,16 +42,19 @@ struct State(frames: Int) is Actor[Int] =
|
|
|
41
42
|
gfx.drawText(io, "abs {gfx.textureLoad(io, "/etc/hosts")} {gfx.fileExists(io, "/etc/hosts")}", 0.0, 0.0, 8.0, Color.White)
|
|
42
43
|
|
|
43
44
|
fun main(io: Io) -> Game[Quiet, Int] =
|
|
44
|
-
Game.start(
|
|
45
|
+
Game.start(screenOf(State(frames: 0)), Quiet(n: 0))
|
|
45
46
|
|
|
46
47
|
# The game sends no messages, so its state never changes.
|
|
47
48
|
struct Quiet(n: Int) is Reducer[Int] =
|
|
48
49
|
fun update(self, msg: Int) -> Quiet = self
|
|
49
50
|
|
|
50
51
|
# The one screen: its tree drawn in screen pixels.
|
|
51
|
-
struct
|
|
52
|
+
struct PageScreen(id: Int) is Screen[Quiet, Int] =
|
|
52
|
-
fun
|
|
53
|
+
fun key(self) -> Int = self.id
|
|
53
54
|
|
|
55
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
56
|
+
|
|
54
|
-
fun
|
|
57
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
55
58
|
|
|
56
|
-
|
|
59
|
+
# The game's tree: its one screen over the actor `top`.
|
|
60
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(PageScreen(id: -2), [FRoseTree.leaf(top)])])
|
compiler/game/tests/fixtures/host_fixture.plum
CHANGED
|
@@ -12,6 +12,7 @@ import std/game/input
|
|
|
12
12
|
import std/game/Affine
|
|
13
13
|
import std/game/Store
|
|
14
14
|
import std/game/Game
|
|
15
|
+
import std/collections/functional/FRoseTree
|
|
15
16
|
import std/game/scene/Place
|
|
16
17
|
import std/game/scene/Actor
|
|
17
18
|
import std/game/scene/Ctx
|
|
@@ -36,16 +37,19 @@ struct State(frames: Int) is Actor[Int] =
|
|
|
36
37
|
gfx.drawText(io, "frames {self.frames}", 0.0, 0.0, 8.0, Color.White)
|
|
37
38
|
|
|
38
39
|
fun main(io: Io) -> Game[Quiet, Int] =
|
|
39
|
-
Game.start(
|
|
40
|
+
Game.start(screenOf(State(frames: 0)), Quiet(n: 0))
|
|
40
41
|
|
|
41
42
|
# The game sends no messages, so its state never changes.
|
|
42
43
|
struct Quiet(n: Int) is Reducer[Int] =
|
|
43
44
|
fun update(self, msg: Int) -> Quiet = self
|
|
44
45
|
|
|
45
46
|
# The one screen: its tree drawn in screen pixels.
|
|
46
|
-
struct
|
|
47
|
+
struct PageScreen(id: Int) is Screen[Quiet, Int] =
|
|
47
|
-
fun
|
|
48
|
+
fun key(self) -> Int = self.id
|
|
48
49
|
|
|
50
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
51
|
+
|
|
49
|
-
fun
|
|
52
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
50
53
|
|
|
51
|
-
|
|
54
|
+
# The game's tree: its one screen over the actor `top`.
|
|
55
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(PageScreen(id: -2), [FRoseTree.leaf(top)])])
|
compiler/game/tests/fixtures/main_game_fixture.plum
CHANGED
|
@@ -12,6 +12,7 @@ import std/game/input
|
|
|
12
12
|
import std/game/Affine
|
|
13
13
|
import std/game/Store
|
|
14
14
|
import std/game/Game
|
|
15
|
+
import std/collections/functional/FRoseTree
|
|
15
16
|
import std/game/scene/Place
|
|
16
17
|
import std/game/scene/Actor
|
|
17
18
|
import std/game/scene/Ctx
|
|
@@ -36,16 +37,19 @@ struct Orbs(x: Float, ship: Texture, input: Input, frame: Int) is Actor[Int] =
|
|
|
36
37
|
gfx.drawText(io, "frame {self.frame} ev {first} {second} x {self.x} tex {self.ship.id} w {gfx.screenWidth(io)}", 0.0, 0.0, 8.0, Color.White)
|
|
37
38
|
|
|
38
39
|
fun main(io: Io) -> Game[Quiet, Int] =
|
|
39
|
-
Game.start(
|
|
40
|
+
Game.start(screenOf(Orbs(x: 0.0, ship: gfx.loadTexture(io, "sprites.png"), input: Input.create(), frame: 0)), Quiet(n: 0))
|
|
40
41
|
|
|
41
42
|
# The game sends no messages, so its state never changes.
|
|
42
43
|
struct Quiet(n: Int) is Reducer[Int] =
|
|
43
44
|
fun update(self, msg: Int) -> Quiet = self
|
|
44
45
|
|
|
45
46
|
# The one screen: its tree drawn in screen pixels.
|
|
46
|
-
struct
|
|
47
|
+
struct PageScreen(id: Int) is Screen[Quiet, Int] =
|
|
47
|
-
fun
|
|
48
|
+
fun key(self) -> Int = self.id
|
|
48
49
|
|
|
50
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
51
|
+
|
|
49
|
-
fun
|
|
52
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
50
53
|
|
|
51
|
-
|
|
54
|
+
# The game's tree: its one screen over the actor `top`.
|
|
55
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(PageScreen(id: -2), [FRoseTree.leaf(top)])])
|
compiler/game/tests/fixtures/main_missing_fixture.plum
CHANGED
|
@@ -10,6 +10,7 @@ import std/game/input
|
|
|
10
10
|
import std/game/Affine
|
|
11
11
|
import std/game/Store
|
|
12
12
|
import std/game/Game
|
|
13
|
+
import std/collections/functional/FRoseTree
|
|
13
14
|
import std/game/scene/Place
|
|
14
15
|
import std/game/scene/Actor
|
|
15
16
|
import std/game/scene/Ctx
|
|
@@ -25,16 +26,19 @@ struct Broken(tex: Texture) is Actor[Int] =
|
|
|
25
26
|
gfx.clear(io, Color.Black)
|
|
26
27
|
|
|
27
28
|
fun main(io: Io) -> Game[Quiet, Int] =
|
|
28
|
-
Game.start(
|
|
29
|
+
Game.start(screenOf(Broken(tex: gfx.loadTexture(io, "nope.png"))), Quiet(n: 0))
|
|
29
30
|
|
|
30
31
|
# The game sends no messages, so its state never changes.
|
|
31
32
|
struct Quiet(n: Int) is Reducer[Int] =
|
|
32
33
|
fun update(self, msg: Int) -> Quiet = self
|
|
33
34
|
|
|
34
35
|
# The one screen: its tree drawn in screen pixels.
|
|
35
|
-
struct
|
|
36
|
+
struct PageScreen(id: Int) is Screen[Quiet, Int] =
|
|
36
|
-
fun
|
|
37
|
+
fun key(self) -> Int = self.id
|
|
37
38
|
|
|
39
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
40
|
+
|
|
38
|
-
fun
|
|
41
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
39
42
|
|
|
40
|
-
|
|
43
|
+
# The game's tree: its one screen over the actor `top`.
|
|
44
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(PageScreen(id: -2), [FRoseTree.leaf(top)])])
|
compiler/game/tests/fixtures/state_draw_keeps.plum
CHANGED
|
@@ -10,6 +10,7 @@ import std/game/input
|
|
|
10
10
|
import std/game/Affine
|
|
11
11
|
import std/game/Store
|
|
12
12
|
import std/game/Game
|
|
13
|
+
import std/collections/functional/FRoseTree
|
|
13
14
|
import std/game/scene/Place
|
|
14
15
|
import std/game/scene/Actor
|
|
15
16
|
import std/game/scene/Ctx
|
|
@@ -32,16 +33,19 @@ struct Run(label: Str, log: Log, t: Int) is Actor[Int] =
|
|
|
32
33
|
self.log.last = Some(self)
|
|
33
34
|
gfx.drawText(io, "{before} {self.t}", 0.0, 0.0, 8.0, Color.White)
|
|
34
35
|
|
|
35
|
-
fun main(io: Io) -> Game[Quiet, Int] = Game.start(
|
|
36
|
+
fun main(io: Io) -> Game[Quiet, Int] = Game.start(screenOf(Run(label: "r", log: Log(last: None), t: 0)), Quiet(n: 0))
|
|
36
37
|
|
|
37
38
|
# The game sends no messages, so its state never changes.
|
|
38
39
|
struct Quiet(n: Int) is Reducer[Int] =
|
|
39
40
|
fun update(self, msg: Int) -> Quiet = self
|
|
40
41
|
|
|
41
42
|
# The one screen: its tree drawn in screen pixels.
|
|
42
|
-
struct
|
|
43
|
+
struct PageScreen(id: Int) is Screen[Quiet, Int] =
|
|
43
|
-
fun
|
|
44
|
+
fun key(self) -> Int = self.id
|
|
44
45
|
|
|
46
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
47
|
+
|
|
45
|
-
fun
|
|
48
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
46
49
|
|
|
47
|
-
|
|
50
|
+
# The game's tree: its one screen over the actor `top`.
|
|
51
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(PageScreen(id: -2), [FRoseTree.leaf(top)])])
|
compiler/game/tests/fixtures/state_in_place.plum
CHANGED
|
@@ -10,6 +10,7 @@ import std/game/input
|
|
|
10
10
|
import std/game/Affine
|
|
11
11
|
import std/game/Store
|
|
12
12
|
import std/game/Game
|
|
13
|
+
import std/collections/functional/FRoseTree
|
|
13
14
|
import std/game/scene/Place
|
|
14
15
|
import std/game/scene/Actor
|
|
15
16
|
import std/game/scene/Ctx
|
|
@@ -34,16 +35,19 @@ struct Run(part: Part, t: Int) is Actor[Int] =
|
|
|
34
35
|
d := self.drawn()
|
|
35
36
|
gfx.drawText(io, "{d.t} {self.t} {self.part.x}", 0.0, 0.0, 8.0, Color.White)
|
|
36
37
|
|
|
37
|
-
fun main(io: Io) -> Game[Quiet, Int] = Game.start(
|
|
38
|
+
fun main(io: Io) -> Game[Quiet, Int] = Game.start(screenOf(Run(part: Part(label: "p", x: 0), t: 0)), Quiet(n: 0))
|
|
38
39
|
|
|
39
40
|
# The game sends no messages, so its state never changes.
|
|
40
41
|
struct Quiet(n: Int) is Reducer[Int] =
|
|
41
42
|
fun update(self, msg: Int) -> Quiet = self
|
|
42
43
|
|
|
43
44
|
# The one screen: its tree drawn in screen pixels.
|
|
44
|
-
struct
|
|
45
|
+
struct PageScreen(id: Int) is Screen[Quiet, Int] =
|
|
45
|
-
fun
|
|
46
|
+
fun key(self) -> Int = self.id
|
|
46
47
|
|
|
48
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
49
|
+
|
|
47
|
-
fun
|
|
50
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
48
51
|
|
|
49
|
-
|
|
52
|
+
# The game's tree: its one screen over the actor `top`.
|
|
53
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(PageScreen(id: -2), [FRoseTree.leaf(top)])])
|
compiler/game/tests/fixtures/state_main_alias.plum
CHANGED
|
@@ -10,6 +10,7 @@ import std/game/input
|
|
|
10
10
|
import std/game/Affine
|
|
11
11
|
import std/game/Store
|
|
12
12
|
import std/game/Game
|
|
13
|
+
import std/collections/functional/FRoseTree
|
|
13
14
|
import std/game/scene/Place
|
|
14
15
|
import std/game/scene/Actor
|
|
15
16
|
import std/game/scene/Ctx
|
|
@@ -31,16 +32,19 @@ struct Run(label: Str, t: Int) is Actor[Int] =
|
|
|
31
32
|
|
|
32
33
|
fun main(io: Io) -> Game[Quiet, Int] =
|
|
33
34
|
s := Start
|
|
34
|
-
Game.start(
|
|
35
|
+
Game.start(screenOf(s), Quiet(n: 0))
|
|
35
36
|
|
|
36
37
|
# The game sends no messages, so its state never changes.
|
|
37
38
|
struct Quiet(n: Int) is Reducer[Int] =
|
|
38
39
|
fun update(self, msg: Int) -> Quiet = self
|
|
39
40
|
|
|
40
41
|
# The one screen: its tree drawn in screen pixels.
|
|
41
|
-
struct
|
|
42
|
+
struct PageScreen(id: Int) is Screen[Quiet, Int] =
|
|
42
|
-
fun
|
|
43
|
+
fun key(self) -> Int = self.id
|
|
43
44
|
|
|
45
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
46
|
+
|
|
44
|
-
fun
|
|
47
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
45
48
|
|
|
46
|
-
|
|
49
|
+
# The game's tree: its one screen over the actor `top`.
|
|
50
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(PageScreen(id: -2), [FRoseTree.leaf(top)])])
|
compiler/game/tests/fixtures/state_reset.plum
CHANGED
|
@@ -10,6 +10,7 @@ import std/game/input
|
|
|
10
10
|
import std/game/Affine
|
|
11
11
|
import std/game/Store
|
|
12
12
|
import std/game/Game
|
|
13
|
+
import std/collections/functional/FRoseTree
|
|
13
14
|
import std/game/scene/Place
|
|
14
15
|
import std/game/scene/Actor
|
|
15
16
|
import std/game/scene/Ctx
|
|
@@ -29,16 +30,19 @@ struct Run(label: Str, t: Int) is Actor[Int] =
|
|
|
29
30
|
fun paint(self, io: Io, m: Affine, alpha: Float) =
|
|
30
31
|
gfx.drawText(io, "{self.t} {Start.t}", 0.0, 0.0, 8.0, Color.White)
|
|
31
32
|
|
|
32
|
-
fun main(io: Io) -> Game[Quiet, Int] = Game.start(
|
|
33
|
+
fun main(io: Io) -> Game[Quiet, Int] = Game.start(screenOf(Start), Quiet(n: 0))
|
|
33
34
|
|
|
34
35
|
# The game sends no messages, so its state never changes.
|
|
35
36
|
struct Quiet(n: Int) is Reducer[Int] =
|
|
36
37
|
fun update(self, msg: Int) -> Quiet = self
|
|
37
38
|
|
|
38
39
|
# The one screen: its tree drawn in screen pixels.
|
|
39
|
-
struct
|
|
40
|
+
struct PageScreen(id: Int) is Screen[Quiet, Int] =
|
|
40
|
-
fun
|
|
41
|
+
fun key(self) -> Int = self.id
|
|
41
42
|
|
|
43
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
44
|
+
|
|
42
|
-
fun
|
|
45
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
43
46
|
|
|
44
|
-
|
|
47
|
+
# The game's tree: its one screen over the actor `top`.
|
|
48
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(PageScreen(id: -2), [FRoseTree.leaf(top)])])
|
compiler/game/tests/fixtures/target_fixture.plum
CHANGED
|
@@ -11,6 +11,7 @@ import std/game/input
|
|
|
11
11
|
import std/game/Affine
|
|
12
12
|
import std/game/Store
|
|
13
13
|
import std/game/Game
|
|
14
|
+
import std/collections/functional/FRoseTree
|
|
14
15
|
import std/game/scene/Place
|
|
15
16
|
import std/game/scene/Actor
|
|
16
17
|
import std/game/scene/Ctx
|
|
@@ -34,16 +35,19 @@ struct State(target: Texture, bad: Int) is Actor[Int] =
|
|
|
34
35
|
gfx.drawText(io, "size {self.target.width(io)}x{self.target.height(io)} bad {self.bad}", 0.0, 0.0, 8.0, Color.White)
|
|
35
36
|
|
|
36
37
|
fun main(io: Io) -> Game[Quiet, Int] =
|
|
37
|
-
Game.start(
|
|
38
|
+
Game.start(screenOf(State(target: gfx.createTexture(io, 64, 32), bad: gfx.textureCreate(io, 0, 5))), Quiet(n: 0))
|
|
38
39
|
|
|
39
40
|
# The game sends no messages, so its state never changes.
|
|
40
41
|
struct Quiet(n: Int) is Reducer[Int] =
|
|
41
42
|
fun update(self, msg: Int) -> Quiet = self
|
|
42
43
|
|
|
43
44
|
# The one screen: its tree drawn in screen pixels.
|
|
44
|
-
struct
|
|
45
|
+
struct PageScreen(id: Int) is Screen[Quiet, Int] =
|
|
45
|
-
fun
|
|
46
|
+
fun key(self) -> Int = self.id
|
|
46
47
|
|
|
48
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
49
|
+
|
|
47
|
-
fun
|
|
50
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
48
51
|
|
|
49
|
-
|
|
52
|
+
# The game's tree: its one screen over the actor `top`.
|
|
53
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(PageScreen(id: -2), [FRoseTree.leaf(top)])])
|
compiler/game/tests/host_test.rs
CHANGED
|
@@ -151,7 +151,7 @@ fn unknownImportTraps() {
|
|
|
151
151
|
let path = dir.join("m.plum");
|
|
152
152
|
std::fs::write(
|
|
153
153
|
&path,
|
|
154
|
-
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/game/Vec2\nimport std/game/Affine\nimport std/game/Color\nimport std/game/gfx\nimport std/game/input\nimport std/game/Store\nimport std/game/Game\nimport std/game/scene/Place\nimport std/game/scene/Actor\nimport std/game/scene/Ctx\nimport std/game/scene/SceneTree\nstruct Quiet(n: Int) is Reducer[Int] =\n fun update(self, msg: Int) -> Quiet = self\nstruct
|
|
154
|
+
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/game/Vec2\nimport std/game/Affine\nimport std/game/Color\nimport std/game/gfx\nimport std/game/input\nimport std/game/Store\nimport std/game/Game\nimport std/game/scene/Place\nimport std/game/scene/Actor\nimport std/game/scene/Ctx\nimport std/game/scene/SceneTree\nimport std/collections/functional/FRoseTree\nstruct Quiet(n: Int) is Reducer[Int] =\n fun update(self, msg: Int) -> Quiet = self\nstruct PageScreen(id: Int) is Screen[Quiet, Int] =\n fun key(self) -> Int = self.id\n fun place(self) -> Place = place.at(0.0, 0.0)\n fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)\nfun screenOf(top: Actor[Int]) -> SceneTree[Int] =\n kid: FRoseTree[Actor[Int]] := FRoseTree.leaf(top)\n page: FRoseTree[Actor[Int]] := FRoseTree.of(PageScreen(id: -2), [kid])\n SceneTree.ofScreens([page])\nstruct State(n: Int) is Actor[Int] =\n fun key(self) -> Int = 1\n fun place(self) -> Place = place.at(0.0, 0.0)\n fun paint(self, io: Io, m: Affine, alpha: Float) =\n x := io.rawExists(\"a\")\n\nfun main(io: Io) -> Game[Quiet, Int] =\n Game.start(screenOf(State(n: 0)), Quiet(n: 0))\n",
|
|
155
155
|
)
|
|
156
156
|
.unwrap();
|
|
157
157
|
let wasm = compile(&path);
|
|
@@ -366,7 +366,7 @@ fn randomSeedProvidedByHost() {
|
|
|
366
366
|
let path = dir.join("m.plum");
|
|
367
367
|
std::fs::write(
|
|
368
368
|
&path,
|
|
369
|
-
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/game/Vec2\nimport std/game/Affine\nimport std/game/Color\nimport std/game/gfx\nimport std/game/input\nimport std/game/Store\nimport std/game/Game\nimport std/game/scene/Place\nimport std/game/scene/Actor\nimport std/game/scene/Ctx\nimport std/game/scene/SceneTree\nstruct Quiet(n: Int) is Reducer[Int] =\n fun update(self, msg: Int) -> Quiet = self\nstruct
|
|
369
|
+
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/game/Vec2\nimport std/game/Affine\nimport std/game/Color\nimport std/game/gfx\nimport std/game/input\nimport std/game/Store\nimport std/game/Game\nimport std/game/scene/Place\nimport std/game/scene/Actor\nimport std/game/scene/Ctx\nimport std/game/scene/SceneTree\nimport std/collections/functional/FRoseTree\nstruct Quiet(n: Int) is Reducer[Int] =\n fun update(self, msg: Int) -> Quiet = self\nstruct PageScreen(id: Int) is Screen[Quiet, Int] =\n fun key(self) -> Int = self.id\n fun place(self) -> Place = place.at(0.0, 0.0)\n fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)\nfun screenOf(top: Actor[Int]) -> SceneTree[Int] =\n kid: FRoseTree[Actor[Int]] := FRoseTree.leaf(top)\n page: FRoseTree[Actor[Int]] := FRoseTree.of(PageScreen(id: -2), [kid])\n SceneTree.ofScreens([page])\nstruct State(seed: Int) is Actor[Int] =\n fun key(self) -> Int = 1\n fun place(self) -> Place = place.at(0.0, 0.0)\n fun paint(self, io: Io, m: Affine, alpha: Float) =\n gfx.drawText(io, \"seed {self.seed}\", 0.0, 0.0, 8.0, Color.White)\n\nfun main(io: Io) -> Game[Quiet, Int] =\n Game.start(screenOf(State(seed: io.randomSeed())), Quiet(n: 0))\n",
|
|
370
370
|
)
|
|
371
371
|
.unwrap();
|
|
372
372
|
let wasm = compile(&path);
|
|
@@ -390,7 +390,7 @@ fn seedGame(name: &str) -> Vec<u8> {
|
|
|
390
390
|
let path = dir.join("m.plum");
|
|
391
391
|
std::fs::write(
|
|
392
392
|
&path,
|
|
393
|
-
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/game/Vec2\nimport std/game/Affine\nimport std/game/Color\nimport std/game/gfx\nimport std/game/input\nimport std/game/Store\nimport std/game/Game\nimport std/game/scene/Place\nimport std/game/scene/Actor\nimport std/game/scene/Ctx\nimport std/game/scene/SceneTree\nstruct Quiet(n: Int) is Reducer[Int] =\n fun update(self, msg: Int) -> Quiet = self\nstruct
|
|
393
|
+
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/game/Vec2\nimport std/game/Affine\nimport std/game/Color\nimport std/game/gfx\nimport std/game/input\nimport std/game/Store\nimport std/game/Game\nimport std/game/scene/Place\nimport std/game/scene/Actor\nimport std/game/scene/Ctx\nimport std/game/scene/SceneTree\nimport std/collections/functional/FRoseTree\nstruct Quiet(n: Int) is Reducer[Int] =\n fun update(self, msg: Int) -> Quiet = self\nstruct PageScreen(id: Int) is Screen[Quiet, Int] =\n fun key(self) -> Int = self.id\n fun place(self) -> Place = place.at(0.0, 0.0)\n fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)\nfun screenOf(top: Actor[Int]) -> SceneTree[Int] =\n kid: FRoseTree[Actor[Int]] := FRoseTree.leaf(top)\n page: FRoseTree[Actor[Int]] := FRoseTree.of(PageScreen(id: -2), [kid])\n SceneTree.ofScreens([page])\nstruct State(seed: Int, ticks: Int, keys: Int, x: Float, w: Float) is Actor[Int] =\n fun key(self) -> Int = 1\n fun place(self) -> Place = place.at(0.0, 0.0)\n fun step(self, ctx: Ctx[Int], dt: Float) -> State =\n State(..self, ticks: self.ticks + 1, keys: self.keys + ctx.input.held.keys.length(), x: ctx.input.pos.x, w: ctx.view.x)\n fun paint(self, io: Io, m: Affine, alpha: Float) =\n gfx.drawText(io, \"seed {self.seed} ticks {self.ticks} keys {self.keys} x {self.x} w {self.w}\", 0.0, 0.0, 8.0, Color.White)\n\nfun main(io: Io) -> Game[Quiet, Int] =\n Game.start(screenOf(State(seed: io.randomSeed(), ticks: 0, keys: 0, x: 0.0, w: 0.0)), Quiet(n: 0))\n",
|
|
394
394
|
)
|
|
395
395
|
.unwrap();
|
|
396
396
|
let wasm = compile(&path);
|
|
@@ -465,7 +465,7 @@ fn everySeedOfARecordedRunReplays() {
|
|
|
465
465
|
let path = dir.join("m.plum");
|
|
466
466
|
std::fs::write(
|
|
467
467
|
&path,
|
|
468
|
-
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/game/Vec2\nimport std/game/Affine\nimport std/game/Color\nimport std/game/gfx\nimport std/game/input\nimport std/game/Store\nimport std/game/Game\nimport std/game/scene/Place\nimport std/game/scene/Actor\nimport std/game/scene/Ctx\nimport std/game/scene/SceneTree\nstruct Seeds(mut c: Int)\nstruct Mixed(seeds: Seeds) is Reducer[Int] =\n fun update(self, msg: Int) -> Mixed = self\n fun handle(self, io: Io, msg: Int) =\n self.seeds.c = self.seeds.c ^ io.randomSeed()\nstruct
|
|
468
|
+
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/game/Vec2\nimport std/game/Affine\nimport std/game/Color\nimport std/game/gfx\nimport std/game/input\nimport std/game/Store\nimport std/game/Game\nimport std/game/scene/Place\nimport std/game/scene/Actor\nimport std/game/scene/Ctx\nimport std/game/scene/SceneTree\nimport std/collections/functional/FRoseTree\nstruct Seeds(mut c: Int)\nstruct Mixed(seeds: Seeds) is Reducer[Int] =\n fun update(self, msg: Int) -> Mixed = self\n fun handle(self, io: Io, msg: Int) =\n self.seeds.c = self.seeds.c ^ io.randomSeed()\nstruct PageScreen(id: Int) is Screen[Mixed, Int] =\n fun key(self) -> Int = self.id\n fun place(self) -> Place = place.at(0.0, 0.0)\n fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)\nfun screenOf(top: Actor[Int]) -> SceneTree[Int] =\n kid: FRoseTree[Actor[Int]] := FRoseTree.leaf(top)\n page: FRoseTree[Actor[Int]] := FRoseTree.of(PageScreen(id: -2), [kid])\n SceneTree.ofScreens([page])\nstruct State(a: Int, b: Int, seeds: Seeds) is Actor[Int] =\n fun key(self) -> Int = 1\n fun place(self) -> Place = place.at(0.0, 0.0)\n fun step(self, ctx: Ctx[Int], dt: Float) -> State =\n ctx.send(1)\n self\n fun paint(self, io: Io, m: Affine, alpha: Float) =\n gfx.drawText(io, \"{self.a} {self.b} {self.seeds.c}\", 0.0, 0.0, 8.0, Color.White)\n\nfun main(io: Io) -> Game[Mixed, Int] =\n a := io.randomSeed()\n seeds := Seeds(c: 0)\n Game.start(screenOf(State(a: a, b: io.randomSeed(), seeds: seeds)), Mixed(seeds: seeds))\n",
|
|
469
469
|
)
|
|
470
470
|
.unwrap();
|
|
471
471
|
let wasm = compile(&path);
|
compiler/game/web-test/fixtures/interleave.plum
CHANGED
|
@@ -14,6 +14,7 @@ import std/game/input
|
|
|
14
14
|
import std/game/Affine
|
|
15
15
|
import std/game/Store
|
|
16
16
|
import std/game/Game
|
|
17
|
+
import std/collections/functional/FRoseTree
|
|
17
18
|
import std/game/scene/Place
|
|
18
19
|
import std/game/scene/Actor
|
|
19
20
|
import std/game/scene/Ctx
|
|
@@ -39,18 +40,21 @@ struct Interleave(tex: Array[Texture], t: Int) is Actor[Int] =
|
|
|
39
40
|
fun paint(self, io: Io, m: Affine, alpha: Float) = drawSprites(io, self.tex, self.t)
|
|
40
41
|
|
|
41
42
|
# The one screen, drawn in screen pixels.
|
|
42
|
-
struct
|
|
43
|
+
struct PageScreen(id: Int) is Screen[Quiet, Int] =
|
|
43
|
-
fun
|
|
44
|
+
fun key(self) -> Int = self.id
|
|
44
45
|
|
|
46
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
47
|
+
|
|
45
|
-
fun
|
|
48
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
46
49
|
|
|
47
|
-
|
|
50
|
+
# The game's tree: its one screen over the actor `top`.
|
|
51
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(PageScreen(id: -2), [FRoseTree.leaf(top)])])
|
|
48
52
|
|
|
49
53
|
fun main(io: Io) -> Game[Quiet, Int] =
|
|
50
54
|
tex := Array.filled(Textures, Texture(id: -1))
|
|
51
55
|
for i := range Textures
|
|
52
56
|
tex.set(i, gfx.loadTexture(io, "tex{i}.png"))
|
|
53
|
-
Game.start(
|
|
57
|
+
Game.start(screenOf(Interleave(tex: tex, t: 0)), Quiet(n: 0))
|
|
54
58
|
|
|
55
59
|
fun drawSprites(io: Io, tex: Array[Texture], t: Int) =
|
|
56
60
|
gfx.clear(io, Color.rgb(0.05, 0.06, 0.1))
|
compiler/game/web-test/fixtures/main_game.plum
CHANGED
|
@@ -11,6 +11,7 @@ import std/game/gfx
|
|
|
11
11
|
import std/game/input
|
|
12
12
|
import std/game/Store
|
|
13
13
|
import std/game/Game
|
|
14
|
+
import std/collections/functional/FRoseTree
|
|
14
15
|
import std/game/scene/Place
|
|
15
16
|
import std/game/scene/Actor
|
|
16
17
|
import std/game/scene/Ctx
|
|
@@ -34,12 +35,15 @@ struct Mover(frame: Int) is Actor[Int] =
|
|
|
34
35
|
gfx.drawText(io, "frame {self.frame}", 20.0, 20.0, 16.0, Color.White)
|
|
35
36
|
|
|
36
37
|
# The one screen, drawn in screen pixels.
|
|
37
|
-
struct
|
|
38
|
+
struct OnlyScreen(id: Int) is Screen[Quiet, Int] =
|
|
38
|
-
fun
|
|
39
|
+
fun key(self) -> Int = self.id
|
|
39
40
|
|
|
41
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
42
|
+
|
|
40
|
-
fun
|
|
43
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
41
44
|
|
|
42
|
-
|
|
45
|
+
# The game's tree: its one screen over the actor `top`.
|
|
46
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(OnlyScreen(id: -2), [FRoseTree.leaf(top)])])
|
|
43
47
|
|
|
44
48
|
fun main(io: Io) -> Game[Quiet, Int] =
|
|
45
|
-
Game.start(
|
|
49
|
+
Game.start(screenOf(Mover(frame: 0)), Quiet(n: 0))
|
compiler/game/web-test/fixtures/rendertarget.plum
CHANGED
|
@@ -22,6 +22,7 @@ import std/game/input
|
|
|
22
22
|
import std/game/Affine
|
|
23
23
|
import std/game/Store
|
|
24
24
|
import std/game/Game
|
|
25
|
+
import std/collections/functional/FRoseTree
|
|
25
26
|
import std/game/scene/Place
|
|
26
27
|
import std/game/scene/Actor
|
|
27
28
|
import std/game/scene/Ctx
|
|
@@ -42,16 +43,19 @@ struct Targets(big: Texture, small: Texture, glass: Texture, t: Int) is Actor[In
|
|
|
42
43
|
fun paint(self, io: Io, m: Affine, alpha: Float) = drawTargets(io, self)
|
|
43
44
|
|
|
44
45
|
# The one screen, drawn in screen pixels.
|
|
45
|
-
struct
|
|
46
|
+
struct PageScreen(id: Int) is Screen[Quiet, Int] =
|
|
46
|
-
fun
|
|
47
|
+
fun key(self) -> Int = self.id
|
|
47
48
|
|
|
49
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
50
|
+
|
|
48
|
-
fun
|
|
51
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
49
52
|
|
|
50
|
-
|
|
53
|
+
# The game's tree: its one screen over the actor `top`.
|
|
54
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(PageScreen(id: -2), [FRoseTree.leaf(top)])])
|
|
51
55
|
|
|
52
56
|
fun main(io: Io) -> Game[Quiet, Int] =
|
|
53
57
|
targets := Targets(big: gfx.createTexture(io, 64, 64), small: gfx.createTexture(io, 16, 16), glass: gfx.createTexture(io, 16, 16), t: 0)
|
|
54
|
-
Game.start(
|
|
58
|
+
Game.start(screenOf(targets), Quiet(n: 0))
|
|
55
59
|
|
|
56
60
|
fun drawTargets(io: Io, g: Targets) =
|
|
57
61
|
gfx.setCamera(io, 10.0, 0.0, 2.0)
|
compiler/game/web-test/fixtures/stress.plum
CHANGED
|
@@ -14,6 +14,7 @@ import std/game/input
|
|
|
14
14
|
import std/game/Affine
|
|
15
15
|
import std/game/Store
|
|
16
16
|
import std/game/Game
|
|
17
|
+
import std/collections/functional/FRoseTree
|
|
17
18
|
import std/game/scene/Place
|
|
18
19
|
import std/game/scene/Actor
|
|
19
20
|
import std/game/scene/Ctx
|
|
@@ -41,15 +42,18 @@ struct Stress(sheet: Texture, t: Int) is Actor[Int] =
|
|
|
41
42
|
fun paint(self, io: Io, m: Affine, alpha: Float) = drawStress(io, self.sheet, self.t)
|
|
42
43
|
|
|
43
44
|
# The one screen, drawn in screen pixels.
|
|
44
|
-
struct
|
|
45
|
+
struct PageScreen(id: Int) is Screen[Quiet, Int] =
|
|
45
|
-
fun
|
|
46
|
+
fun key(self) -> Int = self.id
|
|
46
47
|
|
|
48
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
49
|
+
|
|
47
|
-
fun
|
|
50
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
48
51
|
|
|
49
|
-
|
|
52
|
+
# The game's tree: its one screen over the actor `top`.
|
|
53
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(PageScreen(id: -2), [FRoseTree.leaf(top)])])
|
|
50
54
|
|
|
51
55
|
fun main(io: Io) -> Game[Quiet, Int] =
|
|
52
|
-
Game.start(
|
|
56
|
+
Game.start(screenOf(Stress(sheet: gfx.loadTexture(io, "sprites.png"), t: 0)), Quiet(n: 0))
|
|
53
57
|
|
|
54
58
|
fun hue(i: Int) -> Color =
|
|
55
59
|
Color(r: Float(i % 7) / 7.0, g: Float(i % 5) / 5.0, b: 0.4 + Float(i % 3) / 5.0, a: 0.8)
|
compiler/game/web-test/fixtures/stress_scene_port.plum
CHANGED
|
@@ -120,17 +120,21 @@ struct Quiet(n: Int) is Reducer[Int] =
|
|
|
120
120
|
fun update(self, msg: Int) -> Quiet = self
|
|
121
121
|
|
|
122
122
|
# The one screen: the tree, drawn through its camera.
|
|
123
|
-
struct
|
|
123
|
+
struct WorldScreen(id: Int) is Screen[Quiet, Int] =
|
|
124
|
-
fun
|
|
124
|
+
fun key(self) -> Int = self.id
|
|
125
125
|
|
|
126
|
-
fun
|
|
126
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
127
127
|
|
|
128
|
-
fun draw(self, io: Io) =
|
|
128
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) =
|
|
129
129
|
gfx.clear(io, Color.rgb(0.05, 0.06, 0.1))
|
|
130
|
-
|
|
130
|
+
shown.draw(io)
|
|
131
|
+
|
|
132
|
+
# The game's tree: the one screen over `t`'s actors, through `t`'s camera.
|
|
133
|
+
fun screenOf(t: SceneTree[Int]) -> SceneTree[Int] =
|
|
134
|
+
SceneTree.ofScreens([FRoseTree.of(WorldScreen(id: -2), [t.root])]).pan(t.camX, t.camY).zoom(t.camScale)
|
|
131
135
|
|
|
132
136
|
fun main(io: Io) -> Game[Quiet, Int] =
|
|
133
|
-
Game.start(
|
|
137
|
+
Game.start(screenOf(newTree(gfx.loadTexture(io, "sprites.png"))), Quiet(n: 0))
|
|
134
138
|
|
|
135
139
|
test "the tree holds every actor and steps them"
|
|
136
140
|
t := newTree(Texture(id: -1))
|
compiler/game/web-test/fixtures/stress_state_view.plum
CHANGED
|
@@ -113,16 +113,17 @@ struct Pulser(id: Int, x: Float, y: Float, start: Float, srcX: Float, clock: Flo
|
|
|
113
113
|
actor.spritePaint(io, m, alpha, self.sheet, 0.0, 0.0, 16.0, 16.0, self.srcX, 0.0, 16.0, 16.0, Color.White)
|
|
114
114
|
|
|
115
115
|
# The one screen: the tree and its clock.
|
|
116
|
-
struct
|
|
116
|
+
struct ViewScreen(id: Int, clock: Float) is Screen[Clicks, Int] =
|
|
117
|
-
fun
|
|
117
|
+
fun key(self) -> Int = self.id
|
|
118
118
|
|
|
119
|
-
fun
|
|
119
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
120
120
|
|
|
121
|
-
fun rules(self, ctx: Ctx[Int], state: Clicks, dt: Float) ->
|
|
121
|
+
fun rules(self, t: SceneTree[Int], ctx: Ctx[Int], state: Clicks, dt: Float) -> SceneTree[Int] =
|
|
122
|
+
t.withTop(ViewScreen(..self, clock: self.clock + dt))
|
|
122
123
|
|
|
123
|
-
fun draw(self, io: Io) =
|
|
124
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) =
|
|
124
125
|
gfx.clear(io, Color.rgb(0.05, 0.06, 0.1))
|
|
125
|
-
|
|
126
|
+
shown.draw(io)
|
|
126
127
|
|
|
127
128
|
# The state: how many buttons were clicked (each click sends the button's id).
|
|
128
129
|
struct Clicks(n: Int) is Reducer[Int] =
|
|
@@ -152,7 +153,8 @@ fun newSpinner(sheet: Texture, gi: Int) -> FRoseTree[Actor[Int]] =
|
|
|
152
153
|
fun newButton(i: Int) -> FRoseTree[Actor[Int]] =
|
|
153
154
|
FRoseTree.leaf(ui.button(40000 + i, Float(i % 10 * 70 + 10), Float(i / 10 * 30 + 10), 64.0, 24.0, "b{i}", 40000 + i))
|
|
154
155
|
|
|
156
|
+
# The game's tree: the one screen over group 0, which holds the world and the buttons.
|
|
155
|
-
fun newView(sheet: Texture, white: Texture) ->
|
|
157
|
+
fun newView(sheet: Texture, white: Texture) -> SceneTree[Int] =
|
|
156
158
|
rects := rangeLink(0, Rects, |i| newDrifter(white, 10000 + i, hue(i), Float(i * 61 % WorldW), Float(i * 29 % WorldH), 12.0, 8.0, 2.0, 0.0))
|
|
157
159
|
circles := rangeLink(0, Circles, |i| newDrifter(white, 20000 + i, hue(i + 3), Float(i * 83 % WorldW), Float(i * 47 % WorldH), 12.0, 12.0, 3.0, 1.0))
|
|
158
160
|
counters: List[FRoseTree[Actor[Int]]] := rangeLink(0, Texts, |i| FRoseTree.leaf(Counter(id: 30000 + i, x: Float(i * 30 % WorldW), y: Float(i * 17 % WorldH), n: i)))
|
|
@@ -162,7 +164,7 @@ fun newView(sheet: Texture, white: Texture) -> View =
|
|
|
162
164
|
world: FRoseTree[Actor[Int]] := FRoseTree.of(Scaled(id: WorldId, scale: 0.8), worldKids)
|
|
163
165
|
buttons: FRoseTree[Actor[Int]] := FRoseTree.of(Group(id: ButtonLayer, x: 0.0, y: 0.0), rangeLink(0, Buttons, |i| newButton(i)))
|
|
164
166
|
root: FRoseTree[Actor[Int]] := FRoseTree.of(Group(id: 0, x: 0.0, y: 0.0), [world, buttons])
|
|
165
|
-
|
|
167
|
+
SceneTree.ofScreens([FRoseTree.of(ViewScreen(id: -2, clock: 0.0), [root])])
|
|
166
168
|
|
|
167
169
|
# The world layer: everything in it scaled by `scale`.
|
|
168
170
|
struct Scaled(id: Int, scale: Float) is Actor[Int] =
|
|
@@ -191,31 +193,38 @@ fun newGame() -> Game[Clicks, Int] = Game.start(newView(Texture(id: 0), Texture(
|
|
|
191
193
|
fun pointer(x: Float, y: Float, down: Bool, e: InputEvent) -> Input =
|
|
192
194
|
Input(held: Held(mouse: down, keys: Slice[Int]()), events: [e].toSlice(), pos: Vec2(x: x, y: y), wheel: 0.0)
|
|
193
195
|
|
|
194
|
-
fun viewOf(g: Game[Clicks, Int]) ->
|
|
196
|
+
fun viewOf(g: Game[Clicks, Int]) -> ViewScreen = match g.screenTree().root.top()
|
|
197
|
+
Some(a) => match a
|
|
195
|
-
|
|
198
|
+
ViewScreen as v => v
|
|
196
|
-
|
|
199
|
+
_ => ViewScreen(id: 0, clock: -1.0)
|
|
200
|
+
None => ViewScreen(id: 0, clock: -1.0)
|
|
201
|
+
|
|
202
|
+
# Group 0 of a game's tree, with the world and the buttons.
|
|
203
|
+
fun groupOf(t: SceneTree[Int]) -> SceneTree[Int] = t.subtree(0).unwrap()
|
|
204
|
+
|
|
205
|
+
fun treeOf(g: Game[Clicks, Int]) -> SceneTree[Int] = groupOf(g.tree)
|
|
197
206
|
|
|
198
207
|
test "ten frames of step run"
|
|
199
208
|
g := 0.until(10).fold(newGame(), |current, _| current.step(io, Input.create(), 0.016, Screen800))
|
|
200
|
-
s := viewOf(g)
|
|
201
|
-
assert {
|
|
209
|
+
assert {viewOf(g).clock - 0.16}.abs() < 0.000001
|
|
210
|
+
s := treeOf(g)
|
|
202
|
-
assert s.
|
|
211
|
+
assert s.findAs[Counter](30003).unwrap().n == 13
|
|
203
|
-
assert {s.
|
|
212
|
+
assert {s.findAs[Drifter](10000).unwrap().x - 20.0}.abs() < 0.000001
|
|
204
|
-
assert {s.
|
|
213
|
+
assert {s.findAs[Dancer](1000).unwrap().rotation - 0.192}.abs() < 0.000001
|
|
205
214
|
|
|
206
215
|
test "the tree has one actor per entity"
|
|
207
|
-
s := newView(Texture(id: 0), Texture(id: 0))
|
|
216
|
+
s := groupOf(newView(Texture(id: 0), Texture(id: 0)))
|
|
208
|
-
assert s.
|
|
217
|
+
assert s.root.kids().chain().length() == 2
|
|
209
|
-
assert s.
|
|
218
|
+
assert s.root.kids().kids().chain().length() == 50 + 500 + 200 + 50 + 200
|
|
210
|
-
assert s.
|
|
219
|
+
assert s.root.kids().rest().kids().chain().length() == 20
|
|
211
|
-
assert s.
|
|
220
|
+
assert s.count() == 1 + 1 + 50 + 5000 + 500 + 200 + 50 + 200 + 1 + 20
|
|
212
221
|
|
|
213
222
|
test "button click counts and pulsers animate"
|
|
214
223
|
down := newGame().step(io, pointer(20.0, 20.0, true, PointerDown(x: 20.0, y: 20.0)), 0.016, Screen800)
|
|
215
|
-
assert
|
|
224
|
+
assert treeOf(down).findAs[Button[Int]](40000).unwrap().look == 2
|
|
216
225
|
up := down.step(io, pointer(20.0, 20.0, false, PointerUp(x: 20.0, y: 20.0)), 0.016, Screen800)
|
|
217
226
|
assert up.state().n == 1
|
|
218
|
-
assert
|
|
227
|
+
assert treeOf(up).findAs[Button[Int]](40000).unwrap().look == 1
|
|
219
228
|
assert down.state().n == 0
|
|
220
229
|
a := Anim.at(pulseAnim(), 0.5 - 0.0)
|
|
221
230
|
assert a.dx > 14.9 && a.dx < 15.1
|
compiler/game/web-test/fixtures/stress_world.plum
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
# children are sorted by y, and only the sprites in the view are drawn. The tree is a
|
|
4
4
|
# `SceneTree[Int]` whose root is the camera (a `Group` at minus the camera's position);
|
|
5
5
|
# each sprite is an `Spr` actor that walks in its own `step` and paints itself only when
|
|
6
|
-
# it is in the view. It is a `Game` with one screen, `
|
|
6
|
+
# it is in the view. It is a `Game` with one screen, `WorldScreen`, whose `rules` sort the
|
|
7
7
|
# layer and move the camera after the steps; its state never changes.
|
|
8
8
|
module stress_world
|
|
9
9
|
import std/Str
|
|
@@ -55,26 +55,29 @@ struct Spr(id: Int, x: Float, y: Float, srcX: Float, walker: Int, frame: Int, sh
|
|
|
55
55
|
actor.spritePaint(io, m, alpha, self.sheet, 0.0, 0.0, 16.0, 16.0, self.srcX, 0.0, 16.0, 16.0, Color.White)
|
|
56
56
|
|
|
57
57
|
# The one screen: the tree, the frame and the camera.
|
|
58
|
-
struct
|
|
58
|
+
struct WorldScreen(id: Int, frame: Int, camX: Float, camY: Float) is Screen[Quiet, Int] =
|
|
59
|
-
fun
|
|
59
|
+
fun key(self) -> Int = self.id
|
|
60
60
|
|
|
61
|
-
fun
|
|
61
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
62
62
|
|
|
63
|
-
# After the walkers walked: the camera drifts and the layer is sorted by y.
|
|
63
|
+
# After the walkers walked: the camera drifts and the layer (group 0) is sorted by y.
|
|
64
|
-
fun rules(self, ctx: Ctx[Int], state: Quiet, dt: Float) ->
|
|
64
|
+
fun rules(self, tree: SceneTree[Int], ctx: Ctx[Int], state: Quiet, dt: Float) -> SceneTree[Int] =
|
|
65
65
|
frame := self.frame + 1
|
|
66
66
|
t := Float(frame) * 0.01
|
|
67
67
|
camX := 1300.0 + 600.0 * math.sin(t)
|
|
68
68
|
camY := 800.0 + 350.0 * math.cos(t * 0.7)
|
|
69
|
+
layer := tree.root.kids()
|
|
69
|
-
sorted := match
|
|
70
|
+
sorted := match layer
|
|
70
71
|
FRoseNode(v, k, n) => FRoseNode(value: v, firstChild: sortedByY(k), nextSibling: n)
|
|
71
|
-
FRoseEmpty =>
|
|
72
|
+
FRoseEmpty => layer
|
|
73
|
+
top: Actor[Int] := WorldScreen(..self, frame: frame, camX: camX, camY: camY)
|
|
74
|
+
empty: FRoseTree[Actor[Int]] := FRoseEmpty
|
|
75
|
+
world: FRoseTree[Actor[Int]] := FRoseNode(value: top, firstChild: sorted, nextSibling: empty)
|
|
72
|
-
|
|
76
|
+
tree.withRoot(world).updateAs[Group[Int]](0, |g| Group(..g, x: 0.0 - camX, y: 0.0 - camY))
|
|
73
|
-
World(t: moved, frame: frame, camX: camX, camY: camY)
|
|
74
77
|
|
|
75
|
-
fun draw(self, io: Io) =
|
|
78
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) =
|
|
76
79
|
gfx.clear(io, Color.rgb(0.05, 0.06, 0.1))
|
|
77
|
-
|
|
80
|
+
shown.draw(io)
|
|
78
81
|
|
|
79
82
|
# The game sends no messages, so its state never changes.
|
|
80
83
|
struct Quiet(n: Int) is Reducer[Int] =
|
|
@@ -89,9 +92,10 @@ fun spriteAt(sheet: Texture, i: Int) -> FRoseTree[Actor[Int]] =
|
|
|
89
92
|
fun spritesFrom(sheet: Texture, i: Int) -> List[FRoseTree[Actor[Int]]] =
|
|
90
93
|
if i >= Sprites then Nil else Cons(head: spriteAt(sheet, i), tail: spritesFrom(sheet, i + 1))
|
|
91
94
|
|
|
95
|
+
# The game's tree: the one screen over the layer (group 0) of sprites.
|
|
92
|
-
fun newWorld(sheet: Texture) ->
|
|
96
|
+
fun newWorld(sheet: Texture) -> SceneTree[Int] =
|
|
93
|
-
|
|
97
|
+
layer: FRoseTree[Actor[Int]] := FRoseTree.of(Group(id: 0, x: 0.0, y: 0.0), spritesFrom(sheet, 0))
|
|
94
|
-
|
|
98
|
+
SceneTree.ofScreens([FRoseTree.of(WorldScreen(id: -2, frame: 0, camX: 0.0, camY: 0.0), [layer])])
|
|
95
99
|
|
|
96
100
|
fun yOf(n: FRoseTree[Actor[Int]]) -> Float = match n
|
|
97
101
|
FRoseNode(a, _, _) => a.place().y
|
|
@@ -101,7 +105,7 @@ fun yOf(n: FRoseTree[Actor[Int]]) -> Float = match n
|
|
|
101
105
|
fun sortedByY(t: FRoseTree[Actor[Int]]) -> FRoseTree[Actor[Int]] =
|
|
102
106
|
FRoseTree.chainOf(List.fromSlice(t.chain().toSlice().sort(|a, b| yOf(a) < yOf(b))))
|
|
103
107
|
|
|
104
|
-
fun inView(s:
|
|
108
|
+
fun inView(s: WorldScreen, a: Actor[Int]) -> Bool =
|
|
105
109
|
p := a.place()
|
|
106
110
|
p.x + 16.0 >= s.camX && p.x <= s.camX + ViewW && p.y + 16.0 >= s.camY && p.y <= s.camY + ViewH
|
|
107
111
|
|
|
@@ -113,13 +117,16 @@ fun main(io: Io) -> Game[Quiet, Int] =
|
|
|
113
117
|
test "about 2000 of the 20000 sprites are in view, sorted by y"
|
|
114
118
|
g0: Game[Quiet, Int] := Game.start(newWorld(Texture(id: -1)), Quiet(n: 0))
|
|
115
119
|
g := g0.step(io, Input.create(), 1.0 / 60.0, Vec2(x: 1280.0, y: 720.0))
|
|
120
|
+
tr := g.screenTree()
|
|
116
|
-
s := match
|
|
121
|
+
s := match tr.root.top()
|
|
122
|
+
Some(a) => match a
|
|
117
|
-
|
|
123
|
+
WorldScreen as w => w
|
|
118
|
-
|
|
124
|
+
_ => WorldScreen(id: 0, frame: -1, camX: 0.0, camY: 0.0)
|
|
125
|
+
None => WorldScreen(id: 0, frame: -1, camX: 0.0, camY: 0.0)
|
|
119
126
|
assert s.frame == 1
|
|
120
|
-
kids :=
|
|
127
|
+
kids := tr.root.kids().kids().chain().toSlice()
|
|
121
128
|
assert kids.length() == 20000
|
|
122
129
|
for i := range 19999
|
|
123
130
|
assert yOf(kids.at(i)) <= yOf(kids.at(i + 1))
|
|
124
|
-
shown :=
|
|
131
|
+
shown := tr.root.kids().kids().fold(0, |n, a| if inView(s, a) then n + 1 else n)
|
|
125
132
|
assert shown > 1500 && shown < 2600
|
compiler/game/web-test/fixtures/text_sizes.plum
CHANGED
|
@@ -13,6 +13,7 @@ import std/game/input
|
|
|
13
13
|
import std/game/Affine
|
|
14
14
|
import std/game/Store
|
|
15
15
|
import std/game/Game
|
|
16
|
+
import std/collections/functional/FRoseTree
|
|
16
17
|
import std/game/scene/Place
|
|
17
18
|
import std/game/scene/Actor
|
|
18
19
|
import std/game/scene/Ctx
|
|
@@ -33,15 +34,18 @@ struct Sizes(t: Int) is Actor[Int] =
|
|
|
33
34
|
fun paint(self, io: Io, m: Affine, alpha: Float) = drawSizes(io, self.t)
|
|
34
35
|
|
|
35
36
|
# The one screen, drawn in screen pixels.
|
|
36
|
-
struct
|
|
37
|
+
struct PageScreen(id: Int) is Screen[Quiet, Int] =
|
|
37
|
-
fun
|
|
38
|
+
fun key(self) -> Int = self.id
|
|
38
39
|
|
|
40
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
41
|
+
|
|
39
|
-
fun
|
|
42
|
+
fun draw(self, io: Io, shown: SceneTree[Int]) = shown.paint(io)
|
|
40
43
|
|
|
41
|
-
|
|
44
|
+
# The game's tree: its one screen over the actor `top`.
|
|
45
|
+
fun screenOf(top: Actor[Int]) -> SceneTree[Int] = SceneTree.ofScreens([FRoseTree.of(PageScreen(id: -2), [FRoseTree.leaf(top)])])
|
|
42
46
|
|
|
43
47
|
fun main(io: Io) -> Game[Quiet, Int] =
|
|
44
|
-
Game.start(
|
|
48
|
+
Game.start(screenOf(Sizes(t: 0)), Quiet(n: 0))
|
|
45
49
|
|
|
46
50
|
fun drawSizes(io: Io, t: Int) =
|
|
47
51
|
gfx.clear(io, Color.rgb(0.08, 0.09, 0.14))
|
docs/game.md
CHANGED
|
@@ -14,11 +14,11 @@ This builds the game, starts a local server and opens your browser. Press Enter
|
|
|
14
14
|
|
|
15
15
|
## A minimal game
|
|
16
16
|
|
|
17
|
-
There is one way to make a game: `main` returns a `Game[S, M]` ([`std/game/Game.plum`](../std/game/Game.plum)), made by `Game.start(
|
|
17
|
+
There is one way to make a game: `main` returns a `Game[S, M]` ([`std/game/Game.plum`](../std/game/Game.plum)), made by `Game.start(tree, state)`. A game is three things you write, and `Game` runs the loop:
|
|
18
18
|
|
|
19
19
|
- **Actors.** Each thing on the screen is its own struct that `is Actor[M]` ([Scenes](#scenes)), with its own `step` (its next version, every tick), `paint`, and `input` (a pointer event on it).
|
|
20
|
-
- **Screens.**
|
|
21
|
-
- **A reducer.** The game's state `S` is a `Reducer[M]` ([`std/game/Store.plum`](../std/game/Store.plum)). `update(self, msg)` is the rules of the state: pure, with no `io`. `handle(self, io, msg)` is the effects of a message (a sound, a save), run after the tick. `M` is the game's message type: actors and rules send messages with `ctx.send(msg)`, and a button sends its `onClick`.
|
|
20
|
+
- **Screens.** The game's tree of actors has a root whose children are the screens (a title, the play, a game over). A screen is a struct that `is Screen[S, M]`, an actor too, and its subtree is what it shows. Only one screen is shown at a time, the one the state names. A screen's `rules` run after the actors stepped (collisions, keys), its `react` shows a new state after each message, its `enter` sets it up when the state switches to it, its `transition` says how it comes in, and its `draw` draws the frame ([Screens and transitions](#screens-and-transitions)). By convention a screen's struct name ends in `Screen`.
|
|
21
|
+
- **A reducer.** The game's state `S` is a `Reducer[M]` ([`std/game/Store.plum`](../std/game/Store.plum)). `update(self, msg)` is the rules of the state: pure, with no `io`. `handle(self, io, msg)` is the effects of a message (a sound, a save), run after the tick. `screen(self)` names the screen to show. `M` is the game's message type: actors and rules send messages with `ctx.send(msg)`, and a button sends its `onClick`.
|
|
22
22
|
|
|
23
23
|
```plum
|
|
24
24
|
import std/Int
|
|
@@ -60,51 +60,123 @@ struct Ball(id: Int, x: Float, y: Float) is Actor[Msg] =
|
|
|
60
60
|
fun paint(self, io: Io, m: Affine, alpha: Float) =
|
|
61
61
|
actor.circlePaint(io, m, alpha, 0.0, 0.0, 10.0, Color.Yellow)
|
|
62
62
|
|
|
63
|
-
# The one screen
|
|
63
|
+
# The one screen, the root of the ball, a button that sends `Clicked`, and a label with
|
|
64
|
+
# the count. Its name is its type's, `RoomScreen.name()`; a game of one screen needs no
|
|
65
|
+
# `screen()` in its state.
|
|
64
|
-
struct
|
|
66
|
+
struct RoomScreen(id: Int) is Screen[Clicks, Msg] =
|
|
65
|
-
fun
|
|
67
|
+
fun key(self) -> Int = self.id
|
|
66
68
|
|
|
67
|
-
fun
|
|
69
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
68
70
|
|
|
69
71
|
# Escape clears the count.
|
|
70
|
-
fun rules(self, ctx: Ctx[Msg], state: Clicks, dt: Float) ->
|
|
72
|
+
fun rules(self, t: SceneTree[Msg], ctx: Ctx[Msg], state: Clicks, dt: Float) -> SceneTree[Msg] =
|
|
71
73
|
if ctx.input.isKeyPressed(Key.Escape)
|
|
72
74
|
ctx.send(Cleared)
|
|
73
|
-
|
|
75
|
+
t
|
|
74
76
|
|
|
75
77
|
# After each message: the label shows the new count.
|
|
76
|
-
fun react(self, state: Clicks, msg: Msg) ->
|
|
78
|
+
fun react(self, t: SceneTree[Msg], state: Clicks, msg: Msg) -> SceneTree[Msg] =
|
|
77
|
-
|
|
79
|
+
t.updateAs[Label[Msg]](3, |l| l.relabel("clicks {state.n}"))
|
|
78
80
|
|
|
79
|
-
fun draw(self, io: Io) =
|
|
81
|
+
fun draw(self, io: Io, shown: SceneTree[Msg]) =
|
|
80
82
|
gfx.clear(io, Color.Black)
|
|
81
|
-
|
|
83
|
+
shown.draw(io)
|
|
82
84
|
|
|
83
85
|
fun main(io: Io) -> Game[Clicks, Msg] = start()
|
|
84
86
|
|
|
85
87
|
fun start() -> Game[Clicks, Msg] =
|
|
86
|
-
|
|
88
|
+
room: FRoseTree[Actor[Msg]] := FRoseTree.of(RoomScreen(id: 0), [
|
|
87
89
|
FRoseTree.leaf(Ball(id: 1, x: 20.0, y: 150.0)),
|
|
88
90
|
FRoseTree.leaf(ui.button(2, 20.0, 20.0, 120.0, 40.0, "Click", Clicked)),
|
|
89
91
|
FRoseTree.leaf(Label(id: 3, x: 20.0, y: 80.0, text: "clicks 0", size: 16.0, color: Color.White))])
|
|
90
|
-
Game.start(
|
|
92
|
+
Game.start(SceneTree.ofScreens([room]), Clicks(n: 0))
|
|
91
93
|
```
|
|
92
94
|
|
|
93
|
-
This is [`examples/games/clicks.plum`](../examples/games/clicks.plum). `main` runs once, after your assets have loaded. If `main` returns anything else, `plum run` runs it as an ordinary program. A type of your own with `update` and `draw` was the old way to make a game; `main` returning one is now a compile error that names `Game.start`.
|
|
95
|
+
This is [`examples/games/clicks.plum`](../examples/games/clicks.plum), a game of one screen: `SceneTree.ofScreens(screens)` makes a tree whose root (a `Group` with id -1) has the screens as its children, and a state that doesn't say `screen()` shows the first one. `main` runs once, after your assets have loaded. If `main` returns anything else, `plum run` runs it as an ordinary program. A type of your own with `update` and `draw` was the old way to make a game; `main` returning one is now a compile error that names `Game.start`.
|
|
94
96
|
|
|
95
97
|
### One tick
|
|
96
98
|
|
|
97
99
|
Each tick, `Game.update(io, input, dt)` does this, in this order:
|
|
98
100
|
|
|
99
|
-
1. Each pointer event of the tick goes to the topmost shown actor under it that says it is hit (`hit`), through its `input`, then to each of its parents. Each gets the event in its own coordinates. A `PointerMove` that leaves the pointer where it was goes to no actor.
|
|
100
|
-
2.
|
|
101
|
-
3. The spawns and kills apply (`ctx.out.push(Spawn(parent, node))`, `ctx.out.push(Kill(id))`): those pushed by `input`, then by the steps, then by the rules.
|
|
102
|
-
4. Each message sent, in order: the store reduces it (`update`), then the screen `react`s to the new state
|
|
103
|
-
5. After the tick: `handle(io, msg)` runs for each message, in order, with the state after the tick. Then
|
|
101
|
+
1. Each pointer event of the tick goes to the topmost shown actor of the current screen under it that says it is hit (`hit`), through its `input`, then to each of its parents up to the root. Each gets the event in its own coordinates. A `PointerMove` that leaves the pointer where it was goes to no actor.
|
|
102
|
+
2. The root and the current screen's actors step (`step`), parents before children, then the screen's `rules` run. Both see the tick's input in `ctx.input` (held keys, the pointer) and the screen's size in `ctx.view`.
|
|
103
|
+
3. The spawns and kills apply to the root and the current screen (`ctx.out.push(Spawn(parent, node))`, `ctx.out.push(Kill(id))`): those pushed by `input`, then by the steps, then by the rules.
|
|
104
|
+
4. Each message sent, in order: the store reduces it (`update`), then the current screen `react`s to the new state.
|
|
105
|
+
5. After the tick: `handle(io, msg)` runs for each message, in order, with the state after the tick. Then, if `state.screen()` names another screen, that one `enter`s and becomes current, coming in by its `transition`. Then `ctx.out.push(Restart)` starts the game over from its first tree and state, and `ctx.out.push(Exit)` ends it: no tick runs after it, and the last frame stays drawn.
|
|
106
|
+
|
|
107
|
+
`Game.draw(io)` draws a frame through the current screen's `draw`. Nothing is changed in place: each tick returns the next game.
|
|
108
|
+
|
|
109
|
+
### Screens and transitions
|
|
110
|
+
|
|
111
|
+
The screens are the children of the tree's root, and each is a `Screen[S, M]` ([`std/game/Game.plum`](../std/game/Game.plum)). A screen's name is its type's: `PlayScreen.name()` is `"PlayScreen"` (the trait's static `name()` defaults to `@typeName(Self)`, see [Compile-time built-ins](language.md#compile-time-built-ins)), so game code never writes it as a string. The state keeps the name of the screen to show and its reducer sets it: `Start => State(..self, on: PlayScreen.name())`, with `fun screen(self) -> Str = self.on`. After a tick whose messages changed it, `Game` switches to that screen.
|
|
112
|
+
|
|
113
|
+
Each method gets `t`, the screen's own subtree (its root is the screen), and returns it changed:
|
|
114
|
+
|
|
115
|
+
- `rules(self, t, ctx, state, dt)`: after the actors stepped (collisions, keys); acts through `ctx`.
|
|
116
|
+
- `react(self, t, state, msg)`: after each message was reduced, while the screen is current: show the new state (`t.updateAs[Label[Msg]](ScoreId, ..)`).
|
|
117
|
+
- `enter(self, t, state, tree)`: when the state switches to it. `tree` is the whole tree, so a screen can start from what another shows (`tree.subtree(PlayScreenId)`). By default the screen resumes as it was left.
|
|
118
|
+
- `transition(self, from)`: how it comes in from the screen named `from`: `Cut` (the default), `Fade(secs)`, or `Slide(from: FromRight, secs)` (also `FromLeft`, `FromTop`, `FromBottom`). The screen coming in chooses, since how it appears belongs with it, and the state stays only the game's rules.
|
|
119
|
+
- `draw(self, io, shown)`: draws the frame while it is current; `shown` is the root with what is shown (by default `shown.draw(io)`, through the tree's camera).
|
|
120
|
+
|
|
121
|
+
Only the current screen is shown, gets input and steps. The others are paused and kept: the tick starts from the root and the current screen, and never walks into a hidden screen's subtree, so a big hidden screen costs nothing per tick (2000 ticks with a hidden screen of 20000 actors take as long as with none). Coming back to a screen resumes it where it was. Its `rules`, `react`, `findAs`, `updateAs`, spawns, kills and pointer events reach only its own subtree, so an id needs to be unique only within its screen.
|
|
122
|
+
|
|
123
|
+
During a transition other than `Cut`, both screens are drawn, the one going out fading or sliding out and the one coming in fading or sliding in over `secs` seconds of ticks. Only the one coming in gets input and steps, and reacts to messages; the one going out is frozen. `Game` animates them with a parent of each screen that has an offset and an opacity, so the screens' actors see it in their `ctx` too (and a sliding screen is hit where it is drawn). A transition that starts on a tick is first drawn with the old screen fully shown; `game.shift` is the transition in progress.
|
|
124
|
+
|
|
125
|
+
```plum
|
|
126
|
+
enum Msg = Start | Back
|
|
127
|
+
|
|
128
|
+
# The state names the screen shown, by the screen's own name.
|
|
129
|
+
struct State(on: Str, starts: Int) is Reducer[Msg] =
|
|
130
|
+
fun update(self, msg: Msg) -> State = match msg
|
|
131
|
+
Start => State(..self, on: PlayScreen.name(), starts: self.starts + 1)
|
|
132
|
+
Back => State(..self, on: TitleScreen.name())
|
|
133
|
+
|
|
134
|
+
fun screen(self) -> Str = self.on
|
|
135
|
+
|
|
136
|
+
# The title: Enter starts.
|
|
137
|
+
struct TitleScreen(id: Int) is Screen[State, Msg] =
|
|
138
|
+
fun key(self) -> Int = self.id
|
|
139
|
+
|
|
140
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
141
|
+
|
|
142
|
+
fun rules(self, t: SceneTree[Msg], ctx: Ctx[Msg], state: State, dt: Float) -> SceneTree[Msg] =
|
|
143
|
+
if ctx.input.isKeyPressed(Key.Enter)
|
|
144
|
+
ctx.send(Start)
|
|
145
|
+
t
|
|
146
|
+
|
|
147
|
+
# The play: it fades in over the title in half a second, and Escape goes back to the
|
|
148
|
+
# title, which resumes as it was left.
|
|
149
|
+
struct PlayScreen(id: Int) is Screen[State, Msg] =
|
|
150
|
+
fun key(self) -> Int = self.id
|
|
151
|
+
|
|
152
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
153
|
+
|
|
154
|
+
fun transition(self, from: Str) -> Transition = Fade(secs: 0.5)
|
|
155
|
+
|
|
156
|
+
fun rules(self, t: SceneTree[Msg], ctx: Ctx[Msg], state: State, dt: Float) -> SceneTree[Msg] =
|
|
157
|
+
if ctx.input.isKeyPressed(Key.Escape)
|
|
158
|
+
ctx.send(Back)
|
|
159
|
+
t
|
|
160
|
+
|
|
161
|
+
# Each time the state switches to it: the label shows how many runs there were. (The
|
|
162
|
+
# `Start` that switched came while the title was current, so the title reacted to it.)
|
|
163
|
+
fun enter(self, t: SceneTree[Msg], state: State, tree: SceneTree[Msg]) -> SceneTree[Msg] =
|
|
164
|
+
t.updateAs[Label[Msg]](1, |l| l.relabel("run {state.starts}"))
|
|
165
|
+
|
|
166
|
+
fun draw(self, io: Io, shown: SceneTree[Msg]) =
|
|
167
|
+
gfx.clear(io, Color.Black)
|
|
168
|
+
shown.draw(io)
|
|
169
|
+
|
|
170
|
+
fun main(io: Io) -> Game[State, Msg] =
|
|
171
|
+
tree: SceneTree[Msg] := SceneTree.ofScreens([
|
|
172
|
+
FRoseTree.of(TitleScreen(id: 10), [FRoseTree.leaf(Label(id: 1, x: 20.0, y: 20.0, text: "Press Enter", size: 24.0, color: Color.White))]),
|
|
173
|
+
FRoseTree.of(PlayScreen(id: 20), [FRoseTree.leaf(Label(id: 1, x: 20.0, y: 20.0, text: "run 0", size: 24.0, color: Color.Yellow))])])
|
|
174
|
+
Game.start(tree, State(on: TitleScreen.name(), starts: 0))
|
|
175
|
+
```
|
|
104
176
|
|
|
105
|
-
`
|
|
177
|
+
This is [`examples/games/two_screens.plum`](../examples/games/two_screens.plum). `Game.start` stops the program if a child of the root is not a screen, if two screens have one name, or if no screen has the state's. A name the state asks for later that no screen has keeps the current screen and is reported once on the host's output (`game.unknown` holds it). `Restart` goes back to the tree and the state `Game.start` was given, with no transition.
|
|
106
178
|
|
|
107
|
-
[`orb_run.plum`](../examples/games/orb_run.plum) is a whole game this way, with a title, a play and a game-over screen; its header comment explains how it is put together.
|
|
179
|
+
[`orb_run.plum`](../examples/games/orb_run.plum) is a whole game this way, with a title, a play and a game-over screen, the play fading in over the title; its header comment explains how it is put together.
|
|
108
180
|
|
|
109
181
|
## Fixed timestep and determinism
|
|
110
182
|
|
|
@@ -174,7 +246,7 @@ If the new code has a compile error, the old game keeps running and the error ap
|
|
|
174
246
|
|
|
175
247
|
## Scenes
|
|
176
248
|
|
|
177
|
-
A
|
|
249
|
+
A game's actors live in a scene tree from [`std/game/scene`](../std/game/scene): a `SceneTree[M]` ([`SceneTree.plum`](../std/game/scene/SceneTree.plum)) is a tree of actors that lives from frame to frame, and `M` is the game's message type. It is a value like the rest of your game: every change returns a new tree, and a kept old tree stays as it was. When nothing holds the old one, the compiler writes the new one into its cells.
|
|
178
250
|
|
|
179
251
|
Each kind of actor is its own struct, with only its own fields, that `is Actor[M]` ([`Actor.plum`](../std/game/scene/Actor.plum)). It must say which actor it is (`key`, the id the tree finds it by) and where it is in its parent (`place`, a `Place` with a position, scale, rotation and origin). Everything else has a default that it may override:
|
|
180
252
|
|
|
@@ -186,7 +258,7 @@ Each kind of actor is its own struct, with only its own fields, that `is Actor[M
|
|
|
186
258
|
|
|
187
259
|
Keep an actor's position in plain fields (`x: Float, y: Float`) and build its `Place` in `place`, as `place.at(self.x, self.y)`: a `Place` field costs a step about 1.7 times as much. std gives `Group` (a parent that paints nothing) and `Box`; [`std/game/ui.plum`](../std/game/ui.plum) gives `Label` and `Button` (`ui.button(id, x, y, w, h, label, onClick)`). A button shows the pointer over it and a press on it, and a press and a release on it send its `onClick` message.
|
|
188
260
|
|
|
189
|
-
The tree's `step(ctx, dt)` steps every actor, parents before children, then applies the spawns and kills; `input(ctx, e)` routes one pointer event. `Game` calls both. `add`, `update`, `kill` and `find` work by id, and `updateAs[T](id, f)` and `findAs[T](id)` act on an actor of the type `T` only, which is how a screen's `rules` and `react` read and change one kind's own fields. A pattern `Orb as o` on an `Actor[M]` matches an `Orb`.
|
|
261
|
+
The tree's `step(ctx, dt)` steps every actor, parents before children, then applies the spawns and kills; `input(ctx, e)` routes one pointer event. `Game` calls both. `add`, `update`, `kill` and `find` work by id, and `updateAs[T](id, f)` and `findAs[T](id)` act on an actor of the type `T` only, which is how a screen's `rules` and `react` read and change one kind's own fields in its subtree. `subtree(id)` is an actor with its subtree as a tree of its own, and `withTop(a)` a tree with its root actor replaced. A pattern `Orb as o` on an `Actor[M]` matches an `Orb`.
|
|
190
262
|
|
|
191
263
|
[`std/game/Anim.plum`](../std/game/Anim.plum) adds animation by time.
|
|
192
264
|
|
docs/plans/2026-11-09-actor-trait.md
CHANGED
|
@@ -254,105 +254,111 @@ tree). After this work:
|
|
|
254
254
|
## One way to make a game
|
|
255
255
|
|
|
256
256
|
The user's rule: there is one way to make a game. Every game is a `Game` over a `Store`,
|
|
257
|
-
its actors are `Actor[M]` and
|
|
257
|
+
its actors are `Actor[M]`, and its screens live in one `SceneTree[M]`: the root's children
|
|
258
|
+
are the screens, and only the current one is shown, which makes transitions between them
|
|
258
|
-
|
|
259
|
+
possible. `Game` runs the loop, sends every message to the store and switches screens; a
|
|
259
|
-
|
|
260
|
+
game writes actors, screens and a reducer, never the loop.
|
|
260
261
|
|
|
261
262
|
This brings over master's std/game (`Game(scene, state)`, `Scene(stage)`, `Stage`,
|
|
262
263
|
`State.reduce` with `Actions`, `Restart`, `Exit`, `GoTo`) on traits instead of struct
|
|
263
264
|
embedding and `&self` mutation, which the branch doesn't have. The branch already has the
|
|
264
|
-
fixed-tick `Clock` (std/game/input) and record and replay (std/game/Replay).
|
|
265
|
+
fixed-tick `Clock` (std/game/input) and record and replay (std/game/Replay).
|
|
265
|
-
screen is a `Screen` and the actor tree is a `SceneTree` (strictly a tree), so "scene"
|
|
266
|
-
alone names neither.
|
|
267
266
|
|
|
268
267
|
| master | branch |
|
|
269
268
|
|---|---|
|
|
270
|
-
| `Stage` (actor tree) | `SceneTree[M]` on `FRoseTree[Actor[M]]` |
|
|
269
|
+
| `Stage` (actor tree) | `SceneTree[M]` on `FRoseTree[Actor[M]]`, one per game |
|
|
271
270
|
| `Actor` (base struct) | `trait Actor[M]` |
|
|
272
|
-
| `Scene` (one screen, subclassed) | `trait Screen[S, M]` |
|
|
271
|
+
| `Scene` (one screen, subclassed) | `trait Screen[S, M] is Actor[M]`: a child of the root |
|
|
273
272
|
| `State` + `reduce` + `Actions` | `Reducer[M]` + `Store[S, M]`, driven by `Game` only |
|
|
274
|
-
| `Restart`, `Exit`, `GoTo(scene)` | `Cmd`s `Restart` and `Exit`; `
|
|
273
|
+
| `Restart`, `Exit`, `GoTo(scene)` | `Cmd`s `Restart` and `Exit`; the state's `screen()` for a switch |
|
|
275
|
-
| `Game(scene, state)` | `Game[S, M]`, made only by `Game.start(
|
|
274
|
+
| `Game(scene, state)` | `Game[S, M]`, made only by `Game.start(tree, state)` |
|
|
276
275
|
|
|
277
276
|
```plum
|
|
278
|
-
# std/game/scene
|
|
279
|
-
trait Actor[M] =
|
|
280
|
-
fun key(self) -> Int
|
|
281
|
-
fun place(self) -> Place
|
|
282
|
-
fun step(self, ctx: Ctx[M], dt: Float) -> Self = self # every tick
|
|
283
|
-
fun input(self, ctx: Ctx[M], e: InputEvent) -> Self = self # a pointer event on it
|
|
284
|
-
fun paint(self, io: Io, m: Affine, alpha: Float) = ()
|
|
285
|
-
fun hit(self, p: Vec2) -> Bool = false
|
|
286
|
-
fun shown(self) -> Bool = true
|
|
287
|
-
fun gone(self) -> Bool = false
|
|
288
|
-
fun opacity(self) -> Float = 1.0
|
|
289
|
-
|
|
290
|
-
enum Cmd[M] =
|
|
291
|
-
| Spawn(parent: Int, node: FRoseTree[Actor[M]])
|
|
292
|
-
| Kill(id: Int)
|
|
293
|
-
| Send(msg: M) # to the store
|
|
294
|
-
| Restart # the game's first screen and state again
|
|
295
|
-
| Exit
|
|
296
|
-
|
|
297
|
-
# The parent's transform and opacity, the tick's input (held keys, pointer) and the
|
|
298
|
-
# commands. `ctx.send(m)` is `ctx.out.push(Send(msg: m))`.
|
|
299
|
-
struct Ctx[M](up: Affine, alpha: Float, input: Input, out: Slice[Cmd[M]])
|
|
300
|
-
|
|
301
|
-
struct SceneTree[M](root: FRoseTree[Actor[M]], camX: Float, camY: Float, camScale: Float)
|
|
302
|
-
|
|
303
|
-
# std actors are generic in M so any game's tree takes them.
|
|
304
|
-
struct Group[M](id: Int, at: Place) is Actor[M]
|
|
305
|
-
struct Box[M](id: Int, at: Place, w: Float, h: Float, color: Color) is Actor[M]
|
|
306
|
-
struct Button[M](id: Int, at: Place, label: Str, onClick: M) is Actor[M] # std/game/ui
|
|
307
|
-
|
|
308
277
|
# std/game/Store.plum
|
|
309
278
|
trait Reducer[M] =
|
|
310
279
|
fun update(self, msg: M) -> Self # the rules: pure, no Io
|
|
311
|
-
fun handle(self, io: Io, msg: M) =
|
|
280
|
+
fun handle(self, io: Io, msg: M) = Unit # effects (sound, save), after the tick
|
|
281
|
+
fun screen(self) -> Str = "" # the screen to show; "" is the first
|
|
312
282
|
|
|
313
283
|
# std/game/Game.plum
|
|
284
|
+
enum Transition = Cut | Fade(secs: Float) | Slide(from: Edge, secs: Float)
|
|
285
|
+
enum Edge = FromLeft | FromRight | FromTop | FromBottom
|
|
286
|
+
|
|
314
|
-
trait Screen[S, M] =
|
|
287
|
+
trait Screen[S, M] is Actor[M] =
|
|
315
|
-
fun
|
|
288
|
+
fun name() -> Str = @typeName(Self) # PlayScreen.name() is "PlayScreen"
|
|
316
|
-
fun
|
|
289
|
+
fun screenName(self) -> Str = Self.name() # the name of one held as a Screen
|
|
317
|
-
#
|
|
290
|
+
# Each gets `t`, the screen's own subtree (its root is the screen), and returns it.
|
|
318
|
-
fun rules(self, ctx: Ctx[M], state: S, dt: Float) ->
|
|
291
|
+
fun rules(self, t: SceneTree[M], ctx: Ctx[M], state: S, dt: Float) -> SceneTree[M] = t
|
|
319
|
-
# After each message is reduced: show the new state (a HUD label).
|
|
320
|
-
fun react(self, state: S, msg: M) ->
|
|
292
|
+
fun react(self, t: SceneTree[M], state: S, msg: M) -> SceneTree[M] = t
|
|
321
|
-
# The screen to switch to after this message, if any (title to play to game over).
|
|
322
|
-
fun
|
|
293
|
+
fun enter(self, t: SceneTree[M], state: S, tree: SceneTree[M]) -> SceneTree[M] = t
|
|
294
|
+
fun transition(self, from: Str) -> Transition = Cut
|
|
323
|
-
fun draw(self, io: Io) =
|
|
295
|
+
fun draw(self, io: Io, shown: SceneTree[M]) = shown.draw(io)
|
|
324
296
|
|
|
325
297
|
struct Game[S: Reducer[M], M] =
|
|
326
|
-
fun start(
|
|
298
|
+
fun start(tree: SceneTree[M], state: S) -> Game[S, M] # the only way to make one
|
|
299
|
+
fun state(self) -> S
|
|
300
|
+
fun screenTree(self) -> SceneTree[M] # the current screen's subtree
|
|
301
|
+
fun shown(self) -> SceneTree[M] # what draw draws
|
|
302
|
+
fun step(self, io: Io, input: Input, dt: Float, view: Vec2) -> Game[S, M]
|
|
303
|
+
|
|
304
|
+
# std/game/scene/SceneTree.plum
|
|
305
|
+
fun ofScreens(screens: List[FRoseTree[Actor[M]]]) -> SceneTree[M] # a root group over them
|
|
306
|
+
fun subtree(self, id: Int) -> Option[SceneTree[M]]
|
|
307
|
+
fun withTop(self, a: Actor[M]) -> SceneTree[M]
|
|
327
308
|
```
|
|
328
309
|
|
|
329
310
|
A game:
|
|
330
311
|
|
|
331
312
|
```plum
|
|
332
|
-
enum Msg = Collected(id: Int) | Hurt | StartPressed
|
|
333
|
-
|
|
334
|
-
struct
|
|
313
|
+
struct State(on: Str, score: Int, rng: Random) is Reducer[Msg] =
|
|
314
|
+
fun update(self, msg: Msg) -> State = match msg
|
|
315
|
+
Start => State(..self, on: PlayScreen.name())
|
|
316
|
+
...
|
|
317
|
+
fun screen(self) -> Str = self.on
|
|
318
|
+
|
|
335
|
-
struct
|
|
319
|
+
struct TitleScreen(id: Int) is Screen[State, Msg] = ... # rules: Enter sends Start
|
|
336
|
-
struct
|
|
320
|
+
struct PlayScreen(id: Int) is Screen[State, Msg] = # enter: a fresh run
|
|
337
|
-
|
|
321
|
+
fun transition(self, from: Str) -> Transition = if from == TitleScreen.name() then Fade(secs: 0.5) else Cut
|
|
338
|
-
|
|
322
|
+
...
|
|
323
|
+
|
|
339
|
-
fun main(io: Io) -> Game[
|
|
324
|
+
fun main(io: Io) -> Game[State, Msg] =
|
|
325
|
+
Game.start(SceneTree.ofScreens([FRoseTree.of(TitleScreen(id: 1), [..]), FRoseTree.leaf(PlayScreen(id: 2))]),
|
|
340
|
-
|
|
326
|
+
State.create(io.randomSeed()))
|
|
341
327
|
```
|
|
342
328
|
|
|
329
|
+
Decisions:
|
|
330
|
+
|
|
331
|
+
- **The state picks the screen**, by name: `screen()` returns the current screen's name,
|
|
332
|
+
and a reducer sets it (`on: PlayScreen.name()`). The name is the screen type's own
|
|
333
|
+
(`@typeName(Self)`), so game code never writes it as a string. `Game.start` stops the
|
|
334
|
+
program if a child of the root isn't a `Screen[S, M]`, if two screens share a name, or
|
|
335
|
+
if no screen has the initial state's; a later unknown name keeps the current screen and
|
|
336
|
+
is reported once (a line on the host's output, and `game.unknown`).
|
|
337
|
+
- **A screen's logic is on the screen node.** `Game` finds the current screen among the
|
|
338
|
+
root's children and calls its `rules`, `react`, `enter` and `draw` through a downcast to
|
|
339
|
+
the trait (`Screen[S, M] as s` on an `Actor[M]`, a ref.test over the implementers).
|
|
340
|
+
- **Hidden screens are paused and kept.** The tick walks the root and the current screen
|
|
341
|
+
only, so a hidden screen costs nothing and resumes where it was. A screen's `rules`,
|
|
342
|
+
`react`, spawns, kills and pointer events reach only its own subtree: ids are per screen.
|
|
343
|
+
`Restart` goes back to the tree and state `Game.start` got.
|
|
344
|
+
- **Transitions** are chosen by the screen coming in (`transition(from)`): how a screen
|
|
345
|
+
appears belongs with it, and the state stays the game's rules only. During one, both
|
|
346
|
+
screens are drawn, each under a `ShiftFrame` parent (an offset and an opacity), and only
|
|
347
|
+
the incoming one gets input and steps.
|
|
348
|
+
|
|
343
349
|
One tick, always in this order (std's code, not the game's):
|
|
344
350
|
|
|
345
|
-
1. Each pointer event goes to the topmost actor under it (`hit`),
|
|
351
|
+
1. Each pointer event goes to the topmost actor of the current screen under it (`hit`),
|
|
346
|
-
|
|
352
|
+
then its parents up to the root, through `input`.
|
|
347
|
-
2. The
|
|
353
|
+
2. The root and the current screen step (`step`), then the screen's `rules`.
|
|
348
|
-
3. `Spawn` and `Kill` apply
|
|
354
|
+
3. `Spawn` and `Kill` apply, within the root and the current screen. Each `Send(msg)`, in
|
|
349
|
-
(`update`), then the screen `react`s to it
|
|
355
|
+
order: the store reduces it (`update`), then the current screen `react`s to it.
|
|
350
|
-
that gives a screen wins.
|
|
351
|
-
4. After the tick: `handle(io, msg)` for every message in order
|
|
356
|
+
4. After the tick: `handle(io, msg)` for every message in order; then, if `screen()` names
|
|
357
|
+
another screen, it `enter`s and becomes current by its `transition`; then `Restart` or
|
|
352
|
-
`
|
|
358
|
+
`Exit`.
|
|
353
|
-
|
|
359
|
+
|
|
354
|
-
`draw` calls the current screen's `draw`. Randomness lives in `S` and
|
|
360
|
+
`draw` calls the current screen's `draw` with what is shown. Randomness lives in `S` and
|
|
355
|
-
`update`, so the same seed and inputs replay the same game.
|
|
361
|
+
changes only in `update`, so the same seed and inputs replay the same game.
|
|
356
362
|
|
|
357
363
|
The compiler's game detection (compiler/core/src/game_entry.rs) recognises a game as a
|
|
358
364
|
`main` returning `Game[S, M]` and nothing else; a type with its own `update` and `draw` is
|
docs/std.md
CHANGED
|
@@ -192,7 +192,7 @@ fun main(io: Io) =
|
|
|
192
192
|
|
|
193
193
|
## Game state: Store
|
|
194
194
|
|
|
195
|
-
`std/game/Store` holds a game's state and the messages that change it. The state implements `trait Reducer[M]` (see [Traits](language.md#traits)): `update(self, msg: M) -> Self` is the rules, pure and with no `Io`, so the same messages from the same state give the same state; `handle(self, io: Io, msg: M)` is a message's effects (a sound, a save) and does nothing unless the state overrides it.
|
|
195
|
+
`std/game/Store` holds a game's state and the messages that change it. The state implements `trait Reducer[M]` (see [Traits](language.md#traits)): `update(self, msg: M) -> Self` is the rules, pure and with no `Io`, so the same messages from the same state give the same state; `handle(self, io: Io, msg: M)` is a message's effects (a sound, a save) and does nothing unless the state overrides it. `screen(self) -> Str` is the name of the screen to show (std/game/Game): `""` by default, the game's first screen, so a game of one screen leaves it out and a game of several keeps the name in its state (`State(..self, on: PlayScreen.name())`).
|
|
196
196
|
|
|
197
197
|
```plum
|
|
198
198
|
import std/Int
|
|
@@ -217,4 +217,4 @@ fun main(io: Io) =
|
|
|
217
217
|
- **`Store[S: Reducer[M], M]`**: `Store.of(state)`, `dispatch(msg)` (reduces now and queues the message), `dispatchAll(msgs)`, `pending()` (the queued messages, oldest first), `flush(io)` (calls `handle` on the state for each queued message in order, then empties the queue), and the field `state`. A store is a value: `dispatch` and `flush` return a new one.
|
|
218
218
|
- **`store.reduceAll(s, msgs)`**: the state after `msgs`, with no store and no effects.
|
|
219
219
|
|
|
220
|
-
Only `Game` ([`std/game/Game`](game.md#a-minimal-game)) drives a store: it dispatches each message its actors and screens send,
|
|
220
|
+
Only `Game` ([`std/game/Game`](game.md#a-minimal-game)) drives a store: it dispatches each message its actors and screens send, flushes the store after the tick, and then shows the screen `state.screen()` names. A game writes its `Reducer`.
|
examples/games/clicks.plum
CHANGED
|
@@ -40,34 +40,36 @@ struct Ball(id: Int, x: Float, y: Float) is Actor[Msg] =
|
|
|
40
40
|
fun paint(self, io: Io, m: Affine, alpha: Float) =
|
|
41
41
|
actor.circlePaint(io, m, alpha, 0.0, 0.0, 10.0, Color.Yellow)
|
|
42
42
|
|
|
43
|
-
# The one screen
|
|
43
|
+
# The one screen, the root of the ball, a button that sends `Clicked`, and a label with
|
|
44
|
+
# the count. Its name is its type's, `RoomScreen.name()`; a game of one screen needs no
|
|
45
|
+
# `screen()` in its state.
|
|
44
|
-
struct
|
|
46
|
+
struct RoomScreen(id: Int) is Screen[Clicks, Msg] =
|
|
45
|
-
fun
|
|
47
|
+
fun key(self) -> Int = self.id
|
|
46
48
|
|
|
47
|
-
fun
|
|
49
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
48
50
|
|
|
49
51
|
# Escape clears the count.
|
|
50
|
-
fun rules(self, ctx: Ctx[Msg], state: Clicks, dt: Float) ->
|
|
52
|
+
fun rules(self, t: SceneTree[Msg], ctx: Ctx[Msg], state: Clicks, dt: Float) -> SceneTree[Msg] =
|
|
51
53
|
if ctx.input.isKeyPressed(Key.Escape)
|
|
52
54
|
ctx.send(Cleared)
|
|
53
|
-
|
|
55
|
+
t
|
|
54
56
|
|
|
55
57
|
# After each message: the label shows the new count.
|
|
56
|
-
fun react(self, state: Clicks, msg: Msg) ->
|
|
58
|
+
fun react(self, t: SceneTree[Msg], state: Clicks, msg: Msg) -> SceneTree[Msg] =
|
|
57
|
-
|
|
59
|
+
t.updateAs[Label[Msg]](3, |l| l.relabel("clicks {state.n}"))
|
|
58
60
|
|
|
59
|
-
fun draw(self, io: Io) =
|
|
61
|
+
fun draw(self, io: Io, shown: SceneTree[Msg]) =
|
|
60
62
|
gfx.clear(io, Color.Black)
|
|
61
|
-
|
|
63
|
+
shown.draw(io)
|
|
62
64
|
|
|
63
65
|
fun main(io: Io) -> Game[Clicks, Msg] = start()
|
|
64
66
|
|
|
65
67
|
fun start() -> Game[Clicks, Msg] =
|
|
66
|
-
|
|
68
|
+
room: FRoseTree[Actor[Msg]] := FRoseTree.of(RoomScreen(id: 0), [
|
|
67
69
|
FRoseTree.leaf(Ball(id: 1, x: 20.0, y: 150.0)),
|
|
68
70
|
FRoseTree.leaf(ui.button(2, 20.0, 20.0, 120.0, 40.0, "Click", Clicked)),
|
|
69
71
|
FRoseTree.leaf(Label(id: 3, x: 20.0, y: 80.0, text: "clicks 0", size: 16.0, color: Color.White))])
|
|
70
|
-
Game.start(
|
|
72
|
+
Game.start(SceneTree.ofScreens([room]), Clicks(n: 0))
|
|
71
73
|
|
|
72
74
|
test "a click on the button counts, and Escape clears"
|
|
73
75
|
g := start()
|
examples/games/kids/balloon_pop.plum
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
# child hears the pop and sees the word. Every 10 pops a "Hooray!" party with
|
|
5
5
|
# confetti. There is no way to lose and no timer.
|
|
6
6
|
#
|
|
7
|
-
# The game is a `Game` (std/game/Game) with one screen, `
|
|
7
|
+
# The game is a `Game` (std/game/Game) with one screen, `SkyScreen`, in a tree of actors
|
|
8
|
-
# (std/game/scene) lives from frame to frame. Each balloon is a `Balloon` actor that rises
|
|
8
|
+
# (std/game/scene) that lives from frame to frame. Each balloon is a `Balloon` actor that rises
|
|
9
9
|
# in its own `step` and is the only actor that is ever hit: the game routes a pointer
|
|
10
10
|
# press to the balloon under it (`input`), which bursts and sends `Popped(id)` to the
|
|
11
11
|
# store. The screen's `rules` send the tick (`Tick`) and ask for every balloon that is
|
|
@@ -16,10 +16,11 @@
|
|
|
16
16
|
# not click.
|
|
17
17
|
#
|
|
18
18
|
# 0 root (the camera that fits the arena on screen)
|
|
19
|
+
# └─ 5 the screen
|
|
19
|
-
#
|
|
20
|
+
# ├─ 1 sky
|
|
20
|
-
#
|
|
21
|
+
# ├─ 2 balloons ─ 100..105
|
|
21
|
-
#
|
|
22
|
+
# ├─ 3 hud
|
|
22
|
-
#
|
|
23
|
+
# └─ 4 party (shown while partying)
|
|
23
24
|
module balloon_pop
|
|
24
25
|
import std/Str
|
|
25
26
|
import std/Int
|
|
@@ -57,6 +58,7 @@ PartyEvery = 10
|
|
|
57
58
|
PartyTime = 2.5
|
|
58
59
|
|
|
59
60
|
RootId = 0
|
|
61
|
+
ScreenId = 5
|
|
60
62
|
SkyId = 1
|
|
61
63
|
BalloonLayer = 2
|
|
62
64
|
HudId = 3
|
|
@@ -309,91 +311,87 @@ struct State(rng: Random, pops: Int, clock: Float, partyAt: Float, renewed: Ball
|
|
|
309
311
|
|
|
310
312
|
struct Filled(nodes: List[FRoseTree[Actor[Msg]]], state: State)
|
|
311
313
|
|
|
312
|
-
# The one screen: the sky, the balloons, the hud and the party.
|
|
314
|
+
# The one screen, `SkyScreen`: the sky, the balloons, the hud and the party.
|
|
313
|
-
struct
|
|
315
|
+
struct SkyScreen(id: Int) is Screen[State, Msg] =
|
|
314
|
-
# The
|
|
316
|
+
# The tree and the state of a new game: the camera over the one screen, the balloons
|
|
317
|
+
# spread out below the screen.
|
|
315
318
|
fun start(state: State) -> Play =
|
|
316
319
|
filled := state.firstBalloons(0, Nil)
|
|
317
|
-
|
|
320
|
+
sky: FRoseTree[Actor[Msg]] := FRoseTree.of(SkyScreen(id: ScreenId), [
|
|
318
321
|
FRoseTree.leaf(SkyBox(id: SkyId, clock: 0.0)),
|
|
319
322
|
FRoseTree.of(Group(id: BalloonLayer, x: 0.0, y: 0.0), filled.nodes),
|
|
320
323
|
FRoseTree.leaf(Hud(id: HudId, pops: 0)),
|
|
321
324
|
FRoseTree.leaf(Party(id: PartyId, partyAt: -100.0, clock: 0.0, pops: 0))])
|
|
325
|
+
root: FRoseTree[Actor[Msg]] := FRoseTree.of(Camera(id: RootId, x: 0.0, y: 0.0, zoom: 1.0).fitted(ArenaW, ArenaH), [sky])
|
|
322
|
-
Play(
|
|
326
|
+
Play(tree: SceneTree(root: root, camX: 0.0, camY: 0.0, camScale: 1.0), state: filled.state)
|
|
323
327
|
|
|
324
|
-
fun
|
|
328
|
+
fun key(self) -> Int = self.id
|
|
325
329
|
|
|
326
|
-
fun
|
|
330
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
327
331
|
|
|
328
332
|
# The tick, then a `Renew` for each balloon that is done, in tree order.
|
|
329
|
-
fun rules(self, ctx: Ctx[Msg], state: State, dt: Float) ->
|
|
333
|
+
fun rules(self, t: SceneTree[Msg], ctx: Ctx[Msg], state: State, dt: Float) -> SceneTree[Msg] =
|
|
330
334
|
ctx.send(Tick(dt: dt))
|
|
331
|
-
for b := range
|
|
335
|
+
for b := range balloonsOf(t)
|
|
332
336
|
if b.done(b.clock)
|
|
333
337
|
ctx.send(Renew(id: b.id))
|
|
334
|
-
|
|
338
|
+
t
|
|
335
339
|
|
|
336
|
-
fun react(self, state: State, msg: Msg) ->
|
|
340
|
+
fun react(self, t: SceneTree[Msg], state: State, msg: Msg) -> SceneTree[Msg] = match msg
|
|
337
341
|
Popped(_) =>
|
|
338
342
|
pops := state.pops
|
|
339
343
|
partyAt := state.partyAt
|
|
340
|
-
|
|
344
|
+
t.updateAs[Hud](HudId, |h| Hud(..h, pops: pops)).updateAs[Party](PartyId, |p| Party(..p, partyAt: partyAt, pops: pops))
|
|
341
345
|
Renew(id) =>
|
|
342
346
|
b := state.renewed
|
|
343
|
-
|
|
347
|
+
t.updateAs[Balloon](id, |x| b)
|
|
344
|
-
_ =>
|
|
348
|
+
_ => t
|
|
345
349
|
|
|
346
|
-
fun draw(self, io: Io) =
|
|
350
|
+
fun draw(self, io: Io, shown: SceneTree[Msg]) =
|
|
347
351
|
gfx.clear(io, Color.rgb(0.55, 0.8, 1.0))
|
|
348
|
-
|
|
352
|
+
shown.draw(io)
|
|
349
353
|
|
|
350
|
-
|
|
354
|
+
# The balloons of `t`, in tree order.
|
|
351
|
-
|
|
355
|
+
fun balloonsOf(t: SceneTree[Msg]) -> List[Balloon] =
|
|
352
|
-
|
|
356
|
+
t.root.fold(Nil, |acc, a| match a
|
|
353
|
-
|
|
357
|
+
Balloon as b => Cons(head: b, tail: acc)
|
|
354
|
-
|
|
358
|
+
_ => acc).reverse()
|
|
355
359
|
|
|
356
|
-
# A new game's
|
|
360
|
+
# A new game's tree and state.
|
|
357
|
-
struct Play(
|
|
361
|
+
struct Play(tree: SceneTree[Msg], state: State)
|
|
358
362
|
|
|
359
363
|
fun main(io: Io) -> Game[State, Msg] =
|
|
360
|
-
play :=
|
|
364
|
+
play := SkyScreen.start(State.start(io.randomSeed(), audio.loadSound(io, "pickup.wav"), audio.loadSound(io, "gameover.wav")))
|
|
361
365
|
music := audio.loadSound(io, "music.wav")
|
|
362
366
|
music.loop(io, 0.2)
|
|
363
|
-
Game.start(play.
|
|
367
|
+
Game.start(play.tree, play.state)
|
|
364
368
|
|
|
365
369
|
# --- tests ---
|
|
366
370
|
|
|
367
|
-
|
|
371
|
+
View800 = Vec2(x: 800.0, y: 600.0)
|
|
368
372
|
|
|
369
373
|
fun newGame() -> Game[State, Msg] =
|
|
370
|
-
play :=
|
|
374
|
+
play := SkyScreen.start(State.start(3, Sound.Silent, Sound.Silent))
|
|
371
|
-
Game.start(play.
|
|
375
|
+
Game.start(play.tree, play.state)
|
|
372
|
-
|
|
373
|
-
fun skyOf(g: Game[State, Msg]) -> Sky = match g.screen
|
|
374
|
-
Sky as s => s
|
|
375
|
-
_ => Sky(t: SceneTree.of(Group(id: 0, x: 0.0, y: 0.0)))
|
|
376
376
|
|
|
377
377
|
fun first(g: Game[State, Msg]) -> Balloon =
|
|
378
|
-
|
|
378
|
+
balloonsOf(g.screenTree()).at(0)
|
|
379
379
|
|
|
380
380
|
# A tick of `dt` with `events` on a `view` screen.
|
|
381
381
|
fun tickOn(io: Io, g: Game[State, Msg], events: List[InputEvent], dt: Float, view: Vec2) -> Game[State, Msg] =
|
|
382
382
|
g.step(io, Input(held: Held.none(), events: events.toSlice(), pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0), dt, view)
|
|
383
383
|
|
|
384
|
-
fun tick(io: Io, g: Game[State, Msg], events: List[InputEvent], dt: Float) -> Game[State, Msg] = tickOn(io, g, events, dt,
|
|
384
|
+
fun tick(io: Io, g: Game[State, Msg], events: List[InputEvent], dt: Float) -> Game[State, Msg] = tickOn(io, g, events, dt, View800)
|
|
385
385
|
|
|
386
386
|
# A game whose first balloon is still and in the middle of the screen.
|
|
387
387
|
fun withCentred(g: Game[State, Msg]) -> Game[State, Msg] =
|
|
388
|
-
s := skyOf(g)
|
|
389
|
-
|
|
388
|
+
Game(..g, tree: g.tree.updateAs[Balloon](BalloonId, |b| Balloon(..b, x: 400.0, y: 300.0, speed: 0.0, phase: 0.0)))
|
|
390
|
-
Game(..g, screen: centred)
|
|
391
389
|
|
|
392
390
|
fun pressAt(x: Float, y: Float) -> List[InputEvent] = [PointerDown(x: x, y: y)]
|
|
393
391
|
|
|
394
392
|
test "a new game has its balloons below the screen and no pops"
|
|
395
393
|
g := newGame()
|
|
396
|
-
bs :=
|
|
394
|
+
bs := balloonsOf(g.screenTree())
|
|
397
395
|
assert bs.length() == Count
|
|
398
396
|
assert g.state().pops == 0
|
|
399
397
|
assert bs.fold(true, |ok, b| ok && b.y > ArenaH * 0.4)
|
|
@@ -429,14 +427,14 @@ test "every tenth pop starts a party that ends"
|
|
|
429
427
|
fresh := withCentred(newGame())
|
|
430
428
|
g := Game(..fresh, store: Store.of(State(..fresh.state(), pops: 9)))
|
|
431
429
|
assert !g.state().partying()
|
|
432
|
-
assert !
|
|
430
|
+
assert !g.tree.find(PartyId).unwrap().shown()
|
|
433
431
|
t := tick(io, g, pressAt(400.0, 300.0), 0.01)
|
|
434
432
|
assert t.state().pops == 10
|
|
435
433
|
assert t.state().partying()
|
|
436
|
-
assert
|
|
434
|
+
assert t.tree.find(PartyId).unwrap().shown()
|
|
437
435
|
u := tick(io, t, Nil, PartyTime + 0.1)
|
|
438
436
|
assert !u.state().partying()
|
|
439
|
-
assert !
|
|
437
|
+
assert !u.tree.find(PartyId).unwrap().shown()
|
|
440
438
|
|
|
441
439
|
test "the press is found through the camera"
|
|
442
440
|
g := tickOn(io, withCentred(newGame()), Nil, 0.01, Vec2(x: 1600.0, y: 600.0))
|
|
@@ -446,7 +444,7 @@ test "the press is found through the camera"
|
|
|
446
444
|
assert hit.state().pops == 1
|
|
447
445
|
|
|
448
446
|
test "the same seed gives the same balloons"
|
|
449
|
-
a :=
|
|
447
|
+
a := SkyScreen.start(State.start(9, Sound.Silent, Sound.Silent))
|
|
450
|
-
b :=
|
|
448
|
+
b := SkyScreen.start(State.start(9, Sound.Silent, Sound.Silent))
|
|
451
|
-
assert a.
|
|
449
|
+
assert balloonsOf(a.tree).at(0).x == balloonsOf(b.tree).at(0).x
|
|
452
|
-
assert a.
|
|
450
|
+
assert balloonsOf(a.tree).at(0).hue == balloonsOf(b.tree).at(0).hue
|
examples/games/orb_run.plum
CHANGED
|
@@ -3,9 +3,12 @@
|
|
|
3
3
|
# button starts a run from the title; Enter, R or the "Again" button starts another after
|
|
4
4
|
# the game is over, and Escape goes back to the title.
|
|
5
5
|
#
|
|
6
|
-
# The game is a `Game` (std/game/Game) of three screens, `
|
|
6
|
+
# The game is a `Game` (std/game/Game) of three screens, `TitleScreen`, `PlayScreen` and
|
|
7
|
-
# a store of `State` (std/game/Store).
|
|
7
|
+
# `GameOverScreen`, over a store of `State` (std/game/Store). The screens live in one tree
|
|
8
|
+
# of actors (std/game/scene) that lives from frame to frame, under the root camera that
|
|
9
|
+
# fits the arena on screen; the state names the one shown (`State.on`, set by `update`:
|
|
10
|
+
# `PlayScreen.name()` when a run starts), and only that one steps and is drawn. The play
|
|
8
|
-
#
|
|
11
|
+
# screen fades in over the title. Every kind of body is its own struct (`Ship`, `Orb`,
|
|
9
12
|
# `Mine`, `Star`, `Icon`, ..) with only its own fields, and `is Actor[Msg]`: it says which
|
|
10
13
|
# actor it is (`key`) and where (`place`, made from its plain `x` and `y`), and writes
|
|
11
14
|
# only the behaviour it has: its next version (`step`), its drawing (`paint`), its hit
|
|
@@ -14,19 +17,23 @@
|
|
|
14
17
|
# std's `Box`, the layers std's `Group`, the text std/game/ui's `Label` and the buttons
|
|
15
18
|
# ui's `Button`, which send their `onClick` message when clicked.
|
|
16
19
|
#
|
|
17
|
-
# The
|
|
20
|
+
# The tree:
|
|
18
21
|
#
|
|
19
22
|
# 0 root (the camera that fits the arena on screen)
|
|
23
|
+
# ├─ 900 title ─ 1 backdrop, 2 stars ─ 20..59, 15 name, 16 help, 17 "Play"
|
|
24
|
+
# ├─ 901 play
|
|
20
|
-
# ├─ 1 backdrop
|
|
25
|
+
# │ ├─ 1 backdrop
|
|
21
|
-
# ├─ 2 stars ─ 20..59
|
|
26
|
+
# │ ├─ 2 stars ─ 20..59
|
|
22
|
-
# ├─ 3 mines ─ 200..
|
|
27
|
+
# │ ├─ 3 mines ─ 200..
|
|
23
|
-
# ├─ 4 orbs ─ 100..102
|
|
28
|
+
# │ ├─ 4 orbs ─ 100..102
|
|
24
|
-
# ├─ 5 ship
|
|
29
|
+
# │ ├─ 5 ship
|
|
25
|
-
# └─ 6 hud ─ 8 score, 9 best, 300..302 lives
|
|
30
|
+
# │ └─ 6 hud ─ 8 score, 9 best, 300..302 lives
|
|
31
|
+
# └─ 902 game over ─ the play screen's actors when the run ended, with the ship
|
|
32
|
+
# hidden and the bodies stopped, and `7 panel ─ 13 dim, 11 title,
|
|
33
|
+
# 12 prompt, 14 "Again"` on top
|
|
26
34
|
#
|
|
27
|
-
# and game over adds `7 panel ─ 13 dim, 11 title, 12 prompt, 14 "Again"` on top, with the
|
|
28
|
-
#
|
|
35
|
+
# Play builds a fresh run each time the state switches to it (`enter`), and game over
|
|
29
|
-
#
|
|
36
|
+
# copies the play screen's actors when it comes up.
|
|
30
37
|
#
|
|
31
38
|
# What is between bodies is in the play screen's `rules`, after every actor stepped: the
|
|
32
39
|
# orbs the ship touches (`Collected`), the mines to add as the score grows (`Grow`), a
|
|
@@ -34,7 +41,7 @@
|
|
|
34
41
|
# the rules of the score, the lives and every random spot (all the randomness is the
|
|
35
42
|
# `Random` in `State`, seeded once in `main`, so one seed and one list of inputs give the
|
|
36
43
|
# same game). The screen `react`s to the new state: an orb moves to its new spot, the
|
|
37
|
-
# hud shows the score, a mine is added, the ship flashes and loses an icon.
|
|
44
|
+
# hud shows the score, a mine is added, the ship flashes and loses an icon. The state goes
|
|
38
45
|
# from the title to play (`Start`), from play to game over (the last `Hurt`) and back to
|
|
39
46
|
# play (`Again`). The sounds are the messages' effects (`State.handle`).
|
|
40
47
|
module orb_run
|
|
@@ -103,6 +110,9 @@ StarId = 20
|
|
|
103
110
|
OrbId = 100
|
|
104
111
|
MineId = 200
|
|
105
112
|
LifeId = 300
|
|
113
|
+
TitleScreenId = 900
|
|
114
|
+
PlayScreenId = 901
|
|
115
|
+
GameOverScreenId = 902
|
|
106
116
|
|
|
107
117
|
# A position and velocity: what a spawn returns and what drifters are bounced with.
|
|
108
118
|
struct Motion(x: Float, y: Float, vx: Float, vy: Float)
|
|
@@ -422,23 +432,23 @@ fun shipActor(x: Float, y: Float, sheet: Texture) -> Ship =
|
|
|
422
432
|
# The camera fitted to a `sw` by `sh` screen.
|
|
423
433
|
fun fittedCamera(sw: Float, sh: Float) -> Camera = Camera(id: RootId, x: 0.0, y: 0.0, zoom: 1.0).fitted(sw, sh)
|
|
424
434
|
|
|
425
|
-
# A
|
|
435
|
+
# A screen's subtree: the screen `screen` over `kids`.
|
|
426
|
-
fun
|
|
436
|
+
fun screenTree(screen: Actor[Msg], kids: List[FRoseTree[Actor[Msg]]]) -> SceneTree[Msg] =
|
|
427
|
-
|
|
437
|
+
node: FRoseTree[Actor[Msg]] := FRoseTree.of(screen, kids)
|
|
428
|
-
SceneTree(root:
|
|
438
|
+
SceneTree(root: node, camX: 0.0, camY: 0.0, camScale: 1.0)
|
|
429
439
|
|
|
430
440
|
fun backdrop() -> FRoseTree[Actor[Msg]] =
|
|
431
441
|
FRoseTree.leaf(Box(id: BackId, x: 0.0, y: 0.0, w: ArenaW, h: ArenaH, color: Color.rgb(0.07, 0.08, 0.16)))
|
|
432
442
|
|
|
433
|
-
# The
|
|
443
|
+
# The play screen's subtree of a fresh run, with the run's first orbs and mines
|
|
434
444
|
# (`State.run` places them).
|
|
435
|
-
fun playTree(s: State
|
|
445
|
+
fun playTree(s: State) -> SceneTree[Msg] =
|
|
436
446
|
orbs: List[FRoseTree[Actor[Msg]]] := s.newOrbs.map(|o| FRoseTree.leaf(o))
|
|
437
447
|
mines: List[FRoseTree[Actor[Msg]]] := s.newMines.map(|m| FRoseTree.leaf(m))
|
|
438
448
|
hud := group(HudId, Cons(
|
|
439
449
|
head: label(ScoreId, "Score {s.score}", 16.0, 12.0, 28.0, Color.White),
|
|
440
450
|
tail: Cons(head: label(BestId, "Best {s.best.max(s.score)}", 16.0, 44.0, 20.0, Color.Yellow), tail: lifeNodes(0, s.sheet))))
|
|
441
|
-
|
|
451
|
+
screenTree(PlayScreen(id: PlayScreenId), [
|
|
442
452
|
backdrop(),
|
|
443
453
|
group(StarLayer, starNodes(0)),
|
|
444
454
|
group(MineLayer, mines),
|
|
@@ -454,15 +464,26 @@ fun panelNode() -> FRoseTree[Actor[Msg]] =
|
|
|
454
464
|
label(PromptId, "Press Enter to play again", 270.0, 300.0, 24.0, Color.White),
|
|
455
465
|
FRoseTree.leaf(ui.button(RestartId, 340.0, 342.0, 120.0, 44.0, "Again", Again))])
|
|
456
466
|
|
|
457
|
-
# The title screen's
|
|
467
|
+
# The title screen's subtree.
|
|
458
468
|
fun titleTree() -> SceneTree[Msg] =
|
|
459
|
-
|
|
469
|
+
screenTree(TitleScreen(id: TitleScreenId), [
|
|
460
470
|
backdrop(),
|
|
461
471
|
group(StarLayer, starNodes(0)),
|
|
462
472
|
label(NameId, "Orb Run", 280.0, 200.0, 64.0, Color.rgb(1.0, 0.8, 0.3)),
|
|
463
473
|
label(HelpId, "Steer with the arrows or WASD, collect the orbs, dodge the mines", 90.0, 290.0, 20.0, Color.White),
|
|
464
474
|
FRoseTree.leaf(ui.button(PlayId, 340.0, 342.0, 120.0, 44.0, "Play", Start))])
|
|
465
475
|
|
|
476
|
+
# The game's tree: the camera that fits the arena on screen over the three screens. Play
|
|
477
|
+
# and game over start empty: each builds its subtree when the state switches to it.
|
|
478
|
+
fun gameTree() -> SceneTree[Msg] =
|
|
479
|
+
empty: List[FRoseTree[Actor[Msg]]] := Nil
|
|
480
|
+
screens: List[FRoseTree[Actor[Msg]]] := [
|
|
481
|
+
titleTree().root,
|
|
482
|
+
FRoseTree.of(PlayScreen(id: PlayScreenId), empty),
|
|
483
|
+
FRoseTree.of(GameOverScreen(id: GameOverScreenId), empty)]
|
|
484
|
+
root: FRoseTree[Actor[Msg]] := FRoseTree.of(fittedCamera(ArenaW, ArenaH), screens)
|
|
485
|
+
SceneTree(root: root, camX: 0.0, camY: 0.0, camScale: 1.0)
|
|
486
|
+
|
|
466
487
|
# --- the game ---
|
|
467
488
|
|
|
468
489
|
# What can happen to the state: a run starts from the title or again after game over, the
|
|
@@ -480,6 +501,7 @@ enum Msg =
|
|
|
480
501
|
# run's first orbs and mines, the spot of a collected orb, the mines just added. The
|
|
481
502
|
# sprite sheet and the sounds are for the screens and the effects.
|
|
482
503
|
struct State(
|
|
504
|
+
on: Str,
|
|
483
505
|
seed: Int,
|
|
484
506
|
rng: Random,
|
|
485
507
|
score: Int,
|
|
@@ -497,7 +519,7 @@ struct State(
|
|
|
497
519
|
# The state on the title screen: no run yet. The first run is seeded with `seed`.
|
|
498
520
|
fun create(seed: Int, sheet: Texture, sounds: Sounds) -> State =
|
|
499
521
|
State(
|
|
500
|
-
seed: seed, rng: Random.seeded(seed), score: 0, best: 0, lives: StartLives, over: false, mines: 0,
|
|
522
|
+
on: TitleScreen.name(), seed: seed, rng: Random.seeded(seed), score: 0, best: 0, lives: StartLives, over: false, mines: 0,
|
|
501
523
|
newOrbs: Nil, newMines: Nil, spot: Motion(x: 0.0, y: 0.0, vx: 0.0, vy: 0.0), sheet: sheet, sounds: sounds)
|
|
502
524
|
|
|
503
525
|
fun update(self, msg: Msg) -> State = match msg
|
|
@@ -510,12 +532,12 @@ struct State(
|
|
|
510
532
|
Hurt =>
|
|
511
533
|
lives := self.lives - 1
|
|
512
534
|
done := lives <= 0
|
|
513
|
-
State(..self, lives: lives, over: done, best: if done then self.best.max(self.score) else self.best)
|
|
535
|
+
State(..self, lives: lives, over: done, best: if done then self.best.max(self.score) else self.best, on: if done then GameOverScreen.name() else self.on)
|
|
514
536
|
|
|
515
537
|
# A fresh run seeded with `seed`, keeping `best`: its orbs and mines spread over the
|
|
516
538
|
# arena away from the ship in the middle.
|
|
517
539
|
fun run(self, seed: Int, best: Int) -> State =
|
|
518
|
-
fresh := State(..self, rng: Random.seeded(seed), score: 0, best: best, lives: StartLives, over: false, mines: 0, newOrbs: Nil)
|
|
540
|
+
fresh := State(..self, on: PlayScreen.name(), rng: Random.seeded(seed), score: 0, best: best, lives: StartLives, over: false, mines: 0, newOrbs: Nil)
|
|
519
541
|
orbs := fresh.orbsFrom(0, Nil)
|
|
520
542
|
orbs.grown(ArenaW / 2.0, ArenaH / 2.0, Nil)
|
|
521
543
|
|
|
@@ -538,6 +560,9 @@ struct State(
|
|
|
538
560
|
mine := Mine(id: MineId + self.mines, x: sp.at.x, y: sp.at.y, rotation: 0.0, vx: sp.at.vx, vy: sp.at.vy, spin: 2.0, clock: 0.0, sheet: self.sheet)
|
|
539
561
|
State(..self, rng: sp.rng, mines: self.mines + 1).grown(x, y, Cons(head: mine, tail: acc))
|
|
540
562
|
|
|
563
|
+
# The screen shown: the title, the play or the game over.
|
|
564
|
+
fun screen(self) -> Str = self.on
|
|
565
|
+
|
|
541
566
|
# The sounds, with the state after the tick.
|
|
542
567
|
fun handle(self, io: Io, msg: Msg) = match msg
|
|
543
568
|
Again => self.sounds.startMusic(io)
|
|
@@ -562,112 +587,97 @@ fun musicVolume() -> Float =
|
|
|
562
587
|
0.3
|
|
563
588
|
|
|
564
589
|
# The title: Enter, R or the "Play" button start the first run.
|
|
565
|
-
struct
|
|
590
|
+
struct TitleScreen(id: Int) is Screen[State, Msg] =
|
|
566
|
-
fun create() -> Title = Title(t: titleTree())
|
|
567
|
-
|
|
568
|
-
fun
|
|
591
|
+
fun key(self) -> Int = self.id
|
|
569
592
|
|
|
570
|
-
fun
|
|
593
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
571
594
|
|
|
572
|
-
fun rules(self, ctx: Ctx[Msg], state: State, dt: Float) ->
|
|
595
|
+
fun rules(self, t: SceneTree[Msg], ctx: Ctx[Msg], state: State, dt: Float) -> SceneTree[Msg] =
|
|
573
596
|
if pressedRestart(ctx.input)
|
|
574
597
|
ctx.send(Start)
|
|
575
|
-
|
|
598
|
+
t
|
|
576
|
-
|
|
577
|
-
fun next(self, state: State, msg: Msg) -> Option[Screen[State, Msg]] = match msg
|
|
578
|
-
Start => Some(Play.fresh(state, self.t))
|
|
579
|
-
_ => None
|
|
580
|
-
|
|
581
|
-
fun draw(self, io: Io) = drawTree(io, self.t)
|
|
582
599
|
|
|
583
|
-
# A run: the bodies, the hud and the rules between them.
|
|
584
|
-
struct Play(t: SceneTree[Msg]) is Screen[State, Msg] =
|
|
585
|
-
# The screen of the run `state` just started, under the camera of `from` (the screen
|
|
586
|
-
# before).
|
|
587
|
-
fun
|
|
600
|
+
fun draw(self, io: Io, shown: SceneTree[Msg]) = drawTree(io, shown)
|
|
588
|
-
c := from.findAs[Camera](RootId).unwrapOr(fittedCamera(ArenaW, ArenaH))
|
|
589
|
-
screen: Screen[State, Msg] := Play(t: playTree(state, c))
|
|
590
|
-
screen
|
|
591
601
|
|
|
602
|
+
# A run: the bodies, the hud and the rules between them. It fades in over the title.
|
|
603
|
+
struct PlayScreen(id: Int) is Screen[State, Msg] =
|
|
592
|
-
fun
|
|
604
|
+
fun key(self) -> Int = self.id
|
|
593
|
-
|
|
594
|
-
fun withTree(self, t: SceneTree[Msg]) -> Play = Play(t: t)
|
|
595
605
|
|
|
596
|
-
fun
|
|
606
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
597
607
|
|
|
598
|
-
#
|
|
608
|
+
# Each time the state switches to it, a fresh run.
|
|
599
|
-
fun
|
|
609
|
+
fun enter(self, t: SceneTree[Msg], state: State, tree: SceneTree[Msg]) -> SceneTree[Msg] = playTree(state)
|
|
600
|
-
self.t.root.fold(Nil, |acc, a| match a
|
|
601
|
-
Orb as o => Cons(head: o, tail: acc)
|
|
602
|
-
_ => acc).reverse()
|
|
603
610
|
|
|
604
|
-
fun
|
|
611
|
+
fun transition(self, from: Str) -> Transition = if from == TitleScreen.name() then Fade(secs: TitleFade) else Cut
|
|
605
|
-
self.t.root.fold(Nil, |acc, a| match a
|
|
606
|
-
Mine as m => Cons(head: m, tail: acc)
|
|
607
|
-
_ => acc).reverse()
|
|
608
612
|
|
|
609
613
|
# After every actor stepped: the orbs the ship touches, the mines to grow, a mine that
|
|
610
614
|
# hits the ship (unless it is still flashing from the last).
|
|
611
|
-
fun rules(self, ctx: Ctx[Msg], state: State, dt: Float) ->
|
|
615
|
+
fun rules(self, t: SceneTree[Msg], ctx: Ctx[Msg], state: State, dt: Float) -> SceneTree[Msg] =
|
|
612
|
-
sh :=
|
|
616
|
+
sh := t.findAs[Ship](ShipId).unwrap()
|
|
613
|
-
for o := range
|
|
617
|
+
for o := range orbsOf(t)
|
|
614
618
|
if overlaps(sh.x, sh.y, ShipRadius, o.x, o.y, OrbRadius)
|
|
615
619
|
ctx.send(Collected(id: o.id, x: sh.x, y: sh.y))
|
|
616
620
|
ctx.send(Grow(x: sh.x, y: sh.y))
|
|
617
|
-
if sh.hurt <= 0.0 &&
|
|
621
|
+
if sh.hurt <= 0.0 && minesOf(t).find(|m| overlaps(sh.x, sh.y, ShipRadius, m.x, m.y, MineRadius)).isSome()
|
|
618
622
|
ctx.send(Hurt)
|
|
619
|
-
|
|
623
|
+
t
|
|
620
624
|
|
|
621
|
-
fun react(self, state: State, msg: Msg) ->
|
|
625
|
+
fun react(self, t: SceneTree[Msg], state: State, msg: Msg) -> SceneTree[Msg] = match msg
|
|
622
626
|
Collected(id, _, _) =>
|
|
623
627
|
spot := state.spot
|
|
624
|
-
|
|
628
|
+
showScore(t.updateAs[Orb](id, |o| o.placed(spot)), state)
|
|
625
|
-
Grow(_, _) =>
|
|
629
|
+
Grow(_, _) => state.newMines.fold(t, |tr, m| tr.add(MineLayer, FRoseTree.leaf(m)))
|
|
626
|
-
Hurt =>
|
|
630
|
+
Hurt => t.updateAs[Ship](ShipId, |sh| sh.struck()).kill(LifeId + state.lives)
|
|
627
|
-
_ =>
|
|
631
|
+
_ => t
|
|
632
|
+
|
|
628
|
-
|
|
633
|
+
fun draw(self, io: Io, shown: SceneTree[Msg]) = drawTree(io, shown)
|
|
634
|
+
|
|
635
|
+
# How long the play screen takes to fade in over the title, in seconds.
|
|
636
|
+
TitleFade = 0.5
|
|
637
|
+
|
|
638
|
+
# The orbs and the mines of `t`, in tree order.
|
|
639
|
+
fun orbsOf(t: SceneTree[Msg]) -> List[Orb] =
|
|
640
|
+
t.root.fold(Nil, |acc, a| match a
|
|
641
|
+
Orb as o => Cons(head: o, tail: acc)
|
|
642
|
+
_ => acc).reverse()
|
|
643
|
+
|
|
644
|
+
fun minesOf(t: SceneTree[Msg]) -> List[Mine] =
|
|
645
|
+
t.root.fold(Nil, |acc, a| match a
|
|
646
|
+
Mine as m => Cons(head: m, tail: acc)
|
|
647
|
+
_ => acc).reverse()
|
|
648
|
+
|
|
629
|
-
|
|
649
|
+
# The hud's labels from the score.
|
|
630
|
-
|
|
650
|
+
fun showScore(t: SceneTree[Msg], state: State) -> SceneTree[Msg] =
|
|
631
|
-
|
|
651
|
+
score := "Score {state.score}"
|
|
632
|
-
|
|
652
|
+
best := "Best {state.best.max(state.score)}"
|
|
633
|
-
|
|
653
|
+
t.updateAs[Label[Msg]](ScoreId, |l| l.relabel(score)).updateAs[Label[Msg]](BestId, |l| l.relabel(best))
|
|
654
|
+
|
|
634
|
-
|
|
655
|
+
# Game over: the run's last picture with the ship gone, the bodies stopped (they still
|
|
656
|
+
# animate) and the panel up. Enter, R or "Again" start another run; Escape goes back to
|
|
657
|
+
# the title.
|
|
635
|
-
|
|
658
|
+
struct GameOverScreen(id: Int) is Screen[State, Msg] =
|
|
636
|
-
Hurt =>
|
|
637
|
-
if state.over
|
|
638
|
-
|
|
659
|
+
fun key(self) -> Int = self.id
|
|
639
|
-
Some(screen)
|
|
640
|
-
else
|
|
641
|
-
None
|
|
642
|
-
_ => None
|
|
643
660
|
|
|
644
|
-
fun
|
|
661
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
645
662
|
|
|
646
|
-
#
|
|
663
|
+
# The play screen's actors as they are when the run ends, under this screen.
|
|
647
|
-
# the panel up. Enter, R or "Again" start another run; Escape goes back to the title.
|
|
648
|
-
|
|
664
|
+
fun enter(self, t: SceneTree[Msg], state: State, tree: SceneTree[Msg]) -> SceneTree[Msg] =
|
|
649
|
-
|
|
665
|
+
play := tree.subtree(PlayScreenId).unwrap()
|
|
650
666
|
hidden := play.updateAs[Ship](ShipId, |sh| sh.hidden())
|
|
667
|
+
over: Actor[Msg] := self
|
|
668
|
+
still := hidden.root.withValue(over).map(stilled)
|
|
651
|
-
|
|
669
|
+
hidden.withRoot(still).add(GameOverScreenId, panelNode())
|
|
652
|
-
|
|
653
|
-
fun tree(self) -> SceneTree[Msg] = self.t
|
|
654
|
-
|
|
655
|
-
fun withTree(self, t: SceneTree[Msg]) -> Over = Over(t: t)
|
|
656
670
|
|
|
657
|
-
fun rules(self, ctx: Ctx[Msg], state: State, dt: Float) ->
|
|
671
|
+
fun rules(self, t: SceneTree[Msg], ctx: Ctx[Msg], state: State, dt: Float) -> SceneTree[Msg] =
|
|
658
672
|
if pressedRestart(ctx.input)
|
|
659
673
|
ctx.send(Again)
|
|
660
674
|
else if pressedEscape(ctx.input)
|
|
661
675
|
ctx.out.push(Restart)
|
|
662
|
-
|
|
676
|
+
t
|
|
663
677
|
|
|
664
|
-
fun
|
|
678
|
+
fun draw(self, io: Io, shown: SceneTree[Msg]) = drawTree(io, shown)
|
|
665
|
-
Again => Some(Play.fresh(state, self.t))
|
|
666
|
-
_ => None
|
|
667
679
|
|
|
668
|
-
fun draw(self, io: Io) = drawTree(io, self.t)
|
|
669
|
-
|
|
670
|
-
# Every screen clears to the night, then draws
|
|
680
|
+
# Every screen clears to the night, then draws the tree.
|
|
671
681
|
fun drawTree(io: Io, t: SceneTree[Msg]) =
|
|
672
682
|
gfx.clear(io, Color.rgb(0.04, 0.05, 0.1))
|
|
673
683
|
t.draw(io)
|
|
@@ -680,7 +690,7 @@ fun main(io: Io) -> Game[State, Msg] =
|
|
|
680
690
|
music: audio.loadSound(io, "music.wav"))
|
|
681
691
|
state := State.create(io.randomSeed(), gfx.loadTexture(io, "sprites.png"), sounds)
|
|
682
692
|
sounds.startMusic(io)
|
|
683
|
-
Game.start(
|
|
693
|
+
Game.start(gameTree(), state)
|
|
684
694
|
|
|
685
695
|
|
|
686
696
|
# --- tests ---
|
|
@@ -688,7 +698,7 @@ fun main(io: Io) -> Game[State, Msg] =
|
|
|
688
698
|
View = Vec2(x: 800.0, y: 600.0)
|
|
689
699
|
|
|
690
700
|
fun newGame(seed: Int) -> Game[State, Msg] =
|
|
691
|
-
Game.start(
|
|
701
|
+
Game.start(gameTree(), State.create(seed, Texture(id: -1), Sounds.silent()))
|
|
692
702
|
|
|
693
703
|
fun inputOf(keys: List[Int], events: List[InputEvent]) -> Input =
|
|
694
704
|
Input(held: Held(mouse: false, keys: keys.toSlice()), events: events.toSlice(), pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0)
|
|
@@ -707,13 +717,23 @@ fun ticksOf(io: Io, g: Game[State, Msg], n: Int, keys: List[Int], dt: Float) ->
|
|
|
707
717
|
# A game on the play screen: the title, then Enter.
|
|
708
718
|
fun started(io: Io, seed: Int) -> Game[State, Msg] = tickOf(io, newGame(seed), Nil, press(Key.Enter))
|
|
709
719
|
|
|
720
|
+
# The current screen's subtree.
|
|
710
|
-
fun treeOf(g: Game[State, Msg]) -> SceneTree[Msg] = g.
|
|
721
|
+
fun treeOf(g: Game[State, Msg]) -> SceneTree[Msg] = g.screenTree()
|
|
722
|
+
|
|
723
|
+
fun onScreen(g: Game[State, Msg]) -> Str =
|
|
724
|
+
if g.current == TitleScreen.name()
|
|
725
|
+
"title"
|
|
726
|
+
else if g.current == PlayScreen.name()
|
|
727
|
+
"play"
|
|
728
|
+
else if g.current == GameOverScreen.name()
|
|
729
|
+
"over"
|
|
730
|
+
else
|
|
731
|
+
"?"
|
|
711
732
|
|
|
733
|
+
# The game with the current screen's subtree replaced by `t`.
|
|
712
|
-
fun
|
|
734
|
+
fun withScreenTree(g: Game[State, Msg], t: SceneTree[Msg]) -> Game[State, Msg] =
|
|
713
|
-
Title as t1 => "title"
|
|
714
|
-
Play as p1 => "play"
|
|
715
|
-
Over as o1 => "over"
|
|
716
|
-
|
|
735
|
+
key := g.currentKey()
|
|
736
|
+
Game(..g, tree: g.tree.withRoot(g.tree.root.replace(|a| a.key() == key, |_| t.root)))
|
|
717
737
|
|
|
718
738
|
fun shipOf(g: Game[State, Msg]) -> Ship = treeOf(g).findAs[Ship](ShipId).unwrap()
|
|
719
739
|
|
|
@@ -730,13 +750,11 @@ fun mineCountOf(g: Game[State, Msg]) -> Int = treeOf(g).root.fold(0, |n, a| matc
|
|
|
730
750
|
# The game with every actor of its screen's tree through `f`.
|
|
731
751
|
fun mapped(g: Game[State, Msg], f: fn(Actor[Msg]) -> Actor[Msg]) -> Game[State, Msg] =
|
|
732
752
|
t := treeOf(g)
|
|
733
|
-
|
|
753
|
+
withScreenTree(g, t.withRoot(t.root.map(f)))
|
|
734
|
-
Game(..g, screen: screen)
|
|
735
754
|
|
|
736
755
|
# The game with the actor `id` through `f`.
|
|
737
756
|
fun moved(g: Game[State, Msg], id: Int, m: Motion) -> Game[State, Msg] =
|
|
738
|
-
|
|
757
|
+
withScreenTree(g, treeOf(g).update(id, |a| placedAt(a, m)))
|
|
739
|
-
Game(..g, screen: screen)
|
|
740
758
|
|
|
741
759
|
fun away() -> Motion =
|
|
742
760
|
Motion(x: 5000.0, y: 0.0, vx: 0.0, vy: 0.0)
|
|
@@ -761,6 +779,9 @@ fun lone(sh: Ship) -> SceneTree[Msg] = SceneTree.of(sh)
|
|
|
761
779
|
|
|
762
780
|
fun shipAt(x: Float, y: Float) -> Ship = shipActor(x, y, Texture(id: -1))
|
|
763
781
|
|
|
782
|
+
# What a game draws, in order.
|
|
783
|
+
fun drawnOf(g: Game[State, Msg]) -> List[Drawn[Msg]] = List.fromSlice(g.shown().flatten())
|
|
784
|
+
|
|
764
785
|
fun near(a: Float, b: Float) -> Bool =
|
|
765
786
|
{a - b}.abs() < 0.01
|
|
766
787
|
|
|
@@ -944,18 +965,18 @@ test "a kept old game still has its old tree"
|
|
|
944
965
|
|
|
945
966
|
test "the root camera fits the arena on the screen"
|
|
946
967
|
g := emptyGame(io).step(io, inputOf(Nil, Nil), 1.0 / 60.0, Vec2(x: 1600.0, y: 600.0))
|
|
947
|
-
root :=
|
|
968
|
+
root := g.tree.find(RootId).unwrap().place().local()
|
|
948
969
|
assert near(root.a, 1.0)
|
|
949
970
|
assert near(root.e, 400.0)
|
|
950
971
|
assert near(root.f, 0.0)
|
|
951
972
|
|
|
952
973
|
test "the again button is found through the root's camera"
|
|
953
974
|
o := overWith(io, 0)
|
|
954
|
-
assert
|
|
975
|
+
assert o.shown().hitId(400.0, 364.0).unwrapOr(-1) == RestartId
|
|
955
|
-
assert
|
|
976
|
+
assert o.shown().hitId(100.0, 100.0).unwrapOr(-1) == BackId
|
|
956
|
-
assert
|
|
977
|
+
assert emptyGame(io).shown().hitId(400.0, 364.0).unwrapOr(-1) != RestartId
|
|
957
978
|
wide := o.step(io, inputOf(Nil, Nil), 1.0 / 60.0, Vec2(x: 1600.0, y: 600.0))
|
|
958
|
-
assert
|
|
979
|
+
assert wide.shown().hitId(800.0, 364.0).unwrapOr(-1) == RestartId
|
|
959
980
|
assert onScreen(tickOf(io, wide, Nil, click(800.0, 364.0))) == "play"
|
|
960
981
|
|
|
961
982
|
test "a press on the button shows it pressed, and a release elsewhere does not click"
|
|
@@ -980,3 +1001,19 @@ test "same_seed_same_game: one seed and one input list give the same run"
|
|
|
980
1001
|
assert sameRun(a, b)
|
|
981
1002
|
assert shipOf(a).x != shipOf(started(io, 42)).x
|
|
982
1003
|
assert !sameRun(a, scriptedRun(io, 43))
|
|
1004
|
+
|
|
1005
|
+
test "the play screen fades in over the title, which is frozen meanwhile"
|
|
1006
|
+
g := started(io, 7)
|
|
1007
|
+
assert onScreen(g) == "play" && g.shift.isSome()
|
|
1008
|
+
# both drawn: the title's name and play's ship
|
|
1009
|
+
assert drawnOf(g).find(|d| d.actor.key() == NameId).isSome()
|
|
1010
|
+
assert drawnOf(g).find(|d| d.actor.key() == ShipId).unwrap().alpha == 0.0
|
|
1011
|
+
title := g.tree.subtree(TitleScreenId).unwrap()
|
|
1012
|
+
mid := ticksOf(io, g, 15, Nil, 1.0 / 60.0)
|
|
1013
|
+
assert near(drawnOf(mid).find(|d| d.actor.key() == ShipId).unwrap().alpha, 0.5)
|
|
1014
|
+
assert mid.tree.subtree(TitleScreenId).unwrap().root.kids() == title.root.kids()
|
|
1015
|
+
done := ticksOf(io, mid, 15, Nil, 1.0 / 60.0)
|
|
1016
|
+
assert done.shift.isNone()
|
|
1017
|
+
assert drawnOf(done).find(|d| d.actor.key() == NameId).isNone()
|
|
1018
|
+
# again after game over comes in at once
|
|
1019
|
+
assert tickOf(io, overWith(io, 90), Nil, press(Key.Enter)).shift.isNone()
|
examples/games/two_screens.plum
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# Two screens with a fade (see docs/game.md, "Screens and transitions"): a title that
|
|
2
|
+
# Enter leaves, and a play screen that fades in over it and Escape leaves again.
|
|
3
|
+
# `plum run examples/games/two_screens.plum`.
|
|
4
|
+
module two_screens
|
|
5
|
+
import std/Int
|
|
6
|
+
import std/Float
|
|
7
|
+
import std/Str
|
|
8
|
+
import std/Option
|
|
9
|
+
import std/Io
|
|
10
|
+
import std/game/Vec2
|
|
11
|
+
import std/game/Color
|
|
12
|
+
import std/game/gfx
|
|
13
|
+
import std/game/input
|
|
14
|
+
import std/game/ui
|
|
15
|
+
import std/game/Store
|
|
16
|
+
import std/game/Game
|
|
17
|
+
import std/collections/functional/FRoseTree
|
|
18
|
+
import std/game/scene/Place
|
|
19
|
+
import std/game/scene/Actor
|
|
20
|
+
import std/game/scene/Ctx
|
|
21
|
+
import std/game/scene/SceneTree
|
|
22
|
+
|
|
23
|
+
enum Msg = Start | Back
|
|
24
|
+
|
|
25
|
+
# The state names the screen shown, by the screen's own name.
|
|
26
|
+
struct State(on: Str, starts: Int) is Reducer[Msg] =
|
|
27
|
+
fun update(self, msg: Msg) -> State = match msg
|
|
28
|
+
Start => State(..self, on: PlayScreen.name(), starts: self.starts + 1)
|
|
29
|
+
Back => State(..self, on: TitleScreen.name())
|
|
30
|
+
|
|
31
|
+
fun screen(self) -> Str = self.on
|
|
32
|
+
|
|
33
|
+
# The title: Enter starts.
|
|
34
|
+
struct TitleScreen(id: Int) is Screen[State, Msg] =
|
|
35
|
+
fun key(self) -> Int = self.id
|
|
36
|
+
|
|
37
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
38
|
+
|
|
39
|
+
fun rules(self, t: SceneTree[Msg], ctx: Ctx[Msg], state: State, dt: Float) -> SceneTree[Msg] =
|
|
40
|
+
if ctx.input.isKeyPressed(Key.Enter)
|
|
41
|
+
ctx.send(Start)
|
|
42
|
+
t
|
|
43
|
+
|
|
44
|
+
# The play: it fades in over the title in half a second, and Escape goes back to the
|
|
45
|
+
# title, which resumes as it was left.
|
|
46
|
+
struct PlayScreen(id: Int) is Screen[State, Msg] =
|
|
47
|
+
fun key(self) -> Int = self.id
|
|
48
|
+
|
|
49
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
50
|
+
|
|
51
|
+
fun transition(self, from: Str) -> Transition = Fade(secs: 0.5)
|
|
52
|
+
|
|
53
|
+
fun rules(self, t: SceneTree[Msg], ctx: Ctx[Msg], state: State, dt: Float) -> SceneTree[Msg] =
|
|
54
|
+
if ctx.input.isKeyPressed(Key.Escape)
|
|
55
|
+
ctx.send(Back)
|
|
56
|
+
t
|
|
57
|
+
|
|
58
|
+
# Each time the state switches to it: the label shows how many runs there were. (The
|
|
59
|
+
# `Start` that switched came while the title was current, so the title reacted to it.)
|
|
60
|
+
fun enter(self, t: SceneTree[Msg], state: State, tree: SceneTree[Msg]) -> SceneTree[Msg] =
|
|
61
|
+
t.updateAs[Label[Msg]](1, |l| l.relabel("run {state.starts}"))
|
|
62
|
+
|
|
63
|
+
fun draw(self, io: Io, shown: SceneTree[Msg]) =
|
|
64
|
+
gfx.clear(io, Color.Black)
|
|
65
|
+
shown.draw(io)
|
|
66
|
+
|
|
67
|
+
fun main(io: Io) -> Game[State, Msg] = start()
|
|
68
|
+
|
|
69
|
+
fun start() -> Game[State, Msg] =
|
|
70
|
+
tree: SceneTree[Msg] := SceneTree.ofScreens([
|
|
71
|
+
FRoseTree.of(TitleScreen(id: 10), [FRoseTree.leaf(Label(id: 1, x: 20.0, y: 20.0, text: "Press Enter", size: 24.0, color: Color.White))]),
|
|
72
|
+
FRoseTree.of(PlayScreen(id: 20), [FRoseTree.leaf(Label(id: 1, x: 20.0, y: 20.0, text: "run 0", size: 24.0, color: Color.Yellow))])])
|
|
73
|
+
Game.start(tree, State(on: TitleScreen.name(), starts: 0))
|
|
74
|
+
|
|
75
|
+
fun press(k: Int) -> Input = Input(held: Held.none(), events: [KeyDown(key: k)].toSlice(), pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0)
|
|
76
|
+
|
|
77
|
+
test "Enter fades the play screen in over the title, and Escape goes back"
|
|
78
|
+
view := Vec2(x: 800.0, y: 600.0)
|
|
79
|
+
g := start().step(io, press(Key.Enter), 1.0 / 60.0, view)
|
|
80
|
+
assert g.current == "PlayScreen" && g.shift.isSome()
|
|
81
|
+
assert g.screenTree().findAs[Label[Msg]](1).unwrap().text == "run 1"
|
|
82
|
+
# the title's label is still drawn, fading out
|
|
83
|
+
assert g.shown().flatten().length() == 1 + 2 + 2 + 2
|
|
84
|
+
idle := Input.create()
|
|
85
|
+
done := 0.until(30).fold(g, |cur, _| cur.step(io, idle, 1.0 / 60.0, view))
|
|
86
|
+
assert done.shift.isNone() && done.shown().flatten().length() == 1 + 1 + 1
|
|
87
|
+
back := done.step(io, press(Key.Escape), 1.0 / 60.0, view)
|
|
88
|
+
assert back.current == TitleScreen.name() && back.shift.isNone()
|
std/game/Game.plum
CHANGED
|
@@ -21,69 +21,214 @@ import std/game/scene/Ctx
|
|
|
21
21
|
import std/game/scene/SceneTree
|
|
22
22
|
|
|
23
23
|
# The one way to make a game: a `Game[S, M]` over a store of the state `S` (a
|
|
24
|
-
# `Reducer[M]`, std/game/Store)
|
|
24
|
+
# `Reducer[M]`, std/game/Store) and one tree of actors (`SceneTree[M]` of `Actor[M]`,
|
|
25
|
-
#
|
|
25
|
+
# std/game/scene) whose root's children are the game's screens. `M` is the game's message
|
|
26
|
-
# writes its actors, its screens and its reducer; `Game` runs the loop:
|
|
26
|
+
# type. A game writes its actors, its screens and its reducer; `Game` runs the loop:
|
|
27
27
|
#
|
|
28
28
|
# fun main(io: Io) -> Game[RunState, Msg] =
|
|
29
|
+
# tree: SceneTree[Msg] := SceneTree.ofScreens([
|
|
30
|
+
# FRoseTree.of(TitleScreen(id: 1), [..]),
|
|
31
|
+
# FRoseTree.leaf(PlayScreen(id: 2))])
|
|
29
|
-
# Game.start(
|
|
32
|
+
# Game.start(tree, RunState.create(io.randomSeed())) # its screen() is TitleScreen.name()
|
|
30
33
|
#
|
|
31
34
|
# A `main` that returns a `Game` is a game (the compiler adds the frame entry points the
|
|
32
35
|
# host calls, which call `update` once per fixed 1/60 s tick and `draw` once a frame).
|
|
33
36
|
#
|
|
37
|
+
# The screens. Each child of the root is a screen: an actor that `is Screen[S, M]` (so an
|
|
38
|
+
# `Actor[M]` too) whose subtree is what the screen shows (a title, the play, a game over).
|
|
39
|
+
# Each has a `name`, and the state picks the one shown: `state.screen()` (a `Reducer`
|
|
40
|
+
# method) is the current screen's name, "" (the default) being the first screen. Only the
|
|
41
|
+
# current screen is shown, gets input and steps; the others are paused and kept as they
|
|
42
|
+
# are, so coming back to one resumes it where it was. A screen has rules, reacts to
|
|
43
|
+
# messages, sets itself up when the state switches to it, and says how it comes in (a
|
|
44
|
+
# `Transition`). Ids need to be unique only within a screen (and apart from the root's
|
|
45
|
+
# and the screens' own).
|
|
46
|
+
#
|
|
47
|
+
# `Game.start` stops the program if a child of the root is not a screen, if two screens
|
|
48
|
+
# share a name, or if no screen has the state's. A name the state asks for later that no
|
|
49
|
+
# screen has keeps the current screen, and is reported once on the host's output (and in
|
|
50
|
+
# `game.unknown`).
|
|
51
|
+
#
|
|
34
52
|
# One tick (`update`), always in this order:
|
|
35
53
|
#
|
|
36
54
|
# 1. Each pointer event of the tick goes to the topmost shown actor under it that says
|
|
37
|
-
# it is hit, through `input`, then to each of its parents
|
|
55
|
+
# it is hit, through `input`, then to each of its parents up to the root
|
|
38
|
-
# `PointerMove` that leaves the pointer where it was (the host
|
|
56
|
+
# (`SceneTree.input`). A `PointerMove` that leaves the pointer where it was (the host
|
|
39
|
-
# frame) goes to no actor.
|
|
57
|
+
# reports one every frame) goes to no actor.
|
|
40
|
-
# 2.
|
|
58
|
+
# 2. The root and the current screen's actors step (`step`), then the screen's `rules`
|
|
41
|
-
# rules see the tick's input (held keys, the pointer) in
|
|
59
|
+
# run. The actors and the rules see the tick's input (held keys, the pointer) in
|
|
60
|
+
# `ctx.input`.
|
|
42
|
-
# 3. `Spawn` and `Kill` apply
|
|
61
|
+
# 3. `Spawn` and `Kill` apply to the root and the current screen: those pushed by
|
|
62
|
+
# `input` in the step's own walk, those of the steps after it, those of the rules
|
|
43
|
-
#
|
|
63
|
+
# after that (`SceneTree.step`, `apply`).
|
|
44
64
|
# 4. Each `Send(msg)`, in the order pushed: the store reduces it (`update`), then the
|
|
45
|
-
# screen `react`s to the new state
|
|
65
|
+
# current screen `react`s to the new state.
|
|
46
|
-
# `next` that gives a screen wins; the old screen takes the rest of the tick.
|
|
47
66
|
# 5. After the tick: the store's `flush(io)` runs `handle(io, msg)` for each message in
|
|
67
|
+
# order. Then, if `state.screen()` names another screen, that screen `enter`s and
|
|
48
|
-
#
|
|
68
|
+
# becomes current, coming in by its `transition`. Then `Restart` (the first tree and
|
|
49
|
-
# or `Exit` (no tick runs after it; the last
|
|
69
|
+
# state again) or `Exit` (no tick runs after it; the last frame keeps being drawn).
|
|
50
70
|
#
|
|
51
|
-
# `draw` draws the current screen. All randomness belongs in `S` and changes only in
|
|
52
|
-
#
|
|
71
|
+
# A transition other than `Cut` lasts its `secs`: while it runs both screens are drawn,
|
|
53
|
-
|
|
54
|
-
#
|
|
72
|
+
# the one going out fading or sliding out and the one coming in fading or sliding in, and
|
|
55
|
-
#
|
|
73
|
+
# only the one coming in gets input and steps (the one going out is frozen). `Game`
|
|
74
|
+
# animates them by a parent of each screen with an offset and an opacity, so the screen's
|
|
75
|
+
# actors see them in their `ctx` too.
|
|
56
76
|
#
|
|
77
|
+
# `draw` draws the root and the screens shown, through the current screen's `draw`. All
|
|
78
|
+
# randomness belongs in `S` and changes only in `update`, so one seed and one list of
|
|
79
|
+
# inputs replay the same game.
|
|
80
|
+
|
|
81
|
+
# How a screen comes in when the state switches to it: at once (`Cut`), fading in over
|
|
82
|
+
# the old one (`Fade`), or sliding in from an edge of the screen and pushing the old one
|
|
83
|
+
# out of the other side (`Slide`), over `secs` seconds.
|
|
84
|
+
enum Transition =
|
|
85
|
+
| Cut
|
|
86
|
+
| Fade(secs: Float)
|
|
87
|
+
| Slide(from: Edge, secs: Float)
|
|
88
|
+
|
|
89
|
+
# How long it lasts, in seconds (0 for a cut).
|
|
90
|
+
fun secs(self) -> Float = match self
|
|
91
|
+
Cut => 0.0
|
|
92
|
+
Fade(s) => s
|
|
93
|
+
Slide(_, s) => s
|
|
94
|
+
|
|
95
|
+
# The edge of the screen a sliding screen comes in from.
|
|
96
|
+
enum Edge = FromLeft | FromRight | FromTop | FromBottom
|
|
97
|
+
|
|
98
|
+
# One screen of a game (a title, the play, the game over): an actor whose subtree is what
|
|
99
|
+
# it shows, with what it does with the state. `S` is the game's state and `M` its message
|
|
57
|
-
#
|
|
100
|
+
# type. Each method gets `t`, the screen's own subtree (its root is this screen), and
|
|
101
|
+
# returns it changed; `Game` puts it back in the tree.
|
|
102
|
+
#
|
|
58
|
-
# rules
|
|
103
|
+
# rules rules over the whole screen after its actors stepped (collisions); they
|
|
59
|
-
#
|
|
104
|
+
# act through `ctx` (`ctx.send(msg)`, `ctx.out.push(Kill(..))`)
|
|
60
|
-
# react
|
|
105
|
+
# react the screen after a message was reduced: show the new state (a label)
|
|
61
|
-
#
|
|
106
|
+
# enter the screen when the state switches to it: `tree` is the whole tree, so a
|
|
107
|
+
# screen can start from what another one shows. By default it resumes as
|
|
62
|
-
#
|
|
108
|
+
# it was left.
|
|
109
|
+
# name its name, a static: the type's own by default (`PlayScreen.name()` is
|
|
110
|
+
# "PlayScreen"). The state picks the screen shown by it (`state.screen()`).
|
|
111
|
+
# screenName the name of a screen held as a `Screen` (its type's `name()`)
|
|
112
|
+
# transition how it comes in from the screen named `from`
|
|
113
|
+
# draw draws the frame while it is the current screen: `shown` is the root with
|
|
114
|
+
# the screens shown (this one, and the one going out during a transition)
|
|
63
|
-
trait Screen[S, M] =
|
|
115
|
+
trait Screen[S, M] is Actor[M] =
|
|
116
|
+
fun name() -> Str = @typeName(Self)
|
|
117
|
+
|
|
64
|
-
fun
|
|
118
|
+
fun screenName(self) -> Str = Self.name()
|
|
65
|
-
|
|
66
|
-
|
|
119
|
+
|
|
67
|
-
|
|
68
|
-
fun rules(self, ctx: Ctx[M], state: S, dt: Float) ->
|
|
120
|
+
fun rules(self, t: SceneTree[M], ctx: Ctx[M], state: S, dt: Float) -> SceneTree[M] = t
|
|
69
|
-
|
|
121
|
+
|
|
70
|
-
fun react(self, state: S, msg: M) ->
|
|
122
|
+
fun react(self, t: SceneTree[M], state: S, msg: M) -> SceneTree[M] = t
|
|
71
|
-
|
|
123
|
+
|
|
72
|
-
fun
|
|
124
|
+
fun enter(self, t: SceneTree[M], state: S, tree: SceneTree[M]) -> SceneTree[M] = t
|
|
125
|
+
|
|
73
|
-
|
|
126
|
+
fun transition(self, from: Str) -> Transition = Cut
|
|
127
|
+
|
|
74
|
-
fun draw(self, io: Io) =
|
|
128
|
+
fun draw(self, io: Io, shown: SceneTree[M]) = shown.draw(io)
|
|
129
|
+
|
|
75
|
-
|
|
130
|
+
# A transition in progress: from the screen `from`, `t` seconds in.
|
|
131
|
+
struct Shift(from: Str, how: Transition, t: Float) =
|
|
132
|
+
# How far it is, from 0 to 1.
|
|
133
|
+
fun progress(self) -> Float =
|
|
134
|
+
s := self.how.secs()
|
|
135
|
+
if s <= 0.0 then 1.0 else {self.t / s}.min(1.0)
|
|
136
|
+
|
|
137
|
+
# The offset and opacity of the screen coming in (`incoming`) or going out, on a screen
|
|
138
|
+
# of `view` pixels.
|
|
139
|
+
fun frame[M](self, incoming: Bool, view: Vec2) -> ShiftFrame[M] =
|
|
140
|
+
p := self.progress()
|
|
141
|
+
q := 1.0 - p
|
|
142
|
+
d := if incoming then q else -p
|
|
143
|
+
match self.how
|
|
144
|
+
Fade(_) => ShiftFrame(id: ShiftFrameId, x: 0.0, y: 0.0, alpha: if incoming then p else q)
|
|
145
|
+
Slide(edge, _) => match edge
|
|
146
|
+
FromLeft => ShiftFrame(id: ShiftFrameId, x: -d * view.x, y: 0.0, alpha: 1.0)
|
|
147
|
+
FromRight => ShiftFrame(id: ShiftFrameId, x: d * view.x, y: 0.0, alpha: 1.0)
|
|
148
|
+
FromTop => ShiftFrame(id: ShiftFrameId, x: 0.0, y: -d * view.y, alpha: 1.0)
|
|
149
|
+
FromBottom => ShiftFrame(id: ShiftFrameId, x: 0.0, y: d * view.y, alpha: 1.0)
|
|
150
|
+
Cut => ShiftFrame(id: ShiftFrameId, x: 0.0, y: 0.0, alpha: 1.0)
|
|
151
|
+
|
|
76
|
-
#
|
|
152
|
+
# The parent `Game` puts over a screen during a transition: its offset and its opacity.
|
|
77
|
-
|
|
153
|
+
struct ShiftFrame[M](id: Int, x: Float, y: Float, alpha: Float) is Actor[M] =
|
|
78
|
-
|
|
154
|
+
fun key(self) -> Int = self.id
|
|
79
155
|
|
|
156
|
+
fun place(self) -> Place = place.at(self.x, self.y)
|
|
157
|
+
|
|
158
|
+
fun opacity(self) -> Float = self.alpha
|
|
159
|
+
|
|
160
|
+
# The id of a `ShiftFrame`.
|
|
161
|
+
ShiftFrameId = -1000001
|
|
162
|
+
|
|
163
|
+
# A running game: the tree (the root and its screens), the store, the name of the current
|
|
164
|
+
# screen, a name the state asks for that no screen has ("" if none), the transition in
|
|
165
|
+
# progress, the first tree and state (for `Restart`), whether it has exited, the ticks it
|
|
166
|
+
# has run, where the last pointer event was, and the size of the screen at the last tick.
|
|
167
|
+
struct Game[S: Reducer[M], M](
|
|
168
|
+
tree: SceneTree[M],
|
|
169
|
+
store: Store[S, M],
|
|
170
|
+
current: Str,
|
|
171
|
+
unknown: Str,
|
|
172
|
+
shift: Option[Shift],
|
|
173
|
+
first: SceneTree[M],
|
|
174
|
+
initial: S,
|
|
175
|
+
exited: Bool,
|
|
176
|
+
ticks: Int,
|
|
177
|
+
pointer: Vec2,
|
|
178
|
+
view: Vec2
|
|
179
|
+
) =
|
|
180
|
+
|
|
80
|
-
# The only way to make a game: `
|
|
181
|
+
# The only way to make a game: `tree`, whose root's children are the screens, with
|
|
182
|
+
# `state` in the store; the screen shown is the one `state.screen()` names. Stops the
|
|
183
|
+
# program if a child of the root is not a `Screen[S, M]`, if two screens have one name,
|
|
184
|
+
# or if no screen has the state's (see `screensError`).
|
|
81
|
-
fun start(
|
|
185
|
+
fun start(tree: SceneTree[M], state: S) -> Game[S, M] =
|
|
186
|
+
name := chosen[S, M](tree, state.screen())
|
|
187
|
+
if Some(e) := screensError[S, M](tree, name)
|
|
188
|
+
crash e
|
|
189
|
+
none: Option[Shift] := None
|
|
190
|
+
Game(
|
|
82
|
-
|
|
191
|
+
tree: tree, store: Store.of(state), current: name, unknown: "", shift: none, first: tree, initial: state,
|
|
192
|
+
exited: false, ticks: 0, pointer: NoPointer, view: Vec2(x: 0.0, y: 0.0))
|
|
193
|
+
|
|
194
|
+
# The key of the current screen's actor.
|
|
195
|
+
fun currentKey(self) -> Int = keyOf[S, M](self.tree, self.current)
|
|
83
196
|
|
|
84
197
|
# The state now.
|
|
85
198
|
fun state(self) -> S = self.store.state
|
|
86
199
|
|
|
200
|
+
# The current screen's subtree (its root is the screen), empty if there is no such
|
|
201
|
+
# screen.
|
|
202
|
+
fun screenTree(self) -> SceneTree[M] = self.tree.withRoot(screenNode(self.tree.root.kids(), self.currentKey()))
|
|
203
|
+
|
|
204
|
+
# What `draw` draws: the root over the current screen and, during a transition, the one
|
|
205
|
+
# going out (each under its `ShiftFrame`).
|
|
206
|
+
fun shown(self) -> SceneTree[M] =
|
|
207
|
+
match self.tree.root
|
|
208
|
+
FRoseEmpty => self.tree
|
|
209
|
+
FRoseNode(r, k, _) =>
|
|
210
|
+
now := screenNode(k, self.currentKey())
|
|
211
|
+
kids := match self.shift
|
|
212
|
+
Some(s) =>
|
|
213
|
+
out := framed(screenNode(k, keyOf[S, M](self.tree, s.from)), s.frame(false, self.view))
|
|
214
|
+
out.concat(framed(now, s.frame(true, self.view)))
|
|
215
|
+
None => now
|
|
216
|
+
node: FRoseTree[Actor[M]] := FRoseNode(value: r, firstChild: kids, nextSibling: FRoseEmpty)
|
|
217
|
+
self.tree.withRoot(node)
|
|
218
|
+
|
|
219
|
+
# The root over the screen that is stepped and gets input: the current one, under its
|
|
220
|
+
# `ShiftFrame` during a transition.
|
|
221
|
+
fun live(self) -> SceneTree[M] =
|
|
222
|
+
match self.tree.root
|
|
223
|
+
FRoseEmpty => self.tree
|
|
224
|
+
FRoseNode(r, k, _) =>
|
|
225
|
+
now := screenNode(k, self.currentKey())
|
|
226
|
+
kids := match self.shift
|
|
227
|
+
Some(s) => framed(now, s.frame(true, self.view))
|
|
228
|
+
None => now
|
|
229
|
+
node: FRoseTree[Actor[M]] := FRoseNode(value: r, firstChild: kids, nextSibling: FRoseEmpty)
|
|
230
|
+
self.tree.withRoot(node)
|
|
231
|
+
|
|
87
232
|
# One tick of `dt` seconds with `input` on the host's screen: what the host runs.
|
|
88
233
|
fun update(self, io: Io, input: Input, dt: Float) -> Game[S, M] =
|
|
89
234
|
self.step(io, input, dt, Vec2(x: gfx.screenWidth(io), y: gfx.screenHeight(io)))
|
|
@@ -95,28 +240,216 @@ struct Game[S: Reducer[M], M](screen: Screen[S, M], store: Store[S, M], first: S
|
|
|
95
240
|
|
|
96
241
|
fun tick(self, io: Io, input: Input, dt: Float, view: Vec2) -> Game[S, M] =
|
|
97
242
|
ctx: Ctx[M] := Ctx.of(input, view)
|
|
243
|
+
key := self.currentKey()
|
|
98
|
-
routed := routeAll(self.
|
|
244
|
+
routed := routeAll(self.live(), ctx, input.events, 0, self.pointer)
|
|
99
245
|
stepped := routed.step(ctx, dt)
|
|
100
|
-
ruled :=
|
|
246
|
+
ruled := onScreen[S, M](stepped, key, |s, t| s.rules(t, ctx, self.store.state, dt))
|
|
101
|
-
applied := ruled.
|
|
247
|
+
applied := ruled.apply(ctx.out)
|
|
102
248
|
ctx.keepOnly(true)
|
|
249
|
+
back := self.tree.withRoot(spliced(self.tree.root, applied.root, key))
|
|
103
|
-
begin: Turn[S, M] := Turn(
|
|
250
|
+
begin: Turn[S, M] := Turn(tree: back, store: self.store, restart: false, exit: false)
|
|
104
|
-
turn := 0.until(ctx.out.length()).fold(begin, |tu, i| tu.take(ctx.out.at(i)))
|
|
251
|
+
turn := 0.until(ctx.out.length()).fold(begin, |tu, i| tu.take(ctx.out.at(i), key))
|
|
105
252
|
flushed := turn.store.flush(io)
|
|
106
|
-
shown := match turn.next
|
|
107
|
-
Some(s) => s
|
|
108
|
-
None => turn.screen
|
|
109
253
|
pointer := lastPointer(input.events, self.pointer)
|
|
110
254
|
if turn.restart && !turn.exit
|
|
255
|
+
none: Option[Shift] := None
|
|
256
|
+
Game(
|
|
111
|
-
|
|
257
|
+
..self, tree: self.first, store: Store.of(self.initial), current: chosen[S, M](self.first, self.initial.screen()), unknown: "", shift: none,
|
|
258
|
+
ticks: self.ticks + 1, pointer: pointer, view: view)
|
|
112
259
|
else
|
|
113
|
-
Game(..self,
|
|
260
|
+
ran := Game(..self, tree: turn.tree, store: flushed, shift: advanced(self.shift, dt), exited: turn.exit, ticks: self.ticks + 1, pointer: pointer, view: view)
|
|
114
|
-
|
|
261
|
+
ran.switched(io)
|
|
262
|
+
|
|
263
|
+
# The game with the screen `state.screen()` names current, if it is another one: it
|
|
264
|
+
# `enter`s, and comes in by its `transition`. A name no screen has keeps the current
|
|
265
|
+
# screen, and is reported once (`unknown`, and a line on the host's output).
|
|
266
|
+
fun switched(self, io: Io) -> Game[S, M] =
|
|
267
|
+
to := chosen[S, M](self.tree, self.store.state.screen())
|
|
268
|
+
key := keyOf[S, M](self.tree, to)
|
|
269
|
+
if to == self.current
|
|
270
|
+
Game(..self, unknown: "")
|
|
271
|
+
else if key == NoScreen
|
|
272
|
+
if to != self.unknown
|
|
273
|
+
io.printLn("game: the state asks for the screen '{to}', and no screen has that name: staying on '{self.current}'")
|
|
274
|
+
Game(..self, unknown: to)
|
|
275
|
+
else
|
|
276
|
+
st := self.store.state
|
|
277
|
+
whole := self.tree
|
|
278
|
+
entered := onScreen[S, M](self.tree, key, |s, t| s.enter(t, st, whole))
|
|
279
|
+
how := transitionTo[S, M](entered, key, self.current)
|
|
280
|
+
shift: Option[Shift] := if how.secs() > 0.0 then Some(Shift(from: self.current, how: how, t: 0.0)) else None
|
|
281
|
+
Game(..self, tree: entered, current: to, unknown: "", shift: shift)
|
|
282
|
+
|
|
283
|
+
# Draws a frame: the current screen's `draw` (by default the tree's) of what is shown.
|
|
115
|
-
fun draw(self, io: Io) =
|
|
284
|
+
fun draw(self, io: Io) =
|
|
285
|
+
v := self.shown()
|
|
286
|
+
match screenNode(self.tree.root.kids(), self.currentKey()).top()
|
|
287
|
+
Some(a) => match a
|
|
288
|
+
Screen[S, M] as s => s.draw(io, v)
|
|
289
|
+
_ => v.draw(io)
|
|
290
|
+
None => v.draw(io)
|
|
116
291
|
|
|
117
292
|
# Where the pointer is before a game's first pointer event: nowhere on any screen.
|
|
118
293
|
NoPointer = Vec2(x: -1.0e18, y: -1.0e18)
|
|
119
294
|
|
|
295
|
+
# The transition after `t` more seconds: none once it is over (within a nanosecond, so
|
|
296
|
+
# that 30 ticks of 1/60 s make half a second).
|
|
297
|
+
fun advanced(s: Option[Shift], t: Float) -> Option[Shift] = match s
|
|
298
|
+
Some(x) =>
|
|
299
|
+
y := Shift(..x, t: x.t + t)
|
|
300
|
+
if y.t >= y.how.secs() - 0.000000001 then None else Some(y)
|
|
301
|
+
None => None
|
|
302
|
+
|
|
303
|
+
# How the screen keyed `to` of `tree` comes in from the screen named `from`: its
|
|
304
|
+
# `transition`, or a cut for an actor that is not a `Screen`.
|
|
305
|
+
fun transitionTo[S, M](tree: SceneTree[M], to: Int, from: Str) -> Transition =
|
|
306
|
+
match screenNode(tree.root.kids(), to).top()
|
|
307
|
+
Some(a) => match a
|
|
308
|
+
Screen[S, M] as s => s.transition(from)
|
|
309
|
+
_ => Cut
|
|
310
|
+
None => Cut
|
|
311
|
+
|
|
312
|
+
# The key no actor has: `keyOf` of a name no screen has.
|
|
313
|
+
NoScreen = -1000002
|
|
314
|
+
|
|
315
|
+
# The name of the screen `a` (a child of the root), "" if it is not a `Screen[S, M]`.
|
|
316
|
+
fun nameOf[S, M](a: Actor[M]) -> Str = match a
|
|
317
|
+
Screen[S, M] as s => s.screenName()
|
|
318
|
+
_ => ""
|
|
319
|
+
|
|
320
|
+
# The key of the screen of `tree` named `name`, `NoScreen` if there is none.
|
|
321
|
+
fun keyOf[S, M](tree: SceneTree[M], name: Str) -> Int =
|
|
322
|
+
match tree.root.kids().chain().find(|n| match n.top()
|
|
323
|
+
Some(a) => nameOf[S, M](a) == name
|
|
324
|
+
None => false)
|
|
325
|
+
Some(n) => n.top().map(|a| a.key()).unwrapOr(NoScreen)
|
|
326
|
+
None => NoScreen
|
|
327
|
+
|
|
328
|
+
# The name `asked` stands for in `tree`: itself, or for "" (a `Reducer`'s default) the
|
|
329
|
+
# first screen's.
|
|
330
|
+
fun chosen[S, M](tree: SceneTree[M], asked: Str) -> Str =
|
|
331
|
+
if asked != ""
|
|
332
|
+
asked
|
|
333
|
+
else
|
|
334
|
+
match tree.root.kids().top()
|
|
335
|
+
Some(a) => nameOf[S, M](a)
|
|
336
|
+
None => ""
|
|
337
|
+
|
|
338
|
+
# What is wrong with `tree` as a game's tree whose first screen is `name`, if anything:
|
|
339
|
+
# a child of the root that is not a `Screen[S, M]`, two screens with one name or one key,
|
|
340
|
+
# or no screen named `name`.
|
|
341
|
+
fun screensError[S, M](tree: SceneTree[M], name: Str) -> Option[Str] =
|
|
342
|
+
kids := tree.root.kids().chain()
|
|
343
|
+
names: List[Str] := kids.map(|n| n.top().map(|a| nameOf[S, M](a)).unwrapOr(""))
|
|
344
|
+
keys: List[Int] := kids.map(|n| n.top().map(|a| a.key()).unwrapOr(NoScreen))
|
|
345
|
+
plain := 0.until(kids.length()).find(|i| names.get(i).unwrapOr("") == "")
|
|
346
|
+
twice := 0.until(kids.length()).find(|i| 0.until(i).find(|j| names.get(j).unwrapOr("") == names.get(i).unwrapOr("")).isSome())
|
|
347
|
+
sameKey := 0.until(kids.length()).find(|i| 0.until(i).find(|j| keys.get(j).unwrapOr(NoScreen) == keys.get(i).unwrapOr(NoScreen)).isSome())
|
|
348
|
+
all := names.fold("", |acc, n| if acc == "" then "'{n}'" else "{acc}, '{n}'")
|
|
349
|
+
if kids.isEmpty()
|
|
350
|
+
Some("game: the tree has no screens: its root's children are the screens")
|
|
351
|
+
else if plain.isSome()
|
|
352
|
+
Some("game: the root's child {keys.get(plain.unwrap()).unwrapOr(0)} is not a screen (a Screen[S, M] with a name)")
|
|
353
|
+
else if twice.isSome()
|
|
354
|
+
Some("game: two screens are named '{names.get(twice.unwrap()).unwrapOr("")}'")
|
|
355
|
+
else if sameKey.isSome()
|
|
356
|
+
Some("game: two screens have the key {keys.get(sameKey.unwrap()).unwrapOr(0)}")
|
|
357
|
+
else if !names.fold(false, |acc, n| acc || n == name)
|
|
358
|
+
Some("game: the state's screen '{name}' is not in the tree (its screens are {all})")
|
|
359
|
+
else
|
|
360
|
+
None
|
|
361
|
+
|
|
362
|
+
# The screen `key` among the root's children `kids`, alone (with its subtree, without its
|
|
363
|
+
# siblings); empty if there is none.
|
|
364
|
+
fun screenNode[M](kids: FRoseTree[Actor[M]], key: Int) -> FRoseTree[Actor[M]] = match kids
|
|
365
|
+
FRoseEmpty => kids
|
|
366
|
+
FRoseNode(a, k, n) =>
|
|
367
|
+
if a.key() == key
|
|
368
|
+
empty: FRoseTree[Actor[M]] := FRoseEmpty
|
|
369
|
+
FRoseNode(value: a, firstChild: k, nextSibling: empty)
|
|
370
|
+
else
|
|
371
|
+
screenNode(n, key)
|
|
372
|
+
|
|
373
|
+
# The screen node `n` under the parent `f` (empty if there is no screen).
|
|
374
|
+
fun framed[M](n: FRoseTree[Actor[M]], f: ShiftFrame[M]) -> FRoseTree[Actor[M]] =
|
|
375
|
+
if n.isEmpty()
|
|
376
|
+
n
|
|
377
|
+
else
|
|
378
|
+
frame: Actor[M] := f
|
|
379
|
+
empty: FRoseTree[Actor[M]] := FRoseEmpty
|
|
380
|
+
FRoseNode(value: frame, firstChild: n, nextSibling: empty)
|
|
381
|
+
|
|
382
|
+
# The whole tree `whole` after the tick changed the root and the screen `key` in `live`
|
|
383
|
+
# (the root over that screen, as `Game.live` makes it): the new root, the screen put back
|
|
384
|
+
# in its place among the others (or dropped, if it was killed), and anything spawned
|
|
385
|
+
# under the root added after them.
|
|
386
|
+
fun spliced[M](whole: FRoseTree[Actor[M]], live: FRoseTree[Actor[M]], key: Int) -> FRoseTree[Actor[M]] =
|
|
387
|
+
match whole
|
|
388
|
+
FRoseEmpty => whole
|
|
389
|
+
FRoseNode(_, k, _) => match live
|
|
390
|
+
FRoseEmpty => live
|
|
391
|
+
FRoseNode(r, lk, _) =>
|
|
392
|
+
empty: FRoseTree[Actor[M]] := FRoseEmpty
|
|
393
|
+
kids := replaceKid(k, key, liveScreen(lk, key)).concat(liveExtras(lk, key))
|
|
394
|
+
FRoseNode(value: r, firstChild: kids, nextSibling: empty)
|
|
395
|
+
|
|
396
|
+
# The screen `key` in the live root's children `lk`: there, or the child of its frame.
|
|
397
|
+
fun liveScreen[M](lk: FRoseTree[Actor[M]], key: Int) -> FRoseTree[Actor[M]] = match lk
|
|
398
|
+
FRoseEmpty => lk
|
|
399
|
+
FRoseNode(a, k, n) =>
|
|
400
|
+
if a.key() == ShiftFrameId
|
|
401
|
+
screenNode(k, key)
|
|
402
|
+
else if a.key() == key
|
|
403
|
+
empty: FRoseTree[Actor[M]] := FRoseEmpty
|
|
404
|
+
FRoseNode(value: a, firstChild: k, nextSibling: empty)
|
|
405
|
+
else
|
|
406
|
+
liveScreen(n, key)
|
|
407
|
+
|
|
408
|
+
# The live root's children other than the screen `key` and its frame: spawned this tick.
|
|
409
|
+
fun liveExtras[M](lk: FRoseTree[Actor[M]], key: Int) -> FRoseTree[Actor[M]] = match lk
|
|
410
|
+
FRoseEmpty => lk
|
|
411
|
+
FRoseNode(a, k, n) =>
|
|
412
|
+
if a.key() == ShiftFrameId || a.key() == key
|
|
413
|
+
liveExtras(n, key)
|
|
414
|
+
else
|
|
415
|
+
FRoseNode(value: a, firstChild: k, nextSibling: liveExtras(n, key))
|
|
416
|
+
|
|
417
|
+
# The chain `kids` with the node keyed `key` replaced by the node `by` (removed if `by` is
|
|
418
|
+
# empty).
|
|
419
|
+
fun replaceKid[M](kids: FRoseTree[Actor[M]], key: Int, by: FRoseTree[Actor[M]]) -> FRoseTree[Actor[M]] = match kids
|
|
420
|
+
FRoseEmpty => kids
|
|
421
|
+
FRoseNode(a, k, n) =>
|
|
422
|
+
if a.key() == key
|
|
423
|
+
match by
|
|
424
|
+
FRoseEmpty => n
|
|
425
|
+
FRoseNode(b, bk, _) => FRoseNode(value: b, firstChild: bk, nextSibling: n)
|
|
426
|
+
else
|
|
427
|
+
FRoseNode(value: a, firstChild: k, nextSibling: replaceKid(n, key, by))
|
|
428
|
+
|
|
429
|
+
# The tree with the screen `key` (a child of the root) through `f` with its own subtree,
|
|
430
|
+
# if it is a `Screen[S, M]`; unchanged otherwise.
|
|
431
|
+
fun onScreen[S, M](tree: SceneTree[M], key: Int, f: fn(Screen[S, M], SceneTree[M]) -> SceneTree[M]) -> SceneTree[M] =
|
|
432
|
+
match tree.root
|
|
433
|
+
FRoseEmpty => tree
|
|
434
|
+
FRoseNode(r, k, n) =>
|
|
435
|
+
node := liveScreen(k, key)
|
|
436
|
+
match node.top()
|
|
437
|
+
Some(a) => match a
|
|
438
|
+
Screen[S, M] as s =>
|
|
439
|
+
sub := f(s, tree.withRoot(node))
|
|
440
|
+
framedNow := match k.top()
|
|
441
|
+
Some(x) => x.key() == ShiftFrameId
|
|
442
|
+
None => false
|
|
443
|
+
kids := if framedNow then framedAgain(k, sub.root) else replaceKid(k, key, sub.root)
|
|
444
|
+
tree.withRoot(FRoseNode(value: r, firstChild: kids, nextSibling: n))
|
|
445
|
+
_ => tree
|
|
446
|
+
None => tree
|
|
447
|
+
|
|
448
|
+
# The live root's children `lk` (a frame over the screen) with the screen replaced by `by`.
|
|
449
|
+
fun framedAgain[M](lk: FRoseTree[Actor[M]], by: FRoseTree[Actor[M]]) -> FRoseTree[Actor[M]] = match lk
|
|
450
|
+
FRoseEmpty => lk
|
|
451
|
+
FRoseNode(a, _, n) => FRoseNode(value: a, firstChild: by.alone(), nextSibling: n)
|
|
452
|
+
|
|
120
453
|
# The tree after the pointer events of `evs` from `i` on, in order; `at` is where the
|
|
121
454
|
# pointer was before them. A move to where it was is skipped.
|
|
122
455
|
fun routeAll[M](t: SceneTree[M], ctx: Ctx[M], evs: Slice[InputEvent], i: Int, at: Vec2) -> SceneTree[M] =
|
|
@@ -134,31 +467,43 @@ fun routeAll[M](t: SceneTree[M], ctx: Ctx[M], evs: Slice[InputEvent], i: Int, at
|
|
|
134
467
|
fun lastPointer(evs: Slice[InputEvent], at: Vec2) -> Vec2 =
|
|
135
468
|
0.until(evs.length()).fold(at, |p, j| evs.at(j).position().unwrapOr(p))
|
|
136
469
|
|
|
137
|
-
# A tick's game commands taken so far: the
|
|
470
|
+
# A tick's game commands taken so far: the tree and store after the messages, and whether
|
|
138
|
-
#
|
|
471
|
+
# a `Restart` or an `Exit` came.
|
|
139
|
-
struct Turn[S: Reducer[M], M](
|
|
472
|
+
struct Turn[S: Reducer[M], M](tree: SceneTree[M], store: Store[S, M], restart: Bool, exit: Bool) =
|
|
140
|
-
fun take(self, c: Cmd[M]) -> Turn[S, M] = match c
|
|
473
|
+
fun take(self, c: Cmd[M], current: Int) -> Turn[S, M] = match c
|
|
141
474
|
Send(m) =>
|
|
142
475
|
store := self.store.dispatch(m)
|
|
143
|
-
|
|
476
|
+
st := store.state
|
|
144
|
-
|
|
477
|
+
tree := onScreen[S, M](self.tree, current, |s, t| s.react(t, st, m))
|
|
145
|
-
Turn(..self,
|
|
478
|
+
Turn(..self, tree: tree, store: store)
|
|
146
479
|
Restart => Turn(..self, restart: true)
|
|
147
480
|
Exit => Turn(..self, exit: true)
|
|
148
481
|
_ => self
|
|
149
482
|
|
|
150
483
|
# --- tests ---
|
|
151
484
|
|
|
152
|
-
# A state that notes what happened to it in a shared log, and
|
|
485
|
+
# A state that notes what happened to it in a shared log, counts, and shows the screen
|
|
486
|
+
# `at`.
|
|
153
487
|
enum GMsg = GHit(id: Int) | GStep(id: Int) | GRule | GGo | GBack | GAgain | GQuit
|
|
154
488
|
|
|
489
|
+
GameTitleKey = 1000
|
|
490
|
+
|
|
491
|
+
GamePlayKey = 2000
|
|
492
|
+
|
|
155
|
-
struct GState(n: Int, log: Slice[Str]) is Reducer[GMsg] =
|
|
493
|
+
struct GState(n: Int, at: Str, log: Slice[Str]) is Reducer[GMsg] =
|
|
156
494
|
fun update(self, msg: GMsg) -> GState =
|
|
157
495
|
self.log.push("update {gname(msg)}")
|
|
496
|
+
at := match msg
|
|
497
|
+
GGo => GPlayScreen.name()
|
|
498
|
+
GBack => GTitleScreen.name()
|
|
499
|
+
GQuit => "nope"
|
|
500
|
+
_ => self.at
|
|
158
|
-
GState(..self, n: self.n + 1)
|
|
501
|
+
GState(..self, n: self.n + 1, at: at)
|
|
159
502
|
|
|
160
503
|
fun handle(self, io: Io, msg: GMsg) = self.log.push("handle {gname(msg)} n {self.n}")
|
|
161
504
|
|
|
505
|
+
fun screen(self) -> Str = self.at
|
|
506
|
+
|
|
162
507
|
fun gname(m: GMsg) -> Str = match m
|
|
163
508
|
GHit(id) => "hit {id}"
|
|
164
509
|
GStep(id) => "step {id}"
|
|
@@ -190,42 +535,46 @@ struct GProbe(id: Int, x: Float, log: Slice[Str], go: Bool) is Actor[GMsg] =
|
|
|
190
535
|
ctx.send(GGo)
|
|
191
536
|
self
|
|
192
537
|
|
|
193
|
-
# The first screen
|
|
538
|
+
# The first screen, at (5, 0), over probe 1 and then probe 2 on top of it. Its rules note
|
|
194
|
-
#
|
|
539
|
+
# themselves and send `GRule`; it counts what it reacted to and notes when it enters.
|
|
195
|
-
struct
|
|
540
|
+
struct GTitleScreen(log: Slice[Str], reacted: Int) is Screen[GState, GMsg] =
|
|
196
|
-
fun
|
|
541
|
+
fun key(self) -> Int = GameTitleKey
|
|
197
542
|
|
|
198
|
-
fun
|
|
543
|
+
fun place(self) -> Place = place.at(5.0, 0.0)
|
|
199
544
|
|
|
200
|
-
fun rules(self, ctx: Ctx[GMsg], state: GState, dt: Float) ->
|
|
545
|
+
fun rules(self, t: SceneTree[GMsg], ctx: Ctx[GMsg], state: GState, dt: Float) -> SceneTree[GMsg] =
|
|
201
546
|
self.log.push("rules")
|
|
202
547
|
ctx.send(GRule)
|
|
203
|
-
|
|
548
|
+
t
|
|
204
549
|
|
|
205
|
-
fun react(self, state: GState, msg: GMsg) ->
|
|
550
|
+
fun react(self, t: SceneTree[GMsg], state: GState, msg: GMsg) -> SceneTree[GMsg] =
|
|
206
551
|
self.log.push("react {gname(msg)} n {state.n}")
|
|
552
|
+
# `GAgain` moves its probe 1 (another screen may have a probe 1 too)
|
|
553
|
+
moved := match msg
|
|
554
|
+
GAgain => t.updateAs[GProbe](1, |p| GProbe(..p, x: 3.0))
|
|
555
|
+
_ => t
|
|
207
|
-
|
|
556
|
+
moved.withTop(GTitleScreen(..self, reacted: self.reacted + 1))
|
|
557
|
+
|
|
558
|
+
fun enter(self, t: SceneTree[GMsg], state: GState, tree: SceneTree[GMsg]) -> SceneTree[GMsg] =
|
|
559
|
+
self.log.push("enter title")
|
|
560
|
+
t
|
|
208
561
|
|
|
562
|
+
# The second screen, at (5, 0) over probes 9 and 10, coming in by `how`. When it enters it
|
|
563
|
+
# notes how many actors the whole tree has.
|
|
209
|
-
|
|
564
|
+
struct GPlayScreen(log: Slice[Str], how: Transition) is Screen[GState, GMsg] =
|
|
210
|
-
GGo =>
|
|
211
|
-
|
|
565
|
+
fun key(self) -> Int = GamePlayKey
|
|
212
|
-
s: Screen[GState, GMsg] := GPlay(t: gtree(self.log, 9, false), log: self.log)
|
|
213
|
-
Some(s)
|
|
214
|
-
_ => None
|
|
215
566
|
|
|
216
|
-
# The second screen, with the defaults but for `next`: back to a title on `GBack`.
|
|
217
|
-
struct GPlay(t: SceneTree[GMsg], log: Slice[Str]) is Screen[GState, GMsg] =
|
|
218
|
-
fun
|
|
567
|
+
fun place(self) -> Place = place.at(5.0, 0.0)
|
|
219
568
|
|
|
220
|
-
fun
|
|
569
|
+
fun enter(self, t: SceneTree[GMsg], state: GState, tree: SceneTree[GMsg]) -> SceneTree[GMsg] =
|
|
570
|
+
self.log.push("enter play from a tree of {tree.count()}")
|
|
571
|
+
t
|
|
221
572
|
|
|
222
|
-
fun
|
|
573
|
+
fun transition(self, from: Str) -> Transition = if from == GTitleScreen.name() then self.how else Cut
|
|
223
|
-
GBack =>
|
|
224
|
-
s: Screen[GState, GMsg] := GTitle(t: gtree(self.log, 1, false), log: self.log, reacted: 0)
|
|
225
|
-
Some(s)
|
|
226
|
-
_ => None
|
|
227
574
|
|
|
228
|
-
# An actor that
|
|
575
|
+
# An actor that, on the tick its `at` comes, pushes a `Restart` (`cmd` 0), an `Exit` (1),
|
|
576
|
+
# or sends `GBack` (2) or `GQuit` (3, asking for a screen no one has), or sends `GAgain`
|
|
577
|
+
# and kills actor 2 (4).
|
|
229
578
|
struct GSender(id: Int, at: Int, ticks: Int, cmd: Int) is Actor[GMsg] =
|
|
230
579
|
fun key(self) -> Int = self.id
|
|
231
580
|
|
|
@@ -237,32 +586,76 @@ struct GSender(id: Int, at: Int, ticks: Int, cmd: Int) is Actor[GMsg] =
|
|
|
237
586
|
ctx.out.push(Restart)
|
|
238
587
|
else if self.cmd == 1
|
|
239
588
|
ctx.out.push(Exit)
|
|
240
|
-
else
|
|
589
|
+
else if self.cmd == 2
|
|
241
590
|
ctx.send(GBack)
|
|
591
|
+
else if self.cmd == 3
|
|
592
|
+
ctx.send(GQuit)
|
|
593
|
+
else
|
|
594
|
+
ctx.send(GAgain)
|
|
595
|
+
ctx.out.push(Kill(id: 2))
|
|
242
596
|
GSender(..self, ticks: self.ticks + 1)
|
|
243
597
|
|
|
244
|
-
fun
|
|
598
|
+
fun gprobes(log: Slice[Str], first: Int, go: Bool) -> List[FRoseTree[Actor[GMsg]]] =
|
|
245
|
-
|
|
599
|
+
[FRoseTree.leaf(GProbe(id: first, x: 0.0, log: log, go: go)), FRoseTree.leaf(GProbe(id: first + 1, x: 0.0, log: log, go: false))]
|
|
600
|
+
|
|
601
|
+
# The title's subtree: the screen over probes 1 and 2 (probe 1 sends `GGo` if `go`).
|
|
602
|
+
fun gtitleNode(log: Slice[Str], go: Bool) -> FRoseTree[Actor[GMsg]] =
|
|
246
|
-
|
|
603
|
+
FRoseTree.of(GTitleScreen(log: log, reacted: 0), gprobes(log, 1, go))
|
|
604
|
+
|
|
605
|
+
fun gplayNode(log: Slice[Str], how: Transition, extra: List[FRoseTree[Actor[GMsg]]]) -> FRoseTree[Actor[GMsg]] =
|
|
247
|
-
|
|
606
|
+
FRoseTree.of(GPlayScreen(log: log, how: how), gprobes(log, 9, false).append(extra))
|
|
607
|
+
|
|
608
|
+
fun gtree(log: Slice[Str], go: Bool) -> SceneTree[GMsg] = SceneTree.ofScreens([gtitleNode(log, go), gplayNode(log, Cut, Nil)])
|
|
609
|
+
|
|
610
|
+
# A state that counts its messages and shows screen 0 (the default).
|
|
611
|
+
struct GOne(n: Int) is Reducer[GMsg] =
|
|
612
|
+
fun update(self, msg: GMsg) -> GOne = GOne(n: self.n + 1)
|
|
613
|
+
|
|
248
|
-
|
|
614
|
+
# The one screen of a `GOne` game, with only what a screen must have.
|
|
615
|
+
struct GOnlyScreen(id: Int) is Screen[GOne, GMsg] =
|
|
616
|
+
fun key(self) -> Int = self.id
|
|
249
617
|
|
|
618
|
+
fun place(self) -> Place = place.at(5.0, 0.0)
|
|
619
|
+
|
|
620
|
+
# `n` boxes under one group, for a big screen.
|
|
621
|
+
fun gboxes(n: Int, acc: List[FRoseTree[Actor[GMsg]]]) -> List[FRoseTree[Actor[GMsg]]] =
|
|
250
|
-
|
|
622
|
+
if n == 0 then acc else gboxes(n - 1, Cons(head: FRoseTree.leaf(Box(id: 100 + n, x: 0.0, y: 0.0, w: 1.0, h: 1.0, color: Color.Red)), tail: acc))
|
|
251
623
|
|
|
252
624
|
GameTestView = Vec2(x: 800.0, y: 600.0)
|
|
253
625
|
|
|
254
626
|
fun ginput(events: List[InputEvent]) -> Input =
|
|
255
627
|
Input(held: Held.none(), events: events.toSlice(), pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0)
|
|
256
628
|
|
|
629
|
+
fun gstate(log: Slice[Str]) -> GState = GState(n: 0, at: GTitleScreen.name(), log: log)
|
|
630
|
+
|
|
257
|
-
fun gstart(log: Slice[Str], go: Bool) -> Game[GState, GMsg] = Game.start(
|
|
631
|
+
fun gstart(log: Slice[Str], go: Bool) -> Game[GState, GMsg] = Game.start(gtree(log, go), gstate(log))
|
|
258
632
|
|
|
633
|
+
fun gtick(io: Io, g: Game[GState, GMsg]) -> Game[GState, GMsg] = g.step(io, ginput(Nil), 0.1, GameTestView)
|
|
634
|
+
|
|
635
|
+
# The current screen and how many messages the title reacted to.
|
|
259
|
-
fun gonScreen(g: Game[GState, GMsg]) -> Str = match g.
|
|
636
|
+
fun gonScreen(g: Game[GState, GMsg]) -> Str = match g.screenTree().root.top()
|
|
637
|
+
Some(a) => match a
|
|
260
|
-
|
|
638
|
+
GTitleScreen as t => "title {t.reacted}"
|
|
261
|
-
|
|
639
|
+
GPlayScreen as p => GPlayScreen.name()
|
|
262
|
-
|
|
640
|
+
_ => "?"
|
|
641
|
+
None => "none"
|
|
263
642
|
|
|
264
643
|
fun gcount(log: Slice[Str], prefix: Str) -> Int = 0.until(log.length()).fold(0, |n, i| if log.at(i).startsWith(prefix) then n + 1 else n)
|
|
265
644
|
|
|
645
|
+
fun gexact(log: Slice[Str], want: Str) -> Int = 0.until(log.length()).fold(0, |n, i| if log.at(i) == want then n + 1 else n)
|
|
646
|
+
|
|
647
|
+
# The ids of the actors drawn, in order, with their alphas and world x (`id@alpha@x`).
|
|
648
|
+
fun gdrawn(g: Game[GState, GMsg]) -> Str =
|
|
649
|
+
List.fromSlice(g.shown().flatten()).fold("", |acc, d| "{acc} {d.actor.key()}@{d.alpha}@{d.m.e}")
|
|
650
|
+
|
|
651
|
+
fun gnear(a: Float, b: Float) -> Bool = {a - b}.abs() < 0.000001
|
|
652
|
+
|
|
653
|
+
fun galpha(g: Game[GState, GMsg], id: Int) -> Float =
|
|
654
|
+
List.fromSlice(g.shown().flatten()).fold(-1.0, |acc, d| if d.actor.key() == id then d.alpha else acc)
|
|
655
|
+
|
|
656
|
+
fun gx(g: Game[GState, GMsg], id: Int) -> Float =
|
|
657
|
+
List.fromSlice(g.shown().flatten()).fold(-1.0, |acc, d| if d.actor.key() == id then d.m.e else acc)
|
|
658
|
+
|
|
266
659
|
test "one tick: input up the parents, steps, rules, then each message reduced and reacted to, then handled"
|
|
267
660
|
log := Slice[Str]()
|
|
268
661
|
g := gstart(log, false)
|
|
@@ -283,15 +676,24 @@ test "one tick: input up the parents, steps, rules, then each message reduced an
|
|
|
283
676
|
assert g1.ticks == 1
|
|
284
677
|
assert gonScreen(g1) == "title 4"
|
|
285
678
|
assert g.state().n == 0
|
|
679
|
+
assert gonScreen(g) == "title 0"
|
|
680
|
+
|
|
681
|
+
test "only the current screen is shown and drawn"
|
|
682
|
+
log := Slice[Str]()
|
|
683
|
+
g := gstart(log, false)
|
|
684
|
+
assert g.current == GTitleScreen.name()
|
|
685
|
+
assert gdrawn(g) == " [email protected]@0.0 [email protected]@5.0 [email protected]@5.0 [email protected]@5.0"
|
|
686
|
+
assert g.screenTree().count() == 3
|
|
687
|
+
assert g.tree.count() == 7
|
|
286
688
|
|
|
287
689
|
test "a pointer event goes to the hit actor's parents too, and misses go nowhere"
|
|
288
690
|
log := Slice[Str]()
|
|
289
|
-
t :=
|
|
691
|
+
t := SceneTree.of(GTitleScreen(log: log, reacted: 0)).withRoot(gtitleNode(log, false))
|
|
290
692
|
ctx: Ctx[GMsg] := Ctx.of(ginput(Nil), GameTestView)
|
|
291
693
|
t1 := t.input(ctx, PointerUp(x: 100.0, y: 5.0))
|
|
292
694
|
assert log.length() == 0
|
|
293
695
|
assert t1 == t
|
|
294
|
-
# the
|
|
696
|
+
# the screen is never hit itself, but gets the event on its way up (it ignores it)
|
|
295
697
|
_ := t.input(ctx, PointerUp(x: 6.0, y: 5.0))
|
|
296
698
|
assert log.length() == 1 && log.at(0) == "input 2 at 1.0"
|
|
297
699
|
assert ctx.takeSends().length() == 1
|
|
@@ -307,55 +709,104 @@ test "a move that leaves the pointer where it was goes to no actor"
|
|
|
307
709
|
assert gcount(log, "input") == 3
|
|
308
710
|
assert g2.pointer.x == 7.0
|
|
309
711
|
|
|
310
|
-
test "
|
|
712
|
+
test "the state switches the screen after the tick; the old one is paused and kept, and resumes"
|
|
311
713
|
log := Slice[Str]()
|
|
714
|
+
sender: FRoseTree[Actor[GMsg]] := FRoseTree.leaf(GSender(id: 50, at: 0, ticks: 0, cmd: 2))
|
|
715
|
+
tree := SceneTree.ofScreens([gtitleNode(log, true), gplayNode(log, Cut, [sender])])
|
|
716
|
+
g := Game.start(tree, gstate(log))
|
|
312
|
-
|
|
717
|
+
g1 := gtick(io, g)
|
|
313
|
-
g1 := g.step(io, ginput(Nil), 0.1, GameTestView)
|
|
314
|
-
assert gonScreen(g1) ==
|
|
718
|
+
assert gonScreen(g1) == GPlayScreen.name()
|
|
719
|
+
assert g1.current == GPlayScreen.name()
|
|
720
|
+
assert g1.shift.isNone()
|
|
315
|
-
# GGo came
|
|
721
|
+
# GGo came from probe 1's step: the title reacted to all four, then play entered
|
|
316
722
|
assert gcount(log, "react") == 4
|
|
317
|
-
assert gcount(log, "next") == 1
|
|
318
723
|
assert gcount(log, "handle") == 4
|
|
724
|
+
assert log.at(log.length() - 1) == "enter play from a tree of 8"
|
|
725
|
+
assert gdrawn(g1) == " [email protected]@0.0 [email protected]@5.0 [email protected]@5.0 [email protected]@5.0 [email protected]@5.0"
|
|
319
|
-
# the play screen
|
|
726
|
+
# the play screen steps, the title doesn't, and play's default rules and react do nothing
|
|
320
|
-
g2 :=
|
|
727
|
+
g2 := gtick(io, g1)
|
|
321
728
|
assert gcount(log, "step 9") == 1 && gcount(log, "step 10") == 1
|
|
729
|
+
assert gexact(log, "step 1") == 1
|
|
322
730
|
assert gcount(log, "react") == 4
|
|
323
|
-
assert g2.state().n == 4 + 2
|
|
731
|
+
assert g2.state().n == 4 + 2 + 1
|
|
732
|
+
# the sender's GBack switched back: the title resumes as it was left
|
|
324
|
-
assert gonScreen(g2) == "
|
|
733
|
+
assert gonScreen(g2) == "title 4"
|
|
325
|
-
|
|
734
|
+
assert gcount(log, "enter title") == 1
|
|
735
|
+
_ := gtick(io, g2)
|
|
736
|
+
assert gexact(log, "step 1") == 2
|
|
737
|
+
assert gcount(log, "step 9") == 1
|
|
738
|
+
|
|
739
|
+
test "a fade: both screens drawn, the old one frozen, input and steps only to the new one"
|
|
740
|
+
log := Slice[Str]()
|
|
741
|
+
tree := SceneTree.ofScreens([gtitleNode(log, true), gplayNode(log, Fade(secs: 0.3), Nil)])
|
|
742
|
+
g := Game.start(tree, gstate(log))
|
|
743
|
+
g1 := gtick(io, g)
|
|
744
|
+
assert g1.current == GPlayScreen.name()
|
|
745
|
+
assert g1.shift.isSome()
|
|
746
|
+
# the switch tick: the title still fully drawn, play on top of it, not yet seen
|
|
747
|
+
assert gdrawn(g1) == " [email protected]@0.0 [email protected]@0.0 [email protected]@5.0 [email protected]@5.0 [email protected]@5.0 [email protected]@0.0 [email protected]@5.0 [email protected]@5.0 [email protected]@5.0"
|
|
748
|
+
steps1 := gexact(log, "step 1")
|
|
749
|
+
# (7, 5) is on probe 2 and probe 10, one over the other: only play's gets it
|
|
750
|
+
g2 := g1.step(io, ginput([PointerDown(x: 7.0, y: 5.0)]), 0.1, GameTestView)
|
|
751
|
+
assert gcount(log, "input 10") == 1 && gcount(log, "input 2") == 0
|
|
752
|
+
assert gcount(log, "step 9") == 1
|
|
753
|
+
assert gexact(log, "step 1") == steps1
|
|
754
|
+
assert gnear(galpha(g2, 1), 2.0 / 3.0) && gnear(galpha(g2, 9), 1.0 / 3.0)
|
|
755
|
+
g3 := gtick(io, g2)
|
|
756
|
+
assert gnear(galpha(g3, 2), 1.0 / 3.0) && gnear(galpha(g3, 10), 2.0 / 3.0)
|
|
757
|
+
assert g3.shift.isSome()
|
|
758
|
+
# 0.3 s after the switch it is over: only play is drawn, with no frame
|
|
759
|
+
g4 := gtick(io, g3)
|
|
760
|
+
assert g4.shift.isNone()
|
|
761
|
+
assert gdrawn(g4) == " [email protected]@0.0 [email protected]@5.0 [email protected]@5.0 [email protected]@5.0"
|
|
762
|
+
assert gexact(log, "step 1") == steps1
|
|
763
|
+
assert gcount(log, "step 9") == 3
|
|
764
|
+
|
|
326
|
-
test "a
|
|
765
|
+
test "a slide moves the new screen in from its edge and pushes the old one out"
|
|
327
766
|
log := Slice[Str]()
|
|
328
|
-
|
|
767
|
+
tree := SceneTree.ofScreens([gtitleNode(log, true), gplayNode(log, Slide(from: FromRight, secs: 0.4), Nil)])
|
|
329
|
-
s: Screen[GState, GMsg] := GPlay(t: t, log: log)
|
|
330
|
-
g: Game[GState, GMsg] := Game.start(s, GState(n: 0, log: log))
|
|
331
|
-
g1 :=
|
|
768
|
+
g1 := gtick(io, Game.start(tree, gstate(log)))
|
|
769
|
+
assert gx(g1, 1) == 5.0 && gx(g1, 9) == 805.0
|
|
770
|
+
g2 := gtick(io, g1)
|
|
771
|
+
assert gnear(gx(g2, 1), 5.0 - 200.0) && gnear(gx(g2, 9), 5.0 + 600.0)
|
|
332
|
-
assert
|
|
772
|
+
assert galpha(g2, 1) == 1.0 && galpha(g2, 9) == 1.0
|
|
773
|
+
# the new screen is hit where it is drawn: (607, 5) is on probe 10
|
|
333
|
-
|
|
774
|
+
g3 := g2.step(io, ginput([PointerDown(x: 607.0, y: 5.0)]), 0.1, GameTestView)
|
|
334
|
-
assert
|
|
775
|
+
assert gcount(log, "input 10 at 2.0") == 1
|
|
335
|
-
|
|
776
|
+
g5 := gtick(io, gtick(io, g3))
|
|
777
|
+
assert g5.shift.isNone() && gx(g5, 9) == 5.0
|
|
778
|
+
# from the left and the top the offsets are the other way
|
|
779
|
+
s := Shift(from: "a", how: Slide(from: FromLeft, secs: 1.0), t: 0.25)
|
|
780
|
+
f: ShiftFrame[GMsg] := s.frame(true, GameTestView)
|
|
781
|
+
assert f.x == -600.0
|
|
782
|
+
t := Shift(from: "a", how: Slide(from: FromTop, secs: 1.0), t: 0.25)
|
|
783
|
+
o: ShiftFrame[GMsg] := t.frame(false, GameTestView)
|
|
784
|
+
assert o.y == 150.0
|
|
785
|
+
|
|
336
|
-
test "restart brings back the first
|
|
786
|
+
test "restart brings back the first tree and state after the tick's effects"
|
|
337
787
|
log := Slice[Str]()
|
|
338
|
-
|
|
788
|
+
sender: FRoseTree[Actor[GMsg]] := FRoseTree.leaf(GSender(id: 50, at: 2, ticks: 0, cmd: 0))
|
|
339
|
-
|
|
789
|
+
tree := SceneTree.ofScreens([FRoseTree.of(GTitleScreen(log: log, reacted: 0), gprobes(log, 1, false).append([sender])), gplayNode(log, Cut, Nil)])
|
|
340
|
-
g: Game[GState, GMsg] := Game.start(
|
|
790
|
+
g: Game[GState, GMsg] := Game.start(tree, gstate(log))
|
|
341
|
-
g2 :=
|
|
791
|
+
g2 := gtick(io, gtick(io, g))
|
|
342
792
|
assert g2.state().n == 6
|
|
343
793
|
assert gonScreen(g2) == "title 6"
|
|
344
|
-
g3 :=
|
|
794
|
+
g3 := gtick(io, g2)
|
|
345
795
|
# the restart tick's messages were reduced and handled, then the game started over
|
|
346
796
|
assert gcount(log, "handle") == 9
|
|
347
797
|
assert g3.state().n == 0
|
|
348
798
|
assert gonScreen(g3) == "title 0"
|
|
349
799
|
assert g3.ticks == 3
|
|
350
|
-
# the first
|
|
800
|
+
# the first tree's sender counts from 0 again
|
|
351
|
-
g5 :=
|
|
801
|
+
g5 := gtick(io, gtick(io, gtick(io, g3)))
|
|
352
802
|
assert g5.state().n == 0
|
|
353
803
|
|
|
354
804
|
test "exit stops the ticks after the tick's effects"
|
|
355
805
|
log := Slice[Str]()
|
|
356
|
-
|
|
806
|
+
sender: FRoseTree[Actor[GMsg]] := FRoseTree.leaf(GSender(id: 50, at: 1, ticks: 0, cmd: 1))
|
|
807
|
+
tree := SceneTree.ofScreens([FRoseTree.of(GTitleScreen(log: log, reacted: 0), gprobes(log, 1, false).append([sender]))])
|
|
357
|
-
g: Game[GState, GMsg] := Game.start(
|
|
808
|
+
g: Game[GState, GMsg] := Game.start(tree, gstate(log))
|
|
358
|
-
g2 :=
|
|
809
|
+
g2 := gtick(io, gtick(io, g))
|
|
359
810
|
assert g2.exited
|
|
360
811
|
assert g2.state().n == 6
|
|
361
812
|
assert gcount(log, "handle") == 6
|
|
@@ -365,3 +816,70 @@ test "exit stops the ticks after the tick's effects"
|
|
|
365
816
|
assert gcount(log, "step") == steps
|
|
366
817
|
assert gcount(log, "input") == 0
|
|
367
818
|
assert g3.ticks == 2
|
|
819
|
+
|
|
820
|
+
test "a game of one screen needs no screen(): the first screen is shown"
|
|
821
|
+
log := Slice[Str]()
|
|
822
|
+
g := Game.start(SceneTree.ofScreens([FRoseTree.of(GOnlyScreen(id: 3), gprobes(log, 1, false))]), GOne(n: 0))
|
|
823
|
+
assert g.current == GOnlyScreen.name()
|
|
824
|
+
g1 := g.step(io, ginput([PointerDown(x: 7.0, y: 5.0)]), 0.1, GameTestView)
|
|
825
|
+
assert gcount(log, "input 2") == 1 && gexact(log, "step 1") == 1
|
|
826
|
+
assert g1.state().n == 3
|
|
827
|
+
|
|
828
|
+
test "Game.start's guard: every child of the root a screen, names unique, the state's screen there"
|
|
829
|
+
log := Slice[Str]()
|
|
830
|
+
plain: FRoseTree[Actor[GMsg]] := FRoseTree.leaf(Group(id: 7, x: 0.0, y: 0.0))
|
|
831
|
+
good := SceneTree.ofScreens([gtitleNode(log, false), gplayNode(log, Cut, Nil)])
|
|
832
|
+
assert screensError[GState, GMsg](good, "GPlayScreen").isNone()
|
|
833
|
+
missing := screensError[GState, GMsg](good, "GPlaySceen").unwrap()
|
|
834
|
+
assert missing == "game: the state's screen 'GPlaySceen' is not in the tree (its screens are 'GTitleScreen', 'GPlayScreen')"
|
|
835
|
+
twice := SceneTree.ofScreens([gtitleNode(log, false), gtitleNode(log, false)])
|
|
836
|
+
assert screensError[GState, GMsg](twice, "GTitleScreen").unwrap() == "game: two screens are named 'GTitleScreen'"
|
|
837
|
+
mixed := SceneTree.ofScreens([gtitleNode(log, false), plain])
|
|
838
|
+
assert screensError[GState, GMsg](mixed, "GTitleScreen").unwrap() == "game: the root's child 7 is not a screen (a Screen[S, M] with a name)"
|
|
839
|
+
empty: List[FRoseTree[Actor[GMsg]]] := Nil
|
|
840
|
+
assert screensError[GState, GMsg](SceneTree.ofScreens(empty), "").isSome()
|
|
841
|
+
# "" is the first screen
|
|
842
|
+
assert chosen[GState, GMsg](good, "") == GTitleScreen.name()
|
|
843
|
+
|
|
844
|
+
test "a screen name no screen has keeps the current screen and is reported"
|
|
845
|
+
log := Slice[Str]()
|
|
846
|
+
sender: FRoseTree[Actor[GMsg]] := FRoseTree.leaf(GSender(id: 50, at: 0, ticks: 0, cmd: 3))
|
|
847
|
+
tree := SceneTree.ofScreens([FRoseTree.of(GTitleScreen(log: log, reacted: 0), gprobes(log, 1, false).append([sender])), gplayNode(log, Cut, Nil)])
|
|
848
|
+
g1 := gtick(io, Game.start(tree, gstate(log)))
|
|
849
|
+
assert g1.state().at == "nope"
|
|
850
|
+
assert g1.current == GTitleScreen.name()
|
|
851
|
+
assert g1.unknown == "nope"
|
|
852
|
+
assert gonScreen(g1) == "title 4"
|
|
853
|
+
g2 := gtick(io, g1)
|
|
854
|
+
assert g2.current == GTitleScreen.name() && g2.unknown == "nope"
|
|
855
|
+
assert gexact(log, "step 1") == 2
|
|
856
|
+
|
|
857
|
+
test "a screen's react, updateAs and kills reach only its own subtree: ids are per screen"
|
|
858
|
+
log := Slice[Str]()
|
|
859
|
+
sender: FRoseTree[Actor[GMsg]] := FRoseTree.leaf(GSender(id: 50, at: 0, ticks: 0, cmd: 4))
|
|
860
|
+
title: FRoseTree[Actor[GMsg]] := FRoseTree.of(GTitleScreen(log: log, reacted: 0), gprobes(log, 1, false).append([sender]))
|
|
861
|
+
play: FRoseTree[Actor[GMsg]] := FRoseTree.of(GPlayScreen(log: log, how: Cut), gprobes(log, 1, false))
|
|
862
|
+
g1 := gtick(io, Game.start(SceneTree.ofScreens([title, play]), gstate(log)))
|
|
863
|
+
mine := g1.screenTree()
|
|
864
|
+
assert mine.findAs[GProbe](1).unwrap().x == 3.0
|
|
865
|
+
assert mine.find(2).isNone()
|
|
866
|
+
other := g1.tree.subtree(GamePlayKey).unwrap()
|
|
867
|
+
assert other.findAs[GProbe](1).unwrap().x == 0.0
|
|
868
|
+
assert other.find(2).isSome()
|
|
869
|
+
# and a pointer on (7, 5), over probes of both screens, reaches only the title's
|
|
870
|
+
# (moved to 3, probe 1 is at 8 on the screen)
|
|
871
|
+
_ := g1.step(io, ginput([PointerDown(x: 9.0, y: 5.0)]), 0.1, GameTestView)
|
|
872
|
+
assert gexact(log, "input 1 at 1.0") == 1
|
|
873
|
+
|
|
874
|
+
test "a hidden screen is skipped: not stepped, drawn or hit, and left as it was"
|
|
875
|
+
log := Slice[Str]()
|
|
876
|
+
big: FRoseTree[Actor[GMsg]] := FRoseTree.of(GPlayScreen(log: log, how: Cut), gprobes(log, 9, false).append(gboxes(20000, Nil)))
|
|
877
|
+
g := Game.start(SceneTree.ofScreens([gtitleNode(log, false), big]), gstate(log))
|
|
878
|
+
before := g.tree.subtree(GamePlayKey).unwrap()
|
|
879
|
+
g5 := gtick(io, gtick(io, gtick(io, gtick(io, gtick(io, g)))))
|
|
880
|
+
assert gexact(log, "step 9") == 0 && gexact(log, "step 1") == 5
|
|
881
|
+
assert g5.shown().count() == 4
|
|
882
|
+
assert g5.tree.subtree(GamePlayKey).unwrap().count() == 20003
|
|
883
|
+
after := g5.tree.subtree(GamePlayKey).unwrap()
|
|
884
|
+
assert after.root.kids() == before.root.kids()
|
|
885
|
+
assert after.root.kids().rest().rest().top().unwrap() == before.root.kids().rest().rest().top().unwrap()
|
std/game/Store.plum
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
module std
|
|
2
2
|
|
|
3
3
|
import std/Int
|
|
4
|
+
import std/Str
|
|
4
5
|
import std/List
|
|
5
6
|
import std/Slice
|
|
6
7
|
import std/Io
|
|
@@ -26,6 +27,11 @@ trait Reducer[M] =
|
|
|
26
27
|
# The effects of `msg`, run by `Store.flush` after the tick. None by default.
|
|
27
28
|
fun handle(self, io: Io, msg: M) = Unit
|
|
28
29
|
|
|
30
|
+
# The name of the screen to show (std/game/Game): `Game` switches to it after a tick
|
|
31
|
+
# that changed it. "" (the default) is the tree's first screen, so a game of one screen
|
|
32
|
+
# needs nothing; a game of several keeps the name in its state.
|
|
33
|
+
fun screen(self) -> Str = ""
|
|
34
|
+
|
|
29
35
|
struct Store[S: Reducer[M], M](state: S, queue: List[M]) =
|
|
30
36
|
# A store holding `state` with nothing queued.
|
|
31
37
|
fun of(state: S) -> Store[S, M] = Store(state: state, queue: Nil)
|
std/game/scene/Ctx.plum
CHANGED
|
@@ -16,7 +16,7 @@ import std/game/scene/Actor
|
|
|
16
16
|
# `parent` (by id), on top of its children, or the end of the actor `id` and its subtree.
|
|
17
17
|
# The rest are the game's (std/game/Game), and the tree leaves them in `ctx.out`, in
|
|
18
18
|
# order: `Send` is a message of the game's type `M` for its store, `Restart` starts the
|
|
19
|
-
# game over from its first
|
|
19
|
+
# game over from its first tree and state, and `Exit` ends it (no tick runs after).
|
|
20
20
|
enum Cmd[M] =
|
|
21
21
|
| Spawn(parent: Int, node: FRoseTree[Actor[M]])
|
|
22
22
|
| Kill(id: Int)
|
std/game/scene/SceneTree.plum
CHANGED
|
@@ -62,6 +62,21 @@ struct SceneTree[M](root: FRoseTree[Actor[M]], camX: Float, camY: Float, camScal
|
|
|
62
62
|
# The same camera over another tree.
|
|
63
63
|
fun withRoot(self, r: FRoseTree[Actor[M]]) -> SceneTree[M] = SceneTree(..self, root: r)
|
|
64
64
|
|
|
65
|
+
# A game's tree (std/game/Game): a root group (id `ScreensRootId`) whose children are the
|
|
66
|
+
# screens, in order.
|
|
67
|
+
fun ofScreens(screens: List[FRoseTree[Actor[M]]]) -> SceneTree[M] =
|
|
68
|
+
root: Actor[M] := Group(id: ScreensRootId, x: 0.0, y: 0.0)
|
|
69
|
+
SceneTree(root: FRoseTree.of(root, screens), camX: 0.0, camY: 0.0, camScale: 1.0)
|
|
70
|
+
|
|
71
|
+
# The same tree with its root actor replaced by `a` (its children kept).
|
|
72
|
+
fun withTop(self, a: Actor[M]) -> SceneTree[M] = self.withRoot(self.root.withValue(a))
|
|
73
|
+
|
|
74
|
+
# The actor `id` with its subtree, as a tree of its own (the same camera), if there is
|
|
75
|
+
# one: what a screen copies from another (`tree.subtree(PlayScreen)`).
|
|
76
|
+
fun subtree(self, id: Int) -> Option[SceneTree[M]] =
|
|
77
|
+
n := nodeOf(self.root, id)
|
|
78
|
+
if n.isEmpty() then None else Some(self.withRoot(n))
|
|
79
|
+
|
|
65
80
|
fun pan(self, x: Float, y: Float) -> SceneTree[M] = SceneTree(..self, camX: x, camY: y)
|
|
66
81
|
|
|
67
82
|
fun zoom(self, s: Float) -> SceneTree[M] = SceneTree(..self, camScale: s)
|
|
@@ -171,6 +186,20 @@ struct SceneTree[M](root: FRoseTree[Actor[M]], camX: Float, camY: Float, camScal
|
|
|
171
186
|
# own, or draws in screen pixels.
|
|
172
187
|
fun paint(self, io: Io) = drawChain(self.root, io, Affine.Identity, 1.0)
|
|
173
188
|
|
|
189
|
+
# The id of the root `SceneTree.ofScreens` makes.
|
|
190
|
+
ScreensRootId = -1
|
|
191
|
+
|
|
192
|
+
# The node of the actor `id` in the chain `t` or below, alone (with its subtree, without
|
|
193
|
+
# its siblings); empty if there is none.
|
|
194
|
+
fun nodeOf[M](t: FRoseTree[Actor[M]], id: Int) -> FRoseTree[Actor[M]] = match t
|
|
195
|
+
FRoseEmpty => t
|
|
196
|
+
FRoseNode(a, k, n) =>
|
|
197
|
+
if a.key() == id
|
|
198
|
+
t.alone()
|
|
199
|
+
else
|
|
200
|
+
below := nodeOf(k, id)
|
|
201
|
+
if below.isEmpty() then nodeOf(n, id) else below
|
|
202
|
+
|
|
174
203
|
# One entry of the draw list: a shown actor, its tree transform and its alpha.
|
|
175
204
|
struct Drawn[M](actor: Actor[M], m: Affine, alpha: Float)
|
|
176
205
|
|
tests/port/game/determinism.plum
CHANGED
|
@@ -10,6 +10,7 @@ import std/game/input
|
|
|
10
10
|
import std/game/Affine
|
|
11
11
|
import std/game/Store
|
|
12
12
|
import std/game/Game
|
|
13
|
+
import std/collections/functional/FRoseTree
|
|
13
14
|
import std/game/scene/Place
|
|
14
15
|
import std/game/scene/Actor
|
|
15
16
|
import std/game/scene/Ctx
|
|
@@ -60,13 +61,15 @@ struct Eye(id: Int) is Actor[Saw] =
|
|
|
60
61
|
self
|
|
61
62
|
|
|
62
63
|
# The probe's one screen.
|
|
63
|
-
struct
|
|
64
|
+
struct OnlyScreen(id: Int) is Screen[Probe, Saw] =
|
|
64
|
-
fun
|
|
65
|
+
fun key(self) -> Int = self.id
|
|
65
66
|
|
|
66
|
-
fun
|
|
67
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
67
68
|
|
|
68
|
-
# The probe's game from `seed`.
|
|
69
|
+
# The probe's game from `seed`: its one screen over the eye.
|
|
69
|
-
fun probeGame(seed: Int) -> Game[Probe, Saw] =
|
|
70
|
+
fun probeGame(seed: Int) -> Game[Probe, Saw] =
|
|
71
|
+
screen: FRoseTree[Actor[Saw]] := FRoseTree.of(OnlyScreen(id: 1), [FRoseTree.leaf(Eye(id: 0))])
|
|
72
|
+
Game.start(SceneTree.ofScreens([screen]), Probe.seeded(seed))
|
|
70
73
|
|
|
71
74
|
# The screen the tests run on.
|
|
72
75
|
View = Vec2(x: 800.0, y: 600.0)
|
tests/port/game/replay.plum
CHANGED
|
@@ -14,6 +14,7 @@ import std/game/Replay
|
|
|
14
14
|
import std/game/Affine
|
|
15
15
|
import std/game/Store
|
|
16
16
|
import std/game/Game
|
|
17
|
+
import std/collections/functional/FRoseTree
|
|
17
18
|
import std/game/scene/Place
|
|
18
19
|
import std/game/scene/Actor
|
|
19
20
|
import std/game/scene/Ctx
|
|
@@ -62,13 +63,15 @@ struct Eye(id: Int) is Actor[Saw] =
|
|
|
62
63
|
self
|
|
63
64
|
|
|
64
65
|
# The probe's one screen.
|
|
65
|
-
struct
|
|
66
|
+
struct OnlyScreen(id: Int) is Screen[Probe, Saw] =
|
|
66
|
-
fun
|
|
67
|
+
fun key(self) -> Int = self.id
|
|
67
68
|
|
|
68
|
-
fun
|
|
69
|
+
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
69
70
|
|
|
70
|
-
# The probe's game from `seed`.
|
|
71
|
+
# The probe's game from `seed`: its one screen over the eye.
|
|
71
|
-
fun probeGame(seed: Int) -> Game[Probe, Saw] =
|
|
72
|
+
fun probeGame(seed: Int) -> Game[Probe, Saw] =
|
|
73
|
+
screen: FRoseTree[Actor[Saw]] := FRoseTree.of(OnlyScreen(id: 1), [FRoseTree.leaf(Eye(id: 0))])
|
|
74
|
+
Game.start(SceneTree.ofScreens([screen]), Probe.seeded(seed))
|
|
72
75
|
|
|
73
76
|
# The screen the tests run on.
|
|
74
77
|
View = Vec2(x: 800.0, y: 600.0)
|