plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
51f3b6c
— Peter John
2026-11-16T19:11:48+05:30
U2: screens are named by their type and navigation is by type; `@stepsWhen` removed
- benchmarks/screen_views/scale.plum +0 -4
- compiler/checker/tests/checker/diagnostic_tests.rs +7 -22
- compiler/cli/tests/cli/game_entry_test.rs +30 -17
- compiler/core/src/ast.rs +0 -4
- compiler/core/src/parser.rs +0 -23
- compiler/core/src/steps_derive.rs +18 -84
- compiler/core/src/tests_lower.rs +0 -1
- compiler/core/tests/core/formatter_test.rs +0 -10
- compiler/driver/tests/driver_tests.rs +1 -2
- compiler/game/tests/fixtures/assets_fixture.plum +0 -2
- compiler/game/tests/fixtures/blend_line_fixture.plum +0 -2
- compiler/game/tests/fixtures/camera_fixture.plum +1 -5
- compiler/game/tests/fixtures/cmd_sound_fixture.plum +0 -2
- compiler/game/tests/fixtures/host_fixture.plum +0 -2
- compiler/game/tests/fixtures/main_game_fixture.plum +0 -2
- compiler/game/tests/fixtures/main_missing_fixture.plum +0 -2
- compiler/game/tests/fixtures/state_draw_keeps.plum +0 -2
- compiler/game/tests/fixtures/state_in_place.plum +0 -2
- compiler/game/tests/fixtures/state_main_alias.plum +0 -2
- compiler/game/tests/fixtures/state_reset.plum +0 -2
- compiler/game/tests/fixtures/target_fixture.plum +0 -2
- compiler/game/tests/fixtures/turn_fixture.plum +0 -2
- compiler/game/tests/host_test.rs +5 -5
- compiler/game/web-test/fixtures/interleave.plum +0 -2
- compiler/game/web-test/fixtures/main_game.plum +0 -2
- compiler/game/web-test/fixtures/rendertarget.plum +0 -2
- compiler/game/web-test/fixtures/stress.plum +0 -2
- compiler/game/web-test/fixtures/stress_state_view.plum +0 -2
- compiler/game/web-test/fixtures/text_sizes.plum +0 -2
- compiler/tooling/tree-sitter-plum/grammar.js +1 -8
- compiler/tooling/tree-sitter-plum/queries/plum/format.scm +0 -3
- compiler/tooling/tree-sitter-plum/queries/plum/highlights.scm +0 -4
- compiler/tooling/tree-sitter-plum/src/grammar.json +0 -0
- compiler/tooling/tree-sitter-plum/src/node-types.json +0 -0
- compiler/tooling/tree-sitter-plum/src/parser.c +0 -0
- compiler/tooling/tree-sitter-plum/test/corpus/field_attribute.txt +0 -36
- docs/backlog.md +1 -1
- docs/game.md +21 -24
- docs/language.md +3 -5
- docs/std.md +5 -6
- examples/games/balloonpop/screens/PlaygroundScreen.plum +0 -2
- examples/games/clicks.plum +0 -2
- examples/games/clockwork/screens/OrreryScreen.plum +6 -5
- examples/games/menu/screens/MenuScreen.plum +1 -3
- examples/games/menu/screens/TurnScreen.plum +1 -3
- examples/games/orbitrunner/screens/PlayingScreen.plum +2 -4
- examples/games/two_screens.plum +2 -6
- std/game/Cmd.plum +13 -7
- std/game/Game.plum +13 -12
- std/game/Screen.plum +14 -11
- std/game/ScreenSlice.plum +0 -2
- std/game/TestRig.plum +3 -3
- std/game/View.plum +3 -8
- tests/examples/games/menu/Menu.plum +3 -3
- tests/examples/games/menu/screens/MenuScreen.plum +2 -2
- tests/examples/games/menu/screens/TurnScreen.plum +1 -1
- tests/examples/games/orbitrunner/screens/PlayingScreen.plum +2 -2
- tests/examples/games/two_screens.plum +3 -3
- tests/port/game/determinism.plum +0 -2
- tests/port/game/replay.plum +0 -2
- tests/port/game/screens.plum +6 -14
- tests/std/game/Button.plum +0 -2
- tests/std/game/Cmd.plum +15 -2
- tests/std/game/Game.plum +12 -4
- tests/std/game/Replay.plum +0 -2
- tests/std/game/ScreenSlice.plum +1 -12
- tests/std/game/View.plum +8 -23
benchmarks/screen_views/scale.plum
CHANGED
|
@@ -122,8 +122,6 @@ enum CrowdAct = CrowdTick(dt: Float)
|
|
|
122
122
|
struct CrowdScreen[ScreenSlice<CrowdAct>](hand: Bool, dots: List<View>) =
|
|
123
123
|
fun of(hand: Bool, n: Int) -> CrowdScreen = CrowdScreen(hand: hand, dots: ScaleBench.dotsFrom(hand, 0, n))
|
|
124
124
|
|
|
125
|
-
fun name(self) -> Str = "crowd"
|
|
126
|
-
|
|
127
125
|
fun view(self) -> List<View> = self.dots
|
|
128
126
|
|
|
129
127
|
fun subscriptions(self) -> List<Sub<CrowdAct>> = [OnFrame(|dt| CrowdTick(dt: dt))]
|
|
@@ -136,8 +134,6 @@ struct CrowdScreen[ScreenSlice<CrowdAct>](hand: Bool, dots: List<View>) =
|
|
|
136
134
|
struct SpreadScreen[ScreenSlice<CrowdAct>](dots: List<ViewDot>) =
|
|
137
135
|
fun of(n: Int) -> SpreadScreen = SpreadScreen(dots: Range.until(0, n).map(|i| ViewDot.of(i)))
|
|
138
136
|
|
|
139
|
-
fun name(self) -> Str = "spread"
|
|
140
|
-
|
|
141
137
|
fun view(self) -> List<View> = [
|
|
142
138
|
..self.dots
|
|
143
139
|
Box(w: 800.0, h: 4.0, color: Color.White)
|
compiler/checker/tests/checker/diagnostic_tests.rs
CHANGED
|
@@ -435,7 +435,7 @@ fn stepsIsForAnActorStructHoldingActors() {
|
|
|
435
435
|
assert!(stepsOk("struct P[Screen](n: Int, ws: List<Int>) =\n fun name(self) -> Str = \"p\"\n"), "a screen holding no actors keeps the default `stepped`");
|
|
436
436
|
let body = "struct V(t: Float) =\n fun step(self, dt: Float) -> V = V(t: self.t + dt)\n\n";
|
|
437
437
|
assert!(stepsOk(&format!("{body}struct P[Actor, Steps](v: V)\n")), "a value with its own `step(dt) -> Self` is something to step");
|
|
438
|
-
assert!(stepsOk(&format!("{body}struct P[Screen](on: Bool,
|
|
438
|
+
assert!(stepsOk(&format!("{body}struct P[Screen](on: Bool, v: V) =\n fun name(self) -> Str = \"p\"\n")));
|
|
439
439
|
let es = stepsErrors("struct V(t: Float) =\n fun step(self, dt: Int) -> V = self\n\nstruct P[Actor, Steps](v: V)\n");
|
|
440
440
|
assert!(es.iter().any(|e| e.message.contains("`P` lists `Steps` but holds nothing to step")), "a `step` not of a `Float` is not one: {es:?}");
|
|
441
441
|
}
|
|
@@ -467,27 +467,12 @@ fn showRefusesFunctionsAndTraitsWithoutToStr() {
|
|
|
467
467
|
plum_core::lower::frontEndDiag(&mut fine).expect("a trait with `toStr` and a type parameter are shown");
|
|
468
468
|
}
|
|
469
469
|
|
|
470
|
-
///
|
|
470
|
+
/// A field has no attributes: `@stepsWhen(cond)` is gone (a screen that pauses doesn't call
|
|
471
|
-
///
|
|
471
|
+
/// `stepped`), and writing one is an error.
|
|
472
472
|
#[test]
|
|
473
|
-
fn stepsWhenGoesOnAnActorFieldOfADerivedStepping() {
|
|
474
|
-
let es = stepsErrors("struct P[Screen](on: Bool, @stepsWhen(self.on) n: Int, w: W) =\n fun name(self) -> Str = \"p\"\n");
|
|
475
|
-
assert_eq!(es.len(), 1, "{es:?}");
|
|
476
|
-
assert!(es[0].message.contains("`@stepsWhen` on `P.n`, a `Int`: only a field holding something to step is stepped"), "{es:?}");
|
|
477
|
-
assert_eq!((es[0].line, es[0].column), (12, 39), "{es:?}");
|
|
478
|
-
assert!(stepsOk("struct P[Screen](on: Bool, @stepsWhen(self.on) w: W) =\n fun name(self) -> Str = \"p\"\n"));
|
|
479
|
-
let es = stepsErrors("struct P(on: Bool, @stepsWhen(self.on) w: W)\n");
|
|
480
|
-
assert_eq!(es.len(), 1, "{es:?}");
|
|
481
|
-
assert!(es[0].message.contains("`@stepsWhen` on `P.w`: `P` doesn't list `Steps`"), "{es:?}");
|
|
482
|
-
let es = stepsErrors("struct P[Screen](on: Bool, @stepsWhen(self.on) w: W) =\n fun name(self) -> Str = \"p\"\n\n fun stepped(self, dt: Float) -> P = self\n");
|
|
483
|
-
assert_eq!(es.len(), 1, "{es:?}");
|
|
484
|
-
assert!(es[0].message.contains("`@stepsWhen` on `P.w` does nothing: `P` writes its own `stepped`"), "{es:?}");
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
/// `@stepsWhen` is the one field attribute.
|
|
488
|
-
#[test]
|
|
489
|
-
fn
|
|
473
|
+
fn aFieldAttributeIsAnError() {
|
|
490
|
-
let
|
|
474
|
+
let src = format!("{STEPS_TRAITS}struct P[Screen](on: Bool, @stepsWhen(self.on) w: W) =\n fun name(self) -> Str = \"p\"\n\nprimitive Main[App] =\n fun main() -> Int = 1\n");
|
|
475
|
+
let err = plum_test_support::load("field_attribute", &src).err().expect("a field attribute doesn't parse");
|
|
491
|
-
assert!(
|
|
476
|
+
assert!(err.contains("syntax") || err.contains("parse") || err.contains("unexpected"), "{err}");
|
|
492
477
|
}
|
|
493
478
|
|
compiler/cli/tests/cli/game_entry_test.rs
CHANGED
|
@@ -64,7 +64,7 @@ const HEAD: &str = "module app\nimport std/Int\nimport std/Float\nimport std/Str
|
|
|
64
64
|
/// A whole game: one screen that counts its ticks.
|
|
65
65
|
const GAME: &str = "module app\nimport std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Option\nimport std/collections/List\nimport std/game/Input\nimport std/App\n\
|
|
66
66
|
import std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\n\n\
|
|
67
|
-
struct CountScreen[ScreenSlice<Int>](n: Int) =\n fun
|
|
67
|
+
struct CountScreen[ScreenSlice<Int>](n: Int) =\n fun subscriptions(self) -> List<Sub<Int>> = [OnFrame(|dt| 1)]\n \
|
|
68
68
|
fun update(self, a: Int) -> Next<CountScreen, Int> = Next(state: CountScreen(n: self.n + a))\n\nprimitive Main[App] =\n \
|
|
69
69
|
fun main(io: Io) -> Game =\n only: Screen := CountScreen(n: 0)\n Game(screens: [only])\n";
|
|
70
70
|
|
|
@@ -122,31 +122,44 @@ fn runSource(name: &str, text: &str) -> (bool, String) {
|
|
|
122
122
|
(out.status.success(), String::from_utf8_lossy(&out.stderr).into_owned())
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
/// A game of the
|
|
125
|
+
/// A game of screens of the types `types` (each of `A`, `B` and `C`, slices of one action);
|
|
126
|
+
/// `main` runs one tick in which the first screen goes to the screen of the type `to`
|
|
126
|
-
///
|
|
127
|
+
/// (`Cmd.goTo<to>()`), and prints the screen shown.
|
|
127
|
-
fn screensGame(
|
|
128
|
+
fn screensGame(types: &[&str], to: &str) -> String {
|
|
128
|
-
let screens: Vec<String> = (0..
|
|
129
|
+
let screens: Vec<String> = (0..types.len()).map(|i| format!("s{i}")).collect();
|
|
129
|
-
let made: Vec<String> =
|
|
130
|
+
let made: Vec<String> = types.iter().enumerate().map(|(i, t)| format!(" s{i}: Screen := {t}(n: {i})\n")).collect();
|
|
131
|
+
let decls: Vec<String> = ["A", "B", "C"]
|
|
132
|
+
.iter()
|
|
133
|
+
.map(|t| {
|
|
134
|
+
format!(
|
|
135
|
+
"struct {t}[ScreenSlice<Int>](n: Int) =\n fun subscriptions(self) -> List<Sub<Int>> = [OnFrame(|dt| 1)]\n \
|
|
136
|
+
fun update(self, a: Int) -> Next<{t}, Int> = Next(state: self, cmds: [Cmd.goTo<{to}>()])\n\n"
|
|
137
|
+
)
|
|
138
|
+
})
|
|
139
|
+
.collect();
|
|
130
140
|
format!(
|
|
131
141
|
"module app\nimport std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Option\nimport std/collections/List\nimport std/math/Vec2\nimport std/game/Input\nimport std/App\n\
|
|
132
142
|
import std/game/Cmd\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\n\n\
|
|
133
|
-
struct Named[ScreenSlice<Int>](n: Str) =\n fun name(self) -> Str = self.n\n fun subscriptions(self) -> List<Sub<Int>> = [OnFrame(|dt| 1)]\n \
|
|
134
|
-
|
|
143
|
+
{}primitive Main[App] =\n \
|
|
135
144
|
fun main(io: Io) =\n{} g := Game(screens: [{}])\n g1 := g.step(io, Input(), 0.1, Vec2(x: 800.0, y: 600.0))\n io.printLn(\"showing {{g1.showing()}}\")\n",
|
|
145
|
+
decls.join(""),
|
|
136
146
|
made.join(""),
|
|
137
147
|
screens.join(", ")
|
|
138
148
|
)
|
|
139
149
|
}
|
|
140
150
|
|
|
141
|
-
/// A game
|
|
151
|
+
/// A game finds its screens by type: two of one type stop the program when it is made, going
|
|
142
|
-
/// a
|
|
152
|
+
/// to a screen type the game has none of stops it and lists the screens, a type that is not
|
|
153
|
+
/// a screen is a compile error, and a screen's name is its type's.
|
|
143
154
|
#[test]
|
|
144
|
-
fn
|
|
155
|
+
fn aGameChecksItsScreensTypes() {
|
|
145
|
-
let (ok, err) = runSource("
|
|
156
|
+
let (ok, err) = runSource("two_types", &screensGame(&["A", "A"], "A"));
|
|
146
|
-
assert!(!ok && err.contains("game: two screens are
|
|
157
|
+
assert!(!ok && err.contains("game: two screens are of the type 'A'"), "{err}");
|
|
147
|
-
let (ok, err) = runSource("no_such", &screensGame(&["
|
|
158
|
+
let (ok, err) = runSource("no_such", &screensGame(&["A", "B"], "C"));
|
|
148
|
-
assert!(!ok && err.contains("game: no screen is
|
|
159
|
+
assert!(!ok && err.contains("game: no screen is a 'C' (the screens are A, B)"), "{err}");
|
|
160
|
+
let (ok, err) = runSource("not_a_screen", &screensGame(&["A", "B"], "Int"));
|
|
161
|
+
assert!(!ok && err.contains("Int does not implement Screen (the bound of S in Cmd.goTo)"), "{err}");
|
|
149
|
-
let (ok, err) = runSource("goes", &screensGame(&["
|
|
162
|
+
let (ok, err) = runSource("goes", &screensGame(&["A", "B"], "B"));
|
|
150
163
|
assert!(ok, "{err}");
|
|
151
164
|
}
|
|
152
165
|
|
|
@@ -156,7 +169,7 @@ fn aGameChecksItsScreensNames() {
|
|
|
156
169
|
fn aThenCycleAndAZeroZoomStopTheProgram() {
|
|
157
170
|
let cycle = "module app\nimport std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Option\nimport std/collections/List\nimport std/math/Vec2\nimport std/game/Input\nimport std/App\n\
|
|
158
171
|
import std/game/Cmd\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\n\n\
|
|
159
|
-
struct Loops[ScreenSlice<Int>](n: Int) =\n fun
|
|
172
|
+
struct Loops[ScreenSlice<Int>](n: Int) =\n fun subscriptions(self) -> List<Sub<Int>> = [OnFrame(|dt| 1)]\n \
|
|
160
173
|
fun update(self, a: Int) -> Next<Loops, Int> = Next(state: Loops(n: self.n + 1), then: [a])\n\nprimitive Main[App] =\n \
|
|
161
174
|
fun main(io: Io) =\n s: Screen := Loops(n: 0)\n g := Game(screens: [s]).step(io, Input(), 0.1, Vec2(x: 800.0, y: 600.0))\n io.printLn(\"ran {g.ticks}\")\n";
|
|
162
175
|
let (ok, err) = runSource("then_cycle", cycle);
|
compiler/core/src/ast.rs
CHANGED
|
@@ -624,10 +624,6 @@ pub struct EnumVariant {
|
|
|
624
624
|
/// field has one. A construction that leaves a defaulted field out gets a fresh copy of
|
|
625
625
|
/// its default (`field_defaults::fillFieldDefaults`).
|
|
626
626
|
pub field_defaults: Vec<Option<Expr>>,
|
|
627
|
-
/// `@stepsWhen(cond) name: T` on a `[Steps]` struct's field, aligned with `field_names`;
|
|
628
|
-
/// empty when no field has one. Moved into the derived `stepped`/`step` and emptied by
|
|
629
|
-
/// `steps_derive::deriveSteps`, first in the front end.
|
|
630
|
-
pub field_steps_when: Vec<Option<Expr>>,
|
|
631
627
|
}
|
|
632
628
|
|
|
633
629
|
// ---------- Functions ----------
|
compiler/core/src/parser.rs
CHANGED
|
@@ -469,28 +469,6 @@ impl<'a> AstParser<'a> {
|
|
|
469
469
|
} else {
|
|
470
470
|
Vec::new()
|
|
471
471
|
};
|
|
472
|
-
// `@stepsWhen(cond) name: T`: the one field attribute (std/game's `Steps`).
|
|
473
|
-
let field_steps_when: Vec<Option<Expr>> = if named_fields.iter().any(|n| n.child_by_field_name("attribute").is_some()) {
|
|
474
|
-
named_fields
|
|
475
|
-
.iter()
|
|
476
|
-
.map(|n| {
|
|
477
|
-
let a = n.child_by_field_name("attribute")?;
|
|
478
|
-
let name = a.child_by_field_name("name").map(|c| self.text(c)).unwrap_or_default();
|
|
479
|
-
if name != "stepsWhen" {
|
|
480
|
-
let pos = a.start_position();
|
|
481
|
-
self.litErrors.borrow_mut().push((
|
|
482
|
-
pos.row + 1,
|
|
483
|
-
pos.column + 1,
|
|
484
|
-
format!("`@{name}` is not a field attribute: the one there is is `@stepsWhen(condition)`, on a field of a `[Steps]` type"),
|
|
485
|
-
));
|
|
486
|
-
return None;
|
|
487
|
-
}
|
|
488
|
-
a.child_by_field_name("argument").map(|d| self.parseExpression(self.unwrapExprNode(d)))
|
|
489
|
-
})
|
|
490
|
-
.collect()
|
|
491
|
-
} else {
|
|
492
|
-
Vec::new()
|
|
493
|
-
};
|
|
494
472
|
let (fields, field_names): (Vec<Type>, Vec<String>) = if !named_fields.is_empty() {
|
|
495
473
|
named_fields
|
|
496
474
|
.iter()
|
|
@@ -524,7 +502,6 @@ impl<'a> AstParser<'a> {
|
|
|
524
502
|
field_names,
|
|
525
503
|
mut_fields,
|
|
526
504
|
field_defaults,
|
|
527
|
-
field_steps_when,
|
|
528
505
|
}
|
|
529
506
|
}
|
|
530
507
|
|
compiler/core/src/steps_derive.rs
CHANGED
|
@@ -24,24 +24,22 @@
|
|
|
24
24
|
//! (`step$fields`), so a container writes only what is special about itself (a ship holding
|
|
25
25
|
//! a `Body` writes only its clock), and the fields' conditions see the actor after its own
|
|
26
26
|
//! step. When its own `step(self, dt)` is one spread of `self` that sets none of those
|
|
27
|
-
//! fields
|
|
27
|
+
//! fields, the fields' steps are added to that spread instead
|
|
28
28
|
//! (`Ship(..self, clock: self.clock + dt, body: self.body.step(dt))`): the same values, one
|
|
29
29
|
//! construction.
|
|
30
|
+
//!
|
|
30
|
-
//!
|
|
31
|
+
//! A screen that pauses doesn't call `stepped` while paused (`Tick(dt) => if self.paused
|
|
31
|
-
//!
|
|
32
|
+
//! then .. else self.stepped(dt)..`); there is no per-field condition.
|
|
32
33
|
//!
|
|
33
34
|
//! Errors: `Steps` on a screen (its `stepped` is derived without it), on an enum, on a
|
|
34
|
-
//! struct that is not an `Actor`, or on an actor with no field holding something to step
|
|
35
|
+
//! struct that is not an `Actor`, or on an actor with no field holding something to step.
|
|
35
|
-
//! `@stepsWhen` on a field that holds nothing to step, on an actor that doesn't list `Steps`
|
|
36
|
-
//! or a type that is neither a screen nor an actor, or on a screen that writes its own
|
|
37
|
-
//! `stepped` (the attribute would do nothing).
|
|
38
36
|
|
|
39
37
|
use std::collections::{HashMap, HashSet};
|
|
40
38
|
|
|
41
39
|
use crate::ast::*;
|
|
42
40
|
use crate::diag::{DiagKind, Diagnostic};
|
|
43
41
|
use crate::forms::mapExprs;
|
|
44
|
-
use crate::main_result::
|
|
42
|
+
use crate::main_result::parseItems;
|
|
45
43
|
|
|
46
44
|
/// The trait an actor lists to have its fields stepped.
|
|
47
45
|
pub const STEPS_TRAIT: &str = "Steps";
|
|
@@ -52,7 +50,6 @@ const VIEW: &str = "View";
|
|
|
52
50
|
pub const OWN_STEP: &str = "step$own";
|
|
53
51
|
/// The stepping of an actor's fields, called by the derived `step` after `step$own`.
|
|
54
52
|
pub const FIELDS_STEP: &str = "step$fields";
|
|
55
|
-
const COND: &str = "stepsWhenDerived";
|
|
56
53
|
/// What a field holds to be stepped, for the errors.
|
|
57
54
|
const WHAT: &str = "an `Actor`, a value with its own `step(self, dt: Float) -> Self`, a `List` or an `Option` of either, or a `List<View>`";
|
|
58
55
|
|
|
@@ -69,13 +66,12 @@ enum Holds {
|
|
|
69
66
|
Mapped,
|
|
70
67
|
}
|
|
71
68
|
|
|
72
|
-
/// Writes out each `[Steps]`
|
|
69
|
+
/// Writes out each screen's and `[Steps]` actor's stepping (see the module doc). Returns
|
|
73
|
-
///
|
|
70
|
+
/// the errors.
|
|
74
71
|
pub fn deriveSteps(source: &mut Source) -> Vec<Diagnostic> {
|
|
75
|
-
let anyAttr = source.items.iter().any(|i| matches!(i, Item::Enum(e) if e.variants.iter().any(|v| !v.field_steps_when.is_empty())));
|
|
76
72
|
let anySteps = source.items.iter().any(|i| matches!(i, Item::Enum(e) if lists(e, STEPS_TRAIT)));
|
|
77
73
|
let anyScreen = source.items.iter().any(|i| matches!(i, Item::Trait(t) if t.name == SCREEN));
|
|
78
|
-
if !
|
|
74
|
+
if !anySteps && !anyScreen {
|
|
79
75
|
return Vec::new();
|
|
80
76
|
}
|
|
81
77
|
let supers: HashMap<String, Vec<String>> = source
|
|
@@ -154,15 +150,8 @@ pub fn deriveSteps(source: &mut Source) -> Vec<Diagnostic> {
|
|
|
154
150
|
let mut merges: Vec<(String, Vec<FieldArg>)> = Vec::new();
|
|
155
151
|
for item in &source.items {
|
|
156
152
|
let Item::Enum(e) = item else { continue };
|
|
157
|
-
let
|
|
153
|
+
let here = |msg: String| Diagnostic::new(DiagKind::Type, e.file.clone(), e.line, e.column, msg);
|
|
158
|
-
let here = |msg: String| at(e.line, e.column, msg);
|
|
159
154
|
let name = &e.name;
|
|
160
|
-
let attrs: Vec<(usize, &Expr)> = e
|
|
161
|
-
.variants
|
|
162
|
-
.iter()
|
|
163
|
-
.flat_map(|v| v.field_steps_when.iter().enumerate().filter_map(|(i, c)| c.as_ref().map(|c| (i, c))))
|
|
164
|
-
.collect();
|
|
165
|
-
let attrAt = |c: &Expr, msg: String| at(c.span.startLine.max(e.line), if c.span.startLine == 0 { e.column } else { c.span.startColumn }, msg);
|
|
166
155
|
let traits = &traitsOf[name];
|
|
167
156
|
let actor = traits.contains(ACTOR);
|
|
168
157
|
let screen = traits.contains(SCREEN);
|
|
@@ -174,13 +163,6 @@ pub fn deriveSteps(source: &mut Source) -> Vec<Diagnostic> {
|
|
|
174
163
|
continue;
|
|
175
164
|
}
|
|
176
165
|
if !steps && !screen {
|
|
177
|
-
for (i, c) in &attrs {
|
|
178
|
-
let field = e.variants.iter().find_map(|v| v.field_names.get(*i)).cloned().unwrap_or_default();
|
|
179
|
-
errors.push(attrAt(
|
|
180
|
-
c,
|
|
181
|
-
format!("`@stepsWhen` on `{name}.{field}`: `{name}` doesn't list `Steps`, which steps an actor's fields (`struct {name}[.., Steps](..)`)"),
|
|
182
|
-
));
|
|
183
|
-
}
|
|
184
166
|
continue;
|
|
185
167
|
}
|
|
186
168
|
if !e.isStruct() {
|
|
@@ -196,25 +178,7 @@ pub fn deriveSteps(source: &mut Source) -> Vec<Diagnostic> {
|
|
|
196
178
|
continue;
|
|
197
179
|
}
|
|
198
180
|
let v = &e.variants[0];
|
|
199
|
-
let fields: Vec<(String, Holds, Option<&Expr>)> = v
|
|
200
|
-
.field_names
|
|
201
|
-
.iter()
|
|
202
|
-
.zip(&v.fields)
|
|
203
|
-
.enumerate()
|
|
204
|
-
|
|
181
|
+
let fields: Vec<(String, Holds)> = v.field_names.iter().zip(&v.fields).filter_map(|(n, t)| holds(t).map(|h| (n.clone(), h))).collect();
|
|
205
|
-
.collect();
|
|
206
|
-
for (i, c) in &attrs {
|
|
207
|
-
if holds(&v.fields[*i]).is_none() {
|
|
208
|
-
errors.push(attrAt(
|
|
209
|
-
c,
|
|
210
|
-
format!(
|
|
211
|
-
"`@stepsWhen` on `{name}.{}`, a `{}`: only a field holding something to step is stepped ({WHAT})",
|
|
212
|
-
v.field_names[*i],
|
|
213
|
-
typeText(&v.fields[*i])
|
|
214
|
-
),
|
|
215
|
-
));
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
182
|
if fields.is_empty() {
|
|
219
183
|
if steps {
|
|
220
184
|
errors.push(here(format!(
|
|
@@ -223,17 +187,7 @@ pub fn deriveSteps(source: &mut Source) -> Vec<Diagnostic> {
|
|
|
223
187
|
}
|
|
224
188
|
continue;
|
|
225
189
|
}
|
|
226
|
-
|
|
190
|
+
if !actor && hasMethod(&source.items, name, "stepped") {
|
|
227
|
-
if ownStepped {
|
|
228
|
-
if let Some((i, c)) = attrs.first() {
|
|
229
|
-
errors.push(attrAt(
|
|
230
|
-
c,
|
|
231
|
-
format!(
|
|
232
|
-
"`@stepsWhen` on `{name}.{}` does nothing: `{name}` writes its own `stepped`, which wins over the derived one (leave out the `stepped` or the attribute)",
|
|
233
|
-
v.field_names[*i]
|
|
234
|
-
),
|
|
235
|
-
));
|
|
236
|
-
}
|
|
237
191
|
continue;
|
|
238
192
|
}
|
|
239
193
|
let gens = if e.generics.is_empty() {
|
|
@@ -242,10 +196,9 @@ pub fn deriveSteps(source: &mut Source) -> Vec<Diagnostic> {
|
|
|
242
196
|
format!("<{}>", e.generics.iter().map(|g| g.name.clone()).collect::<Vec<_>>().join(", "))
|
|
243
197
|
};
|
|
244
198
|
let ty = format!("{name}{gens}");
|
|
245
|
-
let mut conds: Vec<Expr> = Vec::new();
|
|
246
199
|
let args: Vec<String> = fields
|
|
247
200
|
.iter()
|
|
248
|
-
.map(|(f, h
|
|
201
|
+
.map(|(f, h)| {
|
|
249
202
|
let stepped = match h {
|
|
250
203
|
Holds::One => format!("self.{f}.step(dt)"),
|
|
251
204
|
Holds::List => format!("Views.stepAll(self.{f}, dt)"),
|
|
@@ -253,17 +206,11 @@ pub fn deriveSteps(source: &mut Source) -> Vec<Diagnostic> {
|
|
|
253
206
|
Holds::Views => format!("Views.stepIn(self.{f}, dt)"),
|
|
254
207
|
Holds::Mapped => format!("self.{f}.map(|x| x.step(dt))"),
|
|
255
208
|
};
|
|
256
|
-
match c {
|
|
257
|
-
Some(c) => {
|
|
258
|
-
conds.push((*c).clone());
|
|
259
|
-
format!("{f}: if {COND}{} then {stepped} else self.{f}", conds.len() - 1)
|
|
260
|
-
}
|
|
261
|
-
|
|
209
|
+
format!("{f}: {stepped}")
|
|
262
|
-
}
|
|
263
210
|
})
|
|
264
211
|
.collect();
|
|
265
212
|
let body = format!("{name}(..self, {})", args.join(", "));
|
|
266
|
-
let ownSpread =
|
|
213
|
+
let ownSpread = ownStepSpread(&source.items, name).is_some_and(|c| !c.fields.iter().any(|a| fields.iter().any(|(f, _)| *f == a.name)));
|
|
267
214
|
let text = if !actor {
|
|
268
215
|
format!("struct {ty} =\n fun stepped(self, dt: Float) -> {ty} = {body}\n")
|
|
269
216
|
} else if ownSpread {
|
|
@@ -294,18 +241,10 @@ pub fn deriveSteps(source: &mut Source) -> Vec<Diagnostic> {
|
|
|
294
241
|
}
|
|
295
242
|
f.name = fixName(&f.name);
|
|
296
243
|
mapExprs(&mut f.body, &mut |x| {
|
|
297
|
-
match &mut x.kind {
|
|
298
|
-
|
|
244
|
+
if let ExprKind::Attribute(a) = &mut x.kind {
|
|
299
|
-
|
|
245
|
+
if let AttrKind::Method(c) = &mut a.attr {
|
|
300
|
-
|
|
246
|
+
c.name = fixName(&c.name);
|
|
301
|
-
}
|
|
302
247
|
}
|
|
303
|
-
ExprKind::Attribute(a) => {
|
|
304
|
-
if let AttrKind::Method(c) = &mut a.attr {
|
|
305
|
-
c.name = fixName(&c.name);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
_ => {}
|
|
309
248
|
}
|
|
310
249
|
});
|
|
311
250
|
made.push(Item::Fn(f));
|
|
@@ -324,11 +263,6 @@ pub fn deriveSteps(source: &mut Source) -> Vec<Diagnostic> {
|
|
|
324
263
|
}
|
|
325
264
|
}
|
|
326
265
|
}
|
|
327
|
-
Item::Enum(e) => {
|
|
328
|
-
for v in &mut e.variants {
|
|
329
|
-
v.field_steps_when.clear();
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
266
|
_ => {}
|
|
333
267
|
}
|
|
334
268
|
}
|
compiler/core/src/tests_lower.rs
CHANGED
|
@@ -48,7 +48,6 @@ fn reportStruct() -> crate::ast::Item {
|
|
|
48
48
|
field_names: vec![REPORT_FIELD.to_string()],
|
|
49
49
|
mut_fields: vec![true],
|
|
50
50
|
field_defaults: Vec::new(),
|
|
51
|
-
field_steps_when: Vec::new(),
|
|
52
51
|
}],
|
|
53
52
|
kind: crate::ast::TypeKind::Struct(crate::ast::StructInfo::default()),
|
|
54
53
|
})
|
compiler/core/tests/core/formatter_test.rs
CHANGED
|
@@ -380,13 +380,3 @@ fn blocksAndListLinesFormat() {
|
|
|
380
380
|
assert_eq!(once, expected);
|
|
381
381
|
assertFormatsUnchanged(expected);
|
|
382
382
|
}
|
|
383
|
-
|
|
384
|
-
/// A field's attribute stays before it, one space between.
|
|
385
|
-
#[test]
|
|
386
|
-
fn aFieldAttributeFormats() {
|
|
387
|
-
let input = "struct P[Screen](over: Bool, @stepsWhen( !self.over ) mines: List<Mine>, rings: List<Ring>) =\n fun name(self) -> Str = \"p\"\n";
|
|
388
|
-
let once = formatSource(input).expect("first pass");
|
|
389
|
-
let expected = "struct P[Screen](over: Bool, @stepsWhen(!self.over) mines: List<Mine>, rings: List<Ring>) =\n fun name(self) -> Str = \"p\"\n";
|
|
390
|
-
assert_eq!(once, expected);
|
|
391
|
-
assertFormatsUnchanged(expected);
|
|
392
|
-
}
|
compiler/driver/tests/driver_tests.rs
CHANGED
|
@@ -213,7 +213,6 @@ fn gameSource(name: &str, main: &str, step: &str, draw: &str) -> Source {
|
|
|
213
213
|
import std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\n\
|
|
214
214
|
struct Stress[ScreenSlice<Float>](label: Str, t: Int, n: Int) =\n \
|
|
215
215
|
const Start = Stress(label: \"s\", t: 0, n: 0)\n \
|
|
216
|
-
fun name(self) -> Str = \"pane\"\n \
|
|
217
216
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]\n \
|
|
218
217
|
fun update(self, dt: Float) -> Next<Stress, Float> =\n Next(state: {step})\n \
|
|
219
218
|
fun later(self) -> Stress = Stress(..self, n: 99)\n \
|
|
@@ -303,7 +302,7 @@ fn unreachableImportedFnsArePruned() {
|
|
|
303
302
|
const DEFAULT_MATCH: &str = "import std/Int\nimport std/Float\nimport std/Str\nimport std/Option\nimport std/collections/List\n\
|
|
304
303
|
import std/game/Color\nimport std/game/View\nimport std/game/Box\n\
|
|
305
304
|
import std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/App\n\n\
|
|
306
|
-
enum Act = Go\n\nstruct One[ScreenSlice<Act>](n: Int) =\n fun
|
|
305
|
+
enum Act = Go\n\nstruct One[ScreenSlice<Act>](n: Int) =\n fun view(self) -> List<View> = [Box(w: 1.0, h: 1.0, color: Color.White)]\n\n\
|
|
307
306
|
primitive Main[App] =\n fun main() -> Int =\n s: Screen := One(n: 1)\n Game(screens: [s]).screens.length()\n";
|
|
308
307
|
|
|
309
308
|
/// A trait default that matches a generic trait (`ScreenSlice`'s `onPress`, `Presses<A> as p`)
|
compiler/game/tests/fixtures/assets_fixture.plum
CHANGED
|
@@ -23,8 +23,6 @@ import std/App
|
|
|
23
23
|
# Loads through the raw `io` (a missing file answers -1 or empty, where `Texture.load`
|
|
24
24
|
# would crash) and draws through it.
|
|
25
25
|
struct State[ScreenSlice<Float>](frames: Int) =
|
|
26
|
-
fun name(self) -> Str = "page"
|
|
27
|
-
|
|
28
26
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
29
27
|
|
|
30
28
|
fun update(self, dt: Float) -> Next<State, Float> = Next(state: State(frames: self.frames + 1))
|
compiler/game/tests/fixtures/blend_line_fixture.plum
CHANGED
|
@@ -23,8 +23,6 @@ import std/App
|
|
|
23
23
|
# (100, 50) holding a multiplied circle, a line under a group scaled by nothing, a box.
|
|
24
24
|
# C makes the screen cull, so it takes the culled walk.
|
|
25
25
|
struct BlendScreen[ScreenSlice<Bool>](cull: Bool) =
|
|
26
|
-
fun name(self) -> Str = "blend"
|
|
27
|
-
|
|
28
26
|
fun size(self) -> Vec2 = Vec2(x: 1000.0, y: 600.0)
|
|
29
27
|
|
|
30
28
|
fun culls(self) -> Bool = self.cull
|
compiler/game/tests/fixtures/camera_fixture.plum
CHANGED
|
@@ -25,8 +25,6 @@ enum CamAct = Use(scaling: Scaling) | Go
|
|
|
25
25
|
# A culled 400 by 300 world: a red box inside it at (10, 100) and a blue one just right of
|
|
26
26
|
# it at (420, 100), both 20 by 20.
|
|
27
27
|
struct CamScreen[ScreenSlice<CamAct>](scaling: Scaling) =
|
|
28
|
-
fun name(self) -> Str = "cam"
|
|
29
|
-
|
|
30
28
|
fun size(self) -> Vec2 = Vec2(x: 400.0, y: 300.0)
|
|
31
29
|
|
|
32
30
|
fun viewport(self) -> Scaling = self.scaling
|
|
@@ -45,12 +43,10 @@ struct CamScreen[ScreenSlice<CamAct>](scaling: Scaling) =
|
|
|
45
43
|
|
|
46
44
|
fun update(self, a: CamAct) -> Next<CamScreen, CamAct> = match a
|
|
47
45
|
Use(s) => Next(state: CamScreen(scaling: s))
|
|
48
|
-
Go => Next(state: self, cmds: [
|
|
46
|
+
Go => Next(state: self, cmds: [Cmd.goTo<TwoScreen>()])
|
|
49
47
|
|
|
50
48
|
# A culled 400 by 300 world, fitted: a green box at (10, 100) and a white one at (300, 100).
|
|
51
49
|
struct TwoScreen[Screen](n: Int) =
|
|
52
|
-
fun name(self) -> Str = "two"
|
|
53
|
-
|
|
54
50
|
fun size(self) -> Vec2 = Vec2(x: 400.0, y: 300.0)
|
|
55
51
|
|
|
56
52
|
fun culls(self) -> Bool = true
|
compiler/game/tests/fixtures/cmd_sound_fixture.plum
CHANGED
|
@@ -16,8 +16,6 @@ import std/App
|
|
|
16
16
|
# The first tick asks for the silent sound's play, loop and stop, then sound 3's play and
|
|
17
17
|
# stop: the game does them once the tick is over, and only sound 3's reach the host.
|
|
18
18
|
struct State[ScreenSlice<Float>](frames: Int) =
|
|
19
|
-
fun name(self) -> Str = "page"
|
|
20
|
-
|
|
21
19
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
22
20
|
|
|
23
21
|
fun update(self, dt: Float) -> Next<State, Float> =
|
compiler/game/tests/fixtures/host_fixture.plum
CHANGED
|
@@ -24,8 +24,6 @@ import std/App
|
|
|
24
24
|
# clears the frame (`Screen.background`, black) before the screen draws. The
|
|
25
25
|
# game is one screen, counting the ticks.
|
|
26
26
|
struct State[ScreenSlice<Float>](frames: Int) =
|
|
27
|
-
fun name(self) -> Str = "page"
|
|
28
|
-
|
|
29
27
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
30
28
|
|
|
31
29
|
fun update(self, dt: Float) -> Next<State, Float> = Next(state: State(frames: self.frames + 1))
|
compiler/game/tests/fixtures/main_game_fixture.plum
CHANGED
|
@@ -27,8 +27,6 @@ import std/App
|
|
|
27
27
|
enum Step = Heard | Saw(held: Held) | Moved(dt: Float)
|
|
28
28
|
|
|
29
29
|
struct Orbs[ScreenSlice<Step>](x: Float, ship: Texture, held: Held, heard: Int, events: Int, frame: Int) =
|
|
30
|
-
fun name(self) -> Str = "page"
|
|
31
|
-
|
|
32
30
|
fun subscriptions(self) -> List<Sub<Step>> = [
|
|
33
31
|
OnKeyDown(key: Key.KeyA, act: Heard)
|
|
34
32
|
OnPointerDown(|at| Heard)
|
compiler/game/tests/fixtures/main_missing_fixture.plum
CHANGED
|
@@ -20,8 +20,6 @@ import std/App
|
|
|
20
20
|
|
|
21
21
|
# Its texture is missing: `main` crashes naming it.
|
|
22
22
|
struct Broken[ScreenSlice<Float>](tex: Texture) =
|
|
23
|
-
fun name(self) -> Str = "page"
|
|
24
|
-
|
|
25
23
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
26
24
|
|
|
27
25
|
fun update(self, dt: Float) -> Next<Broken, Float> = Next(state: self)
|
compiler/game/tests/fixtures/state_draw_keeps.plum
CHANGED
|
@@ -22,8 +22,6 @@ import std/App
|
|
|
22
22
|
struct Log(mut last: Option<Run>)
|
|
23
23
|
|
|
24
24
|
struct Run[ScreenSlice<Float>](label: Str, log: Log, t: Int) =
|
|
25
|
-
fun name(self) -> Str = "page"
|
|
26
|
-
|
|
27
25
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
28
26
|
|
|
29
27
|
fun update(self, dt: Float) -> Next<Run, Float> = Next(state: Run(..self, t: self.t + 1))
|
compiler/game/tests/fixtures/state_in_place.plum
CHANGED
|
@@ -24,8 +24,6 @@ struct Part(label: Str, x: Int) =
|
|
|
24
24
|
# `reduce` writes the screen and its field in place; `draw` updates a value of its own, which
|
|
25
25
|
# must not reach the screen the game keeps for the next frame.
|
|
26
26
|
struct Run[ScreenSlice<Float>](part: Part, t: Int) =
|
|
27
|
-
fun name(self) -> Str = "page"
|
|
28
|
-
|
|
29
27
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
30
28
|
|
|
31
29
|
fun update(self, dt: Float) -> Next<Run, Float> =
|
compiler/game/tests/fixtures/state_main_alias.plum
CHANGED
|
@@ -23,8 +23,6 @@ struct Run[ScreenSlice<Float>](label: Str, t: Int) =
|
|
|
23
23
|
# it in place.
|
|
24
24
|
const Start = Run(label: "start", t: 0)
|
|
25
25
|
|
|
26
|
-
fun name(self) -> Str = "page"
|
|
27
|
-
|
|
28
26
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
29
27
|
|
|
30
28
|
fun update(self, dt: Float) -> Next<Run, Float> = Next(state: Run(..self, t: self.t + 1))
|
compiler/game/tests/fixtures/state_reset.plum
CHANGED
|
@@ -22,8 +22,6 @@ struct Run[ScreenSlice<Float>](label: Str, t: Int) =
|
|
|
22
22
|
# The screen goes back to a constant: writing it in place would overwrite the constant.
|
|
23
23
|
const Start = Run(label: "start", t: 0)
|
|
24
24
|
|
|
25
|
-
fun name(self) -> Str = "page"
|
|
26
|
-
|
|
27
25
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
28
26
|
|
|
29
27
|
fun update(self, dt: Float) -> Next<Run, Float> =
|
compiler/game/tests/fixtures/target_fixture.plum
CHANGED
|
@@ -23,8 +23,6 @@ import std/App
|
|
|
23
23
|
# `bad` is a raw `io.textureCreate` of an impossible size: it answers -1 where
|
|
24
24
|
# `Texture.create` would crash.
|
|
25
25
|
struct State[ScreenSlice<Float>](target: Texture, bad: Int) =
|
|
26
|
-
fun name(self) -> Str = "page"
|
|
27
|
-
|
|
28
26
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
29
27
|
|
|
30
28
|
fun update(self, dt: Float) -> Next<State, Float> = Next(state: self)
|
compiler/game/tests/fixtures/turn_fixture.plum
CHANGED
|
@@ -23,8 +23,6 @@ struct Turned[View](children: List<View>) =
|
|
|
23
23
|
fun view(self) -> List<View> = self.children
|
|
24
24
|
|
|
25
25
|
struct TurnScreen[Screen](n: Int) =
|
|
26
|
-
fun name(self) -> Str = "turn"
|
|
27
|
-
|
|
28
26
|
fun view(self) -> List<View> = [
|
|
29
27
|
Turned() {
|
|
30
28
|
Box(w: 40.0, h: 20.0, color: Color.Red)
|
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/Option\nimport std/collections/List\nimport std/math/Vec2\nimport std/math/Affine\nimport std/math/Aabb\nimport std/game/Color\nimport std/game/Gfx\nimport std/game/Input\nimport std/game/Cmd\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Place\nimport std/App\nstruct State[ScreenSlice<Int>](n: Int) =\n fun
|
|
154
|
+
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/Option\nimport std/collections/List\nimport std/math/Vec2\nimport std/math/Affine\nimport std/math/Aabb\nimport std/game/Color\nimport std/game/Gfx\nimport std/game/Input\nimport std/game/Cmd\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Place\nimport std/App\nstruct State[ScreenSlice<Int>](n: Int) =\n fun update(self, a: Int) -> Next<State, Int> = Next(state: self)\n fun view(self) -> List<View> = [StateDrawing(s: self)]\n\n# What the screen draws each frame, with `Gfx` by hand.\nstruct StateDrawing[View](s: State) =\n fun place(self) -> Place = Place.at(0.0, 0.0)\n fun paintSelf(self, io: Io, m: Affine, alpha: Float) =\n x := io.rawExists(\"a\")\n\nprimitive Main[App] =\n fun main(io: Io) -> Game =\n only: Screen := State(n: 0)\n Game(screens: [only])\n",
|
|
155
155
|
)
|
|
156
156
|
.unwrap();
|
|
157
157
|
let wasm = compile(&path);
|
|
@@ -364,7 +364,7 @@ fn randomSeedProvidedByHost() {
|
|
|
364
364
|
let path = dir.join("m.plum");
|
|
365
365
|
std::fs::write(
|
|
366
366
|
&path,
|
|
367
|
-
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/Option\nimport std/collections/List\nimport std/math/Vec2\nimport std/math/Affine\nimport std/math/Aabb\nimport std/game/Color\nimport std/game/Gfx\nimport std/game/Input\nimport std/game/Cmd\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Place\nimport std/App\nstruct State[ScreenSlice<Int>](seed: Int) =\n fun
|
|
367
|
+
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/Option\nimport std/collections/List\nimport std/math/Vec2\nimport std/math/Affine\nimport std/math/Aabb\nimport std/game/Color\nimport std/game/Gfx\nimport std/game/Input\nimport std/game/Cmd\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Place\nimport std/App\nstruct State[ScreenSlice<Int>](seed: Int) =\n fun update(self, a: Int) -> Next<State, Int> = Next(state: self)\n fun view(self) -> List<View> = [StateDrawing(s: self)]\n\n# What the screen draws each frame, with `Gfx` by hand.\nstruct StateDrawing[View](s: State) =\n fun place(self) -> Place = Place.at(0.0, 0.0)\n fun paintSelf(self, io: Io, m: Affine, alpha: Float) =\n Gfx.drawText(io, \"seed {self.s.seed}\", 0.0, 0.0, 8.0, Color.White)\n\nprimitive Main[App] =\n fun main(io: Io) -> Game =\n only: Screen := State(seed: io.randomSeed())\n Game(screens: [only])\n",
|
|
368
368
|
)
|
|
369
369
|
.unwrap();
|
|
370
370
|
let wasm = compile(&path);
|
|
@@ -388,7 +388,7 @@ fn seedGame(name: &str) -> Vec<u8> {
|
|
|
388
388
|
let path = dir.join("m.plum");
|
|
389
389
|
std::fs::write(
|
|
390
390
|
&path,
|
|
391
|
-
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/Option\nimport std/collections/List\nimport std/math/Vec2\nimport std/math/Affine\nimport std/math/Aabb\nimport std/game/Color\nimport std/game/Gfx\nimport std/game/Input\nimport std/game/Cmd\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Place\nimport std/App\nenum Saw = Seen(keys: Int, x: Float) | Ticked\nstruct State[ScreenSlice<Saw>](seed: Int, ticks: Int, keys: Int, x: Float) =\n fun
|
|
391
|
+
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/Option\nimport std/collections/List\nimport std/math/Vec2\nimport std/math/Affine\nimport std/math/Aabb\nimport std/game/Color\nimport std/game/Gfx\nimport std/game/Input\nimport std/game/Cmd\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Place\nimport std/App\nenum Saw = Seen(keys: Int, x: Float) | Ticked\nstruct State[ScreenSlice<Saw>](seed: Int, ticks: Int, keys: Int, x: Float) =\n fun hud(self, window: Vec2) -> List<View> = [StateDrawing(s: self, w: window.x)]\n fun subscriptions(self) -> List<Sub<Saw>> = [OnHeld(|h| Seen(keys: h.keys.length(), x: h.pointer.x)), OnFrame(|dt| Ticked)]\n fun update(self, a: Saw) -> Next<State, Saw> = match a\n Seen(k, x) => Next(state: State(..self, keys: self.keys + k, x: x))\n Ticked => Next(state: State(..self, ticks: self.ticks + 1))\n\n# What the screen draws each frame, with `Gfx` by hand, and the window's width.\nstruct StateDrawing[View](s: State, w: Float) =\n fun place(self) -> Place = Place.at(0.0, 0.0)\n fun paintSelf(self, io: Io, m: Affine, alpha: Float) =\n Gfx.drawText(io, \"seed {self.s.seed} ticks {self.s.ticks} keys {self.s.keys} x {self.s.x} w {self.w}\", 0.0, 0.0, 8.0, Color.White)\n\nprimitive Main[App] =\n fun main(io: Io) -> Game =\n only: Screen := State(seed: io.randomSeed(), ticks: 0, keys: 0, x: 0.0)\n Game(screens: [only])\n",
|
|
392
392
|
)
|
|
393
393
|
.unwrap();
|
|
394
394
|
let wasm = compile(&path);
|
|
@@ -463,7 +463,7 @@ fn everySeedOfARecordedRunReplays() {
|
|
|
463
463
|
let path = dir.join("m.plum");
|
|
464
464
|
std::fs::write(
|
|
465
465
|
&path,
|
|
466
|
-
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/Option\nimport std/collections/List\nimport std/math/Vec2\nimport std/math/Affine\nimport std/math/Aabb\nimport std/game/Color\nimport std/game/Gfx\nimport std/game/Input\nimport std/game/Cmd\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Place\nimport std/App\nstruct State[ScreenSlice<Int>](a: Int, b: Int, c: Int) =\n fun
|
|
466
|
+
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Slice\nimport std/Option\nimport std/collections/List\nimport std/math/Vec2\nimport std/math/Affine\nimport std/math/Aabb\nimport std/game/Color\nimport std/game/Gfx\nimport std/game/Input\nimport std/game/Cmd\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Place\nimport std/App\nstruct State[ScreenSlice<Int>](a: Int, b: Int, c: Int) =\n fun subscriptions(self) -> List<Sub<Int>> = [OnFrame(|dt| 1)]\n fun update(self, n: Int) -> Next<State, Int> = Next(state: State(..self, c: self.c * 31 + self.a ^ self.b))\n fun view(self) -> List<View> = [StateDrawing(s: self)]\n\n# What the screen draws each frame, with `Gfx` by hand.\nstruct StateDrawing[View](s: State) =\n fun place(self) -> Place = Place.at(0.0, 0.0)\n fun paintSelf(self, io: Io, m: Affine, alpha: Float) =\n Gfx.drawText(io, \"{self.s.a} {self.s.b} {self.s.c}\", 0.0, 0.0, 8.0, Color.White)\n\nprimitive Main[App] =\n fun main(io: Io) -> Game =\n a := io.randomSeed()\n only: Screen := State(a: a, b: io.randomSeed(), c: 0)\n Game(screens: [only])\n",
|
|
467
467
|
)
|
|
468
468
|
.unwrap();
|
|
469
469
|
let wasm = compile(&path);
|
|
@@ -800,7 +800,7 @@ fn countingGame(name: &str, frames: &str) -> Vec<u8> {
|
|
|
800
800
|
std::fs::write(
|
|
801
801
|
&path,
|
|
802
802
|
format!(
|
|
803
|
-
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Option\nimport std/collections/List\nimport std/math/Vec2\nimport std/game/Color\nimport std/game/Input\nimport std/game/Cmd\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Label\nimport std/App\nenum Count = Bump | Tock\nstruct Counting[ScreenSlice<Count>](ticks: Int, bumps: Int) =\n fun
|
|
803
|
+
"import std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Option\nimport std/collections/List\nimport std/math/Vec2\nimport std/game/Color\nimport std/game/Input\nimport std/game/Cmd\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Label\nimport std/App\nenum Count = Bump | Tock\nstruct Counting[ScreenSlice<Count>](ticks: Int, bumps: Int) =\n fun frames(self) -> Frames = {frames}\n fun hud(self, window: Vec2) -> List<View> = [Label(text: \"ticks {{self.ticks}} bumps {{self.bumps}} w {{window.x}}\", size: 8.0)]\n fun subscriptions(self) -> List<Sub<Count>> = [OnKeyDown(key: Key.Space, act: Bump), OnFrame(|dt| Tock)]\n fun update(self, a: Count) -> Next<Counting, Count> = match a\n Bump => Next(state: Counting(..self, bumps: self.bumps + 1))\n Tock => Next(state: Counting(..self, ticks: self.ticks + 1))\n\nprimitive Main[App] =\n fun main(io: Io) -> Game =\n only: Screen := Counting(ticks: 0, bumps: 0)\n Game(screens: [only])\n"
|
|
804
804
|
),
|
|
805
805
|
)
|
|
806
806
|
.unwrap();
|
compiler/game/web-test/fixtures/interleave.plum
CHANGED
|
@@ -24,8 +24,6 @@ import std/App
|
|
|
24
24
|
|
|
25
25
|
# All the sprites, one tick on each tick.
|
|
26
26
|
struct Interleave[ScreenSlice<Float>](tex: Array<Texture>, t: Int) =
|
|
27
|
-
fun name(self) -> Str = "page"
|
|
28
|
-
|
|
29
27
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
30
28
|
|
|
31
29
|
fun update(self, dt: Float) -> Next<Interleave, Float> = Next(state: Interleave(..self, t: self.t + 1))
|
compiler/game/web-test/fixtures/main_game.plum
CHANGED
|
@@ -22,8 +22,6 @@ import std/App
|
|
|
22
22
|
# A game whose `main` returns a `Game`: one screen, a square that moves
|
|
23
23
|
# with the tick count. It ticks and draws the count.
|
|
24
24
|
struct Mover[ScreenSlice<Float>](frame: Int) =
|
|
25
|
-
fun name(self) -> Str = "page"
|
|
26
|
-
|
|
27
25
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
28
26
|
|
|
29
27
|
fun update(self, dt: Float) -> Next<Mover, Float> = Next(state: Mover(frame: self.frame + 1))
|
compiler/game/web-test/fixtures/rendertarget.plum
CHANGED
|
@@ -36,8 +36,6 @@ import std/App
|
|
|
36
36
|
|
|
37
37
|
# The targets and the tick count.
|
|
38
38
|
struct Targets[ScreenSlice<Float>](big: Texture, small: Texture, glass: Texture, t: Int) =
|
|
39
|
-
fun name(self) -> Str = "page"
|
|
40
|
-
|
|
41
39
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
42
40
|
|
|
43
41
|
fun update(self, dt: Float) -> Next<Targets, Float> = Next(state: Targets(..self, t: self.t + 1))
|
compiler/game/web-test/fixtures/stress.plum
CHANGED
|
@@ -24,8 +24,6 @@ import std/App
|
|
|
24
24
|
|
|
25
25
|
# The whole scene, drawn from the tick count.
|
|
26
26
|
struct Stress[ScreenSlice<Float>](sheet: Texture, t: Int) =
|
|
27
|
-
fun name(self) -> Str = "page"
|
|
28
|
-
|
|
29
27
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
30
28
|
|
|
31
29
|
fun update(self, dt: Float) -> Next<Stress, Float> = Next(state: Stress(..self, t: self.t + 1))
|
compiler/game/web-test/fixtures/stress_state_view.plum
CHANGED
|
@@ -92,8 +92,6 @@ struct ViewScreen[ScreenSlice<StressAction>](clock: Float, frames: Int, clicks:
|
|
|
92
92
|
fun create(sheet: Texture, white: Texture) -> ViewScreen =
|
|
93
93
|
ViewScreen(clock: 0.0, frames: 0, clicks: 0, sheet: sheet, white: white, anim: StressStateView.pulseAnim())
|
|
94
94
|
|
|
95
|
-
fun name(self) -> Str = "view"
|
|
96
|
-
|
|
97
95
|
fun background(self) -> Color = Color.rgb(0.05, 0.06, 0.1)
|
|
98
96
|
|
|
99
97
|
fun view(self) -> List<View> = [
|
compiler/game/web-test/fixtures/text_sizes.plum
CHANGED
|
@@ -23,8 +23,6 @@ import std/App
|
|
|
23
23
|
|
|
24
24
|
# The text, one tick older each tick.
|
|
25
25
|
struct Sizes[ScreenSlice<Float>](t: Int) =
|
|
26
|
-
fun name(self) -> Str = "page"
|
|
27
|
-
|
|
28
26
|
fun subscriptions(self) -> List<Sub<Float>> = [OnFrame(|dt| dt)]
|
|
29
27
|
|
|
30
28
|
fun update(self, dt: Float) -> Next<Sizes, Float> = Next(state: Sizes(t: self.t + 1))
|
compiler/tooling/tree-sitter-plum/grammar.js
CHANGED
|
@@ -262,12 +262,9 @@ module.exports = grammar({
|
|
|
262
262
|
),
|
|
263
263
|
|
|
264
264
|
// A struct field: `x: Float`, or with a default a construction may leave it out for:
|
|
265
|
-
// `size: Float = 20.0`. An attribute may come first: `@stepsWhen(!self.over) mines:
|
|
266
|
-
// List<Mine>` (a screen's or a `[Steps]` actor's field stepped only while the condition
|
|
267
|
-
//
|
|
265
|
+
// `size: Float = 20.0`.
|
|
268
266
|
ctor_field: ($) =>
|
|
269
267
|
seq(
|
|
270
|
-
optional(field("attribute", $.field_attribute)),
|
|
271
268
|
optional(field("mutKw", "mut")),
|
|
272
269
|
field("name", $.var_identifier),
|
|
273
270
|
":",
|
|
@@ -275,10 +272,6 @@ module.exports = grammar({
|
|
|
275
272
|
optional(seq("=", field("default", $.expression))),
|
|
276
273
|
),
|
|
277
274
|
|
|
278
|
-
// `@stepsWhen(!self.over)` before a struct field: a name and one expression.
|
|
279
|
-
field_attribute: ($) =>
|
|
280
|
-
seq("@", field("name", $.var_identifier), "(", field("argument", $.expression), ")"),
|
|
281
|
-
|
|
282
275
|
fn: ($) =>
|
|
283
276
|
prec.left(
|
|
284
277
|
seq(
|
compiler/tooling/tree-sitter-plum/queries/plum/format.scm
CHANGED
|
@@ -421,9 +421,6 @@
|
|
|
421
421
|
; `mut x: T` fields and `export fun`.
|
|
422
422
|
"mut" @append_space
|
|
423
423
|
|
|
424
|
-
; `@stepsWhen(!self.over) mines: List<Mine>`: a field's attribute, then the field.
|
|
425
|
-
(field_attribute ")" @append_space)
|
|
426
|
-
|
|
427
424
|
(fn "export" @append_space)
|
|
428
425
|
|
|
429
426
|
; ============================================================
|
compiler/tooling/tree-sitter-plum/queries/plum/highlights.scm
CHANGED
|
@@ -144,10 +144,6 @@
|
|
|
144
144
|
"@" @function.builtin
|
|
145
145
|
name: (var_identifier) @function.builtin)
|
|
146
146
|
|
|
147
|
-
(field_attribute
|
|
148
|
-
"@" @attribute
|
|
149
|
-
name: (var_identifier) @attribute)
|
|
150
|
-
|
|
151
147
|
"crash" @keyword.control.exception
|
|
152
148
|
|
|
153
149
|
"match" @keyword
|
compiler/tooling/tree-sitter-plum/src/grammar.json
CHANGED
|
Binary file
|
compiler/tooling/tree-sitter-plum/src/node-types.json
CHANGED
|
Binary file
|
compiler/tooling/tree-sitter-plum/src/parser.c
CHANGED
|
Binary file
|
compiler/tooling/tree-sitter-plum/test/corpus/field_attribute.txt
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
================================================================================
|
|
2
|
-
field attribute before a struct field
|
|
3
|
-
================================================================================
|
|
4
|
-
|
|
5
|
-
struct P[Screen, Steps](over: Bool, @stepsWhen(!self.over) mines: List<Mine>)
|
|
6
|
-
|
|
7
|
-
--------------------------------------------------------------------------------
|
|
8
|
-
|
|
9
|
-
(source
|
|
10
|
-
(struct
|
|
11
|
-
name: (type_identifier)
|
|
12
|
-
implements: (implements
|
|
13
|
-
(type
|
|
14
|
-
(type_identifier))
|
|
15
|
-
(type
|
|
16
|
-
(type_identifier)))
|
|
17
|
-
field: (enum_named_field
|
|
18
|
-
name: (var_identifier)
|
|
19
|
-
type: (type
|
|
20
|
-
(type_identifier)))
|
|
21
|
-
field: (enum_named_field
|
|
22
|
-
attribute: (field_attribute
|
|
23
|
-
name: (var_identifier)
|
|
24
|
-
argument: (expression
|
|
25
|
-
(not_operator
|
|
26
|
-
argument: (expression
|
|
27
|
-
(primary_expression
|
|
28
|
-
(attribute
|
|
29
|
-
object: (primary_expression
|
|
30
|
-
(self))
|
|
31
|
-
member: (fn_identifier)))))))
|
|
32
|
-
name: (var_identifier)
|
|
33
|
-
type: (type
|
|
34
|
-
(type_identifier)
|
|
35
|
-
generics: (type
|
|
36
|
-
(type_identifier))))))
|
docs/backlog.md
CHANGED
|
@@ -12,7 +12,7 @@ The retained scene tree was replaced by master's screen view in functional form:
|
|
|
12
12
|
- **What std's delivery fold still copies** (the screen is in place since 2026-11-16, `@copy` in `Delivery.deliverTo`; since the Elm-style screens, Orbit Runner's `PlayingScreen.update` 5 (steer, the frame's spread after `stepped`, scored, hit and start over, whose `best` is read before the spread now), `rules`, `deal`, `grown`, `at` and the derived `stepped`: 29 spreads in place, 27 before; a call on a receiver of a bounded type parameter, `s.update(a)` with `S: ScreenSlice<A>`, is stamped with the bound in the analysis' copy of the body and resolves among its implementers, where it resolved to every `update` of the program, `Game.update` and `Viewport.update` among them): an update that reads `self` again after a spread of it copies (Orbit Runner reads its sounds first, `sounds := self.assets`); a screen fold whose result `passes` can't follow (`Range.fold` over the screen; Orbit Runner's `deal` folds a `Rolled<List<Orb>>` instead); Balloon Pop's `update`, one spread in place, the others copies, as its `view` keeps `self` (`s.view()` in `eventInto` then shares the copy) and its frame action spreads inside a fold's closure; an orb's and a mine's `step` (R3: one spread of `self`, the head of the list it is stepped in, `List.head`, which std's read-only walks `List.all`, `filter` and `next` hand to closures and results, and `Ticker.input` binds out of a field read; the screen's `mines` and `orbs` are also handed to the view's upcasts). The proof the fold needed: read-only parameters through calls (`readOnlyParams`: a screen's `view` hands `self` to the fused copies of its closures), a field taken out of a fresh value (rechecked by `analyse` when the field turns out shared), a value stored beside reads of itself (`Next(state: self, then: [if self.over ..])`), a local used once standing for its value in `passes`. The cost: the in-place pass takes about 2.5 times as long (debug, Orbit Runner 183 to 480 ms): 9 version rounds where there were 5, each about 30% dearer.
|
|
13
13
|
- **A program with no `Sized` or `Touch` view drops the dead code pass**: std's `Views.touchOf`, `sizedHit` and `missesArea` match `Touch as t` and `Sized as s` on a `View`, and once the pass has dropped every type that is one, the re-check calls each match impossible ("no type is both a View and a Touch"). Every game has such a view, so only a minimal test program meets it. A trait default's copy is told apart since 2026-11-16 (`defaultPositions` reads the trait's declarations), but these are statics.
|
|
14
14
|
- **The view's cost**: each frame makes the view's list and items. `benchmarks/screen_views` (`orbit.plum`, `scale.plum`, master's drivers) times it; not yet run against master. Master's stepping walk makes the view once more a tick and each actor's view; this branch's `stepped` makes neither, so the two `update` timings are not the same work (`scale.plum`'s header says so).
|
|
15
|
-
- **Stepping without the screen naming its actors**: done at compile time on 2026-11-16: every screen, and an actor that lists `Steps` (std/game/View), has its fields holding actors stepped by a derived `stepped` (or after its own `step`); since the Elm-style screens a screen calls it in its frame action (no stepping phase, no `steps()`), one
|
|
15
|
+
- **Stepping without the screen naming its actors**: done at compile time on 2026-11-16: every screen, and an actor that lists `Steps` (std/game/View), has its fields holding actors stepped by a derived `stepped` (or after its own `step`); since the Elm-style screens a screen calls it in its frame action (no stepping phase, no `steps()`), and one that pauses doesn't call it (`docs/game.md`, "Stepping"; the `@stepsWhen(cond)` field attribute was removed on 2026-11-16). A `List<View>` field is stepped by `Views.stepIn` since R3 (each item's type tested as it is stepped).
|
|
16
16
|
|
|
17
17
|
## Decided, not built
|
|
18
18
|
|
docs/game.md
CHANGED
|
@@ -27,7 +27,7 @@ examples/games/orbitrunner/
|
|
|
27
27
|
assets/
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
`screens/PlayingScreen.plum` writes `import actors/Ship` (one import per file) and `actors/Ship.plum` writes `import OrbitRunner` for the `Arena` it flies in (files may import each other). A file's tests are in its `tests/` mirror (`tests/examples/games/orbitrunner/screens/PlayingScreen.plum`, which imports `examples/games/orbitrunner/screens/PlayingScreen`), and `plum test tests/examples/games/orbitrunner` runs them all. A screen struct's name ends in `Screen` (`PlayingScreen`)
|
|
30
|
+
`screens/PlayingScreen.plum` writes `import actors/Ship` (one import per file) and `actors/Ship.plum` writes `import OrbitRunner` for the `Arena` it flies in (files may import each other). A file's tests are in its `tests/` mirror (`tests/examples/games/orbitrunner/screens/PlayingScreen.plum`, which imports `examples/games/orbitrunner/screens/PlayingScreen`), and `plum test tests/examples/games/orbitrunner` runs them all. A screen struct's name ends in `Screen` (`PlayingScreen`), and that is the screen's name: a screen doesn't write one. The example games carry no comments: their names and their tests say what they do.
|
|
31
31
|
|
|
32
32
|
## A minimal game
|
|
33
33
|
|
|
@@ -51,8 +51,6 @@ struct TapScreen[ScreenSlice<TapAction>](taps: Int, ball: Ball) =
|
|
|
51
51
|
fun start() -> TapScreen =
|
|
52
52
|
TapScreen(taps: 0, ball: Ball(x: 20.0, y: 150.0, vx: 0.0))
|
|
53
53
|
|
|
54
|
-
fun name(self) -> Str = "tap"
|
|
55
|
-
|
|
56
54
|
fun view(self) -> List<View> = [
|
|
57
55
|
Button(x: 350.0, y: 250.0, w: 100.0, h: 100.0, on: Tapped) {
|
|
58
56
|
Box(w: 100.0, h: 100.0, color: Color.Red)
|
|
@@ -160,13 +158,12 @@ Its motion is a `Body` (below), which `Steps` steps for it: its own `step` is it
|
|
|
160
158
|
|
|
161
159
|
**Timers are moments.** Nothing counts down: a ship hit is hurt until a moment on its own clock, `hurtFor(t)` is `Ship(..self, hurtUntil: self.clock + t)` and `hurt()` is `self.clock < self.hurtUntil`, as a balloon keeps `poppedAt` and Balloon Pop's party `partyAt`.
|
|
162
160
|
|
|
163
|
-
**Stepping.** There is no stepping phase: the frame reaches a slice screen as an action (`OnFrame(|dt| Tick(dt: dt))`), and its `update` steps its actors there with `stepped(dt)`, then runs its rules on what has moved. On master the game walks the view and the hud and steps each `Actor` it finds in place; here a screen is a value, and an actor found in a view can't be put back into the field that holds it, so the screen's own fields are stepped. Every screen has its `stepped` derived by the compiler (it lists nothing for it): each field that is an `Actor`, a value whose type has its own `step(self, dt: Float) -> Self` (a `Body`), a `List` of either (of one type, or of a trait type that is an `Actor`), an `Option` of one or a `List<View>` is stepped, and the other fields are as they are.
|
|
161
|
+
**Stepping.** There is no stepping phase: the frame reaches a slice screen as an action (`OnFrame(|dt| Tick(dt: dt))`), and its `update` steps its actors there with `stepped(dt)`, then runs its rules on what has moved. On master the game walks the view and the hud and steps each `Actor` it finds in place; here a screen is a value, and an actor found in a view can't be put back into the field that holds it, so the screen's own fields are stepped. Every screen has its `stepped` derived by the compiler (it lists nothing for it): each field that is an `Actor`, a value whose type has its own `step(self, dt: Float) -> Self` (a `Body`), a `List` of either (of one type, or of a trait type that is an `Actor`), an `Option` of one or a `List<View>` is stepped, and the other fields are as they are. Orbit Runner's:
|
|
164
162
|
|
|
165
163
|
```plum
|
|
166
164
|
struct PlayingScreen[ScreenSlice<PlayAction>](
|
|
167
|
-
|
|
165
|
+
dice: Dice, ticks: Int, assets: Assets, score: Int, best: Int, lives: Int, over: Bool, clock: Float,
|
|
168
|
-
|
|
166
|
+
ship: Ship, orbs: List<Orb>, mines: List<Mine>, rings: List<Ring>)
|
|
169
|
-
fun name(self) -> Str = "play"
|
|
170
167
|
```
|
|
171
168
|
|
|
172
169
|
is stepped as if it had written
|
|
@@ -174,9 +171,9 @@ is stepped as if it had written
|
|
|
174
171
|
```plum
|
|
175
172
|
fun stepped(self, dt: Float) -> PlayingScreen =
|
|
176
173
|
PlayingScreen(..self,
|
|
177
|
-
ship:
|
|
174
|
+
ship: self.ship.step(dt),
|
|
178
|
-
orbs:
|
|
175
|
+
orbs: Views.stepAll(self.orbs, dt),
|
|
179
|
-
mines:
|
|
176
|
+
mines: Views.stepAll(self.mines, dt),
|
|
180
177
|
rings: Views.stepAll(self.rings, dt))
|
|
181
178
|
```
|
|
182
179
|
|
|
@@ -185,26 +182,26 @@ and that is what is compiled: the derive is written out before checking, the sam
|
|
|
185
182
|
```plum
|
|
186
183
|
fun update(self, a: PlayAction) -> Next<PlayingScreen, PlayAction> = match a
|
|
187
184
|
Tick(dt) =>
|
|
188
|
-
stepped := self.stepped(dt)
|
|
185
|
+
stepped := if self.over then PlayingScreen(..self, rings: Views.stepAll(self.rings, dt)) else self.stepped(dt)
|
|
189
186
|
moved := PlayingScreen(..stepped, ticks: stepped.ticks + 1, clock: stepped.clock + dt, rings: stepped.rings.filter(|r| !r.done()))
|
|
190
187
|
Next(state: moved, then: if moved.over then Nil else moved.rules())
|
|
191
188
|
_ => Next(state: self)
|
|
192
189
|
```
|
|
193
190
|
|
|
194
|
-
so a screen that doesn't call it steps nothing, and one that pauses (a menu over the play) skips the call. A plain screen (no slice) has no actions: its tick is its `stepped`. An `OnDemand` screen asleep steps nothing, as it gets no tick; a replay, a rewind and the kit's `tick` go through the same tick.
|
|
191
|
+
so a screen that doesn't call it steps nothing, and one that pauses (a menu over the play; Clockwork's `Tick` while Space has it paused) skips the call; there is no condition on a field. A plain screen (no slice) has no actions: its tick is its `stepped`. An `OnDemand` screen asleep steps nothing, as it gets no tick; a replay, a rewind and the kit's `tick` go through the same tick.
|
|
195
192
|
|
|
196
|
-
An actor that lists `Steps` (`std/game/View`) has the same fields stepped after its own `step`: the compiler renames the `step` it writes `step$own` and makes `step` that and then the fields' stepping
|
|
193
|
+
An actor that lists `Steps` (`std/game/View`) has the same fields stepped after its own `step`: the compiler renames the `step` it writes `step$own` and makes `step` that and then the fields' stepping. When its own `step(self, dt)` is one spread of `self` that sets none of those fields, the fields' steps join that spread instead, one construction: Orbit Runner's ship writes `Ship(..self, clock: self.clock + dt)` and is compiled as `Ship(..self, clock: self.clock + dt, body: self.body.step(dt))`. So a container actor writes only what is special about itself, or no `step` at all when it is only what it holds (clicks' `Ball[Actor, Steps](body: Body)`). An actor that doesn't list `Steps` steps what it holds itself, in its own `step`. `Views.stepIn` steps each `Actor` of a `List<View>` and each one a `Group`, a `Blend` or an `Overlay` in it holds, the others as they are; it doesn't look inside an actor, nor make its view; any other view of your own that holds actors is passed over, so make it an `Actor` that steps them. A `List<View>` field is stepped with `Views.stepIn`: it holds views of any kind, so each item is tested for being an `Actor` (or a `Group`, a `Blend` or an `Overlay`) as it is stepped, the one place the stepping looks at an item's type at run time (`benchmarks/screen_views/scale.plum`'s `CrowdScreen`, 10,000 actors in a `List<View>`). An actor left out of the stepping is not stepped, whether it is in the view or not, and one stepped moves whether it is visible or not.
|
|
197
194
|
|
|
198
|
-
Once Orbit Runner's run is over its ship, orbs and mines stand still under the game over panel, while the stars, which read the screen's clock, keep twinkling, and the ship is left out of the view; the rings
|
|
195
|
+
Once Orbit Runner's run is over it doesn't call `stepped`: its ship, orbs and mines stand still under the game over panel, while the stars, which read the screen's clock, keep twinkling, and the ship is left out of the view; the rings it steps by hand, so the ripple of the last hit grows on under the panel.
|
|
199
196
|
|
|
200
|
-
`Steps` is an error on a screen (its `stepped` is derived without it), on an enum, on a struct that is not an `Actor`, and on an actor with no field holding something to step
|
|
197
|
+
`Steps` is an error on a screen (its `stepped` is derived without it), on an enum, on a struct that is not an `Actor`, and on an actor with no field holding something to step.
|
|
201
198
|
|
|
202
199
|
What else a view is, it says with a small trait:
|
|
203
200
|
|
|
204
201
|
| Trait | What it says |
|
|
205
202
|
|---|---|
|
|
206
203
|
| `Actor` (`std/game/View`) | `step(self, dt) -> Self`: its next version `dt` seconds on, which the screen's `stepped` keeps |
|
|
207
|
-
| `Steps` (`std/game/View`) | nothing to write: on an actor the compiler steps its fields holding actors or bodies after its `step` (above)
|
|
204
|
+
| `Steps` (`std/game/View`) | nothing to write: on an actor the compiler steps its fields holding actors or bodies after its `step` (above). A screen needs none |
|
|
208
205
|
| `Sized` (`std/game/Sized`) | `bounds(self) -> Aabb`, the box it covers in its own coordinates. A view with bounds can be hit; one without cannot. `hit(self, p) -> Bool` is inside the bounds, edges included, unless the view says otherwise for an exact shape (`Circle` is hit inside its circle; a balloon on its body, its knot and its string). Culling goes by the bounds too |
|
|
209
206
|
| `Round` (`std/game/Round`) | `radius(self) -> Float`: it is the circle of that radius about its pivot (`Place.pivot`). `touches(self, other: Round) -> Bool`, given, is whether the two circles overlap (closer than the sum of the radii, centre to centre) |
|
|
210
207
|
| `Touch` (`std/game/Touch`) | `touch(self) -> Touchable`: whether it takes presses. `Enabled`: it is hit where it is (when it is `Sized`) and what is under it is; `Disabled`: neither it nor anything under it is hit; `ChildrenOnly`: what is under it is, it is not. A view that says nothing is `ChildrenOnly` |
|
|
@@ -337,7 +334,7 @@ A missing file stops the game and names the file, so typos show up straight away
|
|
|
337
334
|
|
|
338
335
|
## Screens
|
|
339
336
|
|
|
340
|
-
A game with more than one screen (a title, a level select, the play) gives each its own `Screen`, with its own
|
|
337
|
+
A game with more than one screen (a title, a level select, the play) gives each its own `Screen` type, with its own state and its own view; the game finds a screen by its type, so a game has one screen of each type (two of one type stop the program when the game is made: `game: two screens are of the type 'PlayScreen'`). A screen's `name()` is its type's name (`"PlayingScreen"`, `@typeName(Self)` in the trait's default), which `transition(from)`, `showing()` and `wentTo()` give; a screen doesn't write it. What sits over a view (a game over, a pause, a dialog) is not a screen: it is part of the view of the screen it sits on, drawn while the state says so, as Orbit Runner's game over `Overlay`. The keys still go to the screen's subscriptions, so Enter can play again while the game over shows.
|
|
341
338
|
|
|
342
339
|
The first screen of the list shows first. A screen never gets the game: it changes screen with a command (below). A screen says how it comes in with `transition(self, from: Str) -> Transition`: `Cut` (the default), `Fade(secs: 0.5)` or `Slide(from: FromRight, secs: 0.4)`. While a transition runs, the game draws both screens, the one going out and then the one coming in, each on its own `background()`, moved and faded. Only the screen coming in gets ticks, and its pointer events are dropped until the transition is over (a press on a screen still sliding in lands nowhere); keys still come.
|
|
343
340
|
|
|
@@ -436,12 +433,12 @@ What a slice's `update` asks of the game is data, an `enum Cmd` (`std/game/Cmd`)
|
|
|
436
433
|
|
|
437
434
|
| `Cmd` | What the game does |
|
|
438
435
|
|---|---|
|
|
439
|
-
| `GoTo(
|
|
436
|
+
| `Cmd.goTo<OverScreen>()` (a `GoTo(screen: "OverScreen")`) | shows the game's screen of that type from the next tick on, as it was left, coming in by its `transition(from)`. A type that is not a `Screen` is a compile error (`Cmd.goTo<Int>()`: "Int does not implement Screen (the bound of S in Cmd.goTo)"); a screen type the game has none of stops the program and lists the screens (`game: no screen is a 'OverScreen' (the screens are MenuScreen, TurnScreen)`). |
|
|
440
437
|
| `Exit` | no tick runs after this one. |
|
|
441
438
|
| `Undo`, `Redo`, `RewindTo(tick: k)` | once the tick is recorded, a move through the game's history ([Undo and time travel](#undo-and-time-travel)); nothing if the game keeps none |
|
|
442
439
|
| `Play(sound: s, volume: v)`, `Loop(sound: s, volume: v)`, `Stop(sound: s)` | plays the sound once, starts it looping, or stops it (`Sound.Silent`, a test's, calls no host) |
|
|
443
440
|
|
|
444
|
-
A tick reads nothing from `io` and writes to it only through these, so a rewind plays the tick again with no sound by construction (it does only the `GoTo`s again), and a test reads the commands as values (`t.cmds`, `Cmd.names(t.cmds) == "goTo(
|
|
441
|
+
A tick reads nothing from `io` and writes to it only through these, so a rewind plays the tick again with no sound by construction (it does only the `GoTo`s again), and a test reads the commands as values (`t.cmds`, `Cmd.names(t.cmds) == "goTo(PlayingScreen) exit"`). Navigation is the game's own actions: std has none. Orbit Runner's `update` says it beside the next screen: `Hit` plays the hit, or stops the music for the game over sound once the run is over (`cmds: if over then [Stop(sound: sounds.music), Play(sound: sounds.over, volume: 1.0)] else [Play(..)]`), and `StartOver` starts the music again; Balloon Pop's `Popped` plays a pop, and the fanfare on every tenth, in the tick of the press.
|
|
445
442
|
|
|
446
443
|
## Assets
|
|
447
444
|
|
|
@@ -478,7 +475,7 @@ primitive PlayingScreenTest[UnitTest] =
|
|
|
478
475
|
| Time | `tick()` (the game's own tick: the subscriptions and presses, the frame action, transition and commands), `ticks(n)` (`n` ticks, the input going to the first), `tick(dt)` for a tick of another length, `frame(input, frameDt)` for the host's frame with the `Ticker` turning wall time into ticks |
|
|
479
476
|
| An action | `dispatch(a)`: stepped into the rig's screen through its `update`, as a delivery steps it (with its `then`), then what ends a tick: its commands, an undo, a redo or a rewind done at once; a game with a history keeps it there, so a rewind past it does it again |
|
|
480
477
|
| Reading, the view's then the hud's | `find<T>()` (the items of type `T` the view and the hud draw, in the order drawn, an item before what is under it), `first<T>()` (none stops the test: "headless: first: no actor of the type asked for is in the view"), `count<T>()`, `text(label)`, `shown(actor)` (the view or the hud draws it), `hit(x, y)` (what a press at that screen pixel lands on, the hud first), `worldOf(actor)` (its transform), `toWorld(x, y)` (a screen pixel in the world, through the camera), `drawn()` (what the frame draws, `Game.drawList()`) |
|
|
481
|
-
| What came out of the last `tick` or `ticks` | `actions<A>()` (the actions of type `A` stepped, in order), `cmds` (the commands, in order), `wentTo()` (the last `GoTo`
|
|
478
|
+
| What came out of the last `tick` or `ticks` | `actions<A>()` (the actions of type `A` stepped, in order), `cmds` (the commands, in order), `wentTo()` (the type's name of the last `GoTo`, an `Option`), `exited()`; `showing()` names the shown screen's type |
|
|
482
479
|
| A whole run | `record(seed)`, `recorded()`, `replay(r)`, and `game` (its counters) |
|
|
483
480
|
| Time travel | `TestRig.mountWith(io, screen)` and `TestRig.playWith(io, game)` keep the history; then `rewindTo(k)`, `undo()`, `redo()`, `stepBack()`, `stepForward()`, `pause()`, `resume()`, with `moved()`, and `t.screen` is the one made again |
|
|
484
481
|
|
|
@@ -508,7 +505,7 @@ The engine has master's shape at e91c40ce (`View` and `Actor`, `Screen` with its
|
|
|
508
505
|
| Master | Here | Why |
|
|
509
506
|
|---|---|---|
|
|
510
507
|
| `trait Actor[View]` with `step(&self, dt)` | `step(self, dt) -> Self` | a value is not changed in place: `step` returns the actor's next version |
|
|
511
|
-
| the game walks the view and the hud and steps each `Actor` it finds; an actor steps what it holds, and a view that is not an actor is looked inside only when it says `holdsActors()` (`Group` and `Overlay` do), the kit stopping a test on one that holds actors and says not (`unsteppedIn`) | the screen's frame action calls its `stepped(dt) -> Self`, which maps the fields that hold actors: derived by the compiler for every screen (an `Actor` field, a `List` or an `Option` of actors, a `List<View>`),
|
|
508
|
+
| the game walks the view and the hud and steps each `Actor` it finds; an actor steps what it holds, and a view that is not an actor is looked inside only when it says `holdsActors()` (`Group` and `Overlay` do), the kit stopping a test on one that holds actors and says not (`unsteppedIn`) | the screen's frame action calls its `stepped(dt) -> Self`, which maps the fields that hold actors: derived by the compiler for every screen (an `Actor` field, a `List` or an `Option` of actors, a `List<View>`), or written by hand (`Views.stepAll`, `Views.stepIn`); no `steps()`: a screen pauses by not calling it. An actor that lists `Steps` has its actor fields stepped after its own `step`. `Views.stepIn` steps an actor without looking inside it and makes a `Group` or an `Overlay` again with its actors stepped; any other view is as it is | an actor found in a view can't be put back into the field of the value that holds it, so the screen says where its actors are, one line per screen. Making a view again is only possible for a kind std knows (`Group(..g, children: ..)`), so `holdsActors()` is std's two containers, not a trait method, and a kind of your own that holds actors is an `Actor` that steps them; the kit's `unsteppedIn` check has nothing to find, since nothing is stepped behind the screen's back. The walk makes no view to look under an actor, so `benchmarks/screen_views/scale.plum`'s `update view` makes 10,000 fewer one-`Sprite` lists a tick than master's |
|
|
512
509
|
| `trait Slice<A>[Screen]` | `trait ScreenSlice<A>[Screen]` | `Slice` is std's growable array here, and a trait can't share a type's name |
|
|
513
510
|
| `fun reduce(&self, a: A)`, `fun dispatch(&self, a: A)` | one `fun update(self, a: A) -> Next<Self, A>` returning the next screen with the actions that follow (`then`) and the commands; no `dispatch` | the Elm style: one entry point returns the next screen, and what follows from an action is part of what it returns, not a call that queues it |
|
|
514
511
|
| `onKey(..) -> A?`, `onPointer`, `held(input)` and `tick(dt)`, and a stepping phase before the tick action | `subscriptions(self) -> List<Sub<A>>` (`OnFrame`, `OnKeyDown`, `OnKeyUp`, `OnHeld`, `OnWheel`, `OnPointerDown`, `OnPointerUp`, `OnPointerMove`), read once a tick; the frame is an action, and the screen steps its actors in it | one list says what a screen listens to, an item there only while the state says so; with the frame an action, nothing is stepped behind `update`'s back |
|
|
@@ -525,9 +522,9 @@ The engine has master's shape at e91c40ce (`View` and `Actor`, `Screen` with its
|
|
|
525
522
|
| actor and view fields with defaults (`Box(w:, h:)`, `Held(buttons: [])`) | the same: struct fields with defaults (`Label(x:, y:, text:)`, `Held(keys: k)`, `Held()`) | master's `Box` and `Group` default their size too; here every kind defaults its place (`x: 0.0, y: 0.0`) and names its size |
|
|
526
523
|
| `Tween` advanced in place | `tween.advance(dt) -> Tween` | a value |
|
|
527
524
|
| a replay's ticks and the ticker's in a growable array, read by index | `Replay.ticks: List<Input>`; the `Ticker` replaying holds the ticks not yet played (`played`), its head dropped each tick, with no cursor; the kit's recording is a value too (`t.recorded()`) | a replay is a value: the kit's rig, itself a value, no longer pushes into an array its copies share |
|
|
528
|
-
| an action enum writes its `toStr` for a test's names | `enum PlayAction[Show] = ..`: `[Show]` (std/Str) derives `toStr`, `Steer(1.0, -1.0)` ([language.md](language.md#structs), "
|
|
525
|
+
| an action enum writes its `toStr` for a test's names | `enum PlayAction[Show] = ..`: `[Show]` (std/Str) derives `toStr`, `Steer(1.0, -1.0)` ([language.md](language.md#structs), "Derived methods") | the names a test compares are the variant and its fields; a derive writes them once |
|
|
529
526
|
| `Game` mutated by `update(&self, ..)` | `update(self, io, input, dt) -> Game`, `step(self, io, input, dt, pixels) -> Game` | the game is a value |
|
|
530
|
-
| `Control` (`goTo`, `exit`, `undo`, `redo`, `rewindTo`) handed to an effect | `enum Cmd` (`GoTo`, `Exit`, `Undo`, `Redo`, `RewindTo`, `Play`, `Loop`, `Stop`) returned by `update` | commands are values: a test reads them (`t.cmds`), and a sound is one of them |
|
|
527
|
+
| `Control` (`goTo`, `exit`, `undo`, `redo`, `rewindTo`) handed to an effect | `enum Cmd` (`GoTo`, `Exit`, `Undo`, `Redo`, `RewindTo`, `Play`, `Loop`, `Stop`) returned by `update`; `Cmd.goTo<TurnScreen>()` goes to a screen by its type | commands are values: a test reads them (`t.cmds`), and a sound is one of them; a screen's name is its type's (no `name()` written), so a goTo names a type the compiler checks is a screen, not a string |
|
|
531
528
|
| `rewindTo`, `undo`, `redo`, .. `-> Bool` | `-> Game`, with `game.moved` | the move returns the moved game |
|
|
532
529
|
| the kit's `t.press(k)` (in place) | `u := t.press(k)` | a rig is a value |
|
|
533
530
|
| a view literal's `if`, `for`, `..` elements | `if` and `..` items and children blocks (`Overlay(..) {` + an item a line + `}`); no `for` item: `..xs.map(..)` | a `for` item would be a second way to write a `map` |
|
|
@@ -536,7 +533,7 @@ The engine has master's shape at e91c40ce (`View` and `Actor`, `Screen` with its
|
|
|
536
533
|
|
|
537
534
|
The same on both: `View` (`place`, `view`, `visible`, `paintSelf`, the shared empty default), the walks (`Views.paint`, `paintCulled`, `drawnIn`, `hitIn`, `worldIn`), `Screen` (`name`, `view`, `hud(window)`, `camera(base)`, `size`, `viewport`, `culls`, `busy`, `background`, `transition`, `frames`; no `enter`, `leave`, `resize`, `draw` or `fresh`), no `restart`, pointer events dropped during a transition, each screen on its own background in one, the ready-made views (Circle by its centre, `Label(align:)`, `Overlay` with a required size hit inside its box, `Sprite.frameAt`), `Gfx.drawRectEx` and `drawTextEx` in std, the browser, the native host and the recorder, the input (`ButtonDown`, `ButtonUp`, `Held.buttons`, the extra `Key`s, the replay's button mask and `B`/`b` events), `Math.near`, `Game.pixels`, `frameDrawn()`, `drawList()`, `camera(i)`, `toWorld`, `toScreen`, `visible(i)`, `travelAsked`, frames on demand in the hosts (`frameIdle`, the browser waiting for input, `PlumGame::idle`), `plum run --dev` (its keys, its line, the page's Cmd key fixes), and `plum compile <folder>`. Orbit Runner's state checksum was master's (925878360) until its rules returned what happened as `then` actions (the mines are checked before more are added for the tick's points, and a restart starts at once), 946756219 then, 818414470 since the Elm-style screens (the subscriptions are read once a tick, so the tick Enter restarts on steers nothing), and 181767343 since the rules (counter-based draws, so every spawn lands elsewhere; each orb caught is its own action).
|
|
538
535
|
|
|
539
|
-
Master's `clockwork` and `menu` games are ported, in this branch's style rather than line for line, the same rules and look. Clockwork (`examples/games/clockwork`): an orbit, a planet or moon and a walker each move by a `Body` that `[Steps]` steps (an orbit's moons are its `List<Orbit>`, stepped through the derive, where master's `Orbit` stepped its children with `stepIn`); a press on a body or a walker says what it means (`Presses<SkyAction>`), so the screen tells them apart by an id, not by reference (`@sameRef`); Space pauses
|
|
536
|
+
Master's `clockwork` and `menu` games are ported, in this branch's style rather than line for line, the same rules and look. Clockwork (`examples/games/clockwork`): an orbit, a planet or moon and a walker each move by a `Body` that `[Steps]` steps (an orbit's moons are its `List<Orbit>`, stepped through the derive, where master's `Orbit` stepped its children with `stepIn`); a press on a body or a walker says what it means (`Presses<SkyAction>`), so the screen tells them apart by an id, not by reference (`@sameRef`); Space pauses by not calling `stepped` while paused; the minimap's spots are worked out from the orbits when the hud is made (`Orbit.spots`), where master kept them in a field each tick; a walker wraps round by its body's `Wrap` edge, keeping the distance it went past. The menu (`examples/games/menu`): `Button`s say what a press means, choosing is a `Cmd.goTo<TurnScreen>()` or `Exit` command, and the square of the second screen turns by a `Body`. Both have their tests in the `tests/` mirror, a recorded run each and Clockwork's state checksum among them. This branch keeps `examples/games/clicks.plum` and `two_screens.plum`, which master doesn't have.
|
|
540
537
|
|
|
541
538
|
## Publishing
|
|
542
539
|
|
docs/language.md
CHANGED
|
@@ -233,11 +233,10 @@ primitive Demo[App] =
|
|
|
233
233
|
b.x = 9.0 # a.x is still 1.0
|
|
234
234
|
```
|
|
235
235
|
|
|
236
|
-
**
|
|
236
|
+
**Derived methods:** for std/game's derived stepping, every struct that is a `Screen` gets a `stepped(self, dt)` written out by the compiler before checking, which steps each field holding actors (an `Actor`, a `List` of actors or an `Option` of one, a `List<View>`, the others as they are); an `Actor` that lists `Steps` has those fields stepped after its own `step`. What is derived is an ordinary method, the same static construction one would write (`Screen(..self, ship: self.ship.step(dt), ..)`), so it costs nothing at run time; a screen that writes its own `stepped` keeps it, and a screen that pauses doesn't call it while paused. `Steps` on a screen is an error. A field has no attributes (`@name(expr)` before a field is a syntax error). See docs/game.md, "Stepping".
|
|
237
237
|
|
|
238
238
|
```plum
|
|
239
|
-
struct PlayingScreen[ScreenSlice<PlayAction>](over: Bool,
|
|
239
|
+
struct PlayingScreen[ScreenSlice<PlayAction>](over: Bool, ship: Ship, rings: List<Ring>)
|
|
240
|
-
fun name(self) -> Str = "play"
|
|
241
240
|
```
|
|
242
241
|
|
|
243
242
|
A type that lists `Show` (std/Str, `trait Show = fun toStr(self) -> Str`) and writes no `toStr` gets one derived the same way: its variant's name and its fields' values in order, each as it interpolates (`Steer(1.0, -1.0)`, `Hit`; a struct `Spot(1, 2)`, one with no fields its name), so it interpolates (`"{a}"`) and meets a `T: Show` bound. A type that writes its own `toStr` keeps it. A field whose type is one of the program's structs or enums with no `toStr` that doesn't list `Show` either is an error at the type, since it couldn't go in the string:
|
|
@@ -916,8 +915,7 @@ primitive Demo[App] =
|
|
|
916
915
|
| `@memStoreF32(addr, v)` | stores the `Float` `v` at `addr` as a 32-bit float. |
|
|
917
916
|
| `@memLoadF32(addr)` | the 32-bit float at `addr` as a `Float`. |
|
|
918
917
|
| `@native` | not a call: the whole body of a std function the compiler supplies. See [`@native`](#native). |
|
|
919
|
-
| `@stepsWhen(cond)` | not a call: an attribute before a struct field. See [Structs](#structs), "Field attributes and derived methods". |
|
|
920
|
-
| `[Show]` | not a call: a trait (std/Str) whose `toStr` is derived for a type that lists it and writes none. See [Structs](#structs), "
|
|
918
|
+
| `[Show]` | not a call: a trait (std/Str) whose `toStr` is derived for a type that lists it and writes none. See [Structs](#structs), "Derived methods". |
|
|
921
919
|
|
|
922
920
|
### Reference identity
|
|
923
921
|
|
docs/std.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# The standard library
|
|
2
2
|
|
|
3
|
-
Import what you use: `import std/Str`, `import std/Slice`, and so on. Most modules sit right under `std`; the collections are in `std/collections` (`import std/collections/List`), `Json` and `Base64` in `std/encoding`, `Math`, `Random`, `Aabb`, `Vec2` and `Affine` in `std/math`, and the game engine in `std/game`. A folder is not a module: each file is imported by its full path. The source is in [`std/`](../std), and it is plain Plum, so it's a good place to read how things work.
|
|
3
|
+
Import what you use: `import std/Str`, `import std/Slice`, and so on. Most modules sit right under `std`; the collections are in `std/collections` (`import std/collections/List`), `Json` and `Base64` in `std/encoding`, `Math`, `Random`, `Dice`, `Aabb`, `Vec2` and `Affine` in `std/math`, and the game engine in `std/game`. A folder is not a module: each file is imported by its full path. The source is in [`std/`](../std), and it is plain Plum, so it's a good place to read how things work.
|
|
4
4
|
|
|
5
5
|
The [game engine](game.md) has its own page.
|
|
6
6
|
|
|
@@ -16,7 +16,7 @@ The [game engine](game.md) has its own page.
|
|
|
16
16
|
| [`Json`](#json) | reading and writing JSON |
|
|
17
17
|
| [`Decimal`](#decimal) | exact decimal numbers, to and from `Float` |
|
|
18
18
|
| [`Os`, `Http`](#files-and-network) | files and the network |
|
|
19
|
-
| [`Time`, `Random`, `Uuid`, `Base64`, `Aabb`, `@sameRef`, `@copy`](#utilities) | utilities |
|
|
19
|
+
| [`Time`, `Random`, `Dice`, `Uuid`, `Base64`, `Aabb`, `@sameRef`, `@copy`](#utilities) | utilities |
|
|
20
20
|
| [`Test`](#testing) | helpers for tests |
|
|
21
21
|
| [`game/Screen`, `game/ScreenSlice`](#game-screens-and-slices) | a game's screens, each with its own state, actions, update and subscriptions |
|
|
22
22
|
|
|
@@ -77,7 +77,7 @@ primitive Demo[App] =
|
|
|
77
77
|
|
|
78
78
|
`Str` has no `+`: join strings with interpolation, `"{a}{b}"`, or `a.concat(b)`. To build a string in steps, use `Buffer`: `write`, `writeByte`, `toStr`.
|
|
79
79
|
|
|
80
|
-
`trait Show` (`fun toStr(self) -> Str`) is what a type is shown as. A type that lists it and writes no `toStr` has it derived, its variant's name and its fields' values (`enum PlayAction[Show] = ..` gives `Steer(1.0, -1.0)`; [language.md](language.md#structs), "
|
|
80
|
+
`trait Show` (`fun toStr(self) -> Str`) is what a type is shown as. A type that lists it and writes no `toStr` has it derived, its variant's name and its fields' values (`enum PlayAction[Show] = ..` gives `Steer(1.0, -1.0)`; [language.md](language.md#structs), "Derived methods").
|
|
81
81
|
|
|
82
82
|
## Sequences
|
|
83
83
|
|
|
@@ -258,6 +258,7 @@ primitive Demo[App] =
|
|
|
258
258
|
|
|
259
259
|
- **`Time`:** `Time.now(io)` and `Duration` values such as `Duration.seconds(5)`; both compare with `==`
|
|
260
260
|
- **`Random`:** `Random.seeded(n)`. Each call returns the value and the next generator, so the same seed always gives the same sequence. Or draw by counter, threading nothing: `Random.at(seed, tick, salt)` is a key, and `floatAt(i)`, `intAt(i)` and `intBelowAt(i, n)` its draws, each a pure function of the key and `i` (SplitMix64's mix and stream). `io.randomSeed()` gives a seed from the clock
|
|
261
|
+
- **`Dice`** (`std/math/Dice`): a game's dice, `Dice(seed)` (the seed taken once in `main` from `io.randomSeed()` and kept in a screen), drawing by counter with nothing threaded: `dice.roll(tick, kind, id)` is a `Random` key whose `floatAt(i)`, `intAt(i)` and `intBelowAt(i, n)` are its draws. `tick` is when (the screen's tick count), `kind` what the draw is for (one constant of the game per purpose: an orb dealt, a mine added) and `id` which one of that kind; the kind and the id are mixed in one after the other, not added, so two kinds never draw the same numbers, and the same tick, kind and id always do. `Dice.scatter(i, w, h)` is the `i`th point of a fixed scatter over a `w` by `h` area, with no seed (a backdrop's stars, a party's confetti), the same every run.
|
|
261
262
|
- **`Uuid`:** `Uuid.v4(rng)`, `Uuid.fromStr`, `Uuid.isValid`, `==` (case-insensitive: `fromStr` lowers the text)
|
|
262
263
|
- **`Base64`** (`import std/encoding/Base64`): `Base64.encode(data)` and `Base64.decode(text)` (a `Result`; whitespace and missing padding are fine, data after padding and a last group of one character are `Err(InvalidBase64)`), the URL alphabet `Base64.encodeUrl(data)` (no padding unless `pad` is true) and `Base64.decodeUrl(text)`, and the line-wrapped `Base64.encodePem` and `Base64.encodeMime`
|
|
263
264
|
- **`Aabb`:** an axis-aligned box, corners included: `Aabb.of(x, y, w, h)`, `width`, `height`, `center`, `overlaps`, `contains`, `containsPoint`, `union`, `distSq`, `rayEntry`
|
|
@@ -273,7 +274,7 @@ Tests live in the `tests/` mirror of the code (std's in `tests/std/...`), as the
|
|
|
273
274
|
|
|
274
275
|
## Game screens and slices
|
|
275
276
|
|
|
276
|
-
A game (`std/game/Game`) is a list of screens. A screen (`std/game/Screen`) has
|
|
277
|
+
A game (`std/game/Game`) is a list of screens. A screen (`std/game/Screen`) has its type's name (`name()`, which it doesn't write) and a `view(self) -> List<View>`, what the frame shows, made again from its fields every frame (`std/game/View`: stateless views, the ready-made `Group`, `Box`, `Circle`, `Line`, `Sprite`, `Label`, `Blend` (its children in a blend mode) and `Overlay`, and actors, `Actor[View]`, with `step(self, dt) -> Self`, which a screen steps through its `stepped(dt)`, derived for every screen from its fields holding actors, a `List<View>` among them), and may have a `hud(window)` drawn over it one unit a pixel and a `camera(base)` (`Viewport.lookAt`); a screen with rules is a `ScreenSlice<A>` (`std/game/ScreenSlice`), in the Elm style: its fields are its state, `A` its actions, `update(self, a) -> Next<Self, A>` its one entry point (pure, with no `Io`, so the same actions from the same state give the same state), returning the next screen with what follows: the actions that follow and the commands for the game (`std/game/Cmd`: a sound, `Cmd.goTo<PlayingScreen>()`, which goes to the game's screen of that type), done after the tick. Its `subscriptions(self) -> List<Sub<A>>` say which of each tick's input (`std/game/Input`: its events, and what is held after them, `Held`, their fold) and which frames become its actions: `OnFrame(|dt| ..)`, `OnKeyDown(key:, act:)`, `OnKeyUp`, `OnHeld(|h| ..)`, `OnWheel`, `OnPointerDown`, `OnPointerUp`, `OnPointerMove`, read once a tick; a press on an item that says what it means, a `Presses<A>` such as a `Button` from `std/game/Button`, is that action by default. The frame is an action: the screen steps its actors in it. std keeps the screen each `update` returns. An actor's motion is a `Body` (`std/game/Body`): `pos`, `vel`, `push`, `drag`, `edge` (`Free`, `Halt(box)`, `Bounce(box)`, `Wrap(box)`, a `Walls`), `angle`, `turn` and `faceAbove`, declared, with `step(dt)` written once in std and `place(origin)` where to draw it; a field holding one is stepped by the screen's derived `stepped` or an actor's `Steps` (any field whose type has `step(self, dt: Float) -> Self`). An actor that lists `Round` (`std/game/Round`) declares its `radius()` and is that circle about its pivot (`Place.pivot`, the point its place turns about), and `a.touches(b)` is whether two round actors overlap; under it, `Vec2.near(other, dist)` (`std/math/Vec2`) is the overlap test of two circles (`a.near(b, ra + rb)`). A run records as a `Replay` (`std/game/Replay`: the seed and a `List<Input>`, one a tick). All of it is in [the game engine](game.md).
|
|
277
278
|
|
|
278
279
|
```plum
|
|
279
280
|
import std/Int
|
|
@@ -287,8 +288,6 @@ import std/game/ScreenSlice
|
|
|
287
288
|
enum Act = Collected(points: Int) | Hurt
|
|
288
289
|
|
|
289
290
|
struct Run[ScreenSlice<Act>](score: Int, lives: Int) =
|
|
290
|
-
fun name(self) -> Str = "run"
|
|
291
|
-
|
|
292
291
|
fun subscriptions(self) -> List<Sub<Act>> = [OnKeyDown(key: Key.KeyH, act: Hurt)]
|
|
293
292
|
|
|
294
293
|
fun update(self, a: Act) -> Next<Run, Act> = match a
|
examples/games/balloonpop/screens/PlaygroundScreen.plum
CHANGED
|
@@ -47,8 +47,6 @@ struct PlaygroundScreen[ScreenSlice<PopAction>](
|
|
|
47
47
|
balloons := Range.until(0, PlaygroundScreen.Count).map(|i| Balloon.spawn(Balloon.FirstId + i, dice.roll(0, PlaygroundScreen.LaunchRoll, i), Float(i) * 110.0 - Arena.H * 0.6, 0.0))
|
|
48
48
|
PlaygroundScreen(dice: dice, ticks: 0, assets: assets, pops: 0, partyAt: -100.0, clock: 0.0, balloons: balloons)
|
|
49
49
|
|
|
50
|
-
fun name(self) -> Str = "sky"
|
|
51
|
-
|
|
52
50
|
fun size(self) -> Vec2 = Vec2(x: Arena.W, y: Arena.H)
|
|
53
51
|
|
|
54
52
|
fun background(self) -> Color = Color.rgb(0.55, 0.8, 1.0)
|
examples/games/clicks.plum
CHANGED
|
@@ -35,8 +35,6 @@ struct TapScreen[ScreenSlice<TapAction>](taps: Int, ball: Ball) =
|
|
|
35
35
|
fun start() -> TapScreen =
|
|
36
36
|
TapScreen(taps: 0, ball: Ball(body: Body(pos: Vec2(x: 20.0, y: 150.0))))
|
|
37
37
|
|
|
38
|
-
fun name(self) -> Str = "tap"
|
|
39
|
-
|
|
40
38
|
fun view(self) -> List<View> = [
|
|
41
39
|
Button(x: 350.0, y: 250.0, w: 100.0, h: 100.0, on: Tapped) {
|
|
42
40
|
Box(w: 100.0, h: 100.0, color: Color.Red)
|
examples/games/clockwork/screens/OrreryScreen.plum
CHANGED
|
@@ -34,7 +34,7 @@ enum SkyAction[Show] =
|
|
|
34
34
|
| Tick(dt: Float)
|
|
35
35
|
|
|
36
36
|
struct OrreryScreen[ScreenSlice<SkyAction>](
|
|
37
|
-
|
|
37
|
+
sun: Spinner, orbits: List<Orbit>, walkers: List<Walker>,
|
|
38
38
|
paused: Bool = false, selected: Int = -1, front: Int = -1) =
|
|
39
39
|
const Names = ["Ember", "Cinder", "Ashen", "Glacier"]
|
|
40
40
|
const Moons = [0, 1, 2, 1]
|
|
@@ -48,8 +48,6 @@ struct OrreryScreen[ScreenSlice<SkyAction>](
|
|
|
48
48
|
Orbit.chart(i, Range.until(0, i).fold(i, |id, k| id + OrreryScreen.Moons.at(k)), OrreryScreen.Names.at(i), OrreryScreen.Moons.at(i), tex)),
|
|
49
49
|
walkers: OrreryScreen.crowd(Range.until(0, OrreryScreen.Walkers).map(|i| Walker.spawn(i, tex)), -1))
|
|
50
50
|
|
|
51
|
-
fun name(self) -> Str = "orrery"
|
|
52
|
-
|
|
53
51
|
fun size(self) -> Vec2 = Vec2(x: Arena.W, y: Arena.H)
|
|
54
52
|
|
|
55
53
|
fun viewport(self) -> Scaling = Extend
|
|
@@ -94,8 +92,11 @@ struct OrreryScreen[ScreenSlice<SkyAction>](
|
|
|
94
92
|
Next(state: OrreryScreen(..self, selected: chosen, orbits: self.orbits.map(|o| o.lit(chosen))))
|
|
95
93
|
Front(walker) => Next(state: OrreryScreen(..self, front: walker, walkers: OrreryScreen.crowd(self.walkers, walker)), then: [Pick(body: -1)])
|
|
96
94
|
Tick(dt) =>
|
|
95
|
+
if self.paused
|
|
96
|
+
Next(state: OrreryScreen(..self, walkers: OrreryScreen.crowd(self.walkers, self.front)))
|
|
97
|
+
else
|
|
97
|
-
|
|
98
|
+
moved := self.stepped(dt)
|
|
98
|
-
|
|
99
|
+
Next(state: OrreryScreen(..moved, walkers: OrreryScreen.crowd(moved.walkers, moved.front)))
|
|
99
100
|
|
|
100
101
|
fun star(i: Int) -> Circle =
|
|
101
102
|
p := Dice.scatter(i, 800, 600)
|
examples/games/menu/screens/MenuScreen.plum
CHANGED
|
@@ -35,8 +35,6 @@ struct MenuScreen[ScreenSlice<MenuAction>](selected: Int, highlight: Tween) =
|
|
|
35
35
|
|
|
36
36
|
fun highlightAt(i: Int) -> Float = MenuScreen.buttonY(i) - 6.0
|
|
37
37
|
|
|
38
|
-
fun name(self) -> Str = "menu"
|
|
39
|
-
|
|
40
38
|
fun frames(self) -> Frames = OnDemand
|
|
41
39
|
|
|
42
40
|
fun size(self) -> Vec2 = Vec2(x: Arena.W, y: Arena.H)
|
|
@@ -70,7 +68,7 @@ struct MenuScreen[ScreenSlice<MenuAction>](selected: Int, highlight: Tween) =
|
|
|
70
68
|
MoveBy(step) =>
|
|
71
69
|
n := MenuScreen.Items.length()
|
|
72
70
|
Next(state: self.select({self.selected + step + n} % n))
|
|
73
|
-
Choose => Next(state: self, cmds: [if self.selected == 0 then
|
|
71
|
+
Choose => Next(state: self, cmds: [if self.selected == 0 then Cmd.goTo<TurnScreen>() else Exit])
|
|
74
72
|
Clicked(index) => Next(state: self.select(index), then: [Choose])
|
|
75
73
|
Tick(dt) => Next(state: MenuScreen(..self, highlight: self.highlight.advance(dt)))
|
|
76
74
|
|
examples/games/menu/screens/TurnScreen.plum
CHANGED
|
@@ -21,8 +21,6 @@ enum TurnAction[Show] = Back | Tick(dt: Float)
|
|
|
21
21
|
struct TurnScreen[ScreenSlice<TurnAction>](turner: Turner) =
|
|
22
22
|
fun start() -> TurnScreen = TurnScreen(turner: Turner(body: Body(pos: Vec2(x: Arena.W / 2.0, y: Arena.H / 2.0), turn: 2.0)))
|
|
23
23
|
|
|
24
|
-
fun name(self) -> Str = "play"
|
|
25
|
-
|
|
26
24
|
fun transition(self, from: Str) -> Transition = Fade(secs: 0.3)
|
|
27
25
|
|
|
28
26
|
fun size(self) -> Vec2 = Vec2(x: Arena.W, y: Arena.H)
|
|
@@ -38,5 +36,5 @@ struct TurnScreen[ScreenSlice<TurnAction>](turner: Turner) =
|
|
|
38
36
|
]
|
|
39
37
|
|
|
40
38
|
fun update(self, a: TurnAction) -> Next<TurnScreen, TurnAction> = match a
|
|
41
|
-
Back => Next(state: self, cmds: [
|
|
39
|
+
Back => Next(state: self, cmds: [Cmd.goTo<MenuScreen>()])
|
|
42
40
|
Tick(dt) => Next(state: self.stepped(dt))
|
examples/games/orbitrunner/screens/PlayingScreen.plum
CHANGED
|
@@ -39,7 +39,7 @@ enum PlayAction[Show] =
|
|
|
39
39
|
|
|
40
40
|
struct PlayingScreen[ScreenSlice<PlayAction>](
|
|
41
41
|
dice: Dice, ticks: Int, assets: Assets, score: Int, best: Int, lives: Int, over: Bool, clock: Float,
|
|
42
|
-
|
|
42
|
+
ship: Ship, orbs: List<Orb>, mines: List<Mine>, rings: List<Ring>) =
|
|
43
43
|
const OrbCount = 3
|
|
44
44
|
const StartLives = 3
|
|
45
45
|
const DealRoll = 0
|
|
@@ -54,8 +54,6 @@ struct PlayingScreen[ScreenSlice<PlayAction>](
|
|
|
54
54
|
fun at(seed: Int, assets: Assets, score: Int, lives: Int) -> PlayingScreen =
|
|
55
55
|
PlayingScreen(..PlayingScreen.start(seed, assets), score: score, lives: lives).grown()
|
|
56
56
|
|
|
57
|
-
fun name(self) -> Str = "play"
|
|
58
|
-
|
|
59
57
|
fun size(self) -> Vec2 = Vec2(x: Arena.W, y: Arena.H)
|
|
60
58
|
|
|
61
59
|
fun background(self) -> Color = Color.rgb(0.04, 0.05, 0.1)
|
|
@@ -91,7 +89,7 @@ struct PlayingScreen[ScreenSlice<PlayAction>](
|
|
|
91
89
|
fun update(self, a: PlayAction) -> Next<PlayingScreen, PlayAction> = match a
|
|
92
90
|
Steer(ax, ay) => Next(state: PlayingScreen(..self, ship: self.ship.steer(Vec2(x: ax, y: ay))))
|
|
93
91
|
Tick(dt) =>
|
|
94
|
-
stepped := self.stepped(dt)
|
|
92
|
+
stepped := if self.over then PlayingScreen(..self, rings: Views.stepAll(self.rings, dt)) else self.stepped(dt)
|
|
95
93
|
moved := PlayingScreen(..stepped, ticks: stepped.ticks + 1, clock: stepped.clock + dt, rings: stepped.rings.filter(|r| !r.done()))
|
|
96
94
|
Next(state: moved, then: if moved.over then Nil else moved.rules())
|
|
97
95
|
Caught(orb) =>
|
examples/games/two_screens.plum
CHANGED
|
@@ -21,26 +21,22 @@ enum TitleAction = Start
|
|
|
21
21
|
enum PlayAction = Back
|
|
22
22
|
|
|
23
23
|
struct TitleScreen[ScreenSlice<TitleAction>](starts: Int) =
|
|
24
|
-
fun name(self) -> Str = "title"
|
|
25
|
-
|
|
26
24
|
fun view(self) -> List<View> = [Label(x: 20.0, y: 20.0, text: "Press Enter ({self.starts} so far)", size: 24.0)]
|
|
27
25
|
|
|
28
26
|
fun frames(self) -> Frames = OnDemand
|
|
29
27
|
|
|
30
28
|
fun subscriptions(self) -> List<Sub<TitleAction>> = [OnKeyDown(key: Key.Enter, act: Start)]
|
|
31
29
|
|
|
32
|
-
fun update(self, a: TitleAction) -> Next<TitleScreen, TitleAction> = Next(state: TitleScreen(starts: self.starts + 1), cmds: [
|
|
30
|
+
fun update(self, a: TitleAction) -> Next<TitleScreen, TitleAction> = Next(state: TitleScreen(starts: self.starts + 1), cmds: [Cmd.goTo<PlayScreen>()])
|
|
33
31
|
|
|
34
32
|
struct PlayScreen[ScreenSlice<PlayAction>](runs: Int) =
|
|
35
|
-
fun name(self) -> Str = "play"
|
|
36
|
-
|
|
37
33
|
fun view(self) -> List<View> = [Label(x: 20.0, y: 20.0, text: "run {self.runs}", size: 24.0, color: Color.Yellow)]
|
|
38
34
|
|
|
39
35
|
fun transition(self, from: Str) -> Transition = Fade(secs: 0.5)
|
|
40
36
|
|
|
41
37
|
fun subscriptions(self) -> List<Sub<PlayAction>> = [OnKeyDown(key: Key.Escape, act: Back)]
|
|
42
38
|
|
|
43
|
-
fun update(self, a: PlayAction) -> Next<PlayScreen, PlayAction> = Next(state: PlayScreen(runs: self.runs + 1), cmds: [
|
|
39
|
+
fun update(self, a: PlayAction) -> Next<PlayScreen, PlayAction> = Next(state: PlayScreen(runs: self.runs + 1), cmds: [Cmd.goTo<TitleScreen>()])
|
|
44
40
|
|
|
45
41
|
primitive TwoScreens[App] =
|
|
46
42
|
fun game() -> Game =
|
std/game/Cmd.plum
CHANGED
|
@@ -7,18 +7,21 @@ import std/Bool
|
|
|
7
7
|
import std/Io
|
|
8
8
|
import std/collections/List
|
|
9
9
|
import std/game/Audio
|
|
10
|
+
import std/game/Screen
|
|
10
11
|
|
|
11
12
|
# What an update asks of the game, as a value: a slice's `update(a)` returns its commands
|
|
12
13
|
# with its next state (std/game/ScreenSlice's `Next`), and the game does them once the tick
|
|
13
14
|
# is over, in the order given, with the `Io` the tick has (std/game/Game):
|
|
14
15
|
#
|
|
15
16
|
# fun update(self, a: MenuAction) -> Next<MenuScreen, MenuAction> = match a
|
|
16
|
-
# Start => Next(state: self, cmds: [
|
|
17
|
+
# Start => Next(state: self, cmds: [Cmd.goTo<PlayingScreen>(), Play(sound: self.assets.click, volume: 1.0)])
|
|
17
18
|
# Quit => Next(state: self, cmds: [Exit])
|
|
18
19
|
#
|
|
19
|
-
# GoTo shows the screen of that
|
|
20
|
+
# GoTo shows the screen of the game of that type from the next tick on, as it was
|
|
21
|
+
# left, coming in by its `transition(from)`. Made by `Cmd.goTo<S>()`, which
|
|
22
|
+
# takes a type that is a `Screen` (another is a compile error) and keeps its
|
|
20
|
-
#
|
|
23
|
+
# name; a type the game has no screen of stops the program and lists the
|
|
21
|
-
#
|
|
24
|
+
# screens.
|
|
22
25
|
# Exit no tick runs after this one.
|
|
23
26
|
# Undo goes back to just before the last user action (docs/game.md, "Undo and time
|
|
24
27
|
# travel"); nothing if there is none, or the game keeps no history
|
|
@@ -33,7 +36,7 @@ import std/game/Audio
|
|
|
33
36
|
# then does its `GoTo`s again and nothing else, so no sound plays twice and no rewind
|
|
34
37
|
# rewinds again.
|
|
35
38
|
enum Cmd =
|
|
36
|
-
| GoTo(
|
|
39
|
+
| GoTo(screen: Str)
|
|
37
40
|
| Exit
|
|
38
41
|
| Undo
|
|
39
42
|
| Redo
|
|
@@ -42,8 +45,11 @@ enum Cmd =
|
|
|
42
45
|
| Loop(sound: Sound, volume: Float)
|
|
43
46
|
| Stop(sound: Sound)
|
|
44
47
|
|
|
48
|
+
# Goes to the game's screen of type `S` (`GoTo`).
|
|
49
|
+
fun goTo<S: Screen>() -> Cmd = GoTo(screen: @typeName(S))
|
|
50
|
+
|
|
45
51
|
fun toStr(self) -> Str = match self
|
|
46
|
-
GoTo(
|
|
52
|
+
GoTo(screen) => "goTo({screen})"
|
|
47
53
|
Exit => "exit"
|
|
48
54
|
Undo => "undo"
|
|
49
55
|
Redo => "redo"
|
|
@@ -67,5 +73,5 @@ enum Cmd =
|
|
|
67
73
|
Stop(s) => s.stop(io)
|
|
68
74
|
_ => Unit
|
|
69
75
|
|
|
70
|
-
# The commands of `cmds` as one line, for tests: "goTo(
|
|
76
|
+
# The commands of `cmds` as one line, for tests: "goTo(PlayingScreen) exit".
|
|
71
77
|
fun names(cmds: List<Cmd>) -> Str = cmds.map(|c| c.toStr()).join(" ")
|
std/game/Game.plum
CHANGED
|
@@ -33,10 +33,11 @@ import std/game/Replay
|
|
|
33
33
|
# fun main(io: Io) -> Game =
|
|
34
34
|
# Game(screens: [TitleScreen(..), PlayingScreen.start(Random.seeded(io.randomSeed()), Assets.load(io))])
|
|
35
35
|
#
|
|
36
|
-
# The first screen shows first.
|
|
36
|
+
# The first screen shows first. The game goes to another when a slice's step asks it to
|
|
37
|
-
#
|
|
37
|
+
# (`Cmd.goTo<PlayingScreen>()`, std/game/Cmd), after the tick, coming in by that screen's
|
|
38
|
-
#
|
|
38
|
+
# `transition`; it finds the screen by its type's name (`name()`). A type the game has no
|
|
39
|
-
# screens there are, as two screens of one
|
|
39
|
+
# screen of stops the program and names the screens there are, as two screens of one type
|
|
40
|
+
# do when the game is made.
|
|
40
41
|
#
|
|
41
42
|
# A game is a value: each tick returns the next game, and the screens in it are the ones
|
|
42
43
|
# the tick left. A screen left and shown again is as it was left.
|
|
@@ -146,15 +147,15 @@ struct Game(screens: List<Screen>) =
|
|
|
146
147
|
if self.screens.isEmpty()
|
|
147
148
|
crash "game: a game needs a screen"
|
|
148
149
|
if Some(dup) := Game.duplicateName(self.screens, Nil)
|
|
149
|
-
crash "game: two screens are
|
|
150
|
+
crash "game: two screens are of the type '{dup}'"
|
|
150
151
|
|
|
151
152
|
# The screen shown.
|
|
152
153
|
fun screen(self) -> Screen = self.screens.at(self.current)
|
|
153
154
|
|
|
154
|
-
# The name of the screen
|
|
155
|
+
# The name of the shown screen's type.
|
|
155
156
|
fun showing(self) -> Str = self.screen().name()
|
|
156
157
|
|
|
157
|
-
# The index of the screen named `name`, -1 if there is none.
|
|
158
|
+
# The index of the screen of the type named `name`, -1 if there is none.
|
|
158
159
|
fun indexOf(self, name: Str) -> Int = Game.indexIn(self.screens, name, 0)
|
|
159
160
|
|
|
160
161
|
# Whether a transition is drawing two screens.
|
|
@@ -275,7 +276,7 @@ struct Game(screens: List<Screen>) =
|
|
|
275
276
|
else
|
|
276
277
|
c.sound(io)
|
|
277
278
|
match c
|
|
278
|
-
GoTo(
|
|
279
|
+
GoTo(screen) => self.goTo(screen)
|
|
279
280
|
Exit => Game(..self, exited: true)
|
|
280
281
|
Loop(_, _) => Game(..self, loops: Looping.after(self.loops, c))
|
|
281
282
|
Stop(_) => Game(..self, loops: Looping.after(self.loops, c))
|
|
@@ -285,7 +286,7 @@ struct Game(screens: List<Screen>) =
|
|
|
285
286
|
# and its `Loop`s and `Stop`s kept in `loops`, and nothing else (no sound, no exit, no
|
|
286
287
|
# move through the history).
|
|
287
288
|
fun redone(self, cmds: List<Cmd>) -> Game = cmds.fold(self, |g, c| match c
|
|
288
|
-
GoTo(
|
|
289
|
+
GoTo(screen) => g.goTo(screen)
|
|
289
290
|
Loop(_, _) => Game(..g, loops: Looping.after(g.loops, c))
|
|
290
291
|
Stop(_) => Game(..g, loops: Looping.after(g.loops, c))
|
|
291
292
|
_ => g)
|
|
@@ -303,12 +304,12 @@ struct Game(screens: List<Screen>) =
|
|
|
303
304
|
Looping.changes(before, self.loops).each(|c| c.sound(io))
|
|
304
305
|
self
|
|
305
306
|
|
|
306
|
-
# Shows the screen named `name` from the next tick on, as it was left, coming in by its
|
|
307
|
+
# Shows the screen of the type named `name` from the next tick on, as it was left, coming in by its
|
|
307
308
|
# `transition`. The screen shown already stays as it is.
|
|
308
309
|
fun goTo(self, name: Str) -> Game =
|
|
309
310
|
i := self.indexOf(name)
|
|
310
311
|
if i < 0
|
|
311
|
-
crash "game: no screen is
|
|
312
|
+
crash "game: no screen is a '{name}' (the screens are {Game.namesOf(self.screens)})"
|
|
312
313
|
if i == self.current
|
|
313
314
|
self
|
|
314
315
|
else
|
|
@@ -557,7 +558,7 @@ struct Game(screens: List<Screen>) =
|
|
|
557
558
|
|
|
558
559
|
fun namesOf(xs: List<Screen>) -> Str = xs.fold("", |acc, s| if acc == "" then s.name() else "{acc}, {s.name()}")
|
|
559
560
|
|
|
560
|
-
# A
|
|
561
|
+
# A type two screens of `xs` share, the names of the types before them being `seen`.
|
|
561
562
|
fun duplicateName(xs: List<Screen>, seen: List<Str>) -> Option<Str> = match xs
|
|
562
563
|
Nil => None
|
|
563
564
|
Cons(x, rest) => if seen.any(|n| n == x.name()) then Some(x.name()) else Game.duplicateName(rest, Cons(x.name(), seen))
|
std/game/Screen.plum
CHANGED
|
@@ -17,9 +17,9 @@ import std/game/Viewport
|
|
|
17
17
|
import std/game/Cmd
|
|
18
18
|
import std/game/ScreenSlice
|
|
19
19
|
|
|
20
|
-
# One screen of a game (a title, the play, a level select):
|
|
20
|
+
# One screen of a game (a title, the play, a level select): its state, and the `view` that
|
|
21
|
-
#
|
|
21
|
+
# draws it. The game shows one screen at a time (std/game/Game) and goes to another by its
|
|
22
|
-
#
|
|
22
|
+
# type, when a slice's step asks it to (`Cmd.goTo<PlayingScreen>()`, std/game/Cmd).
|
|
23
23
|
#
|
|
24
24
|
# A screen with rules is a `ScreenSlice<A>` (std/game/ScreenSlice), which extends
|
|
25
25
|
# `Screen`: its fields are its state, `A` its actions, `update` its one entry point, and
|
|
@@ -33,26 +33,29 @@ import std/game/ScreenSlice
|
|
|
33
33
|
# seconds on. The game can't step an actor where it finds it in the view, since the
|
|
34
34
|
# screen holding it is a value, so the screen's own fields are stepped. The compiler
|
|
35
35
|
# derives it from the fields holding actors (an `Actor`, a `List` or `Option` of them, a
|
|
36
|
-
# `List<View>`)
|
|
36
|
+
# `List<View>`):
|
|
37
37
|
#
|
|
38
|
-
# struct PlayingScreen[ScreenSlice<PlayAction>](over: Bool,
|
|
38
|
+
# struct PlayingScreen[ScreenSlice<PlayAction>](over: Bool, ship: Ship, mines: List<Mine>)
|
|
39
39
|
#
|
|
40
40
|
# is stepped as if it wrote
|
|
41
41
|
#
|
|
42
42
|
# fun stepped(self, dt: Float) -> PlayingScreen =
|
|
43
|
-
# PlayingScreen(..self, ship:
|
|
43
|
+
# PlayingScreen(..self, ship: self.ship.step(dt), mines: Views.stepAll(self.mines, dt))
|
|
44
44
|
#
|
|
45
45
|
# which a screen may write itself (it wins over the derive). A screen holding no actors
|
|
46
46
|
# has the default, the screen as it is. A slice calls it in its frame action
|
|
47
|
-
# (`Tick(dt) => self.stepped(dt).rules()`); a plain screen's tick is its `stepped`.
|
|
47
|
+
# (`Tick(dt) => self.stepped(dt).rules()`); a plain screen's tick is its `stepped`. A
|
|
48
|
+
# screen that pauses doesn't call it while paused
|
|
49
|
+
# (`Tick(dt) => if self.paused then self else self.stepped(dt)`).
|
|
48
50
|
#
|
|
49
51
|
# What sits over a view (a game over, a pause, a dialog) is not a screen: it is part of
|
|
50
52
|
# the screen it sits on, an `Overlay` (std/game/Overlay) in its view, there while the
|
|
51
53
|
# state says so, and the screen maps its keys.
|
|
52
54
|
#
|
|
53
|
-
# By convention a screen struct's name ends in `Screen` (`PlayingScreen`)
|
|
55
|
+
# By convention a screen struct's name ends in `Screen` (`PlayingScreen`).
|
|
54
|
-
# stays short ("play").
|
|
55
56
|
#
|
|
57
|
+
# name the name of its type ("PlayingScreen"), which the game finds it by: a
|
|
58
|
+
# screen doesn't write it.
|
|
56
59
|
# view what it draws, made again each frame from its fields: a list of views
|
|
57
60
|
# (std/game/View) in the world's coordinates, drawn in order (later on
|
|
58
61
|
# top). Empty by default.
|
|
@@ -75,7 +78,7 @@ import std/game/ScreenSlice
|
|
|
75
78
|
# busy true while something moves that the screen wants its next tick for (a
|
|
76
79
|
# slide): an `OnDemand` screen keeps getting ticks. False by default.
|
|
77
80
|
# background what the game clears the frame with first. Black by default.
|
|
78
|
-
# transition how it comes in from the screen named `from`: at once (`Cut`, the
|
|
81
|
+
# transition how it comes in from the screen named `from` (its type's name): at once (`Cut`, the
|
|
79
82
|
# default), fading in (`Fade`) or sliding in from an edge (`Slide`).
|
|
80
83
|
# frames whether the game runs the screen's ticks every frame (`Always`, the
|
|
81
84
|
# default) or only when something happened (`OnDemand`: "Frames on demand"
|
|
@@ -95,7 +98,7 @@ import std/game/ScreenSlice
|
|
|
95
98
|
# gets ticks, and it gets no pointer events until the transition is over (keys still
|
|
96
99
|
# come).
|
|
97
100
|
trait Screen =
|
|
98
|
-
fun name(self) -> Str
|
|
101
|
+
fun name(self) -> Str = @typeName(Self)
|
|
99
102
|
|
|
100
103
|
fun view(self) -> List<View> = Nil
|
|
101
104
|
|
std/game/ScreenSlice.plum
CHANGED
|
@@ -23,8 +23,6 @@ import std/game/Screen
|
|
|
23
23
|
# enum Act = Bump | Tick(dt: Float)
|
|
24
24
|
#
|
|
25
25
|
# struct CountScreen[ScreenSlice<Act>](count: Int, ball: Ball) =
|
|
26
|
-
# fun name(self) -> Str = "count"
|
|
27
|
-
#
|
|
28
26
|
# fun subscriptions(self) -> List<Sub<Act>> = [
|
|
29
27
|
# OnFrame(|dt| Tick(dt: dt))
|
|
30
28
|
# OnKeyDown(key: Key.Space, act: Bump)
|
std/game/TestRig.plum
CHANGED
|
@@ -80,8 +80,8 @@ import std/game/Game
|
|
|
80
80
|
#
|
|
81
81
|
# What came out of the last `tick` or `ticks`: `actions<A>()` lists the actions of type `A`
|
|
82
82
|
# stepped, in order; `cmds` the commands they gave (std/game/Cmd), in order, as values;
|
|
83
|
-
# `wentTo()` the screen a `GoTo` asked for last, `exited()` whether the game has exited.
|
|
83
|
+
# `wentTo()` the name of the screen type a `GoTo` asked for last, `exited()` whether the game has exited.
|
|
84
|
-
# `showing()` names the screen
|
|
84
|
+
# `showing()` names the shown screen's type. `record(seed)`, `recorded()` and `replay(r)` record
|
|
85
85
|
# and replay the game (std/game/Replay).
|
|
86
86
|
#
|
|
87
87
|
# Undo and time travel (std/game/History): `TestRig.mountWith(io, screen)` and
|
|
@@ -299,7 +299,7 @@ struct TestRig<S>(
|
|
|
299
299
|
fun wentTo(self) -> Option<Str> =
|
|
300
300
|
none: Option<Str> := None
|
|
301
301
|
self.cmds.fold(none, |w, c| match c
|
|
302
|
-
GoTo(
|
|
302
|
+
GoTo(screen) => Some(screen)
|
|
303
303
|
_ => w)
|
|
304
304
|
|
|
305
305
|
fun exited(self) -> Bool = self.game.exited
|
std/game/View.plum
CHANGED
|
@@ -110,14 +110,9 @@ trait Actor[View] =
|
|
|
110
110
|
# std/game/Body), a `List` or an `Option` of either, or a `List<View>` (`Views.stepIn`,
|
|
111
111
|
# each item's type tested as it is stepped), the others as they are, at compile time (the
|
|
112
112
|
# same static construction a hand-written step is), so its own `step` holds only what is
|
|
113
|
-
# special about it (`Ship(..self, clock: self.clock + dt)`, its body stepped with it). A
|
|
113
|
+
# special about it (`Ship(..self, clock: self.clock + dt)`, its body stepped with it). A
|
|
114
|
-
# from the same fields (std/game/Screen). `@stepsWhen(cond)` before a field, on a screen or
|
|
115
|
-
# a `[Steps]` actor, steps it only while `cond` holds, read on `self`:
|
|
116
|
-
#
|
|
117
|
-
# struct PlayingScreen[ScreenSlice<PlayAction>](
|
|
118
|
-
# over: Bool, @stepsWhen(!self.over) ship: Ship, rings: List<Ring>)
|
|
119
|
-
#
|
|
120
|
-
#
|
|
114
|
+
# screen needs no `Steps`: its `stepped` is derived from the same fields (std/game/Screen,
|
|
115
|
+
# docs/game.md, "Stepping").
|
|
121
116
|
trait Steps
|
|
122
117
|
|
|
123
118
|
# One entry of a draw list: an item drawn, its transform (its own coordinates to the
|
tests/examples/games/menu/Menu.plum
CHANGED
|
@@ -25,8 +25,8 @@ primitive MenuTest[UnitTest] =
|
|
|
25
25
|
fun clickingPlayGoesToThePlayAndEscapeComesBack() =
|
|
26
26
|
t := TestRig.play(io, Menu.game())
|
|
27
27
|
u := t.clickAt(400.0, MenuScreen.buttonY(0) + 28.0).tick()
|
|
28
|
-
assert u.wentTo().unwrap() == "
|
|
28
|
+
assert u.wentTo().unwrap() == "TurnScreen"
|
|
29
|
-
assert u.showing() == "
|
|
29
|
+
assert u.showing() == "TurnScreen"
|
|
30
30
|
assert u.game.shifting()
|
|
31
31
|
v := u.ticks(30)
|
|
32
32
|
assert v.game.skipped == 0
|
|
@@ -35,7 +35,7 @@ primitive MenuTest[UnitTest] =
|
|
|
35
35
|
square := v.worldOf(turner).unwrap()
|
|
36
36
|
assert Math.near(square.rotation(), turner.place().rotation, 0.000001)
|
|
37
37
|
w := v.press(Key.Escape).tick()
|
|
38
|
-
assert w.showing() == "
|
|
38
|
+
assert w.showing() == "MenuScreen"
|
|
39
39
|
assert MenuHelpers.menu(w).selected == 0
|
|
40
40
|
|
|
41
41
|
# a recorded session with long idle stretches replays to the same menu, sleeping through the same ticks
|
tests/examples/games/menu/screens/MenuScreen.plum
CHANGED
|
@@ -54,8 +54,8 @@ primitive MenuScreenTest[UnitTest] =
|
|
|
54
54
|
fun choosingPlaysOrQuits() =
|
|
55
55
|
t := TestRig.play(io, Menu.game()).press(Key.Enter).tick()
|
|
56
56
|
assert t.actions<MenuAction>().map(|a| a.toStr()).join(" ") == "Choose Tick({Ticker.Tick})"
|
|
57
|
-
assert t.showing() == "
|
|
57
|
+
assert t.showing() == "TurnScreen"
|
|
58
|
-
assert Cmd.names(MenuScreen.start().update(Choose).cmds) == "goTo(
|
|
58
|
+
assert Cmd.names(MenuScreen.start().update(Choose).cmds) == "goTo(TurnScreen)"
|
|
59
59
|
assert Cmd.names(MenuScreen.start().update(MoveBy(step: 1)).state.update(Choose).cmds) == "exit"
|
|
60
60
|
u := MenuScreenHelpers.menu(io).press(Key.Down).press(Key.Space).tick()
|
|
61
61
|
assert u.exited()
|
tests/examples/games/menu/screens/TurnScreen.plum
CHANGED
|
@@ -20,4 +20,4 @@ primitive TurnScreenTest[UnitTest] =
|
|
|
20
20
|
t := TestRig.mount(io, TurnScreen.start()).ticks(30)
|
|
21
21
|
assert t.screen.turner.place().rotation > 0.9
|
|
22
22
|
assert t.tick().actions<TurnAction>().map(|a| a.toStr()).join(" ") == "Tick({Ticker.Tick})"
|
|
23
|
-
assert Cmd.names(t.screen.update(Back).cmds) == "goTo(
|
|
23
|
+
assert Cmd.names(t.screen.update(Back).cmds) == "goTo(MenuScreen)"
|
tests/examples/games/orbitrunner/screens/PlayingScreen.plum
CHANGED
|
@@ -287,7 +287,7 @@ primitive PlayingScreenTest[UnitTest] =
|
|
|
287
287
|
assert t.screen.lives == 0
|
|
288
288
|
assert !t.shown(t.screen.ship)
|
|
289
289
|
assert t.count<Overlay>() == 1
|
|
290
|
-
assert t.showing() == "
|
|
290
|
+
assert t.showing() == "PlayingScreen"
|
|
291
291
|
|
|
292
292
|
# while the game over panel shows, Enter or R plays again: StartOver at once, which starts the music again; in play they do nothing
|
|
293
293
|
fun whileTheGameOverPanelShowsEnterOr() =
|
|
@@ -332,7 +332,7 @@ primitive PlayingScreenTest[UnitTest] =
|
|
|
332
332
|
u := t.tick()
|
|
333
333
|
assert u.screen.over
|
|
334
334
|
v := u.press(Key.KeyR).tick()
|
|
335
|
-
assert v.showing() == "
|
|
335
|
+
assert v.showing() == "PlayingScreen"
|
|
336
336
|
assert !v.screen.over
|
|
337
337
|
assert v.screen.score == 0
|
|
338
338
|
assert v.screen.best == 30
|
tests/examples/games/two_screens.plum
CHANGED
|
@@ -19,11 +19,11 @@ primitive TwoScreensTest[UnitTest] =
|
|
|
19
19
|
# Enter fades in the play; Escape counts a run and goes back to the title as it was left
|
|
20
20
|
fun enterFadesInThePlayEscapeCountsA() =
|
|
21
21
|
t := TestRig.play(io, TwoScreens.game()).press(Key.Enter).tick()
|
|
22
|
-
assert t.showing() == "
|
|
22
|
+
assert t.showing() == "PlayScreen"
|
|
23
23
|
assert t.game.shifting()
|
|
24
24
|
assert t.text(t.first<Label>()) == "run 0"
|
|
25
25
|
u := t.ticks(30).press(Key.Escape).tick()
|
|
26
|
-
assert u.showing() == "
|
|
26
|
+
assert u.showing() == "TitleScreen"
|
|
27
27
|
assert u.text(u.first<Label>()) == "Press Enter (1 so far)"
|
|
28
28
|
v := u.press(Key.Enter).tick()
|
|
29
29
|
assert v.text(v.first<Label>()) == "run 1"
|
|
@@ -33,4 +33,4 @@ primitive TwoScreensTest[UnitTest] =
|
|
|
33
33
|
t := TestRig.play(io, TwoScreens.game()).press(Key.Enter).tick()
|
|
34
34
|
assert t.game.shifting()
|
|
35
35
|
u := t.clickAt(20.0, 20.0).press(Key.Escape).tick()
|
|
36
|
-
assert u.showing() == "
|
|
36
|
+
assert u.showing() == "TitleScreen"
|
tests/port/game/determinism.plum
CHANGED
|
@@ -30,8 +30,6 @@ struct Probe[ScreenSlice<Saw>](rng: Random, ticks: Int, time: Float, minDt: Floa
|
|
|
30
30
|
fun seeded(seed: Int) -> Probe =
|
|
31
31
|
Probe(rng: Random.seeded(seed), ticks: 0, time: 0.0, minDt: 1.0, maxDt: 0.0, presses: 0, held: 0, roll: 0)
|
|
32
32
|
|
|
33
|
-
fun name(self) -> Str = "probe"
|
|
34
|
-
|
|
35
33
|
fun subscriptions(self) -> List<Sub<Saw>> = [
|
|
36
34
|
OnKeyDown(key: Key.Space, act: Pressed)
|
|
37
35
|
OnHeld(|h| Seen(spaceHeld: h.isKeyDown(Key.Space)))
|
tests/port/game/replay.plum
CHANGED
|
@@ -35,8 +35,6 @@ struct Probe[ScreenSlice<Saw>](rng: Random, ticks: Int, presses: Int, held: Int,
|
|
|
35
35
|
fun seeded(seed: Int) -> Probe =
|
|
36
36
|
Probe(rng: Random.seeded(seed), ticks: 0, presses: 0, held: 0, roll: 0, x: 0.0, y: 0.0, px: 0.0, py: 0.0)
|
|
37
37
|
|
|
38
|
-
fun name(self) -> Str = "probe"
|
|
39
|
-
|
|
40
38
|
fun subscriptions(self) -> List<Sub<Saw>> = [
|
|
41
39
|
OnKeyDown(key: Key.Space, act: Pressed)
|
|
42
40
|
OnPointerDown(|at| Pressed)
|
tests/port/game/screens.plum
CHANGED
|
@@ -70,8 +70,6 @@ struct ProbeScreen[ScreenSlice<ProbeAct>](
|
|
|
70
70
|
under: Box(x: 400.0, y: 400.0, w: 50.0, h: 50.0, color: Color.Red, touch: Enabled),
|
|
71
71
|
covered: false, hits: Nil, misses: 0, keys: 0, echoes: 0, ticks: 0, left: 0, frames: frames, rng: Random.seeded(5), roll: 0)
|
|
72
72
|
|
|
73
|
-
fun name(self) -> Str = "probe"
|
|
74
|
-
|
|
75
73
|
fun frames(self) -> Frames = self.frames
|
|
76
74
|
|
|
77
75
|
fun size(self) -> Vec2 = Vec2(x: 800.0, y: 600.0)
|
|
@@ -111,7 +109,7 @@ struct ProbeScreen[ScreenSlice<ProbeAct>](
|
|
|
111
109
|
r := self.rng.nextInt()
|
|
112
110
|
Next(state: ProbeScreen(..self, rng: r.rng, roll: r.value))
|
|
113
111
|
Keyed => Next(state: ProbeScreen(..self, keys: self.keys + 1, left: 3), then: [Echo])
|
|
114
|
-
GoPlay => Next(state: self, cmds: [
|
|
112
|
+
GoPlay => Next(state: self, cmds: [Cmd.goTo<PlayProbe>()])
|
|
115
113
|
Quit => Next(state: self, cmds: [Exit])
|
|
116
114
|
_ => Next(state: self)
|
|
117
115
|
|
|
@@ -152,8 +150,6 @@ struct Turned[View, Touch](group: Group) =
|
|
|
152
150
|
|
|
153
151
|
# The play: Escape goes back, fading in from the probe.
|
|
154
152
|
struct PlayProbe[ScreenSlice<ProbeAct>](presses: Int, ticks: Int) =
|
|
155
|
-
fun name(self) -> Str = "play"
|
|
156
|
-
|
|
157
153
|
fun transition(self, from: Str) -> Transition = Fade(secs: 0.5)
|
|
158
154
|
|
|
159
155
|
fun subscriptions(self) -> List<Sub<ProbeAct>> = [
|
|
@@ -165,7 +161,7 @@ struct PlayProbe[ScreenSlice<ProbeAct>](presses: Int, ticks: Int) =
|
|
|
165
161
|
fun update(self, a: ProbeAct) -> Next<PlayProbe, ProbeAct> = match a
|
|
166
162
|
Ticked(_) => Next(state: PlayProbe(..self, ticks: self.ticks + 1))
|
|
167
163
|
Missed => Next(state: PlayProbe(..self, presses: self.presses + 1))
|
|
168
|
-
Back => Next(state: self, cmds: [
|
|
164
|
+
Back => Next(state: self, cmds: [Cmd.goTo<ProbeScreen>()])
|
|
169
165
|
_ => Next(state: self)
|
|
170
166
|
|
|
171
167
|
# A walker that moves right `speed` a second, stepped by the game.
|
|
@@ -184,8 +180,6 @@ enum WalkAct = Saw(x: Float) | Walked(dt: Float) | TakeBack | Pause | Follow | H
|
|
|
184
180
|
struct WalkScreen[ScreenSlice<WalkAct>](walker: Walker, seen: List<Float>, paused: Bool, follow: Bool, hudPresses: Int) =
|
|
185
181
|
fun create() -> WalkScreen = WalkScreen(walker: Walker(x: 0.0, speed: 60.0), seen: Nil, paused: false, follow: false, hudPresses: 0)
|
|
186
182
|
|
|
187
|
-
fun name(self) -> Str = "walk"
|
|
188
|
-
|
|
189
183
|
fun size(self) -> Vec2 = Vec2(x: 800.0, y: 600.0)
|
|
190
184
|
|
|
191
185
|
fun view(self) -> List<View> = [self.walker]
|
|
@@ -221,8 +215,6 @@ struct WalkScreen[ScreenSlice<WalkAct>](walker: Walker, seen: List<Float>, pause
|
|
|
221
215
|
# A screen with no rules holding a walker: the game steps it all the same, through the
|
|
222
216
|
# `stepped` derived for every screen.
|
|
223
217
|
struct StillScreen[Screen](walker: Walker) =
|
|
224
|
-
fun name(self) -> Str = "still"
|
|
225
|
-
|
|
226
218
|
fun view(self) -> List<View> = [self.walker]
|
|
227
219
|
|
|
228
220
|
primitive ScreensTest[UnitTest] =
|
|
@@ -281,7 +273,7 @@ primitive ScreensTest[UnitTest] =
|
|
|
281
273
|
# a step's commands are values, done after the tick and read back in order
|
|
282
274
|
fun aStepsCommandsAreReadBackInOrder() =
|
|
283
275
|
t := TestRig.play(io, Screens.twoScreens()).press(Key.KeyG).press(Key.KeyQ).tick()
|
|
284
|
-
assert Cmd.names(t.cmds) == "goTo(
|
|
276
|
+
assert Cmd.names(t.cmds) == "goTo(PlayProbe) exit"
|
|
285
277
|
assert t.exited()
|
|
286
278
|
assert Cmd.names(ProbeScreen.create(Always).update(Quit).cmds) == "exit"
|
|
287
279
|
|
|
@@ -297,8 +289,8 @@ primitive ScreensTest[UnitTest] =
|
|
|
297
289
|
# a command goes to another screen, which fades in, its presses dropped until it is in; the old one is kept and resumes
|
|
298
290
|
fun effectsGoToAnotherScreenWhichFadesIn() =
|
|
299
291
|
t := TestRig.play(io, Screens.twoScreens()).press(Key.KeyG).tick()
|
|
300
|
-
assert t.wentTo().unwrap() == "
|
|
292
|
+
assert t.wentTo().unwrap() == "PlayProbe"
|
|
301
|
-
assert t.showing() == "
|
|
293
|
+
assert t.showing() == "PlayProbe"
|
|
302
294
|
assert t.game.shifting()
|
|
303
295
|
sliding := t.clickAt(5.0, 5.0).tick()
|
|
304
296
|
assert Screens.playOf(sliding).presses == 0
|
|
@@ -307,7 +299,7 @@ primitive ScreensTest[UnitTest] =
|
|
|
307
299
|
assert !u.game.shifting()
|
|
308
300
|
assert Screens.playOf(u).ticks == 30
|
|
309
301
|
back := u.press(Key.Escape).tick()
|
|
310
|
-
assert back.showing() == "
|
|
302
|
+
assert back.showing() == "ProbeScreen"
|
|
311
303
|
assert Screens.probeOf(back.game.screen()).ticks == 1
|
|
312
304
|
|
|
313
305
|
fun exitStopsTheTicks() =
|
tests/std/game/Button.plum
CHANGED
|
@@ -37,8 +37,6 @@ struct Token[View, Sized, Touch, Presses<Pick>](x: Float, n: Int) =
|
|
|
37
37
|
# A screen whose presses are all what its items say: two buttons (one inside a group), a
|
|
38
38
|
# token, a box that takes presses but says nothing, and a button of another action type.
|
|
39
39
|
struct PickScreen[ScreenSlice<Pick>](picks: List<Int>, others: Int) =
|
|
40
|
-
fun name(self) -> Str = "pick"
|
|
41
|
-
|
|
42
40
|
fun view(self) -> List<View> = [
|
|
43
41
|
Button(x: 10.0, y: 10.0, w: 40.0, h: 20.0, on: Picked(n: 1)) {
|
|
44
42
|
Box(w: 40.0, h: 20.0, color: Color.Red)
|
tests/std/game/Cmd.plum
CHANGED
|
@@ -5,14 +5,25 @@ import std/Bool
|
|
|
5
5
|
import std/collections/List
|
|
6
6
|
import std/game/Audio
|
|
7
7
|
import std/game/Cmd
|
|
8
|
+
import std/game/Screen
|
|
8
9
|
import std/Test
|
|
9
10
|
|
|
10
11
|
primitive CmdTest[UnitTest] =
|
|
11
12
|
fun commandsAreValuesThatNameThemselves() =
|
|
12
|
-
cmds: List<Cmd> := [GoTo(
|
|
13
|
+
cmds: List<Cmd> := [GoTo(screen: "PlayScreen"), Exit, Undo, Redo, RewindTo(tick: 12), Play(sound: Sound.Silent, volume: 0.5), Loop(sound: Sound(id: 3), volume: 1.0), Stop(sound: Sound(id: 3))]
|
|
13
|
-
assert Cmd.names(cmds) == "goTo(
|
|
14
|
+
assert Cmd.names(cmds) == "goTo(PlayScreen) exit undo redo rewindTo(12) play(-1, 0.5) loop(3, 1.0) stop(3)"
|
|
14
15
|
assert cmds.filter(|c| c.travels()).length() == 3
|
|
15
16
|
|
|
17
|
+
# goTo names a screen by its type, which must be a screen (`Cmd.goTo<Int>()` is a compile
|
|
18
|
+
# error: "Int does not implement Screen (the bound of S in Cmd.goTo)")
|
|
19
|
+
fun goToNamesAScreenByItsType() =
|
|
20
|
+
assert Cmd.goTo<Lobby>().toStr() == "goTo(Lobby)"
|
|
21
|
+
assert Cmd.names([Cmd.goTo<Lobby>(), Exit]) == "goTo(Lobby) exit"
|
|
22
|
+
to := match Cmd.goTo<Lobby>()
|
|
23
|
+
GoTo(screen) => screen
|
|
24
|
+
_ => ""
|
|
25
|
+
assert to == "Lobby"
|
|
26
|
+
|
|
16
27
|
# `plum test`'s host traps on any game call, so getting to the end is the check; the
|
|
17
28
|
# recorded calls are checked in compiler/game/tests/host_test.rs, aSilentSoundsCommandsCallNoHost
|
|
18
29
|
fun aSilentSoundsCommandsCallNoHost() =
|
|
@@ -20,3 +31,5 @@ primitive CmdTest[UnitTest] =
|
|
|
20
31
|
Loop(sound: Sound.Silent, volume: 1.0).sound(io)
|
|
21
32
|
Stop(sound: Sound.Silent).sound(io)
|
|
22
33
|
Exit.sound(io)
|
|
34
|
+
|
|
35
|
+
struct Lobby[Screen](n: Int)
|
tests/std/game/Game.plum
CHANGED
|
@@ -21,6 +21,18 @@ import std/App
|
|
|
21
21
|
import std/Test
|
|
22
22
|
|
|
23
23
|
primitive GameTest[UnitTest] =
|
|
24
|
+
# a screen's name is its type's, which a goTo of the type finds; going back shows it as it was left
|
|
25
|
+
fun aScreensNameIsItsTypesAndGoToFindsIt() =
|
|
26
|
+
counted: Screen := Counted.start()
|
|
27
|
+
quiet: Screen := Quiet(n: 0)
|
|
28
|
+
assert counted.name() == "Counted" && quiet.name() == "Quiet"
|
|
29
|
+
g := Game(screens: [counted, quiet])
|
|
30
|
+
assert g.showing() == "Counted"
|
|
31
|
+
there := g.obey(io, Cmd.goTo<Quiet>())
|
|
32
|
+
assert there.showing() == "Quiet" && there.current == 1
|
|
33
|
+
assert there.obey(io, Cmd.goTo<Counted>()).showing() == "Counted"
|
|
34
|
+
assert there.obey(io, Cmd.goTo<Quiet>()).current == 1
|
|
35
|
+
|
|
24
36
|
# a tick takes the window from its input, and an input with no window leaves it as it was
|
|
25
37
|
fun aTickTakesTheWindowFromItsInput() =
|
|
26
38
|
g := GameHelpers.game().update(io, Input().sized(640.0, 480.0), Ticker.Tick)
|
|
@@ -155,8 +167,6 @@ enum Act = Tick(dt: Float) | Bump | Steer(n: Int) | Back | Music(on: Bool)
|
|
|
155
167
|
struct Counted[ScreenSlice<Act>](ticks: Int, keys: Int, steer: Int, sum: Int) =
|
|
156
168
|
fun start() -> Counted = Counted(ticks: 0, keys: 0, steer: 0, sum: 0)
|
|
157
169
|
|
|
158
|
-
fun name(self) -> Str = "counted"
|
|
159
|
-
|
|
160
170
|
fun subscriptions(self) -> List<Sub<Act>> = [
|
|
161
171
|
OnFrame(|dt| Tick(dt: dt))
|
|
162
172
|
OnHeld(|h| Steer(n: Int(h.axis(Key.Left, Key.Right))))
|
|
@@ -175,8 +185,6 @@ struct Counted[ScreenSlice<Act>](ticks: Int, keys: Int, steer: Int, sum: Int) =
|
|
|
175
185
|
|
|
176
186
|
# A screen that sleeps through the ticks with nothing to do.
|
|
177
187
|
struct Quiet[Screen](n: Int) =
|
|
178
|
-
fun name(self) -> Str = "quiet"
|
|
179
|
-
|
|
180
188
|
fun view(self) -> List<View> = Nil
|
|
181
189
|
|
|
182
190
|
fun frames(self) -> Frames = OnDemand
|
tests/std/game/Replay.plum
CHANGED
|
@@ -17,8 +17,6 @@ import std/Test
|
|
|
17
17
|
|
|
18
18
|
# A 800 by 600 world fitted to the window that keeps the world x of each press.
|
|
19
19
|
struct Clicks[ScreenSlice<Float>](xs: List<Float>) =
|
|
20
|
-
fun name(self) -> Str = "clicks"
|
|
21
|
-
|
|
22
20
|
fun size(self) -> Vec2 = Vec2(x: 800.0, y: 600.0)
|
|
23
21
|
|
|
24
22
|
fun subscriptions(self) -> List<Sub<Float>> = [OnPointerDown(|at| at.x)]
|
tests/std/game/ScreenSlice.plum
CHANGED
|
@@ -18,8 +18,6 @@ enum Turn = Rolled(dy: Float, x: Float) | Steered(ax: Float, x: Float)
|
|
|
18
18
|
|
|
19
19
|
# A screen that keeps each wheel turn with where it happened, and what `OnHeld` saw.
|
|
20
20
|
struct WheelScreen[ScreenSlice<Turn>](turns: List<Turn>) =
|
|
21
|
-
fun name(self) -> Str = "wheel"
|
|
22
|
-
|
|
23
21
|
fun subscriptions(self) -> List<Sub<Turn>> = [
|
|
24
22
|
OnWheel(|dy, at| Rolled(dy: dy, x: at.x))
|
|
25
23
|
OnHeld(|h| Steered(ax: h.axis(Key.Left, Key.Right), x: h.pointer.x))
|
|
@@ -36,8 +34,6 @@ enum Heard = Down(key: Int) | Up(key: Int) | Pressed(x: Float) | Moved(x: Float)
|
|
|
36
34
|
# A screen that logs what each subscription gave, in the order the actions came. It hears
|
|
37
35
|
# Space only while it is not stopped, and `Stop` (Escape) stops it.
|
|
38
36
|
struct LogScreen[ScreenSlice<Heard>](log: List<Str>, stopped: Bool) =
|
|
39
|
-
fun name(self) -> Str = "log"
|
|
40
|
-
|
|
41
37
|
fun subscriptions(self) -> List<Sub<Heard>> = [
|
|
42
38
|
OnFrame(|dt| Frame(dt: dt))
|
|
43
39
|
OnHeld(|h| Holding(n: h.keys.length()))
|
|
@@ -65,13 +61,10 @@ struct LogScreen[ScreenSlice<Heard>](log: List<Str>, stopped: Bool) =
|
|
|
65
61
|
Stop => "stop"
|
|
66
62
|
|
|
67
63
|
# A screen with no actions: the default `update` and `subscriptions`.
|
|
68
|
-
struct StillScreen[ScreenSlice<Int>](n: Int)
|
|
64
|
+
struct StillScreen[ScreenSlice<Int>](n: Int)
|
|
69
|
-
fun name(self) -> Str = "still"
|
|
70
65
|
|
|
71
66
|
# An `OnDemand` screen that steers only while it says so: what is held wakes it then.
|
|
72
67
|
struct SleepyScreen[ScreenSlice<Int>](steering: Bool, seen: Int) =
|
|
73
|
-
fun name(self) -> Str = "sleepy"
|
|
74
|
-
|
|
75
68
|
fun frames(self) -> Frames = OnDemand
|
|
76
69
|
|
|
77
70
|
fun subscriptions(self) -> List<Sub<Int>> = [if self.steering then OnHeld(|h| h.keys.length())]
|
|
@@ -80,8 +73,6 @@ struct SleepyScreen[ScreenSlice<Int>](steering: Bool, seen: Int) =
|
|
|
80
73
|
|
|
81
74
|
# A screen that logs the other mouse buttons.
|
|
82
75
|
struct ButtonScreen[ScreenSlice<Str>](log: List<Str>) =
|
|
83
|
-
fun name(self) -> Str = "buttons"
|
|
84
|
-
|
|
85
76
|
fun subscriptions(self) -> List<Sub<Str>> = [
|
|
86
77
|
OnButtonDown(|b, at| "down {b} at {at.x}")
|
|
87
78
|
OnButtonUp(|b, at| "up {b} at {at.x}")
|
|
@@ -91,8 +82,6 @@ struct ButtonScreen[ScreenSlice<Str>](log: List<Str>) =
|
|
|
91
82
|
|
|
92
83
|
# A screen whose action `n` asks for `n - 1` after it, down to 0.
|
|
93
84
|
struct ChainScreen[ScreenSlice<Int>](n: Int) =
|
|
94
|
-
fun name(self) -> Str = "chain"
|
|
95
|
-
|
|
96
85
|
fun update(self, k: Int) -> Next<ChainScreen, Int> = Next(state: ChainScreen(n: self.n + 1), then: if k > 0 then [k - 1] else Nil)
|
|
97
86
|
|
|
98
87
|
primitive ScreenSliceTest[UnitTest] =
|
tests/std/game/View.plum
CHANGED
|
@@ -164,29 +164,22 @@ primitive ViewTest[UnitTest] =
|
|
|
164
164
|
assert s.scene.map(|x| ViewHelpers.timeOf(x)) == [7.5, -1.0]
|
|
165
165
|
assert s.scene.at(1).view().map(|x| ViewHelpers.timeOf(x)) == [8.5]
|
|
166
166
|
|
|
167
|
-
# `@stepsWhen` steps a field only while its condition holds, the others always
|
|
168
|
-
fun stepsWhenPausesOneField() =
|
|
169
|
-
on := Paused(paused: false, walker: Stepper(time: 0.0), clocks: [Stepper(time: 0.0)])
|
|
170
|
-
off := Paused(..on, paused: true)
|
|
171
|
-
assert on.stepped(1.0).walker.time == 1.0 && on.stepped(1.0).clocks.map(|x| x.time) == [1.0]
|
|
172
|
-
assert off.stepped(1.0).walker.time == 0.0 && off.stepped(1.0).clocks.map(|x| x.time) == [1.0]
|
|
173
|
-
|
|
174
167
|
# a screen that writes its own `stepped` keeps it
|
|
175
168
|
fun aHandWrittenSteppedWins() =
|
|
176
169
|
assert Owned(one: Stepper(time: 0.0)).stepped(1.0).one.time == 2.0
|
|
177
170
|
|
|
178
171
|
# a [Steps] actor's fields are stepped after its own `step`, which sees them unstepped
|
|
179
172
|
fun aStepsActorStepsItsFieldsAfterItsOwnStep() =
|
|
180
|
-
n := Nest(time: 0.0, kids: [Stepper(time: 1.0)],
|
|
173
|
+
n := Nest(time: 0.0, kids: [Stepper(time: 1.0)], inner: Stepper(time: 0.0)).step(0.5)
|
|
181
174
|
assert n.time == 0.5
|
|
182
175
|
assert n.kids.map(|x| x.time) == [1.5, 0.5]
|
|
183
|
-
assert n.inner.time == 0.
|
|
176
|
+
assert n.inner.time == 0.5
|
|
184
177
|
again := n.step(0.5)
|
|
185
|
-
assert again.inner.time ==
|
|
178
|
+
assert again.inner.time == 1.0 && again.kids.length() == 3
|
|
186
179
|
|
|
187
180
|
# a [Steps] actor with no `step` of its own steps its fields alone, nested actors too
|
|
188
181
|
fun aStepsActorWithoutAStepStepsItsFields() =
|
|
189
|
-
p := Pair(left: Stepper(time: 0.0), right: Some(Nest(time: 0.0, kids: Nil,
|
|
182
|
+
p := Pair(left: Stepper(time: 0.0), right: Some(Nest(time: 0.0, kids: Nil, inner: Stepper(time: 0.0)))).step(0.25)
|
|
190
183
|
assert p.left.time == 0.25
|
|
191
184
|
assert p.right.map(|x| x.time) == Some(0.25)
|
|
192
185
|
assert p.right.map(|x| x.kids.map(|k| k.time)) == Some([0.25])
|
|
@@ -257,25 +250,17 @@ primitive ViewHelpers =
|
|
|
257
250
|
fun probeAt(x: Float, log: Slice<Float>) -> View = ViewProbe(x: x, log: log, fade: 1.0)
|
|
258
251
|
|
|
259
252
|
# A screen whose actors are stepped for it, of each kind a field can hold them in.
|
|
260
|
-
struct Stage[Screen](n: Int, one: Stepper, many: List<Stepper>, maybe: Option<Stepper>, nothing: Option<Stepper>, anyOne: Actor, anyMany: List<Actor>, loose: View, scene: List<View>)
|
|
253
|
+
struct Stage[Screen](n: Int, one: Stepper, many: List<Stepper>, maybe: Option<Stepper>, nothing: Option<Stepper>, anyOne: Actor, anyMany: List<Actor>, loose: View, scene: List<View>)
|
|
261
|
-
fun name(self) -> Str = "stage"
|
|
262
|
-
|
|
263
|
-
# A walker paused with the screen, clocks that run on.
|
|
264
|
-
struct Paused[Screen](paused: Bool, @stepsWhen(!self.paused) walker: Stepper, clocks: List<Stepper>) =
|
|
265
|
-
fun name(self) -> Str = "paused"
|
|
266
254
|
|
|
267
255
|
# A screen that steps its actor twice as fast itself.
|
|
268
256
|
struct Owned[Screen](one: Stepper) =
|
|
269
|
-
fun name(self) -> Str = "owned"
|
|
270
|
-
|
|
271
257
|
fun stepped(self, dt: Float) -> Owned = Owned(one: self.one.step(dt * 2.0))
|
|
272
258
|
|
|
273
|
-
# An actor that adds a kid each step, its kids
|
|
259
|
+
# An actor that adds a kid each step, its kids and its inner stepper stepped after.
|
|
274
|
-
# while not held, read after its own step (which holds it from the second step on).
|
|
275
|
-
struct Nest[Actor, Steps](time: Float, kids: List<Stepper>,
|
|
260
|
+
struct Nest[Actor, Steps](time: Float, kids: List<Stepper>, inner: Stepper) =
|
|
276
261
|
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
277
262
|
|
|
278
|
-
fun step(self, dt: Float) -> Nest = Nest(..self, time: self.time + dt, kids: self.kids.append([Stepper(time: 0.0)])
|
|
263
|
+
fun step(self, dt: Float) -> Nest = Nest(..self, time: self.time + dt, kids: self.kids.append([Stepper(time: 0.0)]))
|
|
279
264
|
|
|
280
265
|
# An actor that is only what it holds.
|
|
281
266
|
struct Pair[Actor, Steps](left: Stepper, right: Option<Nest>) =
|