plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
d6a473b
— Peter John
2026-11-13T17:55:48+05:30
std/game: no top-level funs; gfx.plum is Gfx.plum with `primitive Gfx` (`Gfx.clear(io, ..)`, the host's `extern fun`s inside it), audio.plum is Audio.plum with `primitive Audio`, input.plum is Input.plum with the input externs statics of `InputEvent` (`InputEvent.isKeyDown(io, k)`; `Input` already has methods of those names); `Texture.load`/`Texture.create` (were `loadTexture`/`createTexture`), `Sound.load` (`loadSound`), `Held.keysOnly` for `input.inHeld`, `Held.withOrWithout`; Replay's externs and codecs statics of `Replay` (`Replay.floatCode`, `Replay.sameInput`), `Scaled.halveBig` and `wholeUp`; `a.at(t)` and `a.length()` methods of `Anim` (were `Anim.at(a, t)`), its helpers statics (`Anim.endless` for `animForever`), `AnimPose.identity` and `combine`; View's list walks in `primitive Views` (`Views.paint` for `paintView`, `Views.stepAll`, `Views.all`, `Views.hitIn`, ..); `Place.at`, `Place.pivoted`, `Place.turned`; `Box.draw`, `Box.contains` (`boxHit`), `Circle.draw`, `Sprite.draw`, `Label.draw`, `Label.textWidth`, `Align.lead`; `Easing.powIn` ..; History's list helpers statics of `History`; Game's helpers and `cameraOf`/`baseCamera` (from Screen.plum) statics of `Game`; `Delivery.deliverTo` and its steps; `primitive Effects` (`Effects.queued`, `Effects.sent<A>`, the dispatch slot); `TestRig.mount`, `mountWith`, `play`, `playWith` (were `headless.mount` ..); the test-only probes (`ViewProbe`, `Viewed`, ..) and `animNear`, `histTick` moved to their tests/ mirror in `ViewHelpers`, `AnimHelpers`, `HistoryHelpers`; checker: another type's `extern fun` is a static taking `io: Io` first, and codegen passes neither its receiver slot nor its io to the host, so the wasm import names stay (`plum::clear`); every caller (std, examples, games, tests, benchmarks, compiler tests and fixtures, web-test fixtures), the LSP index test, the loader test on `Gfx.missingAsset`; docs (game.md, std.md, language.md, backlog)
- benchmarks/screen_views/orbit.plum +1 -1
- benchmarks/screen_views/scale.plum +7 -7
- benchmarks/screen_views/timing.plum +1 -1
- compiler/checker/src/fncheck.rs +4 -2
- compiler/checker/tests/checker/io_primitive_tests.rs +7 -0
- compiler/checker/tests/checker/modules_tests.rs +1 -1
- compiler/cli/tests/cli/game_entry_test.rs +3 -3
- compiler/cli/tests/cli/host_stub_test.rs +3 -3
- compiler/cli/tests/cli/old_forms_test.rs +3 -3
- compiler/core/src/game_entry.rs +1 -1
- compiler/core/src/native.rs +1 -1
- compiler/core/tests/core/loader_test.rs +21 -12
- compiler/driver/tests/driver_tests.rs +1 -1
- compiler/game/src/api.rs +1 -1
- compiler/game/tests/fixtures/assets_fixture.plum +14 -14
- compiler/game/tests/fixtures/camera_fixture.plum +1 -1
- compiler/game/tests/fixtures/host_fixture.plum +9 -9
- compiler/game/tests/fixtures/main_cli_fixture.plum +2 -2
- compiler/game/tests/fixtures/main_game_fixture.plum +6 -6
- compiler/game/tests/fixtures/main_missing_fixture.plum +6 -6
- compiler/game/tests/fixtures/state_draw_keeps.plum +5 -5
- compiler/game/tests/fixtures/state_in_place.plum +5 -5
- compiler/game/tests/fixtures/state_main_alias.plum +5 -5
- compiler/game/tests/fixtures/state_reset.plum +5 -5
- compiler/game/tests/fixtures/target_fixture.plum +11 -11
- compiler/game/tests/fixtures/turn_fixture.plum +1 -1
- compiler/game/tests/host_test.rs +4 -4
- compiler/game/web-test/fixtures/interleave.plum +6 -6
- compiler/game/web-test/fixtures/main_game.plum +7 -7
- compiler/game/web-test/fixtures/rendertarget.plum +26 -26
- compiler/game/web-test/fixtures/stress.plum +12 -12
- compiler/game/web-test/fixtures/stress_state_view.plum +13 -13
- compiler/game/web-test/fixtures/tests/stress_state_view.plum +6 -6
- compiler/game/web-test/fixtures/text_sizes.plum +16 -16
- compiler/host/src/lib.rs +2 -2
- compiler/lsp/src/completion.rs +3 -3
- compiler/lsp/src/main.rs +6 -5
- compiler/wasm-codegen/src/driver.rs +8 -1
- compiler/wasm-codegen/tests/wasm_codegen/codegen_tests.rs +1 -1
- compiler/wasm-codegen/tests/wasm_codegen/e2e_types/io_entry.rs +3 -3
- docs/backlog.md +1 -1
- docs/game.md +28 -28
- docs/language.md +1 -1
- docs/std.md +1 -1
- examples/games/balloonpop/BalloonPop.plum +2 -2
- examples/games/balloonpop/actors/Balloon.plum +3 -3
- examples/games/balloonpop/screens/PlaygroundScreen.plum +5 -5
- examples/games/clicks.plum +2 -2
- examples/games/orbitrunner/OrbitRunner.plum +4 -4
- examples/games/orbitrunner/actors/Mine.plum +1 -1
- examples/games/orbitrunner/actors/Orb.plum +6 -6
- examples/games/orbitrunner/actors/Ring.plum +1 -1
- examples/games/orbitrunner/actors/Ship.plum +1 -1
- examples/games/orbitrunner/actors/Star.plum +1 -1
- examples/games/orbitrunner/screens/PlayingScreen.plum +8 -8
- examples/games/two_screens.plum +1 -1
- std/Io.plum +2 -2
- std/game/Anim.plum +81 -87
- std/game/Audio.plum +55 -0
- std/game/Box.plum +23 -23
- std/game/Circle.plum +8 -8
- std/game/Effects.plum +27 -25
- std/game/Game.plum +76 -60
- std/game/Gfx.plum +121 -0
- std/game/Group.plum +1 -1
- std/game/History.plum +35 -38
- std/game/{input.plum → Input.plum} +22 -24
- std/game/Label.plum +29 -26
- std/game/Overlay.plum +3 -3
- std/game/Place.plum +15 -15
- std/game/Replay.plum +120 -121
- std/game/Screen.plum +3 -20
- std/game/ScreenSlice.plum +55 -55
- std/game/Sprite.plum +12 -12
- std/game/Tween.plum +19 -21
- std/game/View.plum +121 -167
- std/game/Viewport.plum +1 -1
- std/game/audio.plum +0 -54
- std/game/gfx.plum +0 -120
- std/game/headless.plum +44 -43
- tests/benchmarks/screen_views/orbit.plum +1 -1
- tests/benchmarks/screen_views/scale.plum +3 -3
- tests/benchmarks/screen_views/timing.plum +1 -1
- tests/examples/games/balloonpop/actors/Balloon.plum +1 -1
- tests/examples/games/balloonpop/screens/PlaygroundScreen.plum +1 -1
- tests/examples/games/clicks.plum +2 -2
- tests/examples/games/orbitrunner/actors/Orb.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 +4 -4
- tests/port/game/replay.plum +21 -21
- tests/port/game/routing.plum +9 -9
- tests/port/game/screens.plum +15 -15
- tests/std/game/Anim.plum +36 -32
- tests/std/game/Box.plum +2 -2
- tests/std/game/Circle.plum +1 -1
- tests/std/game/{gfx.plum → Gfx.plum} +2 -2
- tests/std/game/History.plum +9 -5
- tests/std/game/{input.plum → Input.plum} +11 -11
- tests/std/game/Label.plum +1 -1
- tests/std/game/Overlay.plum +6 -6
- tests/std/game/Place.plum +4 -4
- tests/std/game/Screen.plum +1 -1
- tests/std/game/Sprite.plum +1 -1
- tests/std/game/View.plum +64 -17
benchmarks/screen_views/orbit.plum
CHANGED
|
@@ -25,7 +25,7 @@ import std/Io
|
|
|
25
25
|
import std/Slice
|
|
26
26
|
import std/collections/List
|
|
27
27
|
import std/math/Random
|
|
28
|
-
import std/game/
|
|
28
|
+
import std/game/Input
|
|
29
29
|
import std/math/Vec2
|
|
30
30
|
import std/game/Screen
|
|
31
31
|
import std/game/Game
|
benchmarks/screen_views/scale.plum
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# 10,000 actors on one screen, each moving its clock each tick, drawn by the screen's
|
|
2
2
|
# view (as master's benchmarks/screen_views/scale.plum): with `view`, actors whose view is
|
|
3
3
|
# one `Sprite`, made again at each draw; with `hand`, the same actors drawing the same
|
|
4
|
-
# sprite with a hand-written `paintSelf` (`
|
|
4
|
+
# sprite with a hand-written `paintSelf` (`Sprite.draw`). Each is an `Actor`,
|
|
5
5
|
# and the game steps every one each tick, before the tick action, through the screen's
|
|
6
|
-
# `stepped` (`
|
|
6
|
+
# `stepped` (`Views.stepIn` over its list); the tick action does nothing. One difference
|
|
7
7
|
# from master: master's stepping walk makes each actor's view to look under it, and this
|
|
8
8
|
# branch's does not (an actor found under another could not be put back into a value), so
|
|
9
9
|
# `update view` makes 10,000 fewer one-`Sprite` lists a tick here. 20 ticks to warm up,
|
|
@@ -34,7 +34,7 @@ import std/math/Affine
|
|
|
34
34
|
import std/math/Vec2
|
|
35
35
|
import std/math/Aabb
|
|
36
36
|
import std/game/Color
|
|
37
|
-
import std/game/
|
|
37
|
+
import std/game/Input
|
|
38
38
|
import std/game/Place
|
|
39
39
|
import std/game/View
|
|
40
40
|
import std/game/Sprite
|
|
@@ -47,7 +47,7 @@ import benchmarks/screen_views/timing
|
|
|
47
47
|
Count = 10000
|
|
48
48
|
|
|
49
49
|
struct ViewDot[Actor](i: Int, at: Place, clock: Float) =
|
|
50
|
-
fun of(i: Int) -> ViewDot = ViewDot(i: i, at:
|
|
50
|
+
fun of(i: Int) -> ViewDot = ViewDot(i: i, at: Place.at(Float(i % 100) * 8.0, Float(i / 100) * 6.0), clock: 0.0)
|
|
51
51
|
|
|
52
52
|
fun place(self) -> Place = self.at
|
|
53
53
|
|
|
@@ -57,7 +57,7 @@ struct ViewDot[Actor](i: Int, at: Place, clock: Float) =
|
|
|
57
57
|
[Sprite(x: 0.0, y: 0.0, tex: Texture(id: 1), w: 16.0, h: 16.0, srcX: 0.0, srcY: 0.0, srcW: 16.0, srcH: 16.0, frame: Int(self.clock * 8.0) % 4, tint: Color.White, touch: Disabled)]
|
|
58
58
|
|
|
59
59
|
struct HandDot[Actor](i: Int, at: Place, clock: Float) =
|
|
60
|
-
fun of(i: Int) -> HandDot = HandDot(i: i, at:
|
|
60
|
+
fun of(i: Int) -> HandDot = HandDot(i: i, at: Place.at(Float(i % 100) * 8.0, Float(i / 100) * 6.0), clock: 0.0)
|
|
61
61
|
|
|
62
62
|
fun place(self) -> Place = self.at
|
|
63
63
|
|
|
@@ -65,7 +65,7 @@ struct HandDot[Actor](i: Int, at: Place, clock: Float) =
|
|
|
65
65
|
|
|
66
66
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) =
|
|
67
67
|
frame := Int(self.clock * 8.0) % 4
|
|
68
|
-
|
|
68
|
+
Sprite.draw(io, m, alpha, 16.0, 16.0, Color.White, Texture(id: 1), Float(frame) * 16.0, 0.0, 16.0, 16.0)
|
|
69
69
|
|
|
70
70
|
enum CrowdAct = CrowdTick(dt: Float)
|
|
71
71
|
|
|
@@ -76,7 +76,7 @@ struct CrowdScreen[ScreenSlice<CrowdAct>](hand: Bool, dots: List<View>) =
|
|
|
76
76
|
|
|
77
77
|
fun view(self) -> List<View> = self.dots
|
|
78
78
|
|
|
79
|
-
fun stepped(self, dt: Float) -> CrowdScreen = CrowdScreen(..self, dots:
|
|
79
|
+
fun stepped(self, dt: Float) -> CrowdScreen = CrowdScreen(..self, dots: Views.stepIn(self.dots, dt))
|
|
80
80
|
|
|
81
81
|
fun tick(self, dt: Float) -> Option<CrowdAct> = Some(CrowdTick(dt: dt))
|
|
82
82
|
|
benchmarks/screen_views/timing.plum
CHANGED
|
@@ -9,7 +9,7 @@ import std/Bool
|
|
|
9
9
|
import std/Slice
|
|
10
10
|
import std/collections/List
|
|
11
11
|
import std/math/Vec2
|
|
12
|
-
import std/game/
|
|
12
|
+
import std/game/Input
|
|
13
13
|
|
|
14
14
|
# One of nine ways (or none) held for 20 ticks at a time, picked by the seed and the tick
|
|
15
15
|
# alone, and Enter going down every 150 ticks.
|
compiler/checker/src/fncheck.rs
CHANGED
|
@@ -130,11 +130,13 @@ pub(crate) fn checkFnBody(f: &ast::Fn, global_env: &TypeEnv, ctx: &CheckCtx) ->
|
|
|
130
130
|
// report it plainly rather than trying to guess which side was "right".
|
|
131
131
|
match (f.is_extern, &f.body) {
|
|
132
132
|
(true, ast::FnBody::Extern) => {
|
|
133
|
+
// On a type other than `Io`, a host call is a static that takes the
|
|
134
|
+
// capability first (`Gfx.drawText(io, ..)`).
|
|
133
|
-
if f.type_param.as_deref().is_some_and(|t| t != ast::IO_TYPE) {
|
|
135
|
+
if f.type_param.as_deref().is_some_and(|t| t != ast::IO_TYPE) && (f.has_self || !ast::takesIoFirst(f)) {
|
|
134
136
|
errors.push(CheckError::resolution(
|
|
135
137
|
f.line,
|
|
136
138
|
f.column,
|
|
137
|
-
format!("fn '{}': extern methods are only allowed on Io", f.name),
|
|
139
|
+
format!("fn '{}': extern methods are only allowed on Io; another type's extern fun is a static with `io: Io` first", f.name),
|
|
138
140
|
));
|
|
139
141
|
}
|
|
140
142
|
return errors;
|
compiler/checker/tests/checker/io_primitive_tests.rs
CHANGED
|
@@ -30,6 +30,13 @@ fn externMethodsAreOnlyAllowedOnIo() {
|
|
|
30
30
|
assert!(e.contains("extern methods are only allowed on Io"), "{e}");
|
|
31
31
|
let e = run("primitive Int =\n extern fun g(self)\n").unwrap_err();
|
|
32
32
|
assert!(e.contains("extern methods are only allowed on Io"), "{e}");
|
|
33
|
+
let e = run("primitive Gfx =\n extern fun h(x: Int)\n").unwrap_err();
|
|
34
|
+
assert!(e.contains("a static with `io: Io` first"), "{e}");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
#[test]
|
|
38
|
+
fn anotherTypesStaticExternTakesIoFirst() {
|
|
39
|
+
run(&format!("{IO}primitive Gfx =\n extern fun clear(io: Io, c: Int)\n\nfun f(io: Io) =\n Gfx.clear(io, 1)\n")).unwrap();
|
|
33
40
|
}
|
|
34
41
|
|
|
35
42
|
#[test]
|
compiler/checker/tests/checker/modules_tests.rs
CHANGED
|
@@ -170,7 +170,7 @@ fn the_old_namespace_form_is_an_error() {
|
|
|
170
170
|
fn a_game_calls_the_host_through_module_functions() {
|
|
171
171
|
run(
|
|
172
172
|
"gamemods",
|
|
173
|
-
"import std/Float\nimport std/Io\nimport std/game/Color\nimport std/game/
|
|
173
|
+
"import std/Float\nimport std/Io\nimport std/game/Color\nimport std/game/Gfx\nimport std/game/Input\n\nfun f(io: Io) -> Float =\n Gfx.clear(io, Color.Black)\n _ := Held.keysOnly(InputEvent.keyCodes()).isDown(Key.Left)\n Input.create().poll(io).axis(Key.Left, Key.Right)\n",
|
|
174
174
|
)
|
|
175
175
|
.unwrap();
|
|
176
176
|
}
|
compiler/cli/tests/cli/game_entry_test.rs
CHANGED
|
@@ -59,10 +59,10 @@ fn compileSource(name: &str, text: &str) -> (bool, String) {
|
|
|
59
59
|
(out.status.success() && exports, err)
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
const HEAD: &str = "module app\nimport std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/game/
|
|
62
|
+
const HEAD: &str = "module app\nimport std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/game/Input\n\nstruct Counter(n: Int) =\n";
|
|
63
63
|
|
|
64
64
|
/// A whole game: one screen that counts its ticks.
|
|
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/
|
|
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\n\
|
|
66
66
|
import std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\n\n\
|
|
67
67
|
struct CountScreen[ScreenSlice<Int>](n: Int) =\n fun name(self) -> Str = \"count\"\n fun tick(self, dt: Float) -> Option<Int> = Some(1)\n \
|
|
68
68
|
fun reduce(self, a: Int) -> CountScreen = CountScreen(n: self.n + a)\n\n\
|
|
@@ -128,7 +128,7 @@ fn screensGame(names: &[&str], to: &str) -> String {
|
|
|
128
128
|
let screens: Vec<String> = (0..names.len()).map(|i| format!("s{i}")).collect();
|
|
129
129
|
let made: Vec<String> = names.iter().enumerate().map(|(i, n)| format!(" s{i}: Screen := Named(n: \"{n}\")\n")).collect();
|
|
130
130
|
format!(
|
|
131
|
-
"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/
|
|
131
|
+
"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\n\
|
|
132
132
|
import std/game/Control\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\n\n\
|
|
133
133
|
struct Named[ScreenSlice<Int>](n: Str) =\n fun name(self) -> Str = self.n\n fun tick(self, dt: Float) -> Option<Int> = Some(1)\n \
|
|
134
134
|
fun reduce(self, a: Int) -> Named = self\n fun effect(self, io: Io, a: Int, game: Control) = game.goTo(\"{to}\")\n\n\
|
compiler/cli/tests/cli/host_stub_test.rs
CHANGED
|
@@ -35,7 +35,7 @@ fn runPlumTest(name: &str, source: &str) -> (bool, String) {
|
|
|
35
35
|
fn stubbed_host_import_unused_test_passes() {
|
|
36
36
|
let (ok, all) = runPlumTest(
|
|
37
37
|
"pass",
|
|
38
|
-
"import std/Test\nimport std/Int\nimport std/game/
|
|
38
|
+
"import std/Test\nimport std/Int\nimport std/game/Gfx\n\nprimitive SampleTest[UnitTest] =\n fun pure() =\n assert 1 + 1 == 2\n",
|
|
39
39
|
);
|
|
40
40
|
assert!(ok, "output: {all}");
|
|
41
41
|
assert!(all.contains("1 passed, 0 failed"), "output: {all}");
|
|
@@ -45,7 +45,7 @@ fn stubbed_host_import_unused_test_passes() {
|
|
|
45
45
|
fn stubbed_host_import_traps_when_called() {
|
|
46
46
|
let (ok, all) = runPlumTest(
|
|
47
47
|
"trap",
|
|
48
|
-
"import std/Test\nimport std/Int\nimport std/Float\nimport std/Io\nimport std/game/
|
|
48
|
+
"import std/Test\nimport std/Int\nimport std/Float\nimport std/Io\nimport std/game/Gfx\n\nprimitive SampleTest[UnitTest] =\n fun callsHost() =\n assert Gfx.screenWidth(io) > 0.0\n",
|
|
49
49
|
);
|
|
50
50
|
assert!(!ok, "output: {all}");
|
|
51
51
|
assert!(
|
|
@@ -58,7 +58,7 @@ fn stubbed_host_import_traps_when_called() {
|
|
|
58
58
|
fn real_host_functions_still_work_next_to_stubs() {
|
|
59
59
|
let (ok, all) = runPlumTest(
|
|
60
60
|
"real",
|
|
61
|
-
"import std/Test\nimport std/Int\nimport std/Str\nimport std/Io\nimport std/game/
|
|
61
|
+
"import std/Test\nimport std/Int\nimport std/Str\nimport std/Io\nimport std/game/Gfx\n\nprimitive SampleTest[UnitTest] =\n fun real() =\n io.printLn(\"hello\")\n assert 1 == 1\n",
|
|
62
62
|
);
|
|
63
63
|
assert!(ok, "output: {all}");
|
|
64
64
|
assert!(all.contains("hello"), "output: {all}");
|
compiler/cli/tests/cli/old_forms_test.rs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#![allow(non_snake_case)]
|
|
2
2
|
//! The forms std and the language no longer have are errors: `Palette.shade` (a module is lowercase),
|
|
3
|
-
//! `io.clear(..)` (the game host calls are `
|
|
3
|
+
//! `io.clear(..)` (the game host calls are `Gfx` statics), type parameters and arguments in
|
|
4
4
|
//! `[ ]` (now `< >`) and the `is` clause (now `[ ]` after the name).
|
|
5
5
|
use std::process::Command;
|
|
6
6
|
|
|
@@ -45,13 +45,13 @@ fn aBareModuleFunctionIsAnError() {
|
|
|
45
45
|
fn hostCallsAreNoLongerMethodsOfIo() {
|
|
46
46
|
let (ok, text) = compileOutcome(
|
|
47
47
|
"ioclear",
|
|
48
|
-
"import std/Io\nimport std/game/Color\nimport std/game/
|
|
48
|
+
"import std/Io\nimport std/game/Color\nimport std/game/Gfx\n\nfun main(io: Io) =\n io.clear(Color.Black)\n",
|
|
49
49
|
);
|
|
50
50
|
assert!(!ok, "{text}");
|
|
51
51
|
assert!(text.contains("Io has no method 'clear'"), "{text}");
|
|
52
52
|
let (ok, text) = compileOutcome(
|
|
53
53
|
"gfxclear",
|
|
54
|
-
"import std/Io\nimport std/game/Color\nimport std/game/
|
|
54
|
+
"import std/Io\nimport std/game/Color\nimport std/game/Gfx\n\nfun main(io: Io) =\n Gfx.clear(io, Color.Black)\n",
|
|
55
55
|
);
|
|
56
56
|
assert!(ok, "{text}");
|
|
57
57
|
}
|
compiler/core/src/game_entry.rs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
//! A game is a `main` returning `Game` (std/game/Game), made by `Game(screens: [..])`: there is
|
|
2
2
|
//! no other kind of game. This lowering (run before checking) renames the game's `main`
|
|
3
3
|
//! to `gameUserMain` and appends the state the host holds, `GameRun(game, clock)` with the
|
|
4
|
-
//! `Clock` of std/game/
|
|
4
|
+
//! `Clock` of std/game/Input, a `main` that builds it, and the two frame entry points the host
|
|
5
5
|
//! calls: `gameUpdate(io, r, dt)` polls the input once and runs the fixed 1/60 s ticks the frame
|
|
6
6
|
//! covers (at most 8, in `gameTicks`), each one `Game.update`, and `gameDraw(io, r)` runs
|
|
7
7
|
//! `Game.draw`. The wasm module exports them as `update(state, dt) -> state` and `draw(state)`.
|
compiler/core/src/native.rs
CHANGED
|
@@ -123,7 +123,7 @@ pub fn isNative(f: &Fn) -> bool {
|
|
|
123
123
|
/// The name the compiler knows `f` by: `Type.name` for a method or a static, `@name` for a
|
|
124
124
|
/// built-in's declaration, and `module.name` for a module-level function. The module is the
|
|
125
125
|
/// one an import of the file names (`modules::moduleAlias`): the file's own name without
|
|
126
|
-
/// its folders, lower-cased, so `std/game/
|
|
126
|
+
/// its folders, lower-cased, so `std/game/Gfx.plum`'s `clear` is `Gfx.clear` wherever the
|
|
127
127
|
/// file is. A function of no file (an in-memory source) is just `name`.
|
|
128
128
|
pub fn qualifiedName(f: &Fn) -> String {
|
|
129
129
|
// A built-in's leading `@` is not the module mark of a renamed function (`clear@gfx`).
|
compiler/core/tests/core/loader_test.rs
CHANGED
|
@@ -637,20 +637,29 @@ fn fnNames(merged: &plum_core::ast::Source) -> Vec<String> {
|
|
|
637
637
|
.collect()
|
|
638
638
|
}
|
|
639
639
|
|
|
640
|
-
/// F-T8-2: a user function may share a name with a std function (`missingAsset`
|
|
640
|
+
/// F-T8-2: a user function may share a name with a std function (`missingAsset`, a static
|
|
641
|
-
/// std/game/
|
|
641
|
+
/// of std/game/Gfx's `Gfx`). Both keep their plain names, std's under its type; an export
|
|
642
|
+
/// keeps it too.
|
|
642
643
|
#[test]
|
|
643
644
|
fn userFunctionMaySharePlainNameWithStdFunction() {
|
|
645
|
+
let owners = |merged: &plum_core::ast::Source| -> Vec<(Option<String>, String)> {
|
|
646
|
+
merged
|
|
647
|
+
.items
|
|
648
|
+
.iter()
|
|
649
|
+
.filter_map(|i| match i {
|
|
650
|
+
plum_core::ast::Item::Fn(f) if f.name.starts_with("missingAsset") => Some((f.type_param.clone(), f.name.clone())),
|
|
651
|
+
_ => None,
|
|
652
|
+
})
|
|
653
|
+
.collect()
|
|
654
|
+
};
|
|
655
|
+
for (label, decl) in [("user_shares_std_name", "fun"), ("export_shares_std_name", "export fun")] {
|
|
644
|
-
|
|
656
|
+
let src = format!("import std/Int\nimport std/game/Gfx\n\n{decl} missingAsset(n: Int) -> Int =\n n\n\nfun main() -> Int =\n missingAsset(1)\n");
|
|
645
|
-
|
|
657
|
+
let merged = plum_test_support::load(label, &src).expect("user fn shares a std name");
|
|
646
|
-
|
|
658
|
+
let names = owners(&merged);
|
|
647
|
-
|
|
659
|
+
assert!(names.contains(&(None, "missingAsset".to_string())), "{names:?}");
|
|
660
|
+
assert!(names.contains(&(Some("Gfx".to_string()), "missingAsset".to_string())), "{names:?}");
|
|
648
|
-
|
|
661
|
+
assert!(names.iter().all(|(_, n)| n == "missingAsset"), "{names:?}");
|
|
649
|
-
let exported = "import std/Int\nimport std/game/gfx\n\nexport fun missingAsset(n: Int) -> Int =\n n\n\nfun main() -> Int =\n missingAsset(1)\n";
|
|
650
|
-
let merged = plum_test_support::load("export_shares_std_name", exported).expect("export shares a std name");
|
|
651
|
-
|
|
662
|
+
}
|
|
652
|
-
assert!(names.contains(&"missingAsset".to_string()), "{names:?}");
|
|
653
|
-
assert!(names.iter().any(|n| n.starts_with("missingAsset@")), "{names:?}");
|
|
654
663
|
}
|
|
655
664
|
|
|
656
665
|
#[test]
|
compiler/driver/tests/driver_tests.rs
CHANGED
|
@@ -252,7 +252,7 @@ fn aSharedReceiverOfAnotherTypeDoesNotProveItsOwnMethod() {
|
|
|
252
252
|
/// `draw` and `main` are the bodies of its reducer, its draw and the screen the game starts with.
|
|
253
253
|
fn gameSource(name: &str, main: &str, step: &str, draw: &str) -> Source {
|
|
254
254
|
let text = format!(
|
|
255
|
-
"module {name}\nimport std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Option\nimport std/collections/List\nimport std/math/Affine\nimport std/math/Aabb\nimport std/game/
|
|
255
|
+
"module {name}\nimport std/Int\nimport std/Float\nimport std/Str\nimport std/Io\nimport std/Option\nimport std/collections/List\nimport std/math/Affine\nimport std/math/Aabb\nimport std/game/Input\n\
|
|
256
256
|
import std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\n\
|
|
257
257
|
Start = Stress(label: \"s\", t: 0, n: 0)\n\
|
|
258
258
|
struct Stress[ScreenSlice<Float>](label: Str, t: Int, n: Int) =\n \
|
compiler/game/src/api.rs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
//! The host API a Plum game programs against: one `Backend` method per
|
|
2
|
-
//! extern in `std/game/
|
|
2
|
+
//! extern in `std/game/Gfx.plum`, in Rust types (`Float` is `f32`, `Int` is
|
|
3
3
|
//! `i32`, `Str` is `&str`, a `Color` is `[f32; 4]`, a `Vec2` is `(f32, f32)`).
|
|
4
4
|
//! `Int` handles are indexes into host vectors, `-1` on failure. The browser
|
|
5
5
|
//! bridge (`web/plum_bridge.js`) implements the same API; this crate's
|
compiler/game/tests/fixtures/assets_fixture.plum
CHANGED
|
@@ -6,9 +6,9 @@ import std/Str
|
|
|
6
6
|
import std/Os
|
|
7
7
|
import std/math/Vec2
|
|
8
8
|
import std/game/Color
|
|
9
|
-
import std/game/
|
|
9
|
+
import std/game/Gfx
|
|
10
10
|
import std/Io
|
|
11
|
-
import std/game/
|
|
11
|
+
import std/game/Input
|
|
12
12
|
import std/math/Affine
|
|
13
13
|
import std/Option
|
|
14
14
|
import std/collections/List
|
|
@@ -32,21 +32,21 @@ struct State[ScreenSlice<Float>](frames: Int) =
|
|
|
32
32
|
fun view(self) -> List<View> = [StateDrawing(s: self)]
|
|
33
33
|
|
|
34
34
|
fun draw(self, io: Io, m: Affine, alpha: Float, area: Aabb) =
|
|
35
|
-
t := Texture(id:
|
|
35
|
+
t := Texture(id: Gfx.textureLoad(io, "sprites.png"))
|
|
36
|
-
|
|
36
|
+
Gfx.drawText(io, "size {Gfx.textureWidth(io, t.id)}x{Gfx.textureHeight(io, t.id)}", 0.0, 0.0, 8.0, Color.White)
|
|
37
37
|
t.setSmooth(io, true)
|
|
38
38
|
t.draw(io, 1.0, 2.0, Color.White)
|
|
39
|
-
|
|
39
|
+
Gfx.drawTextureEx(io, t.id, 16.0, 0.0, 16.0, 16.0, 10.0, 20.0, 32.0, 48.0, 0.5, true, Color.Blue)
|
|
40
|
-
|
|
40
|
+
Gfx.drawText(io, "bad {Gfx.textureWidth(io, 99)} {Gfx.textureLoad(io, "missing.png")}", 0.0, 0.0, 8.0, Color.White)
|
|
41
|
-
if
|
|
41
|
+
if Gfx.fileExists(io, "hello.txt")
|
|
42
|
-
|
|
42
|
+
Gfx.drawText(io, "file [{Gfx.fileLoad(io, "hello.txt")}]", 0.0, 0.0, 8.0, Color.White)
|
|
43
|
-
|
|
43
|
+
Gfx.drawText(io, "missing [{Gfx.fileLoad(io, "nope.txt")}] {Gfx.fileExists(io, "nope.txt")}", 0.0, 0.0, 8.0, Color.White)
|
|
44
|
-
|
|
44
|
+
Gfx.drawText(io, "escape {Gfx.textureLoad(io, "../sprites.png")} {Gfx.fileExists(io, "../hello.txt")} [{Gfx.fileLoad(io, "../hello.txt")}]", 0.0, 0.0, 8.0, Color.White)
|
|
45
|
-
|
|
45
|
+
Gfx.drawText(io, "abs {Gfx.textureLoad(io, "/etc/hosts")} {Gfx.fileExists(io, "/etc/hosts")}", 0.0, 0.0, 8.0, Color.White)
|
|
46
|
-
|
|
46
|
+
|
|
47
|
-
# What the screen draws each frame, with `
|
|
47
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
48
48
|
struct StateDrawing[View](s: State) =
|
|
49
|
-
fun place(self) -> Place =
|
|
49
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
50
50
|
|
|
51
51
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
52
52
|
|
compiler/game/tests/fixtures/camera_fixture.plum
CHANGED
|
@@ -8,7 +8,7 @@ import std/Option
|
|
|
8
8
|
import std/collections/List
|
|
9
9
|
import std/math/Vec2
|
|
10
10
|
import std/game/Color
|
|
11
|
-
import std/game/
|
|
11
|
+
import std/game/Input
|
|
12
12
|
import std/game/View
|
|
13
13
|
import std/game/Box
|
|
14
14
|
import std/game/Viewport
|
compiler/game/tests/fixtures/host_fixture.plum
CHANGED
|
@@ -6,9 +6,9 @@ import std/Str
|
|
|
6
6
|
import std/Os
|
|
7
7
|
import std/math/Vec2
|
|
8
8
|
import std/game/Color
|
|
9
|
-
import std/game/
|
|
9
|
+
import std/game/Gfx
|
|
10
10
|
import std/Io
|
|
11
|
-
import std/game/
|
|
11
|
+
import std/game/Input
|
|
12
12
|
import std/math/Affine
|
|
13
13
|
import std/Option
|
|
14
14
|
import std/collections/List
|
|
@@ -32,16 +32,16 @@ struct State[ScreenSlice<Float>](frames: Int) =
|
|
|
32
32
|
fun view(self) -> List<View> = [StateDrawing(s: self)]
|
|
33
33
|
|
|
34
34
|
fun draw(self, io: Io, m: Affine, alpha: Float, area: Aabb) =
|
|
35
|
-
|
|
35
|
+
Gfx.drawRect(io, 1.0, 2.0, 3.0, 4.0, Color.Red)
|
|
36
|
-
|
|
36
|
+
Gfx.drawCircle(io, 5.0, 6.0, 7.0, Color.Green)
|
|
37
|
-
p :=
|
|
37
|
+
p := InputEvent.mousePos(io)
|
|
38
|
-
|
|
38
|
+
Gfx.drawText(io, "hi", p.x, p.y, 16.0, Color.White)
|
|
39
39
|
if self.frames > 0
|
|
40
|
-
|
|
40
|
+
Gfx.drawText(io, "frames {self.frames}", 0.0, 0.0, 8.0, Color.White)
|
|
41
41
|
|
|
42
|
-
# What the screen draws each frame, with `
|
|
42
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
43
43
|
struct StateDrawing[View](s: State) =
|
|
44
|
-
fun place(self) -> Place =
|
|
44
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
45
45
|
|
|
46
46
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
47
47
|
|
compiler/game/tests/fixtures/main_cli_fixture.plum
CHANGED
|
@@ -2,9 +2,9 @@ module main_cli_fixture
|
|
|
2
2
|
|
|
3
3
|
import std/Int
|
|
4
4
|
import std/Str
|
|
5
|
-
import std/game/
|
|
5
|
+
import std/game/Input
|
|
6
6
|
import std/Io
|
|
7
7
|
|
|
8
|
-
# Imports std/game/
|
|
8
|
+
# Imports std/game/Gfx but `main` does not return a Game: a CLI program, no update/draw.
|
|
9
9
|
fun main(io: Io) =
|
|
10
10
|
io.printLn("{Key.Space}")
|
compiler/game/tests/fixtures/main_game_fixture.plum
CHANGED
|
@@ -7,8 +7,8 @@ import std/Bool
|
|
|
7
7
|
import std/Slice
|
|
8
8
|
import std/math/Vec2
|
|
9
9
|
import std/game/Color
|
|
10
|
-
import std/game/
|
|
10
|
+
import std/game/Gfx
|
|
11
|
-
import std/game/
|
|
11
|
+
import std/game/Input
|
|
12
12
|
import std/math/Affine
|
|
13
13
|
import std/Option
|
|
14
14
|
import std/collections/List
|
|
@@ -42,15 +42,15 @@ struct Orbs[ScreenSlice<Step>](x: Float, ship: Texture, input: Input, frame: Int
|
|
|
42
42
|
fun draw(self, io: Io, m: Affine, alpha: Float, area: Aabb) =
|
|
43
43
|
first := self.input.events.length()
|
|
44
44
|
second := self.input.events.length()
|
|
45
|
-
|
|
45
|
+
Gfx.drawText(io, "frame {self.frame} ev {first} {second} x {self.x} tex {self.ship.id} w {Gfx.screenWidth(io)}", 0.0, 0.0, 8.0, Color.White)
|
|
46
46
|
|
|
47
|
-
# What the screen draws each frame, with `
|
|
47
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
48
48
|
struct OrbsDrawing[View](s: Orbs) =
|
|
49
|
-
fun place(self) -> Place =
|
|
49
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
50
50
|
|
|
51
51
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
52
52
|
|
|
53
53
|
fun main(io: Io) -> Game =
|
|
54
|
-
Game(screens: [page(Orbs(x: 0.0, ship:
|
|
54
|
+
Game(screens: [page(Orbs(x: 0.0, ship: Texture.load(io, "sprites.png"), input: Input.create(), frame: 0))])
|
|
55
55
|
|
|
56
56
|
fun page(s: Screen) -> Screen = s
|
compiler/game/tests/fixtures/main_missing_fixture.plum
CHANGED
|
@@ -4,9 +4,9 @@ import std/Int
|
|
|
4
4
|
import std/Float
|
|
5
5
|
import std/Str
|
|
6
6
|
import std/game/Color
|
|
7
|
-
import std/game/
|
|
7
|
+
import std/game/Gfx
|
|
8
8
|
import std/Io
|
|
9
|
-
import std/game/
|
|
9
|
+
import std/game/Input
|
|
10
10
|
import std/math/Affine
|
|
11
11
|
import std/Option
|
|
12
12
|
import std/collections/List
|
|
@@ -28,15 +28,15 @@ struct Broken[ScreenSlice<Float>](tex: Texture) =
|
|
|
28
28
|
fun view(self) -> List<View> = [BrokenDrawing(s: self)]
|
|
29
29
|
|
|
30
30
|
fun draw(self, io: Io, m: Affine, alpha: Float, area: Aabb) =
|
|
31
|
-
|
|
31
|
+
Gfx.clear(io, Color.Black)
|
|
32
32
|
|
|
33
|
-
# What the screen draws each frame, with `
|
|
33
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
34
34
|
struct BrokenDrawing[View](s: Broken) =
|
|
35
|
-
fun place(self) -> Place =
|
|
35
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
36
36
|
|
|
37
37
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
38
38
|
|
|
39
39
|
fun main(io: Io) -> Game =
|
|
40
|
-
Game(screens: [page(Broken(tex:
|
|
40
|
+
Game(screens: [page(Broken(tex: Texture.load(io, "nope.png")))])
|
|
41
41
|
|
|
42
42
|
fun page(s: Screen) -> Screen = s
|
compiler/game/tests/fixtures/state_draw_keeps.plum
CHANGED
|
@@ -4,9 +4,9 @@ import std/Int
|
|
|
4
4
|
import std/Float
|
|
5
5
|
import std/Str
|
|
6
6
|
import std/game/Color
|
|
7
|
-
import std/game/
|
|
7
|
+
import std/game/Gfx
|
|
8
8
|
import std/Io
|
|
9
|
-
import std/game/
|
|
9
|
+
import std/game/Input
|
|
10
10
|
import std/math/Affine
|
|
11
11
|
import std/Option
|
|
12
12
|
import std/collections/List
|
|
@@ -34,11 +34,11 @@ struct Run[ScreenSlice<Float>](label: Str, log: Log, t: Int) =
|
|
|
34
34
|
Some(r) => r.t
|
|
35
35
|
None => -1
|
|
36
36
|
self.log.last = Some(self)
|
|
37
|
-
|
|
37
|
+
Gfx.drawText(io, "{before} {self.t}", 0.0, 0.0, 8.0, Color.White)
|
|
38
38
|
|
|
39
|
-
# What the screen draws each frame, with `
|
|
39
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
40
40
|
struct RunDrawing[View](s: Run) =
|
|
41
|
-
fun place(self) -> Place =
|
|
41
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
42
42
|
|
|
43
43
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
44
44
|
|
compiler/game/tests/fixtures/state_in_place.plum
CHANGED
|
@@ -4,9 +4,9 @@ import std/Int
|
|
|
4
4
|
import std/Float
|
|
5
5
|
import std/Str
|
|
6
6
|
import std/game/Color
|
|
7
|
-
import std/game/
|
|
7
|
+
import std/game/Gfx
|
|
8
8
|
import std/Io
|
|
9
|
-
import std/game/
|
|
9
|
+
import std/game/Input
|
|
10
10
|
import std/math/Affine
|
|
11
11
|
import std/Option
|
|
12
12
|
import std/collections/List
|
|
@@ -36,11 +36,11 @@ struct Run[ScreenSlice<Float>](part: Part, t: Int) =
|
|
|
36
36
|
|
|
37
37
|
fun draw(self, io: Io, m: Affine, alpha: Float, area: Aabb) =
|
|
38
38
|
d := self.drawn()
|
|
39
|
-
|
|
39
|
+
Gfx.drawText(io, "{d.t} {self.t} {self.part.x}", 0.0, 0.0, 8.0, Color.White)
|
|
40
40
|
|
|
41
|
-
# What the screen draws each frame, with `
|
|
41
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
42
42
|
struct RunDrawing[View](s: Run) =
|
|
43
|
-
fun place(self) -> Place =
|
|
43
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
44
44
|
|
|
45
45
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
46
46
|
|
compiler/game/tests/fixtures/state_main_alias.plum
CHANGED
|
@@ -4,9 +4,9 @@ import std/Int
|
|
|
4
4
|
import std/Float
|
|
5
5
|
import std/Str
|
|
6
6
|
import std/game/Color
|
|
7
|
-
import std/game/
|
|
7
|
+
import std/game/Gfx
|
|
8
8
|
import std/Io
|
|
9
|
-
import std/game/
|
|
9
|
+
import std/game/Input
|
|
10
10
|
import std/math/Affine
|
|
11
11
|
import std/Option
|
|
12
12
|
import std/collections/List
|
|
@@ -31,11 +31,11 @@ struct Run[ScreenSlice<Float>](label: Str, t: Int) =
|
|
|
31
31
|
fun view(self) -> List<View> = [RunDrawing(s: self)]
|
|
32
32
|
|
|
33
33
|
fun draw(self, io: Io, m: Affine, alpha: Float, area: Aabb) =
|
|
34
|
-
|
|
34
|
+
Gfx.drawText(io, "{self.t} {Start.t}", 0.0, 0.0, 8.0, Color.White)
|
|
35
35
|
|
|
36
|
-
# What the screen draws each frame, with `
|
|
36
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
37
37
|
struct RunDrawing[View](s: Run) =
|
|
38
|
-
fun place(self) -> Place =
|
|
38
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
39
39
|
|
|
40
40
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
41
41
|
|
compiler/game/tests/fixtures/state_reset.plum
CHANGED
|
@@ -4,9 +4,9 @@ import std/Int
|
|
|
4
4
|
import std/Float
|
|
5
5
|
import std/Str
|
|
6
6
|
import std/game/Color
|
|
7
|
-
import std/game/
|
|
7
|
+
import std/game/Gfx
|
|
8
8
|
import std/Io
|
|
9
|
-
import std/game/
|
|
9
|
+
import std/game/Input
|
|
10
10
|
import std/math/Affine
|
|
11
11
|
import std/Option
|
|
12
12
|
import std/collections/List
|
|
@@ -31,11 +31,11 @@ struct Run[ScreenSlice<Float>](label: Str, t: Int) =
|
|
|
31
31
|
fun view(self) -> List<View> = [RunDrawing(s: self)]
|
|
32
32
|
|
|
33
33
|
fun draw(self, io: Io, m: Affine, alpha: Float, area: Aabb) =
|
|
34
|
-
|
|
34
|
+
Gfx.drawText(io, "{self.t} {Start.t}", 0.0, 0.0, 8.0, Color.White)
|
|
35
35
|
|
|
36
|
-
# What the screen draws each frame, with `
|
|
36
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
37
37
|
struct RunDrawing[View](s: Run) =
|
|
38
|
-
fun place(self) -> Place =
|
|
38
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
39
39
|
|
|
40
40
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
41
41
|
|
compiler/game/tests/fixtures/target_fixture.plum
CHANGED
|
@@ -5,9 +5,9 @@ import std/Float
|
|
|
5
5
|
import std/Str
|
|
6
6
|
import std/math/Vec2
|
|
7
7
|
import std/game/Color
|
|
8
|
-
import std/game/
|
|
8
|
+
import std/game/Gfx
|
|
9
9
|
import std/Io
|
|
10
|
-
import std/game/
|
|
10
|
+
import std/game/Input
|
|
11
11
|
import std/math/Affine
|
|
12
12
|
import std/Option
|
|
13
13
|
import std/collections/List
|
|
@@ -31,21 +31,21 @@ struct State[ScreenSlice<Float>](target: Texture, bad: Int) =
|
|
|
31
31
|
|
|
32
32
|
fun draw(self, io: Io, m: Affine, alpha: Float, area: Aabb) =
|
|
33
33
|
self.target.renderTarget(io)
|
|
34
|
-
|
|
34
|
+
Gfx.clear(io, Color.Transparent)
|
|
35
|
-
|
|
35
|
+
Gfx.drawRect(io, 0.0, 0.0, 10.0, 10.0, Color.Red)
|
|
36
|
-
|
|
36
|
+
Gfx.renderToScreen(io)
|
|
37
|
-
|
|
37
|
+
Gfx.blendMode(io, Blend.Add)
|
|
38
38
|
self.target.draw(io, 20.0, 20.0, Color.White)
|
|
39
|
-
|
|
39
|
+
Gfx.blendMode(io, Blend.Normal)
|
|
40
|
-
|
|
40
|
+
Gfx.drawText(io, "size {self.target.width(io)}x{self.target.height(io)} bad {self.bad}", 0.0, 0.0, 8.0, Color.White)
|
|
41
41
|
|
|
42
|
-
# What the screen draws each frame, with `
|
|
42
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
43
43
|
struct StateDrawing[View](s: State) =
|
|
44
|
-
fun place(self) -> Place =
|
|
44
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
45
45
|
|
|
46
46
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
47
47
|
|
|
48
48
|
fun main(io: Io) -> Game =
|
|
49
|
-
Game(screens: [page(State(target:
|
|
49
|
+
Game(screens: [page(State(target: Texture.create(io, 64, 32), bad: Gfx.textureCreate(io, 0, 5)))])
|
|
50
50
|
|
|
51
51
|
fun page(s: Screen) -> Screen = s
|
compiler/game/tests/fixtures/turn_fixture.plum
CHANGED
|
@@ -17,7 +17,7 @@ import std/game/Game
|
|
|
17
17
|
# A group at (100, 100) turned a quarter, holding a red 40 by 20 box and a label, and a
|
|
18
18
|
# label in the hud at (5, 5).
|
|
19
19
|
struct Turned[View](children: List<View>) =
|
|
20
|
-
fun place(self) -> Place =
|
|
20
|
+
fun place(self) -> Place = Place.at(100.0, 100.0).rotateTo(Float.HalfPi)
|
|
21
21
|
|
|
22
22
|
fun view(self) -> List<View> = self.children
|
|
23
23
|
|
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/
|
|
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/Control\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Place\nstruct State[ScreenSlice<Int>](n: Int) =\n fun name(self) -> Str = \"page\"\n fun reduce(self, a: Int) -> 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\nfun 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/
|
|
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/Control\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Place\nstruct State[ScreenSlice<Int>](seed: Int) =\n fun name(self) -> Str = \"page\"\n fun reduce(self, a: Int) -> 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\nfun 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/
|
|
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/Control\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Place\nenum Saw = Held(keys: Int, x: Float) | Ticked\nstruct State[ScreenSlice<Saw>](seed: Int, ticks: Int, keys: Int, x: Float) =\n fun name(self) -> Str = \"page\"\n fun hud(self, window: Vec2) -> List<View> = [StateDrawing(s: self, w: window.x)]\n fun held(self, i: Input) -> Option<Saw> = Some(Held(keys: i.held.keys.length(), x: i.pos.x))\n fun tick(self, dt: Float) -> Option<Saw> = Some(Ticked)\n fun reduce(self, a: Saw) -> State = match a\n Held(k, x) => State(..self, keys: self.keys + k, x: x)\n Ticked => 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\nfun 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/
|
|
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/Control\nimport std/game/Screen\nimport std/game/ScreenSlice\nimport std/game/Game\nimport std/game/View\nimport std/game/Place\nstruct Seeds(mut c: Int)\nstruct State[ScreenSlice<Int>](a: Int, b: Int, seeds: Seeds) =\n fun name(self) -> Str = \"page\"\n fun tick(self, dt: Float) -> Option<Int> = Some(1)\n fun reduce(self, n: Int) -> State = self\n fun effect(self, io: Io, n: Int, game: Control) =\n self.seeds.c = self.seeds.c ^ io.randomSeed()\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.seeds.c}\", 0.0, 0.0, 8.0, Color.White)\n\nfun main(io: Io) -> Game =\n a := io.randomSeed()\n seeds := Seeds(c: 0)\n only: Screen := State(a: a, b: io.randomSeed(), seeds: seeds)\n Game(screens: [only])\n",
|
|
467
467
|
)
|
|
468
468
|
.unwrap();
|
|
469
469
|
let wasm = compile(&path);
|
compiler/game/web-test/fixtures/interleave.plum
CHANGED
|
@@ -8,9 +8,9 @@ import std/Float
|
|
|
8
8
|
import std/Array
|
|
9
9
|
import std/math/Vec2
|
|
10
10
|
import std/game/Color
|
|
11
|
-
import std/game/
|
|
11
|
+
import std/game/Gfx
|
|
12
12
|
import std/Io
|
|
13
|
-
import std/game/
|
|
13
|
+
import std/game/Input
|
|
14
14
|
import std/math/Affine
|
|
15
15
|
import std/Option
|
|
16
16
|
import std/collections/List
|
|
@@ -38,20 +38,20 @@ struct Interleave[ScreenSlice<Float>](tex: Array<Texture>, t: Int) =
|
|
|
38
38
|
|
|
39
39
|
fun draw(self, io: Io, m: Affine, alpha: Float, area: Aabb) = drawSprites(io, self.tex, self.t)
|
|
40
40
|
|
|
41
|
-
# What the screen draws each frame, with `
|
|
41
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
42
42
|
struct InterleaveDrawing[View](s: Interleave) =
|
|
43
|
-
fun place(self) -> Place =
|
|
43
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
44
44
|
|
|
45
45
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
46
46
|
|
|
47
47
|
fun main(io: Io) -> Game =
|
|
48
48
|
tex := Array.filled(Textures, Texture(id: -1))
|
|
49
49
|
for i := range Textures
|
|
50
|
-
tex.set(i,
|
|
50
|
+
tex.set(i, Texture.load(io, "tex{i}.png"))
|
|
51
51
|
Game(screens: [page(Interleave(tex: tex, t: 0))])
|
|
52
52
|
|
|
53
53
|
fun drawSprites(io: Io, tex: Array<Texture>, t: Int) =
|
|
54
|
-
|
|
54
|
+
Gfx.clear(io, Color.rgb(0.05, 0.06, 0.1))
|
|
55
55
|
for i := range Sprites
|
|
56
56
|
x := {i * 37 + t * {1 + i % 5}} % WorldW
|
|
57
57
|
y := {i * 53 + t * {1 + i % 3}} % WorldH
|
compiler/game/web-test/fixtures/main_game.plum
CHANGED
|
@@ -7,8 +7,8 @@ import std/Io
|
|
|
7
7
|
import std/math/Vec2
|
|
8
8
|
import std/math/Affine
|
|
9
9
|
import std/game/Color
|
|
10
|
-
import std/game/
|
|
10
|
+
import std/game/Gfx
|
|
11
|
-
import std/game/
|
|
11
|
+
import std/game/Input
|
|
12
12
|
import std/Option
|
|
13
13
|
import std/collections/List
|
|
14
14
|
import std/math/Aabb
|
|
@@ -30,13 +30,13 @@ struct Mover[ScreenSlice<Float>](frame: Int) =
|
|
|
30
30
|
fun view(self) -> List<View> = [MoverDrawing(s: self)]
|
|
31
31
|
|
|
32
32
|
fun draw(self, io: Io, m: Affine, alpha: Float, area: Aabb) =
|
|
33
|
-
|
|
33
|
+
Gfx.clear(io, Color.rgb(0.2, 0.2, 0.2))
|
|
34
|
-
|
|
34
|
+
Gfx.drawRect(io, Float(self.frame % 600), 100.0, 40.0, 40.0, Color.Yellow)
|
|
35
|
-
|
|
35
|
+
Gfx.drawText(io, "frame {self.frame}", 20.0, 20.0, 16.0, Color.White)
|
|
36
36
|
|
|
37
|
-
# What the screen draws each frame, with `
|
|
37
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
38
38
|
struct MoverDrawing[View](s: Mover) =
|
|
39
|
-
fun place(self) -> Place =
|
|
39
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
40
40
|
|
|
41
41
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
42
42
|
|
compiler/game/web-test/fixtures/rendertarget.plum
CHANGED
|
@@ -19,9 +19,9 @@ import std/Int
|
|
|
19
19
|
import std/Float
|
|
20
20
|
import std/math/Vec2
|
|
21
21
|
import std/game/Color
|
|
22
|
-
import std/game/
|
|
22
|
+
import std/game/Gfx
|
|
23
23
|
import std/Io
|
|
24
|
-
import std/game/
|
|
24
|
+
import std/game/Input
|
|
25
25
|
import std/math/Affine
|
|
26
26
|
import std/Option
|
|
27
27
|
import std/collections/List
|
|
@@ -44,43 +44,43 @@ struct Targets[ScreenSlice<Float>](big: Texture, small: Texture, glass: Texture,
|
|
|
44
44
|
|
|
45
45
|
fun draw(self, io: Io, m: Affine, alpha: Float, area: Aabb) = drawTargets(io, self)
|
|
46
46
|
|
|
47
|
-
# What the screen draws each frame, with `
|
|
47
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
48
48
|
struct TargetsDrawing[View](s: Targets) =
|
|
49
|
-
fun place(self) -> Place =
|
|
49
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
50
50
|
|
|
51
51
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
52
52
|
|
|
53
53
|
fun main(io: Io) -> Game =
|
|
54
|
-
targets := Targets(big:
|
|
54
|
+
targets := Targets(big: Texture.create(io, 64, 64), small: Texture.create(io, 16, 16), glass: Texture.create(io, 16, 16), t: 0)
|
|
55
55
|
Game(screens: [page(targets)])
|
|
56
56
|
|
|
57
57
|
fun drawTargets(io: Io, g: Targets) =
|
|
58
|
-
|
|
58
|
+
Gfx.setCamera(io, 10.0, 0.0, 2.0)
|
|
59
59
|
g.big.renderTarget(io)
|
|
60
|
-
|
|
60
|
+
Gfx.clear(io, Color.Transparent)
|
|
61
|
-
|
|
61
|
+
Gfx.drawRect(io, 0.0, 0.0, 32.0, 32.0, Color.rgb(1.0, 0.0, 0.0))
|
|
62
|
-
|
|
62
|
+
Gfx.drawRect(io, 32.0, 32.0, 32.0, 32.0, Color.rgb(0.0, 1.0, 0.0))
|
|
63
63
|
g.small.renderTarget(io)
|
|
64
|
-
|
|
64
|
+
Gfx.clear(io, Color.rgb(0.0, 0.0, 1.0))
|
|
65
65
|
g.glass.renderTarget(io)
|
|
66
|
-
|
|
66
|
+
Gfx.clear(io, Color.Transparent)
|
|
67
|
-
|
|
67
|
+
Gfx.drawRect(io, 0.0, 0.0, 16.0, 16.0, Color(r: 1.0, g: 0.0, b: 0.0, a: 0.5))
|
|
68
|
-
|
|
68
|
+
Gfx.renderToScreen(io)
|
|
69
|
-
|
|
69
|
+
Gfx.clear(io, Color.rgb(0.2, 0.2, 0.2))
|
|
70
70
|
g.big.draw(io, 60.0, 50.0, Color.White)
|
|
71
|
-
|
|
71
|
+
Gfx.resetCamera(io)
|
|
72
|
-
|
|
72
|
+
Gfx.blendMode(io, Blend.Add)
|
|
73
|
-
|
|
73
|
+
Gfx.drawRect(io, 400.0, 100.0, 100.0, 100.0, Color.rgb(0.2, 0.4, 0.6))
|
|
74
|
-
|
|
74
|
+
Gfx.blendMode(io, Blend.Multiply)
|
|
75
|
-
|
|
75
|
+
Gfx.drawRect(io, 550.0, 100.0, 100.0, 100.0, Color.rgb(0.5, 1.0, 0.5))
|
|
76
|
-
|
|
76
|
+
Gfx.blendMode(io, Blend.Screen)
|
|
77
|
-
|
|
77
|
+
Gfx.drawRect(io, 700.0, 100.0, 100.0, 100.0, Color.rgb(0.5, 0.5, 0.5))
|
|
78
|
-
|
|
78
|
+
Gfx.blendMode(io, Blend.Normal)
|
|
79
|
-
|
|
79
|
+
Gfx.drawRect(io, 850.0, 100.0, 100.0, 100.0, Color(r: 1.0, g: 0.0, b: 0.0, a: 0.5))
|
|
80
80
|
g.small.draw(io, 400.0, 300.0, Color.White)
|
|
81
81
|
g.glass.draw(io, 400.0, 400.0, Color.White)
|
|
82
|
-
|
|
82
|
+
Gfx.drawRectEx(io, 600.0, 440.0, 100.0, 20.0, Float.HalfPi, Color.White)
|
|
83
|
-
|
|
83
|
+
Gfx.drawText(io, "frame {g.t}", 20.0, 500.0, 16.0, Color.White)
|
|
84
|
-
|
|
84
|
+
Gfx.drawRect(io, Float(g.t % 600), 600.0, 40.0, 6.0, Color.Yellow)
|
|
85
85
|
|
|
86
86
|
fun page(s: Screen) -> Screen = s
|
compiler/game/web-test/fixtures/stress.plum
CHANGED
|
@@ -8,9 +8,9 @@ import std/Int
|
|
|
8
8
|
import std/Float
|
|
9
9
|
import std/math/Vec2
|
|
10
10
|
import std/game/Color
|
|
11
|
-
import std/game/
|
|
11
|
+
import std/game/Gfx
|
|
12
12
|
import std/Io
|
|
13
|
-
import std/game/
|
|
13
|
+
import std/game/Input
|
|
14
14
|
import std/math/Affine
|
|
15
15
|
import std/Option
|
|
16
16
|
import std/collections/List
|
|
@@ -40,36 +40,36 @@ struct Stress[ScreenSlice<Float>](sheet: Texture, t: Int) =
|
|
|
40
40
|
|
|
41
41
|
fun draw(self, io: Io, m: Affine, alpha: Float, area: Aabb) = drawStress(io, self.sheet, self.t)
|
|
42
42
|
|
|
43
|
-
# What the screen draws each frame, with `
|
|
43
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
44
44
|
struct StressDrawing[View](s: Stress) =
|
|
45
|
-
fun place(self) -> Place =
|
|
45
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
46
46
|
|
|
47
47
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
48
48
|
|
|
49
49
|
fun main(io: Io) -> Game =
|
|
50
|
-
Game(screens: [page(Stress(sheet:
|
|
50
|
+
Game(screens: [page(Stress(sheet: Texture.load(io, "sprites.png"), t: 0))])
|
|
51
51
|
|
|
52
52
|
fun hue(i: Int) -> Color =
|
|
53
53
|
Color(r: Float(i % 7) / 7.0, g: Float(i % 5) / 5.0, b: 0.4 + Float(i % 3) / 5.0, a: 0.8)
|
|
54
54
|
|
|
55
55
|
fun drawStress(io: Io, sheet: Texture, t: Int) =
|
|
56
|
-
|
|
56
|
+
Gfx.clear(io, Color.rgb(0.05, 0.06, 0.1))
|
|
57
|
-
|
|
57
|
+
Gfx.setCamera(io, 0.0, 0.0, 0.8)
|
|
58
58
|
for i := range Sprites
|
|
59
59
|
x := {i * 37 + t * {1 + i % 5}} % WorldW
|
|
60
60
|
y := {i * 53 + t * {1 + i % 3}} % WorldH
|
|
61
61
|
frame := {i + t / 6} % 4
|
|
62
|
-
|
|
62
|
+
Gfx.drawTextureEx(io, sheet.id, Float(frame * 16), 0.0, 16.0, 16.0, Float(x), Float(y), 16.0, 16.0, Float(t) * 0.02 + Float(i), i % 2 == 0, Color.White)
|
|
63
63
|
for i := range Rects
|
|
64
64
|
x := {i * 61 + t * 2} % WorldW
|
|
65
65
|
y := {i * 29} % WorldH
|
|
66
|
-
|
|
66
|
+
Gfx.drawRect(io, Float(x), Float(y), 12.0, 8.0, hue(i))
|
|
67
67
|
for i := range Circles
|
|
68
68
|
x := {i * 83 + t * 3} % WorldW
|
|
69
69
|
y := {i * 47 + t} % WorldH
|
|
70
|
-
|
|
70
|
+
Gfx.drawCircle(io, Float(x), Float(y), 6.0, hue(i + 3))
|
|
71
71
|
for i := range Texts
|
|
72
|
-
|
|
72
|
+
Gfx.drawText(io, "n {t + i}", Float(i * 30 % WorldW), Float(i * 17 % WorldH), 14.0, Color.White)
|
|
73
|
-
|
|
73
|
+
Gfx.resetCamera(io)
|
|
74
74
|
|
|
75
75
|
fun page(s: Screen) -> Screen = s
|
compiler/game/web-test/fixtures/stress_state_view.plum
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
# its `view` makes every item again each frame from them (a view is drawn, not live).
|
|
5
5
|
#
|
|
6
6
|
# Entity counts: 5000 sprites (50 groups x 100), 500 rects, 200 circles, 50 texts,
|
|
7
|
-
# plus 200 animated sprites (pulsers, pose =
|
|
7
|
+
# plus 200 animated sprites (pulsers, pose = anim.at(clock - start)) and 20 buttons
|
|
8
8
|
# (screen space, outside the scaled world) that take presses: a press on one is the
|
|
9
9
|
# screen's `Clicked`, which counts it.
|
|
10
10
|
module stress_state_view
|
|
@@ -18,9 +18,9 @@ import std/math/Vec2
|
|
|
18
18
|
import std/math/Affine
|
|
19
19
|
import std/math/Aabb
|
|
20
20
|
import std/game/Color
|
|
21
|
-
import std/game/
|
|
21
|
+
import std/game/Gfx
|
|
22
22
|
import std/game/Anim
|
|
23
|
-
import std/game/
|
|
23
|
+
import std/game/Input
|
|
24
24
|
import std/game/Place
|
|
25
25
|
import std/game/View
|
|
26
26
|
import std/game/Box
|
|
@@ -48,20 +48,20 @@ fun rangeLink(i: Int, n: Int, f: fn(Int) -> View) -> List<View> =
|
|
|
48
48
|
|
|
49
49
|
# A sprite of the sheet at (x, y), turned by `rotation` about its centre.
|
|
50
50
|
struct Dancer[View](x: Float, y: Float, rotation: Float, srcX: Float, sheet: Texture) =
|
|
51
|
-
fun place(self) -> Place =
|
|
51
|
+
fun place(self) -> Place = Place.at(self.x, self.y).rotateTo(self.rotation).pivotAt(8.0, 8.0)
|
|
52
52
|
|
|
53
53
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) =
|
|
54
|
-
|
|
54
|
+
Sprite.draw(io, m, alpha, 16.0, 16.0, Color.White, self.sheet, self.srcX, 0.0, 16.0, 16.0)
|
|
55
55
|
|
|
56
56
|
# A group of dancers turned by `rotation` about (75, 85).
|
|
57
57
|
struct Spinner[View](x: Float, y: Float, rotation: Float, children: List<View>) =
|
|
58
|
-
fun place(self) -> Place =
|
|
58
|
+
fun place(self) -> Place = Place.at(self.x, self.y).rotateTo(self.rotation).pivotAt(75.0, 85.0)
|
|
59
59
|
|
|
60
60
|
fun view(self) -> List<View> = self.children
|
|
61
61
|
|
|
62
62
|
# The world layer: everything in it scaled by `scale`.
|
|
63
63
|
struct Scaled[View](scale: Float, children: List<View>) =
|
|
64
|
-
fun place(self) -> Place =
|
|
64
|
+
fun place(self) -> Place = Place.at(0.0, 0.0).scaleTo(self.scale, self.scale)
|
|
65
65
|
|
|
66
66
|
fun view(self) -> List<View> = self.children
|
|
67
67
|
|
|
@@ -81,7 +81,7 @@ struct ViewScreen[ScreenSlice<StressAction>](clock: Float, frames: Int, clicks:
|
|
|
81
81
|
|
|
82
82
|
fun view(self) -> List<View> =
|
|
83
83
|
world: View := Scaled(scale: 0.8, children: rangeLink(0, Groups, |gi| self.spinner(gi)).append(rangeLink(0, Pulsers, |i| self.pulser(i))).append(rangeLink(0, Rects, |i| self.rect(i))).append(rangeLink(0, Circles, |i| self.circle(i))).append(rangeLink(0, Texts, |i| self.counter(i))))
|
|
84
|
-
buttons :=
|
|
84
|
+
buttons := Views.all(self.buttons)
|
|
85
85
|
labels := rangeLink(0, Buttons, |i| self.buttonLabel(i))
|
|
86
86
|
Cons(world, buttons.append(labels))
|
|
87
87
|
|
|
@@ -102,7 +102,7 @@ struct ViewScreen[ScreenSlice<StressAction>](clock: Float, frames: Int, clicks:
|
|
|
102
102
|
Dancer(x: Float({j * j * 13 + j * 37} % 150), y: Float({j * j * 29 + j * 53} % 170), rotation: Float(i) + 1.2 * self.clock, srcX: Float(i % 4 * 16), sheet: self.sheet)
|
|
103
103
|
|
|
104
104
|
fun pulser(self, i: Int) -> View =
|
|
105
|
-
pose :=
|
|
105
|
+
pose := self.anim.at(self.clock - Float(i % 10) * 0.1)
|
|
106
106
|
Sprite(
|
|
107
107
|
x: Float(i * 97 % 1500) + pose.dx, y: Float(i * 53 % 850) + pose.dy, tex: self.sheet, w: 16.0, h: 16.0, srcX: Float(i % 4 * 16), srcY: 0.0,
|
|
108
108
|
srcW: 16.0, srcH: 16.0, frame: 0, tint: Color.White, touch: Disabled)
|
|
@@ -132,12 +132,12 @@ fun hue(i: Int) -> Color =
|
|
|
132
132
|
Color(r: Float(i % 7) / 7.0, g: Float(i % 5) / 5.0, b: 0.4 + Float(i % 3) / 5.0, a: 0.8)
|
|
133
133
|
|
|
134
134
|
fun whiteTexture(io: Io) -> Texture =
|
|
135
|
-
t :=
|
|
135
|
+
t := Texture.create(io, 1, 1)
|
|
136
136
|
t.renderTarget(io)
|
|
137
|
-
|
|
137
|
+
Gfx.clear(io, Color.White)
|
|
138
|
-
|
|
138
|
+
Gfx.renderToScreen(io)
|
|
139
139
|
t
|
|
140
140
|
|
|
141
141
|
fun main(io: Io) -> Game =
|
|
142
|
-
only: Screen := ViewScreen.create(
|
|
142
|
+
only: Screen := ViewScreen.create(Texture.load(io, "sprites.png"), whiteTexture(io))
|
|
143
143
|
Game(screens: [only])
|
compiler/game/web-test/fixtures/tests/stress_state_view.plum
CHANGED
|
@@ -8,9 +8,9 @@ import std/math/Vec2
|
|
|
8
8
|
import std/math/Affine
|
|
9
9
|
import std/math/Aabb
|
|
10
10
|
import std/game/Color
|
|
11
|
-
import std/game/
|
|
11
|
+
import std/game/Gfx
|
|
12
12
|
import std/game/Anim
|
|
13
|
-
import std/game/
|
|
13
|
+
import std/game/Input
|
|
14
14
|
import std/game/Place
|
|
15
15
|
import std/game/View
|
|
16
16
|
import std/game/Box
|
|
@@ -26,13 +26,13 @@ import std/Test
|
|
|
26
26
|
|
|
27
27
|
primitive StressStateViewTest[UnitTest] =
|
|
28
28
|
fun tenTicksMoveTheScene() =
|
|
29
|
-
t :=
|
|
29
|
+
t := TestRig.mount(io, ViewScreen.create(Texture(id: 0), Texture(id: 0))).ticks(10)
|
|
30
30
|
assert t.screen.frames == 10
|
|
31
31
|
assert {t.screen.clock - 10.0 / 60.0}.abs() < 0.000001
|
|
32
32
|
assert t.text(t.find<Label>().at(3)) == "n 13"
|
|
33
33
|
|
|
34
34
|
fun theViewDrawsOneItemPerEntity() =
|
|
35
|
-
t :=
|
|
35
|
+
t := TestRig.mount(io, ViewScreen.create(Texture(id: 0), Texture(id: 0)))
|
|
36
36
|
assert t.count<Dancer>() == stress_state_view.Sprites
|
|
37
37
|
assert t.count<Spinner>() == stress_state_view.Groups
|
|
38
38
|
assert t.count<Sprite>() == stress_state_view.Pulsers + stress_state_view.Rects + stress_state_view.Circles
|
|
@@ -41,8 +41,8 @@ primitive StressStateViewTest[UnitTest] =
|
|
|
41
41
|
|
|
42
42
|
# a click on a button counts, one on the world does not
|
|
43
43
|
fun aClickOnAButtonCountsOneOn() =
|
|
44
|
-
t :=
|
|
44
|
+
t := TestRig.mount(io, ViewScreen.create(Texture(id: 0), Texture(id: 0)))
|
|
45
45
|
u := t.clickAt(20.0, 20.0).tick().clickAt(700.0, 500.0).tick()
|
|
46
46
|
assert u.screen.clicks == 1
|
|
47
|
-
a :=
|
|
47
|
+
a := stress_state_view.pulseAnim().at(0.5)
|
|
48
48
|
assert a.dx > 14.9 && a.dx < 15.1
|
compiler/game/web-test/fixtures/text_sizes.plum
CHANGED
|
@@ -7,9 +7,9 @@ import std/Int
|
|
|
7
7
|
import std/Float
|
|
8
8
|
import std/math/Vec2
|
|
9
9
|
import std/game/Color
|
|
10
|
-
import std/game/
|
|
10
|
+
import std/game/Gfx
|
|
11
11
|
import std/Io
|
|
12
|
-
import std/game/
|
|
12
|
+
import std/game/Input
|
|
13
13
|
import std/math/Affine
|
|
14
14
|
import std/Option
|
|
15
15
|
import std/collections/List
|
|
@@ -32,9 +32,9 @@ struct Sizes[ScreenSlice<Float>](t: Int) =
|
|
|
32
32
|
|
|
33
33
|
fun draw(self, io: Io, m: Affine, alpha: Float, area: Aabb) = drawSizes(io, self.t)
|
|
34
34
|
|
|
35
|
-
# What the screen draws each frame, with `
|
|
35
|
+
# What the screen draws each frame, with `Gfx` by hand.
|
|
36
36
|
struct SizesDrawing[View](s: Sizes) =
|
|
37
|
-
fun place(self) -> Place =
|
|
37
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
38
38
|
|
|
39
39
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.s.draw(io, m, alpha, Aabb.of(0.0, 0.0, 0.0, 0.0))
|
|
40
40
|
|
|
@@ -42,17 +42,17 @@ fun main(io: Io) -> Game =
|
|
|
42
42
|
Game(screens: [page(Sizes(t: 0))])
|
|
43
43
|
|
|
44
44
|
fun drawSizes(io: Io, t: Int) =
|
|
45
|
-
|
|
45
|
+
Gfx.clear(io, Color.rgb(0.08, 0.09, 0.14))
|
|
46
|
-
|
|
46
|
+
Gfx.drawText(io, "Size 8: The quick brown fox jumps over the lazy dog 0123456789", 10.0, 6.0, 8.0, Color.White)
|
|
47
|
-
|
|
47
|
+
Gfx.drawText(io, "Size 16: The quick brown fox jumps over the lazy dog", 10.0, 24.0, 16.0, Color.White)
|
|
48
|
-
|
|
48
|
+
Gfx.drawText(io, "Size 48: Quick brown fox", 10.0, 52.0, 48.0, Color.Yellow)
|
|
49
|
-
|
|
49
|
+
Gfx.drawText(io, "Size 128 Wg", 10.0, 112.0, 128.0, Color.rgb(0.5, 0.9, 1.0))
|
|
50
|
-
|
|
50
|
+
Gfx.drawText(io, "Latin-1: café naïve über ½ ©", 10.0, 250.0, 24.0, Color.White)
|
|
51
|
-
|
|
51
|
+
Gfx.drawText(io, "frame {t}", 700.0, 8.0, 16.0, Color.Green)
|
|
52
|
-
|
|
52
|
+
Gfx.drawRect(io, Float(t % 400), 300.0, 60.0, 4.0, Color.Red)
|
|
53
|
-
|
|
53
|
+
Gfx.setCamera(io, 0.0, 0.0, 3.0)
|
|
54
|
-
|
|
54
|
+
Gfx.drawText(io, "Zoomed x3: size 16", 10.0, 112.0, 16.0, Color.White)
|
|
55
|
-
|
|
55
|
+
Gfx.drawText(io, "Zoomed x3: size 8", 10.0, 134.0, 8.0, Color.White)
|
|
56
|
-
|
|
56
|
+
Gfx.resetCamera(io)
|
|
57
57
|
|
|
58
58
|
fun page(s: Screen) -> Screen = s
|
compiler/host/src/lib.rs
CHANGED
|
@@ -13,7 +13,7 @@ pub struct Caps {
|
|
|
13
13
|
pub http: bool,
|
|
14
14
|
pub clock: bool,
|
|
15
15
|
pub rng: bool,
|
|
16
|
-
/// The game API (`std/game/
|
|
16
|
+
/// The game API (`std/game/Gfx`, `Audio`, `Input`) this host doesn't define answers with
|
|
17
17
|
/// zeros and nulls and draws nothing, in place of trapping: for timing a game's draw walk
|
|
18
18
|
/// on the command line (benchmarks/screen_views). Off unless `PLUM_NULL_GFX` is set in
|
|
19
19
|
/// the environment.
|
|
@@ -562,7 +562,7 @@ pub fn hostImports(
|
|
|
562
562
|
},
|
|
563
563
|
)))
|
|
564
564
|
}
|
|
565
|
-
// A `plum` import this host doesn't define (the game API of `std/game/
|
|
565
|
+
// A `plum` import this host doesn't define (the game API of `std/game/Gfx`)
|
|
566
566
|
// is stubbed with a function that traps when called, so a program
|
|
567
567
|
// that imports it can still run or test code that never calls it.
|
|
568
568
|
("plum", n) => {
|
compiler/lsp/src/completion.rs
CHANGED
|
@@ -625,9 +625,9 @@ pub fn stdLibIndex(lib_path: &Path) -> HashMap<String, String> {
|
|
|
625
625
|
continue;
|
|
626
626
|
};
|
|
627
627
|
let source = plum_core::AstParser::new(&text).parseSourceRaw(tree.root_node());
|
|
628
|
-
// A file with module-level functions or constants is a module: its name
|
|
628
|
+
// A file with module-level functions or constants is a module: its name
|
|
629
|
-
// `
|
|
629
|
+
// (`headless`) is what completion offers and auto-imports. The functions themselves
|
|
630
|
-
// are written `
|
|
630
|
+
// are written `module.name(..)`, so they are not indexed by name.
|
|
631
631
|
let isModule = source.items.iter().any(|i| match i {
|
|
632
632
|
ast::Item::Fn(f) => f.type_param.is_none() && !f.is_test,
|
|
633
633
|
ast::Item::Const(c) => !c.name.contains(ast::MEMBER_SEP),
|
compiler/lsp/src/main.rs
CHANGED
|
@@ -717,7 +717,7 @@ mod tests {
|
|
|
717
717
|
let _ = std::fs::remove_dir_all(&dir);
|
|
718
718
|
}
|
|
719
719
|
|
|
720
|
-
/// The std
|
|
720
|
+
/// The std namespaces (`Os`, `Base64`, `Test`, `Http`, `Gfx`) and types (`Input`) are indexed for
|
|
721
721
|
/// auto-import by name, and a namespace completes its functions after `Name.`.
|
|
722
722
|
#[test]
|
|
723
723
|
fn stdNamespacesCompleteAndAutoImport() {
|
|
@@ -728,18 +728,19 @@ mod tests {
|
|
|
728
728
|
("Test", "std/Test"),
|
|
729
729
|
("Http", "std/Http"),
|
|
730
730
|
("Math", "std/math/Math"),
|
|
731
|
-
("
|
|
731
|
+
("Gfx", "std/game/Gfx"),
|
|
732
|
+
("Audio", "std/game/Audio"),
|
|
732
|
-
("
|
|
733
|
+
("Input", "std/game/Input"),
|
|
733
734
|
("Int", "std/Int"),
|
|
734
735
|
("Viewport", "std/game/Viewport"),
|
|
735
736
|
("Io", "std/Io"),
|
|
736
|
-
("Key", "std/game/
|
|
737
|
+
("Key", "std/game/Input"),
|
|
737
738
|
("Vec2", "std/math/Vec2"),
|
|
738
739
|
] {
|
|
739
740
|
assert_eq!(index.get(name).map(String::as_str), Some(path), "{name}");
|
|
740
741
|
}
|
|
741
742
|
// The files are `primitive` namespaces now, not modules: no `os` or `base64` alias.
|
|
742
|
-
for alias in ["os", "base64", "testing", "http", "math"] {
|
|
743
|
+
for alias in ["os", "base64", "testing", "http", "math", "gfx", "audio", "input"] {
|
|
743
744
|
assert!(index.get(alias).is_none(), "{alias}");
|
|
744
745
|
}
|
|
745
746
|
// Completing `Io` offers `std/Io` and auto-imports that path.
|
compiler/wasm-codegen/src/driver.rs
CHANGED
|
@@ -411,7 +411,14 @@ impl<'a> Setup<'a> {
|
|
|
411
411
|
// An extern METHOD (of the primitive `Io`) takes its receiver as the first
|
|
412
412
|
// wrapper param; the host import has no receiver.
|
|
413
413
|
// A module-level extern's `io: Io` is the capability too: not passed to the host.
|
|
414
|
+
// A static of a type (`Gfx.clear(io, ..)`) has the unused receiver slot every
|
|
415
|
+
// static has, and then its `io`: neither is passed.
|
|
414
|
-
let skip =
|
|
416
|
+
let skip = match (f.type_param.is_some(), f.has_self, ast::takesIoFirst(f)) {
|
|
417
|
+
(true, true, _) => 1,
|
|
418
|
+
(true, false, true) => 2,
|
|
419
|
+
(false, _, true) => 1,
|
|
420
|
+
_ => 0,
|
|
421
|
+
};
|
|
415
422
|
let key = if f.type_param.is_some() { fnKey(f) } else { f.name.clone() };
|
|
416
423
|
if f.type_param.is_none() {
|
|
417
424
|
known_fns.insert(f.name.clone());
|
compiler/wasm-codegen/tests/wasm_codegen/codegen_tests.rs
CHANGED
|
@@ -2305,7 +2305,7 @@ fun main() -> Int =
|
|
|
2305
2305
|
#[test]
|
|
2306
2306
|
fn primitive_not_a_type() {
|
|
2307
2307
|
// `Key` is one of std's constant groups (a `primitive`): not a type, not a value.
|
|
2308
|
-
let head = format!("{STD_HEAD}import std/game/
|
|
2308
|
+
let head = format!("{STD_HEAD}import std/game/Input\n");
|
|
2309
2309
|
let as_type = checkErrorsMerged(
|
|
2310
2310
|
"prim_type",
|
|
2311
2311
|
&format!(
|
compiler/wasm-codegen/tests/wasm_codegen/e2e_types/io_entry.rs
CHANGED
|
@@ -15,12 +15,12 @@ fn instantiate(engine: &wasmtime::Engine, module: &wasmtime::Module) -> (wasmtim
|
|
|
15
15
|
(store, instance)
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
const PRE_IO: &str = "import std/Str\nimport std/Int\nimport std/Float\nimport std/Io\nimport std/game/
|
|
18
|
+
const PRE_IO: &str = "import std/Str\nimport std/Int\nimport std/Float\nimport std/Io\nimport std/game/Gfx\n\n";
|
|
19
19
|
|
|
20
20
|
#[test]
|
|
21
21
|
fn entry_point_receives_io() {
|
|
22
22
|
let src = format!(
|
|
23
|
-
"{PRE_IO}export fun update(io: Io, n: Int) -> Int =\n w :=
|
|
23
|
+
"{PRE_IO}export fun update(io: Io, n: Int) -> Int =\n w := Gfx.screenWidth(io)\n n + 1\n\nfun main(io: Io) -> Int =\n h := Gfx.screenHeight(io)\n 41\n"
|
|
24
24
|
);
|
|
25
25
|
let (engine, module) = build("recv", &src);
|
|
26
26
|
let (mut store, instance) = instantiate(&engine, &module);
|
|
@@ -32,7 +32,7 @@ fn entry_point_receives_io() {
|
|
|
32
32
|
|
|
33
33
|
#[test]
|
|
34
34
|
fn export_abi_unchanged_with_io_param() {
|
|
35
|
-
let withIo = format!("{PRE_IO}export fun update(io: Io, n: Int, f: Float) -> Int =\n w :=
|
|
35
|
+
let withIo = format!("{PRE_IO}export fun update(io: Io, n: Int, f: Float) -> Int =\n w := Gfx.screenWidth(io)\n n\n");
|
|
36
36
|
let without = format!("{PRE_IO}export fun update(n: Int, f: Float) -> Int = n\n");
|
|
37
37
|
let sig = |name: &str, src: &str| {
|
|
38
38
|
let (_, module) = build(name, src);
|
docs/backlog.md
CHANGED
|
@@ -7,7 +7,7 @@ Open work on `simpler-language`, rewritten 2026-11-11. Only what is still open i
|
|
|
7
7
|
The retained scene tree was replaced by master's screen view in functional form: a `Game(screens: ..)` of `Screen`s, a `ScreenSlice<A>` per screen with rules (master's `Slice<A>`), a `view` each frame that the game draws and hit-tests itself, `Control` for effects, undo and time travel by re-simulation, and the headless kit (`docs/game.md`, "Beside master", has the table of differences). `SceneTree`, `Ctx`, `Clickable`, `Store` and `ui` are gone, with the examples that used them (`orb_run`, `kids/balloon_pop`, now `examples/games/orbitrunner` and `balloonpop`), `benchmarks/scene_trees` and two web-test stress fixtures (their results stay in `docs/benchmarks` and `compiler/game/web-test/BENCHMARK.md`). Orbit Runner's state checksum over four seeds of 3,600 ticks is master's (925878360). On 2026-11-13 it was brought up to master at e91c40ce (stages 24 and 25 and the review after 24): `View` and `Actor[View]`, actors stepped by the game through the screen's `stepped(dt)`, `Screen.hud`, `steps` and `camera`, one walk over `view`, turned boxes and text, the other mouse buttons and keys, the per-screen backgrounds and dropped pointer events of a transition (the "Beside master" table has what still differs). Open:
|
|
8
8
|
|
|
9
9
|
- **A `match` on a nullary variant inside a closure in a function body matches every value.** `xs.fold(0, |s, e| match e` with an arm `B => s + 1` counts every element; the same match in a named function, or in a closure in a test, is right, and a variant with fields (`A(n) =>`) is right. std/game works around it (the matches are in methods: `Game.travelled`, `Game.redone`). Repro: `fun count(xs: List<E>) -> Int = xs.fold(0, |s, e| match e` / `B => s + 1` / `_ => s)` with `enum E = A(n: Int) | B | C`, `count([C])` gives 1.
|
|
10
|
-
- **Pass re-check failures met on the way** (fixed in std, not in the compiler): fusion devirtualising `s.frames().onDemand()` on a `Screen` with one implementer mistypes the fused call (std matches on `frames()` instead); trmc on `List.map` with a coercion (`xs.map(|m| m)` into a `List<View>`) fails its re-check (`
|
|
10
|
+
- **Pass re-check failures met on the way** (fixed in std, not in the compiler): fusion devirtualising `s.frames().onDemand()` on a `Screen` with one implementer mistypes the fused call (std matches on `frames()` instead); trmc on `List.map` with a coercion (`xs.map(|m| m)` into a `List<View>`) fails its re-check (`Views.all(xs)` lifts through a typed helper); dead-code removal is reverted when it removes every type a downcast to a trait could match (it met `Container as c`, which master has since folded into `view` too).
|
|
11
11
|
- **Frames on demand in the hosts**: `Game.idle()` works, but no host is told (master's `frameIdle` extern, and the browser skipping the draw and waiting for input).
|
|
12
12
|
- **Development keys** (`plum run --dev`, master's `remakeGame` host call): a game offers undo only through its own effects.
|
|
13
13
|
- **Master's other games** (`clockwork`, `menu`) are not ported.
|
docs/game.md
CHANGED
|
@@ -39,7 +39,7 @@ This game counts the clicks on a box, rolls a ball while Right is held, and ends
|
|
|
39
39
|
enum TapAction = Tapped | Push(dx: Float) | Quit
|
|
40
40
|
|
|
41
41
|
struct Ball[Actor](x: Float, y: Float, vx: Float) =
|
|
42
|
-
fun place(self) -> Place =
|
|
42
|
+
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
43
43
|
|
|
44
44
|
fun view(self) -> List<View> = [Circle.of(0.0, 0.0, 10.0, Color.Yellow)]
|
|
45
45
|
|
|
@@ -88,10 +88,10 @@ A screen's `view(self) -> List<View>` is what the frame shows: a list of views,
|
|
|
88
88
|
```plum
|
|
89
89
|
fun view(self) -> List<View> =
|
|
90
90
|
back: List<View> := [Box.of(0.0, 0.0, Arena.W, Arena.H, Color.rgb(0.07, 0.08, 0.16))]
|
|
91
|
-
mines :=
|
|
91
|
+
mines := Views.all(self.mines)
|
|
92
|
-
orbs :=
|
|
92
|
+
orbs := Views.all(self.orbs)
|
|
93
93
|
ship: List<View> := if self.over then Nil else [self.ship]
|
|
94
|
-
rings :=
|
|
94
|
+
rings := Views.all(self.rings)
|
|
95
95
|
hud: List<View> := [
|
|
96
96
|
Label.of(16.0, 12.0, "Score {self.score}", 28.0, Color.White),
|
|
97
97
|
Label.of(16.0, 44.0, "Best {self.best.max(self.score)}", 20.0, Color.Yellow)]
|
|
@@ -104,7 +104,7 @@ fun view(self) -> List<View> =
|
|
|
104
104
|
back.append(starsFrom(0, self.clock)).append(mines).append(orbs).append(ship).append(rings).append(hud).append(lifeIcons(0, self.lives, self.assets.sheet)).append(over)
|
|
105
105
|
```
|
|
106
106
|
|
|
107
|
-
What shows only sometimes is left out (the ship once the run is over, the game over panel until it is), what repeats is a list made by a function or a fold, and a list the screen holds is appended, lifted to a `List<View>` by `
|
|
107
|
+
What shows only sometimes is left out (the ship once the run is over, the game over panel until it is), what repeats is a list made by a function or a fold, and a list the screen holds is appended, lifted to a `List<View>` by `Views.all` (a `List<Mine>` is not a `List<View>`, see [Coercion](language.md#coercion)). Later items draw over earlier ones. (Master writes the same view as one list literal with `if`, `for` and `..` elements, which this dialect's list literals don't have.)
|
|
108
108
|
|
|
109
109
|
- **Drawn, not live.** The view is made again every time it is walked (to draw, to hit-test a press), and an item made inside it keeps nothing from one walk to the next. So each takes plain values read from the fields (a `Label`'s `text`, a `Sprite`'s `frame`). A `Group` or an `Overlay` in a view has the children it is made with as its own `view`, and an actor with a view of its own draws that, so views nest.
|
|
110
110
|
- **Two kinds of item.** An actor the screen holds in a field and names in its view (the ship, each mine of `self.mines`) is the very object in the field when the frame is drawn: a press on it reaches the screen as that object ([Clicks](#clicks)), and `@sameRef(target, self.box)` tells which. The game steps it through the screen's `stepped`, which returns the screen with the actor's next version. An item made inside `view` (the score label, the stars, the game over panel) is a new object every frame, made from the fields.
|
|
@@ -127,7 +127,7 @@ What else a screen says about its frame, each with a default:
|
|
|
127
127
|
| `busy(self) -> Bool` | false | true asks an `OnDemand` screen's next tick ([Frames on demand](#frames-on-demand)) |
|
|
128
128
|
| `frames(self) -> Frames` | `Always` | `OnDemand` for a screen that only changes when something happens |
|
|
129
129
|
|
|
130
|
-
The game works out each screen's camera from its `size()`, its `viewport()` and the window's size in pixels (`Game.pixels`), then the screen's `camera(base)` (`
|
|
130
|
+
The game works out each screen's camera from its `size()`, its `viewport()` and the window's size in pixels (`Game.pixels`), then the screen's `camera(base)` (`Game.cameraOf`). The pointer is mapped through the same camera, so a press arrives in the world's coordinates: a screen has no fit or resize code of its own. A screen that follows its player returns `base.lookAt(self.player.at.x, self.player.at.y, 1.0)` (`std/game/Viewport`: that world point at the window's centre, `zoom` times as close, culling to what is on the window).
|
|
131
131
|
|
|
132
132
|
### Views and actors
|
|
133
133
|
|
|
@@ -144,7 +144,7 @@ trait Actor[View] = # acts: state that advan
|
|
|
144
144
|
fun step(self, dt: Float) -> Self
|
|
145
145
|
```
|
|
146
146
|
|
|
147
|
-
`place` is its `Place` (`std/game/Place`: position, rotation, scale, the origin it turns about and its `alpha`); `view` is what is under it, ready-made items placed in its coordinates; `visible` hides it, with what is under it, from the draw and from presses; `paintSelf` is what it draws itself, before its view, for what no ready-made view draws (an additive glow, a line): it draws with `
|
|
147
|
+
`place` is its `Place` (`std/game/Place`: position, rotation, scale, the origin it turns about and its `alpha`); `view` is what is under it, ready-made items placed in its coordinates; `visible` hides it, with what is under it, from the draw and from presses; `paintSelf` is what it draws itself, before its view, for what no ready-made view draws (an additive glow, a line): it draws with `Gfx` through `m`, its own coordinates to the world's, at `alpha` (Orbit Runner's `Orb`, Balloon Pop's `Balloon`, whose string is a line). Its other fields are plain and public. `step` returns the actor's next version, `dt` seconds on. The ready-made views (`Group`, `Box`, `Circle`, `Sprite`, `Label`, `Overlay`) are stateless and never stepped.
|
|
148
148
|
|
|
149
149
|
```plum
|
|
150
150
|
struct Mine[Actor](at: Place, vx: Float, vy: Float, clock: Float, tex: Texture) =
|
|
@@ -158,20 +158,20 @@ struct Mine[Actor](at: Place, vx: Float, vy: Float, clock: Float, tex: Texture)
|
|
|
158
158
|
Mine(..moved, at: moved.at.rotateBy(2.0 * dt))
|
|
159
159
|
```
|
|
160
160
|
|
|
161
|
-
**Stepping.** Each tick the shown screen gets, between `held` and its tick action, the game has it step its actors: it calls the screen's `stepped(dt)` and keeps the screen it returns, unless the screen's `steps()` is false. On master the game walks the view and the hud and steps each `Actor` it finds in place; here a screen is a value, and an actor found in a view can't be put back into the field that holds it, so the screen says where its actors are, in one line that maps its fields (`
|
|
161
|
+
**Stepping.** Each tick the shown screen gets, between `held` and its tick action, the game has it step its actors: it calls the screen's `stepped(dt)` and keeps the screen it returns, unless the screen's `steps()` is false. On master the game walks the view and the hud and steps each `Actor` it finds in place; here a screen is a value, and an actor found in a view can't be put back into the field that holds it, so the screen says where its actors are, in one line that maps its fields (`Views.stepAll` steps each actor of a list, `Views.stepIn` each `Actor` of a `List<View>`, the others as they are):
|
|
162
162
|
|
|
163
163
|
```plum
|
|
164
164
|
fun steps(self) -> Bool = !self.over
|
|
165
165
|
|
|
166
166
|
fun stepped(self, dt: Float) -> PlayingScreen =
|
|
167
|
-
PlayingScreen(..self, mines:
|
|
167
|
+
PlayingScreen(..self, mines: Views.stepAll(self.mines, dt), orbs: Views.stepAll(self.orbs, dt), ship: self.ship.step(dt), rings: Views.stepAll(self.rings, dt))
|
|
168
168
|
```
|
|
169
169
|
|
|
170
170
|
That is the only line a game writes for stepping, one per screen that holds actors; the default returns the screen as it is. A plain screen (no slice) is stepped the same way; an `OnDemand` screen asleep steps nothing, as it gets no tick; a replay, a rewind and the kit's `tick` go through the same tick. An actor the screen leaves out of `stepped` is not stepped, whether it is in the view or not, and one stepped there moves whether it is visible or not. So the tick action only runs the rules on what has moved:
|
|
171
171
|
|
|
172
172
|
```plum
|
|
173
173
|
fun advance(self, dt: Float) -> PlayingScreen =
|
|
174
|
-
rings := if self.over then
|
|
174
|
+
rings := if self.over then Views.stepAll(self.rings, dt) else self.rings
|
|
175
175
|
moved := PlayingScreen(..self, clock: self.clock + dt, rings: rings.filter(|r| !r.done()))
|
|
176
176
|
ruled := if self.over then moved else moved.rules()
|
|
177
177
|
if ruled.again then PlayingScreen(..ruled, again: false).dispatch(StartOver) else ruled
|
|
@@ -187,7 +187,7 @@ What else a view is, it says with a small trait:
|
|
|
187
187
|
| `Sized` (`std/game/Sized`) | `bounds(self) -> Aabb`, the box it covers in its own coordinates. A view with bounds can be hit; one without cannot. `hit(self, p) -> Bool` is inside the bounds, edges included, unless the view says otherwise for an exact shape (`Circle` is hit inside its circle; a balloon on its body, its knot and its string). Culling goes by the bounds too |
|
|
188
188
|
| `Touch` (`std/game/Touch`) | `touch(self) -> Touchable`: whether it takes presses. `Enabled`: it is hit where it is (when it is `Sized`) and what is under it is; `Disabled`: neither it nor anything under it is hit; `ChildrenOnly`: what is under it is, it is not. A view that says nothing is `ChildrenOnly` |
|
|
189
189
|
|
|
190
|
-
The ready-made kinds take every field (struct fields have no defaults in this dialect), and each has a static that fills in the usual ones. Text and shapes are decoration (`Disabled`). A colour's alpha is the colour's: a box in a half-transparent red paints at half alpha. Each turns with its place: a box and a label in a turned group are drawn turned (`
|
|
190
|
+
The ready-made kinds take every field (struct fields have no defaults in this dialect), and each has a static that fills in the usual ones. Text and shapes are decoration (`Disabled`). A colour's alpha is the colour's: a box in a half-transparent red paints at half alpha. Each turns with its place: a box and a label in a turned group are drawn turned (`Gfx.drawRectEx`, `Gfx.drawTextEx`). Their `Box.draw`, `Circle.draw`, `Sprite.draw` and `Label.draw` paint the same shapes for your own kinds.
|
|
191
191
|
|
|
192
192
|
| Kind | Fields | Usual |
|
|
193
193
|
|---|---|---|
|
|
@@ -202,13 +202,13 @@ A kind of your own that takes children has a `children: List<View>` field and re
|
|
|
202
202
|
|
|
203
203
|
### How std draws a view
|
|
204
204
|
|
|
205
|
-
The game draws the shown screen once per frame (`Game.draw`): it clears with the screen's `background()` and draws the screen's `view()`, made for the frame, through its camera's transform (the world to the window's pixels) with `
|
|
205
|
+
The game draws the shown screen once per frame (`Game.draw`): it clears with the screen's `background()` and draws the screen's `view()`, made for the frame, through its camera's transform (the world to the window's pixels) with `Views.paint`: each visible item through its place, its `paintSelf` and then its own view, multiplying the alphas down. A screen that `culls()` is drawn with `Views.paintCulled`, which skips each `Sized` item whose bounds miss what the camera shows, with what is under it. Then the screen's `hud(window)` is drawn one unit a pixel. During a transition both screens are drawn this way, each on its own background, moved and faded. `Game.drawList()` lists what a frame draws, each item with its transform and alpha (`Drawn`), culled as the frame is, the hud's after the view's; std/game/headless reads it.
|
|
206
206
|
|
|
207
|
-
Nothing is kept between frames, and there is no tree: the game keeps the screens and their state, the camera's inputs (each screen's `size()`, `viewport()` and `camera`, the window's size) and the transition, and works out the rest when it needs it. One walk goes over a view for everything (draw, cull, list, hit test, find): an item, then its `view()`. The walks are the
|
|
207
|
+
Nothing is kept between frames, and there is no tree: the game keeps the screens and their state, the camera's inputs (each screen's `size()`, `viewport()` and `camera`, the window's size) and the transition, and works out the rest when it needs it. One walk goes over a view for everything (draw, cull, list, hit test, find): an item, then its `view()`. The walks are the statics of `Views` in `std/game/View` (`paint`, `paintCulled`, `drawnIn`, `hitIn`, `worldIn`), with `stepAll` and `stepIn` for a screen's `stepped`.
|
|
208
208
|
|
|
209
209
|
## Fixed timestep and determinism
|
|
210
210
|
|
|
211
|
-
The host adds up the time between frames and runs one tick (`Game.update`) for each whole tick, always with the same `dt` of 1/60 s. A slow frame runs several ticks (a 50 ms frame runs 3) and a fast one, on a 120 Hz screen say, may run none. A frame runs at most eight ticks: after a longer stall the rest is dropped, so the game slows down for a moment instead of falling ever further behind. The clock that does this is `Clock` in [`std/game/
|
|
211
|
+
The host adds up the time between frames and runs one tick (`Game.update`) for each whole tick, always with the same `dt` of 1/60 s. A slow frame runs several ticks (a 50 ms frame runs 3) and a fast one, on a 120 Hz screen say, may run none. A frame runs at most eight ticks: after a longer stall the rest is dropped, so the game slows down for a moment instead of falling ever further behind. The clock that does this is `Clock` in [`std/game/Input.plum`](../std/game/Input.plum).
|
|
212
212
|
|
|
213
213
|
The tick's `Input` is read from the host once per frame, before the ticks. A frame's key and pointer events go to its first tick, or to the next frame's if it ran none, so a press counts once. Later ticks of the frame see what is held, with no events. A slice gets it through its mapping (`onKey`, `held`, ..); don't read input any other way.
|
|
214
214
|
|
|
@@ -272,22 +272,22 @@ A move that can't be made (no history, a full one, a tick out of reach) leaves t
|
|
|
272
272
|
|
|
273
273
|
## What a game can use
|
|
274
274
|
|
|
275
|
-
Three
|
|
275
|
+
Three files of `std/game` talk to the host. Each host call takes `io` first.
|
|
276
276
|
|
|
277
|
-
**`std/game/
|
|
277
|
+
**`std/game/Gfx`:** drawing, statics of `Gfx` (`Gfx.clear(io, Color.Black)`), and the `Texture` handle
|
|
278
278
|
|
|
279
279
|
| Group | Functions |
|
|
280
280
|
|---|---|
|
|
281
281
|
| Screen | `screenWidth(io)`, `screenHeight(io)`, `frameTime(io)`, `clear(io, color)` |
|
|
282
282
|
| Shapes | `drawRect(io, x, y, w, h, c)`, `drawRectEx(io, x, y, w, h, rotation, c)` (turned clockwise about its centre), `drawCircle(io, x, y, r, c)`, `drawLine(io, x1, y1, x2, y2, thickness, c)` |
|
|
283
283
|
| Text | `drawText(io, s, x, y, size, c)`, `drawTextEx(io, s, x, y, size, rotation, c)` (turned clockwise about its top-left) |
|
|
284
|
-
| Images | `
|
|
284
|
+
| Images | `Texture.load(io, name)` gives a `Texture` with `draw(io, x, y)`, `width(io)`, `height(io)`, `setSmooth(io, smooth)`; `drawTextureEx(io, t.id, sx, sy, sw, sh, x, y, w, h, rotation, flipX, tint)` |
|
|
285
285
|
| Camera | `setCamera(io, x, y, scale)`, `resetCamera(io)` |
|
|
286
|
-
| Draw into an image | `
|
|
286
|
+
| Draw into an image | `Texture.create(io, w, h)`, `t.renderTarget(io)`, `renderToScreen(io)` |
|
|
287
287
|
| Blending | `blendMode(io, m)` with `Blend.Normal`, `Blend.Add`, `Blend.Multiply`, `Blend.Screen` |
|
|
288
288
|
| Files | `loadFile(io, name)`, the file's text; `fileExists(io, name)` |
|
|
289
289
|
|
|
290
|
-
**`std/game/
|
|
290
|
+
**`std/game/Input`:** keyboard, mouse and touch. A tick's input is an `Input`, a value: a slice's `held(input)` gets it, and its `onKey` and `onPointer` get its events one at a time.
|
|
291
291
|
|
|
292
292
|
| Method | What it gives you |
|
|
293
293
|
|---|---|
|
|
@@ -297,7 +297,7 @@ Three modules talk to the host, files of `std/game`. Each function takes `io` fi
|
|
|
297
297
|
| `pos`, `wheel` | where the pointer is, in window pixels, and how far the wheel turned |
|
|
298
298
|
| `events` | this tick's `InputEvent`s: `PointerDown`, `PointerUp`, `PointerMove` (the left button or a finger), `ButtonDown(button, x, y)`, `ButtonUp(..)` (the right and middle buttons), `KeyDown`, `KeyUp` |
|
|
299
299
|
|
|
300
|
-
**`std/game/
|
|
300
|
+
**`std/game/Audio`:** `Sound.load(io, name)` gives a `Sound` with `play(io, volume)`, `loop(io, volume)`, `stop(io)` and `setVolume(io, volume)`.
|
|
301
301
|
|
|
302
302
|
A missing file stops the game and names the file, so typos show up straight away. Load an asset once, in `main`. The example games keep theirs in a typed `Assets` struct of their own (`Assets.load(io)` in `main`) and pass it to the screens that draw it and play it. For a test with no host, `Assets.none()` fills it with `Texture.none()` and `Sound.none()`, placeholders that draw and play nothing.
|
|
303
303
|
|
|
@@ -407,14 +407,14 @@ Put your rules in code that takes no `Io`, test them from a `primitive XTest[Uni
|
|
|
407
407
|
# tests/examples/games/orbitrunner/screens/PlayingScreen.plum
|
|
408
408
|
primitive PlayingScreenTest[UnitTest] =
|
|
409
409
|
fun thePlayScoresAndStartsOver() =
|
|
410
|
-
t :=
|
|
410
|
+
t := TestRig.mount(io, PlayingScreen.start(Random.seeded(7), Assets.none()))
|
|
411
411
|
u := t.hold(Key.Right).ticks(30)
|
|
412
412
|
assert u.text(u.first<Label>()) == "Score {u.screen.score}"
|
|
413
413
|
v := u.press(Key.Enter).tick()
|
|
414
414
|
assert v.count<Overlay>() == 0
|
|
415
415
|
```
|
|
416
416
|
|
|
417
|
-
`
|
|
417
|
+
`TestRig.mount(io, screen)` makes a game of that one screen; `TestRig.play(io, game)` takes a whole game. Either sizes the window to 800 by 600 pixels (`t.resize(w, h)` changes it). The rig, a `TestRig<S>`, is generic over the screen's type, so `t.screen` is the screen itself as the last tick left it (for `play`, the first screen, a `Screen`).
|
|
418
418
|
|
|
419
419
|
| | |
|
|
420
420
|
|---|---|
|
|
@@ -424,7 +424,7 @@ primitive PlayingScreenTest[UnitTest] =
|
|
|
424
424
|
| 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()`) |
|
|
425
425
|
| What came out of the last `tick` or `ticks` | `effects<A>()` (the actions of type `A` whose effects ran, in order), `wentTo()` (an `Option`), `exited()`; `showing()` names the screen shown |
|
|
426
426
|
| A whole run | `record(seed)`, `recorded()`, `replay(r)`, and `game` (its counters) |
|
|
427
|
-
| Time travel | `mountWith(io, screen)` and `playWith(io, game)` keep the history; then `rewindTo(k)`, `undo()`, `redo()`, `stepBack()`, `stepForward()`, `pause()`, `resume()`, with `moved()`, and `t.screen` is the one made again |
|
|
427
|
+
| 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 |
|
|
428
428
|
|
|
429
429
|
Orbit Runner's game over test:
|
|
430
430
|
|
|
@@ -451,15 +451,15 @@ The engine has master's shape at e91c40ce (`View` and `Actor`, `Screen` with its
|
|
|
451
451
|
| Master | Here | Why |
|
|
452
452
|
|---|---|---|
|
|
453
453
|
| `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 |
|
|
454
|
-
| the game walks the view and the hud and steps every `Actor` it finds, nested ones included (`
|
|
454
|
+
| the game walks the view and the hud and steps every `Actor` it finds, nested ones included (`Views.stepIn` from `Delivery.stepScreen`) | the game calls the screen's `stepped(dt) -> Self` (default: the screen as it is), which maps the fields that hold actors (`Views.stepAll`, `Views.stepIn`); `steps()` pauses it as on master | an actor found in a view can't be put back into the field of the value that holds it, so the screen says where its actors are, one line per screen. An actor held under another is stepped by its holder's `step`. The walk also makes no view to look under an actor, so `benchmarks/screen_views/scale.plum`'s `update view` makes 10,000 fewer one-`Sprite` lists a tick than master's |
|
|
455
455
|
| `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 |
|
|
456
456
|
| `fun reduce(&self, a: A)`, `fun dispatch(&self, a: A)` | `fun reduce(self, a: A) -> Self`, `fun dispatch(self, a: A) -> Self` | the reducer returns the next screen |
|
|
457
457
|
| `onKey(..) -> A?` and the other mappings | `-> Option<A>` | no `T?` in this dialect |
|
|
458
458
|
| `deliver(&self, d: Delivery)`, the delivery noting `acted` | `deliver(self, d: Delivery) -> Delivered` (the next screen, the tick's effects, whether it was a user action) | the delivery returns what it made |
|
|
459
|
-
| `Effects`, one queue of `Queued<A>` holding each action with its slice, set in one slot around a delivery | one slot too (`
|
|
459
|
+
| `Effects`, one queue of `Queued<A>` holding each action with its slice, set in one slot around a delivery | one slot too (`Effects.startDispatch`), holding the delivery's queue of its own action type; after the tick each action is held with the slice as the tick left it (`Queued<A>`), and `Effects.sent<A>(list)` reads them | an effect must see the slice the tick left, which is a new value, not the one the action was reduced into |
|
|
460
460
|
| `Viewport` updated in place (`v.update(w, h)`), `lookAt(x, y, zoom = 1.0)` | `Viewport.of(..).update(w, h) -> Viewport`, the same `lookAt` and `moved` | a value |
|
|
461
461
|
| the frame sets the host's camera (`setCamera`) for a camera with one scale, and puts a `Stretch` camera into the transform | every camera goes into the transform the view is drawn through, in window pixels, and culling maps its area through it | this branch's draw has always gone through the transform; the host sees no `setCamera`/`resetCamera` in a frame, so the recorder's calls differ (`compiler/game/tests/host_test.rs` checks the pixels) |
|
|
462
|
-
| the window's size on each tick's input (`input.screen`, `Input.sized`) | `Game.update(io, input, dt)` reads it from `
|
|
462
|
+
| the window's size on each tick's input (`input.screen`, `Input.sized`) | `Game.update(io, input, dt)` reads it from `Gfx`; `Game.step(io, input, dt, pixels)` takes it (a test's) | not ported: the replay records the size in its header, as before |
|
|
463
463
|
| the fixed-timestep driver is `Ticker` (`Ticker.Tick`, `game.ticker.ticks`) | `Clock` (`Clock.Tick`), `game.ticks`, the rig's `t.frame(..)` | named before master's rename; not ported |
|
|
464
464
|
| actor and view fields with defaults (`Box(w:, h:)`, `Held(buttons: [])`) | every field given, or a static (`Box.of`, `Label.of(.., align = Left)`, `Sprite.cell`, `Overlay.of`, `Tween.of`, `Held.keysOnly`) | no struct field defaults in this dialect |
|
|
465
465
|
| `Tween` advanced in place | `tween.advance(dt) -> Tween` | a value |
|
|
@@ -468,11 +468,11 @@ The engine has master's shape at e91c40ce (`View` and `Actor`, `Screen` with its
|
|
|
468
468
|
| `timeTravel(make: fn(Io) -> Game, cap)` | `timeTravel(cap) -> Game` | the game keeps the screens it was made with |
|
|
469
469
|
| `rewindTo`, `undo`, `redo`, .. `-> Bool` | `-> Game`, with `game.moved` | the move returns the moved game |
|
|
470
470
|
| the kit's `t.press(k)` (in place) | `u := t.press(k)` | a rig is a value |
|
|
471
|
-
| a view literal's `if`, `for`, `..` elements | `append`, `map`, functions, `
|
|
471
|
+
| a view literal's `if`, `for`, `..` elements | `append`, `map`, functions, `Views.all` | not in this dialect's list literals |
|
|
472
472
|
| std's test helper types private (`_ViewProbe`, `_KitScreen`, ..) | public names (`ViewProbe`, `Stepper`, ..) | a type's name here is the module's: a game's own `Stepper` beside std's compiles, so there is no clash to avoid (checked with a probe) |
|
|
473
473
|
| `std/game` without `Anim` | `std/game/Anim` kept (pure animations of time) | a web-test fixture uses it; no example game does |
|
|
474
474
|
|
|
475
|
-
The same on both: `View` (`place`, `view`, `visible`, `paintSelf`, the shared empty default), the walks (`
|
|
475
|
+
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)`, `steps`, `camera(base)`, `size`, `viewport`, `culls`, `busy`, `background`, `transition`, `frames`; no `enter`, `leave`, `resize`, `draw` or `fresh`), `Control` (`goTo`, `exit`, `undo`, `redo`, `rewindTo`; no `restart`), pointer events dropped during a transition, each screen on its own background in one, the ready-made views (Circle by its centre, `Label(align:)`, `Overlay` with a required size hit inside its box, `Sprite.frameAt`), `Gfx.drawRectEx` and `drawTextEx` in std, the browser, the native host and the recorder, the input (`ButtonDown`, `ButtonUp`, `Held.buttons`, the extra `Key`s, `Input.axis`, `isMouseDown(b)`, `isMousePressed(b)`, `isMouseReleased(b)`, the replay's button mask and `B`/`b` events), `Math.near`, `Game.pixels`, `frameDrawn()`, `drawList()`, `camera(i)`, `toWorld`, `toScreen`, `visible(i)`, `travelAsked`, and Orbit Runner's state checksum (925878360).
|
|
476
476
|
|
|
477
477
|
Not ported: master's frames-on-demand host call (`frameIdle`), its development keys (`plum run --dev`, `remakeGame`, `TravelKeys`), and its `clockwork` and `menu` games; see the [backlog](backlog.md). This branch keeps `examples/games/clicks.plum` and `two_screens.plum`, which master doesn't have.
|
|
478
478
|
|
docs/language.md
CHANGED
|
@@ -818,7 +818,7 @@ fun main() -> Int =
|
|
|
818
818
|
|
|
819
819
|
This means two modules can each have a `clamp` without clashing, while two modules that define the same type name are an error. The program's `main` is the entry file's; the `main` of an imported file (a benchmark driver importing a game's main file) is an ordinary function of that module. A module's own functions and constants need no prefix inside it, and a local with the same name as a module hides the module. `shapes.clamp` without a call is the function as a value, like a bare `clamp` inside `shapes`.
|
|
820
820
|
|
|
821
|
-
Module-level constants can be any pure expression, and they may refer to each other in any order. The same goes for std's game modules: `import std/game/
|
|
821
|
+
Module-level constants can be any pure expression, and they may refer to each other in any order. The same goes for std's game modules: `import std/game/Gfx` gives `Gfx.drawRect(io, ..)`. The rest of std has no module-level functions: `import std/math/Math` gives the primitive `Math`, and `Math.sqrt(2.0)`.
|
|
822
822
|
|
|
823
823
|
A module-level `extern fun` binds to a function the host provides. It takes `io: Io` first, because it reaches outside the program:
|
|
824
824
|
|
docs/std.md
CHANGED
|
@@ -263,7 +263,7 @@ import std/Int
|
|
|
263
263
|
import std/Str
|
|
264
264
|
import std/Option
|
|
265
265
|
import std/collections/List
|
|
266
|
-
import std/game/
|
|
266
|
+
import std/game/Input
|
|
267
267
|
import std/game/Screen
|
|
268
268
|
import std/game/ScreenSlice
|
|
269
269
|
|
examples/games/balloonpop/BalloonPop.plum
CHANGED
|
@@ -6,7 +6,7 @@ import std/Str
|
|
|
6
6
|
import std/Io
|
|
7
7
|
import std/collections/List
|
|
8
8
|
import std/game/Color
|
|
9
|
-
import std/game/
|
|
9
|
+
import std/game/Audio
|
|
10
10
|
import std/game/Screen
|
|
11
11
|
import std/game/History
|
|
12
12
|
import std/game/Game
|
|
@@ -37,7 +37,7 @@ struct Hue(name: Str, color: Color) =
|
|
|
37
37
|
fun count() -> Int = 7
|
|
38
38
|
|
|
39
39
|
struct Assets(pop: Sound, party: Sound, music: Sound) =
|
|
40
|
-
fun load(io: Io) -> Assets = Assets(pop:
|
|
40
|
+
fun load(io: Io) -> Assets = Assets(pop: Sound.load(io, "pickup.wav"), party: Sound.load(io, "gameover.wav"), music: Sound.load(io, "music.wav"))
|
|
41
41
|
|
|
42
42
|
fun none() -> Assets = Assets(pop: Sound.none(), party: Sound.none(), music: Sound.none())
|
|
43
43
|
|
examples/games/balloonpop/actors/Balloon.plum
CHANGED
|
@@ -12,7 +12,7 @@ import std/math/Random
|
|
|
12
12
|
import std/math/Vec2
|
|
13
13
|
import std/math/Math
|
|
14
14
|
import std/game/Color
|
|
15
|
-
import std/game/
|
|
15
|
+
import std/game/Gfx
|
|
16
16
|
import std/game/Place
|
|
17
17
|
import std/game/View
|
|
18
18
|
import std/game/Circle
|
|
@@ -47,7 +47,7 @@ struct Balloon[Actor, Sized, Touch](id: Int, x: Float, y: Float, speed: Float, h
|
|
|
47
47
|
speed := Balloon.MinSpeed + rs.value * {Balloon.MaxSpeed - Balloon.MinSpeed}
|
|
48
48
|
Spawned(balloon: Balloon.at(id, x, Arena.H + Balloon.Radius + depth, speed, rh.value, rx.value * Float.Tau, clock), rng: rh.rng)
|
|
49
49
|
|
|
50
|
-
fun place(self) -> Place =
|
|
50
|
+
fun place(self) -> Place = Place.at(self.drawnX(), self.y)
|
|
51
51
|
|
|
52
52
|
fun pop(self) -> Balloon = Balloon(..self, x: self.drawnX(), poppedAt: self.clock)
|
|
53
53
|
|
|
@@ -94,7 +94,7 @@ struct Balloon[Actor, Sized, Touch](id: Int, x: Float, y: Float, speed: Float, h
|
|
|
94
94
|
r := Balloon.Radius
|
|
95
95
|
a := m.apply(Vec2(x: 0.0, y: r))
|
|
96
96
|
b := m.apply(Vec2(x: self.stringX(), y: r * 2.6))
|
|
97
|
-
|
|
97
|
+
Gfx.drawLine(io, a.x, a.y, b.x, b.y, 2.0 * m.scaleX(), Color.rgb(0.35, 0.35, 0.35).withAlpha(alpha))
|
|
98
98
|
|
|
99
99
|
fun burst(i: Int, reach: Float, color: Color) -> List<View> =
|
|
100
100
|
if i >= 8
|
examples/games/balloonpop/screens/PlaygroundScreen.plum
CHANGED
|
@@ -12,7 +12,7 @@ import std/math/Random
|
|
|
12
12
|
import std/math/Vec2
|
|
13
13
|
import std/math/Math
|
|
14
14
|
import std/game/Color
|
|
15
|
-
import std/game/
|
|
15
|
+
import std/game/Input
|
|
16
16
|
import std/game/Replay
|
|
17
17
|
import std/game/View
|
|
18
18
|
import std/game/Box
|
|
@@ -67,14 +67,14 @@ struct PlaygroundScreen[ScreenSlice<PopAction>](
|
|
|
67
67
|
|
|
68
68
|
fun background(self) -> Color = Color.rgb(0.55, 0.8, 1.0)
|
|
69
69
|
|
|
70
|
-
fun view(self) -> List<View> = sky(self.clock).append(
|
|
70
|
+
fun view(self) -> List<View> = sky(self.clock).append(Views.all(self.balloons)).append(self.party())
|
|
71
71
|
|
|
72
72
|
fun hud(self, window: Vec2) -> List<View> = [
|
|
73
73
|
Box.of(16.0, 14.0, 170.0, 56.0, Color.White.withAlpha(0.7)),
|
|
74
74
|
Circle.of(46.0, 42.0, 18.0, Hue.of(0).color),
|
|
75
75
|
Label.of(80.0, 20.0, "{self.pops}", 44.0, Color.rgb(0.15, 0.15, 0.3))]
|
|
76
76
|
|
|
77
|
-
fun stepped(self, dt: Float) -> PlaygroundScreen = PlaygroundScreen(..self, balloons:
|
|
77
|
+
fun stepped(self, dt: Float) -> PlaygroundScreen = PlaygroundScreen(..self, balloons: Views.stepAll(self.balloons, dt))
|
|
78
78
|
|
|
79
79
|
fun onPress(self, target: View, at: Vec2) -> Option<PopAction> = match target
|
|
80
80
|
Balloon as b => Some(Pop(balloon: b))
|
|
@@ -162,9 +162,9 @@ fun cloud(x: Float, y: Float) -> List<View> =
|
|
|
162
162
|
white := Color.White.withAlpha(0.9)
|
|
163
163
|
[Circle.of(x - 30.0, y + 8.0, 24.0, white), Circle.of(x, y, 32.0, white), Circle.of(x + 32.0, y + 8.0, 24.0, white)]
|
|
164
164
|
|
|
165
|
-
fun field(io: Io, seed: Int) -> TestRig<PlaygroundScreen> =
|
|
165
|
+
fun field(io: Io, seed: Int) -> TestRig<PlaygroundScreen> = TestRig.mount(io, PlaygroundScreen.seeded(seed, Assets.none()))
|
|
166
166
|
|
|
167
|
-
fun travelling(io: Io, seed: Int) -> TestRig<PlaygroundScreen> =
|
|
167
|
+
fun travelling(io: Io, seed: Int) -> TestRig<PlaygroundScreen> = TestRig.mountWith(io, PlaygroundScreen.seeded(seed, Assets.none()))
|
|
168
168
|
|
|
169
169
|
fun bursting(p: PlaygroundScreen) -> Int = p.balloons.filter(|b| b.popped()).length()
|
|
170
170
|
|
examples/games/clicks.plum
CHANGED
|
@@ -8,7 +8,7 @@ import std/Io
|
|
|
8
8
|
import std/collections/List
|
|
9
9
|
import std/math/Vec2
|
|
10
10
|
import std/game/Color
|
|
11
|
-
import std/game/
|
|
11
|
+
import std/game/Input
|
|
12
12
|
import std/game/Place
|
|
13
13
|
import std/game/View
|
|
14
14
|
import std/game/Box
|
|
@@ -23,7 +23,7 @@ import std/game/headless
|
|
|
23
23
|
enum TapAction = Tapped | Push(dx: Float) | Quit
|
|
24
24
|
|
|
25
25
|
struct Ball[Actor](x: Float, y: Float, vx: Float) =
|
|
26
|
-
fun place(self) -> Place =
|
|
26
|
+
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
27
27
|
|
|
28
28
|
fun view(self) -> List<View> = [Circle.of(0.0, 0.0, 10.0, Color.Yellow)]
|
|
29
29
|
|
examples/games/orbitrunner/OrbitRunner.plum
CHANGED
|
@@ -8,8 +8,8 @@ import std/Str
|
|
|
8
8
|
import std/collections/List
|
|
9
9
|
import std/math/Random
|
|
10
10
|
import std/math/Math
|
|
11
|
-
import std/game/
|
|
11
|
+
import std/game/Gfx
|
|
12
|
-
import std/game/
|
|
12
|
+
import std/game/Audio
|
|
13
13
|
import std/game/Screen
|
|
14
14
|
import std/game/Game
|
|
15
15
|
import screens/PlayingScreen
|
|
@@ -60,8 +60,8 @@ fun musicVolume() -> Float = 0.3
|
|
|
60
60
|
struct Assets(sheet: Texture, pickup: Sound, hit: Sound, over: Sound, music: Sound) =
|
|
61
61
|
fun load(io: Io) -> Assets =
|
|
62
62
|
Assets(
|
|
63
|
-
sheet:
|
|
63
|
+
sheet: Texture.load(io, "sprites.png"), pickup: Sound.load(io, "pickup.wav"), hit: Sound.load(io, "hit.wav"),
|
|
64
|
-
over:
|
|
64
|
+
over: Sound.load(io, "gameover.wav"), music: Sound.load(io, "music.wav"))
|
|
65
65
|
|
|
66
66
|
fun none() -> Assets = Assets(sheet: Texture.none(), pickup: Sound.none(), hit: Sound.none(), over: Sound.none(), music: Sound.none())
|
|
67
67
|
|
examples/games/orbitrunner/actors/Mine.plum
CHANGED
|
@@ -13,7 +13,7 @@ struct Mine[Actor](at: Place, vx: Float, vy: Float, clock: Float, tex: Texture)
|
|
|
13
13
|
Radius = 11.0
|
|
14
14
|
|
|
15
15
|
fun create(start: Motion, tex: Texture) -> Mine =
|
|
16
|
-
Mine(at:
|
|
16
|
+
Mine(at: Place.pivoted(start.x, start.y, Sheet.Size / 2.0, Sheet.Size / 2.0), vx: start.vx, vy: start.vy, clock: 0.0, tex: tex)
|
|
17
17
|
|
|
18
18
|
fun countFor(score: Int) -> Int = {3 + score / 50}.min(8)
|
|
19
19
|
|
examples/games/orbitrunner/actors/Orb.plum
CHANGED
|
@@ -8,7 +8,7 @@ import std/collections/List
|
|
|
8
8
|
import std/math/Vec2
|
|
9
9
|
import std/math/Affine
|
|
10
10
|
import std/game/Color
|
|
11
|
-
import std/game/
|
|
11
|
+
import std/game/Gfx
|
|
12
12
|
import std/game/Place
|
|
13
13
|
import std/game/View
|
|
14
14
|
import std/game/Sprite
|
|
@@ -19,7 +19,7 @@ struct Orb[Actor](at: Place, vx: Float, vy: Float, phase: Float, clock: Float, t
|
|
|
19
19
|
Points = 10
|
|
20
20
|
|
|
21
21
|
fun create(start: Motion, phase: Float, tex: Texture) -> Orb =
|
|
22
|
-
Orb(at:
|
|
22
|
+
Orb(at: Place.pivoted(start.x, start.y, Sheet.Size / 2.0, Sheet.Size / 2.0), vx: start.vx, vy: start.vy, phase: phase, clock: 0.0, tex: tex)
|
|
23
23
|
|
|
24
24
|
fun place(self) -> Place = self.at
|
|
25
25
|
|
|
@@ -35,7 +35,7 @@ struct Orb[Actor](at: Place, vx: Float, vy: Float, phase: Float, clock: Float, t
|
|
|
35
35
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) =
|
|
36
36
|
c := m.apply(Vec2(x: Sheet.Size / 2.0, y: Sheet.Size / 2.0))
|
|
37
37
|
k := m.scaleX()
|
|
38
|
-
|
|
38
|
+
Gfx.blendMode(io, Blend.Add)
|
|
39
|
-
|
|
39
|
+
Gfx.drawCircle(io, c.x, c.y, 26.0 * k, Color(r: 0.35, g: 0.22, b: 0.0, a: 0.5 * alpha))
|
|
40
|
-
|
|
40
|
+
Gfx.drawCircle(io, c.x, c.y, 17.0 * k, Color(r: 0.6, g: 0.4, b: 0.05, a: 0.5 * alpha))
|
|
41
|
-
|
|
41
|
+
Gfx.blendMode(io, Blend.Normal)
|
examples/games/orbitrunner/actors/Ring.plum
CHANGED
|
@@ -15,7 +15,7 @@ struct Ring[Actor](x: Float, y: Float, age: Float) =
|
|
|
15
15
|
|
|
16
16
|
fun at(x: Float, y: Float) -> Ring = Ring(x: x, y: y, age: 0.0)
|
|
17
17
|
|
|
18
|
-
fun place(self) -> Place =
|
|
18
|
+
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
19
19
|
|
|
20
20
|
fun view(self) -> List<View> =
|
|
21
21
|
k := self.age / Ring.Secs
|
examples/games/orbitrunner/actors/Ship.plum
CHANGED
|
@@ -21,7 +21,7 @@ struct Ship[Actor](tex: Texture, at: Place, vx: Float, vy: Float, ax: Float, ay:
|
|
|
21
21
|
fun create(tex: Texture) -> Ship = Ship.startAt(tex, Arena.W / 2.0, Arena.H / 2.0)
|
|
22
22
|
|
|
23
23
|
fun startAt(tex: Texture, x: Float, y: Float) -> Ship =
|
|
24
|
-
Ship(tex: tex, at:
|
|
24
|
+
Ship(tex: tex, at: Place.pivoted(x, y, Sheet.Size / 2.0, Sheet.Size / 2.0), vx: 0.0, vy: 0.0, ax: 0.0, ay: 0.0, hurt: 0.0, clock: 0.0)
|
|
25
25
|
|
|
26
26
|
fun place(self) -> Place = self.at
|
|
27
27
|
|
examples/games/orbitrunner/actors/Star.plum
CHANGED
|
@@ -12,7 +12,7 @@ import std/game/Circle
|
|
|
12
12
|
struct Star[View](index: Int, clock: Float) =
|
|
13
13
|
fun r(self) -> Float = 1.0 + Float(self.index % 3) * 0.5
|
|
14
14
|
|
|
15
|
-
fun place(self) -> Place =
|
|
15
|
+
fun place(self) -> Place = Place.at(Float({self.index * 7919} % 800), Float({self.index * 104729} % 600))
|
|
16
16
|
|
|
17
17
|
fun view(self) -> List<View> = [Circle.of(0.0, 0.0, self.r(), Color(r: 0.7, g: 0.8, b: 1.0, a: self.twinkle()))]
|
|
18
18
|
|
examples/games/orbitrunner/screens/PlayingScreen.plum
CHANGED
|
@@ -12,7 +12,7 @@ import std/math/Vec2
|
|
|
12
12
|
import std/math/Random
|
|
13
13
|
import std/math/Math
|
|
14
14
|
import std/game/Color
|
|
15
|
-
import std/game/
|
|
15
|
+
import std/game/Input
|
|
16
16
|
import std/game/Replay
|
|
17
17
|
import std/game/View
|
|
18
18
|
import std/game/Box
|
|
@@ -70,14 +70,14 @@ struct PlayingScreen[ScreenSlice<PlayAction>](
|
|
|
70
70
|
fun steps(self) -> Bool = !self.over
|
|
71
71
|
|
|
72
72
|
fun stepped(self, dt: Float) -> PlayingScreen =
|
|
73
|
-
PlayingScreen(..self, mines:
|
|
73
|
+
PlayingScreen(..self, mines: Views.stepAll(self.mines, dt), orbs: Views.stepAll(self.orbs, dt), ship: self.ship.step(dt), rings: Views.stepAll(self.rings, dt))
|
|
74
74
|
|
|
75
75
|
fun view(self) -> List<View> =
|
|
76
76
|
back: List<View> := [Box.of(0.0, 0.0, Arena.W, Arena.H, Color.rgb(0.07, 0.08, 0.16))]
|
|
77
|
-
mines :=
|
|
77
|
+
mines := Views.all(self.mines)
|
|
78
|
-
orbs :=
|
|
78
|
+
orbs := Views.all(self.orbs)
|
|
79
79
|
ship: List<View> := if self.over then Nil else [self.ship]
|
|
80
|
-
rings :=
|
|
80
|
+
rings := Views.all(self.rings)
|
|
81
81
|
hud: List<View> := [
|
|
82
82
|
Label.of(16.0, 12.0, "Score {self.score}", 28.0, Color.White),
|
|
83
83
|
Label.of(16.0, 44.0, "Best {self.best.max(self.score)}", 20.0, Color.Yellow)]
|
|
@@ -129,7 +129,7 @@ struct PlayingScreen[ScreenSlice<PlayAction>](
|
|
|
129
129
|
PlayingScreen(..self, best: self.best.max(self.score), score: 0, lives: PlayingScreen.StartLives, over: false).deal()
|
|
130
130
|
|
|
131
131
|
fun advance(self, dt: Float) -> PlayingScreen =
|
|
132
|
-
rings := if self.over then
|
|
132
|
+
rings := if self.over then Views.stepAll(self.rings, dt) else self.rings
|
|
133
133
|
moved := PlayingScreen(..self, clock: self.clock + dt, rings: rings.filter(|r| !r.done()))
|
|
134
134
|
ruled := if self.over then moved else moved.rules()
|
|
135
135
|
if ruled.again then PlayingScreen(..ruled, again: false).dispatch(StartOver) else ruled
|
|
@@ -195,12 +195,12 @@ fun ruled(out: List<PlayAction>) -> List<PlayAction> = out.filter(|a| match a
|
|
|
195
195
|
fun names(out: List<PlayAction>) -> Str = out.fold("", |s, a| if s == "" then a.toStr() else "{s} {a.toStr()}")
|
|
196
196
|
|
|
197
197
|
fun mounted(io: Io, seed: Int, score: Int, lives: Int) -> TestRig<PlayingScreen> =
|
|
198
|
-
|
|
198
|
+
TestRig.mount(io, PlayingScreen.at(Random.seeded(seed), Assets.none(), score, lives))
|
|
199
199
|
|
|
200
200
|
fun cleared(s: PlayingScreen) -> PlayingScreen = PlayingScreen(..s, orbs: s.orbs.map(|o| o.put(away())), mines: s.mines.map(|m| m.put(away())))
|
|
201
201
|
|
|
202
202
|
fun emptied(io: Io, score: Int, lives: Int) -> TestRig<PlayingScreen> =
|
|
203
|
-
|
|
203
|
+
TestRig.mount(io, cleared(PlayingScreen.at(Random.seeded(7), Assets.none(), score, lives)))
|
|
204
204
|
|
|
205
205
|
fun poked(t: TestRig<PlayingScreen>, s: PlayingScreen) -> TestRig<PlayingScreen> =
|
|
206
206
|
only: Screen := s
|
examples/games/two_screens.plum
CHANGED
|
@@ -7,7 +7,7 @@ import std/Option
|
|
|
7
7
|
import std/Io
|
|
8
8
|
import std/collections/List
|
|
9
9
|
import std/game/Color
|
|
10
|
-
import std/game/
|
|
10
|
+
import std/game/Input
|
|
11
11
|
import std/game/View
|
|
12
12
|
import std/game/Label
|
|
13
13
|
import std/game/Control
|
std/Io.plum
CHANGED
|
@@ -8,8 +8,8 @@ import std/Slice
|
|
|
8
8
|
# The host capability. Every call that leaves the program (drawing, input, sound,
|
|
9
9
|
# files, environment, clock, randomness, network) takes an `Io`. `Io` is a built-in
|
|
10
10
|
# primitive with no fields. The general host calls are the `extern fun` methods below;
|
|
11
|
-
# the game host's calls are module functions that take `io` first: `std/game/
|
|
11
|
+
# the game host's calls are module functions that take `io` first: `std/game/Gfx`,
|
|
12
|
-
# `std/game/
|
|
12
|
+
# `std/game/Input` and `std/game/Audio`. The host entry points pass the one `Io` value,
|
|
13
13
|
# so a function with no `Io` reachable cannot touch the host: that is what "pure" means.
|
|
14
14
|
#
|
|
15
15
|
# `Int` in an extern signature is narrowed to i32 at the host boundary, so wall-clock
|
std/game/Anim.plum
CHANGED
|
@@ -4,7 +4,7 @@ import std/Int
|
|
|
4
4
|
import std/Float
|
|
5
5
|
import std/collections/List
|
|
6
6
|
|
|
7
|
-
# Animations as pure functions of time. An `Anim` is plain data; `
|
|
7
|
+
# Animations as pure functions of time. An `Anim` is plain data; `a.at(t)`
|
|
8
8
|
# is the pose `t` seconds after it started, so a view can animate with nothing
|
|
9
9
|
# but a clock in its state. Before the start (t < 0) the pose is the identity;
|
|
10
10
|
# past the end it clamps to the final pose. A zero-length step applies fully at
|
|
@@ -14,7 +14,13 @@ import std/collections/List
|
|
|
14
14
|
# gives the identity pose.
|
|
15
15
|
|
|
16
16
|
# The animated part of a transform: an offset, a rotation and a scale.
|
|
17
|
-
struct AnimPose(dx: Float, dy: Float, rotation: Float, scaleX: Float, scaleY: Float)
|
|
17
|
+
struct AnimPose(dx: Float, dy: Float, rotation: Float, scaleX: Float, scaleY: Float) =
|
|
18
|
+
fun identity() -> AnimPose =
|
|
19
|
+
AnimPose(dx: 0.0, dy: 0.0, rotation: 0.0, scaleX: 1.0, scaleY: 1.0)
|
|
20
|
+
|
|
21
|
+
# Offsets add, rotations add, scales multiply.
|
|
22
|
+
fun combine(self, q: AnimPose) -> AnimPose =
|
|
23
|
+
AnimPose(dx: self.dx + q.dx, dy: self.dy + q.dy, rotation: self.rotation + q.rotation, scaleX: self.scaleX * q.scaleX, scaleY: self.scaleY * q.scaleY)
|
|
18
24
|
|
|
19
25
|
enum Anim =
|
|
20
26
|
| MoveBy(dx: Float, dy: Float, secs: Float)
|
|
@@ -26,91 +32,79 @@ enum Anim =
|
|
|
26
32
|
| Forever(anim: Anim)
|
|
27
33
|
|
|
28
34
|
# The pose `t` seconds into the animation.
|
|
29
|
-
fun at(
|
|
35
|
+
fun at(self, t: Float) -> AnimPose =
|
|
30
|
-
|
|
36
|
+
if t < 0.0
|
|
37
|
+
AnimPose.identity()
|
|
38
|
+
else
|
|
39
|
+
match self
|
|
40
|
+
MoveBy(dx, dy, secs) =>
|
|
41
|
+
p := Anim.progress(secs, t)
|
|
42
|
+
AnimPose(dx: dx * p, dy: dy * p, rotation: 0.0, scaleX: 1.0, scaleY: 1.0)
|
|
43
|
+
RotateBy(radians, secs) =>
|
|
44
|
+
AnimPose(dx: 0.0, dy: 0.0, rotation: radians * Anim.progress(secs, t), scaleX: 1.0, scaleY: 1.0)
|
|
45
|
+
ScaleTo(sx, sy, secs) =>
|
|
46
|
+
p := Anim.progress(secs, t)
|
|
47
|
+
AnimPose(dx: 0.0, dy: 0.0, rotation: 0.0, scaleX: 1.0 + {sx - 1.0} * p, scaleY: 1.0 + {sy - 1.0} * p)
|
|
48
|
+
Delay(_) => AnimPose.identity()
|
|
49
|
+
Seq(steps) => Anim.seqAt(steps, t, AnimPose.identity())
|
|
50
|
+
Par(steps) => Anim.parAt(steps, t, AnimPose.identity())
|
|
51
|
+
Forever(inner) => inner.foreverAt(t)
|
|
52
|
+
|
|
31
|
-
# Duration in seconds. `Forever` has no end: it reports
|
|
53
|
+
# Duration in seconds. `Forever` has no end: it reports `Anim.endless()`, a large
|
|
32
54
|
# constant (Float has no Infinity constant).
|
|
33
|
-
fun length(
|
|
55
|
+
fun length(self) -> Float = match self
|
|
34
|
-
|
|
56
|
+
MoveBy(_, _, secs) => Anim.max0(secs)
|
|
57
|
+
RotateBy(_, secs) => Anim.max0(secs)
|
|
58
|
+
ScaleTo(_, _, secs) => Anim.max0(secs)
|
|
59
|
+
Delay(secs) => Anim.max0(secs)
|
|
60
|
+
Seq(steps) => Anim.seqLength(steps, 0.0)
|
|
61
|
+
Par(steps) => Anim.parLength(steps, 0.0)
|
|
62
|
+
Forever(_) => Anim.endless()
|
|
63
|
+
|
|
35
|
-
# Stands in for an infinite length.
|
|
64
|
+
# Stands in for an infinite length.
|
|
36
|
-
fun
|
|
65
|
+
fun endless() -> Float = 1e30
|
|
37
|
-
|
|
38
|
-
|
|
66
|
+
|
|
39
|
-
AnimPose(dx: 0.0, dy: 0.0, rotation: 0.0, scaleX: 1.0, scaleY: 1.0)
|
|
40
|
-
|
|
41
|
-
fun animCombine(p: AnimPose, q: AnimPose) -> AnimPose =
|
|
42
|
-
AnimPose(dx: p.dx + q.dx, dy: p.dy + q.dy, rotation: p.rotation + q.rotation, scaleX: p.scaleX * q.scaleX, scaleY: p.scaleY * q.scaleY)
|
|
43
|
-
|
|
44
|
-
# Fraction of a `secs` long step done at t, clamped to [0, 1]; a zero-length step is done at once.
|
|
67
|
+
# Fraction of a `secs` long step done at t, clamped to [0, 1]; a zero-length step is done at once.
|
|
45
|
-
fun
|
|
68
|
+
fun progress(secs: Float, t: Float) -> Float =
|
|
46
|
-
|
|
69
|
+
if secs <= 0.0
|
|
47
|
-
|
|
70
|
+
1.0
|
|
48
|
-
|
|
71
|
+
else if t >= secs
|
|
49
|
-
|
|
72
|
+
1.0
|
|
50
|
-
else
|
|
51
|
-
t / secs
|
|
52
|
-
|
|
53
|
-
fun animLength(a: Anim) -> Float = match a
|
|
54
|
-
MoveBy(_, _, secs) => animMax0(secs)
|
|
55
|
-
RotateBy(_, secs) => animMax0(secs)
|
|
56
|
-
ScaleTo(_, _, secs) => animMax0(secs)
|
|
57
|
-
Delay(secs) => animMax0(secs)
|
|
58
|
-
Seq(steps) => animSeqLength(steps, 0.0)
|
|
59
|
-
Par(steps) => animParLength(steps, 0.0)
|
|
60
|
-
Forever(_) => animForever()
|
|
61
|
-
|
|
62
|
-
fun animMax0(x: Float) -> Float =
|
|
63
|
-
if x > 0.0
|
|
64
|
-
x
|
|
65
|
-
else
|
|
66
|
-
0.0
|
|
67
|
-
|
|
68
|
-
fun animSeqLength(steps: List<Anim>, acc: Float) -> Float = match steps
|
|
69
|
-
Nil => acc
|
|
70
|
-
Cons(head, tail) => animSeqLength(tail, acc + animLength(head))
|
|
71
|
-
|
|
72
|
-
fun animParLength(steps: List<Anim>, acc: Float) -> Float = match steps
|
|
73
|
-
Nil => acc
|
|
74
|
-
Cons(head, tail) => animParLength(tail, if animLength(head) > acc then animLength(head) else acc)
|
|
75
|
-
|
|
76
|
-
fun animAt(a: Anim, t: Float) -> AnimPose =
|
|
77
|
-
if t < 0.0
|
|
78
|
-
animIdentity()
|
|
79
|
-
else
|
|
80
|
-
match a
|
|
81
|
-
MoveBy(dx, dy, secs) =>
|
|
82
|
-
p := animProgress(secs, t)
|
|
83
|
-
AnimPose(dx: dx * p, dy: dy * p, rotation: 0.0, scaleX: 1.0, scaleY: 1.0)
|
|
84
|
-
RotateBy(radians, secs) =>
|
|
85
|
-
AnimPose(dx: 0.0, dy: 0.0, rotation: radians * animProgress(secs, t), scaleX: 1.0, scaleY: 1.0)
|
|
86
|
-
ScaleTo(sx, sy, secs) =>
|
|
87
|
-
p := animProgress(secs, t)
|
|
88
|
-
AnimPose(dx: 0.0, dy: 0.0, rotation: 0.0, scaleX: 1.0 + {sx - 1.0} * p, scaleY: 1.0 + {sy - 1.0} * p)
|
|
89
|
-
Delay(_) => animIdentity()
|
|
90
|
-
Seq(steps) => animSeqAt(steps, t, animIdentity())
|
|
91
|
-
Par(steps) => animParAt(steps, t, animIdentity())
|
|
92
|
-
Forever(inner) => animForeverAt(inner, t)
|
|
93
|
-
|
|
94
|
-
# Finished steps contribute their final pose; the first unfinished one its partial pose.
|
|
95
|
-
fun animSeqAt(steps: List<Anim>, t: Float, acc: AnimPose) -> AnimPose = match steps
|
|
96
|
-
Nil => acc
|
|
97
|
-
Cons(head, tail) =>
|
|
98
|
-
len := animLength(head)
|
|
99
|
-
if t >= len
|
|
100
|
-
animSeqAt(tail, t - len, animCombine(acc, animAt(head, len)))
|
|
101
73
|
else
|
|
102
|
-
|
|
74
|
+
t / secs
|
|
103
75
|
|
|
76
|
+
fun max0(x: Float) -> Float =
|
|
77
|
+
if x > 0.0
|
|
78
|
+
x
|
|
79
|
+
else
|
|
80
|
+
0.0
|
|
81
|
+
|
|
104
|
-
fun
|
|
82
|
+
fun seqLength(steps: List<Anim>, acc: Float) -> Float = match steps
|
|
105
|
-
|
|
83
|
+
Nil => acc
|
|
106
|
-
|
|
84
|
+
Cons(head, tail) => Anim.seqLength(tail, acc + head.length())
|
|
107
|
-
|
|
85
|
+
|
|
108
|
-
fun
|
|
86
|
+
fun parLength(steps: List<Anim>, acc: Float) -> Float = match steps
|
|
87
|
+
Nil => acc
|
|
88
|
+
Cons(head, tail) => Anim.parLength(tail, if head.length() > acc then head.length() else acc)
|
|
89
|
+
|
|
90
|
+
# Finished steps contribute their final pose; the first unfinished one its partial pose.
|
|
91
|
+
fun seqAt(steps: List<Anim>, t: Float, acc: AnimPose) -> AnimPose = match steps
|
|
92
|
+
Nil => acc
|
|
93
|
+
Cons(head, tail) =>
|
|
109
|
-
|
|
94
|
+
len := head.length()
|
|
95
|
+
if t >= len
|
|
110
|
-
|
|
96
|
+
Anim.seqAt(tail, t - len, acc.combine(head.at(len)))
|
|
111
|
-
animAt(inner, t)
|
|
112
|
-
|
|
97
|
+
else
|
|
98
|
+
acc.combine(head.at(t))
|
|
99
|
+
|
|
100
|
+
fun parAt(steps: List<Anim>, t: Float, acc: AnimPose) -> AnimPose = match steps
|
|
101
|
+
Nil => acc
|
|
102
|
+
Cons(head, tail) => Anim.parAt(tail, t, acc.combine(head.at(t)))
|
|
103
|
+
|
|
104
|
+
# This animation as the lap of a `Forever`, `t` seconds in.
|
|
105
|
+
fun foreverAt(self, t: Float) -> AnimPose =
|
|
106
|
+
lap := self.length()
|
|
107
|
+
if lap <= 0.0 || lap >= Anim.endless()
|
|
108
|
+
self.at(t)
|
|
109
|
+
else
|
|
113
|
-
|
|
110
|
+
self.at(t - {t / lap}.floor() * lap)
|
|
114
|
-
|
|
115
|
-
fun animNear(a: Float, b: Float) -> Bool =
|
|
116
|
-
{a - b}.abs() < 1e-6
|
std/game/Audio.plum
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
module std
|
|
2
|
+
|
|
3
|
+
import std/Int
|
|
4
|
+
import std/Float
|
|
5
|
+
import std/Str
|
|
6
|
+
import std/Io
|
|
7
|
+
import std/game/Gfx
|
|
8
|
+
|
|
9
|
+
# Sound: the host's sound calls, statics of `Audio` that take the `Io` capability first,
|
|
10
|
+
# and the `Sound` handle: `Sound.load(io, "boop.wav")`. Volume is 0..1. A sound handle is
|
|
11
|
+
# -1 on failure. Paths are relative to the asset root. The host decodes every sound file in
|
|
12
|
+
# the asset root before `init` runs.
|
|
13
|
+
|
|
14
|
+
primitive Audio =
|
|
15
|
+
# Loads a WAV or OGG file relative to the asset root; -1 on failure.
|
|
16
|
+
extern fun soundLoad(io: Io, path: Str) -> Int
|
|
17
|
+
# A one-shot play; plays of the same sound may overlap. Volume is 0..1.
|
|
18
|
+
extern fun soundPlay(io: Io, s: Int, volume: Float)
|
|
19
|
+
# Starts the sound looping. Call `soundStop` first to restart it cleanly.
|
|
20
|
+
extern fun soundLoop(io: Io, s: Int, volume: Float)
|
|
21
|
+
# Stops every play and loop of the sound.
|
|
22
|
+
extern fun soundStop(io: Io, s: Int)
|
|
23
|
+
# Changes the volume of a sound that is playing or looping.
|
|
24
|
+
extern fun soundSetVolume(io: Io, s: Int, volume: Float)
|
|
25
|
+
|
|
26
|
+
# A sound handle; the id is the host's. `Sound.Silent` (id -1) is no sound: it plays
|
|
27
|
+
# nothing and never calls the host, so a test can run a game's effects.
|
|
28
|
+
struct Sound(id: Int) =
|
|
29
|
+
Silent = Sound(id: -1)
|
|
30
|
+
|
|
31
|
+
# No sound: `Sound.Silent`, what a test's assets hold.
|
|
32
|
+
fun none() -> Sound = Sound.Silent
|
|
33
|
+
|
|
34
|
+
# Loads a sound relative to the asset root; crashes naming the file if it is missing.
|
|
35
|
+
fun load(io: Io, name: Str) -> Sound =
|
|
36
|
+
id := Audio.soundLoad(io, name)
|
|
37
|
+
if id < 0
|
|
38
|
+
crash Gfx.missingAsset("sound", name)
|
|
39
|
+
Sound(id: id)
|
|
40
|
+
|
|
41
|
+
fun play(self, io: Io, volume: Float = 1.0) =
|
|
42
|
+
if self.id >= 0
|
|
43
|
+
Audio.soundPlay(io, self.id, volume)
|
|
44
|
+
|
|
45
|
+
fun loop(self, io: Io, volume: Float = 1.0) =
|
|
46
|
+
if self.id >= 0
|
|
47
|
+
Audio.soundLoop(io, self.id, volume)
|
|
48
|
+
|
|
49
|
+
fun stop(self, io: Io) =
|
|
50
|
+
if self.id >= 0
|
|
51
|
+
Audio.soundStop(io, self.id)
|
|
52
|
+
|
|
53
|
+
fun setVolume(self, io: Io, volume: Float) =
|
|
54
|
+
if self.id >= 0
|
|
55
|
+
Audio.soundSetVolume(io, self.id, volume)
|
std/game/Box.plum
CHANGED
|
@@ -8,7 +8,7 @@ import std/math/Vec2
|
|
|
8
8
|
import std/math/Affine
|
|
9
9
|
import std/math/Aabb
|
|
10
10
|
import std/game/Color
|
|
11
|
-
import std/game/
|
|
11
|
+
import std/game/Gfx
|
|
12
12
|
import std/game/Place
|
|
13
13
|
import std/game/View
|
|
14
14
|
import std/game/Sized
|
|
@@ -20,30 +20,30 @@ import std/game/Touch
|
|
|
20
20
|
struct Box[View, Sized, Touch](x: Float, y: Float, w: Float, h: Float, color: Color, touch: Touchable) =
|
|
21
21
|
fun of(x: Float, y: Float, w: Float, h: Float, color: Color) -> Box = Box(x: x, y: y, w: w, h: h, color: color, touch: Disabled)
|
|
22
22
|
|
|
23
|
-
fun place(self) -> Place =
|
|
23
|
+
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
24
24
|
|
|
25
25
|
fun bounds(self) -> Aabb = Aabb.of(0.0, 0.0, self.w, self.h)
|
|
26
26
|
|
|
27
27
|
fun touch(self) -> Touchable = self.touch
|
|
28
28
|
|
|
29
|
-
fun paintSelf(self, io: Io, m: Affine, alpha: Float) =
|
|
29
|
+
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = Box.draw(io, m, alpha * self.color.a, self.w, self.h, self.color)
|
|
30
|
-
|
|
30
|
+
|
|
31
|
-
fun hit(self, p: Vec2) -> Bool =
|
|
31
|
+
fun hit(self, p: Vec2) -> Bool = Box.contains(self.w, self.h, p)
|
|
32
|
-
|
|
32
|
+
|
|
33
|
-
# Whether `p` is inside a `w` by `h` box at (0, 0); a box with no size is never hit.
|
|
33
|
+
# Whether `p` is inside a `w` by `h` box at (0, 0); a box with no size is never hit.
|
|
34
|
-
fun
|
|
34
|
+
fun contains(w: Float, h: Float, p: Vec2) -> Bool =
|
|
35
|
-
|
|
35
|
+
w > 0.0 && h > 0.0 && p.x >= 0.0 && p.x <= w && p.y >= 0.0 && p.y <= h
|
|
36
|
-
|
|
36
|
+
|
|
37
|
-
# Draws a `w` by `h` box at the transform `m`, turned as `m` turns it, for your own kinds.
|
|
37
|
+
# Draws a `w` by `h` box at the transform `m`, turned as `m` turns it, for your own kinds.
|
|
38
|
-
# Paints with `alpha`, the alpha `paintSelf` receives (what it is drawn under, its own
|
|
38
|
+
# Paints with `alpha`, the alpha `paintSelf` receives (what it is drawn under, its own
|
|
39
|
-
# place's included), in place of the colour's alpha: to paint at the colour's too, pass
|
|
39
|
+
# place's included), in place of the colour's alpha: to paint at the colour's too, pass
|
|
40
|
-
# `alpha * color.a`, and to fade one shape of a view, `alpha * fade`. An unturned box is a
|
|
40
|
+
# `alpha * color.a`, and to fade one shape of a view, `alpha * fade`. An unturned box is a
|
|
41
|
-
# `drawRect`, a turned one a `drawRectEx`.
|
|
41
|
+
# `drawRect`, a turned one a `drawRectEx`.
|
|
42
|
-
fun
|
|
42
|
+
fun draw(io: Io, m: Affine, alpha: Float, w: Float, h: Float, color: Color) =
|
|
43
|
-
|
|
43
|
+
sx := w * m.scaleX()
|
|
44
|
-
|
|
44
|
+
sy := h * m.scaleY()
|
|
45
|
-
|
|
45
|
+
c := m.apply(Vec2(x: w / 2.0, y: h / 2.0))
|
|
46
|
-
|
|
46
|
+
if m.b == 0.0 && m.a > 0.0
|
|
47
|
-
|
|
47
|
+
Gfx.drawRect(io, c.x - sx / 2.0, c.y - sy / 2.0, sx, sy, color.withAlpha(alpha))
|
|
48
|
-
|
|
48
|
+
else
|
|
49
|
-
|
|
49
|
+
Gfx.drawRectEx(io, c.x - sx / 2.0, c.y - sy / 2.0, sx, sy, m.rotation(), color.withAlpha(alpha))
|
std/game/Circle.plum
CHANGED
|
@@ -11,7 +11,7 @@ import std/math/Vec2
|
|
|
11
11
|
import std/math/Affine
|
|
12
12
|
import std/math/Aabb
|
|
13
13
|
import std/game/Color
|
|
14
|
-
import std/game/
|
|
14
|
+
import std/game/Gfx
|
|
15
15
|
import std/game/Place
|
|
16
16
|
import std/game/View
|
|
17
17
|
import std/game/Sized
|
|
@@ -24,18 +24,18 @@ import std/game/Touch
|
|
|
24
24
|
struct Circle[View, Sized, Touch](x: Float, y: Float, r: Float, color: Color, touch: Touchable) =
|
|
25
25
|
fun of(x: Float, y: Float, r: Float, color: Color) -> Circle = Circle(x: x, y: y, r: r, color: color, touch: Disabled)
|
|
26
26
|
|
|
27
|
-
fun place(self) -> Place =
|
|
27
|
+
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
28
28
|
|
|
29
29
|
fun bounds(self) -> Aabb = Aabb.of(0.0 - self.r, 0.0 - self.r, 2.0 * self.r, 2.0 * self.r)
|
|
30
30
|
|
|
31
31
|
fun touch(self) -> Touchable = self.touch
|
|
32
32
|
|
|
33
|
-
fun paintSelf(self, io: Io, m: Affine, alpha: Float) =
|
|
33
|
+
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = Circle.draw(io, m, alpha * self.color.a, self.r, self.color)
|
|
34
34
|
|
|
35
35
|
fun hit(self, p: Vec2) -> Bool = Math.hypot(p.x, p.y) <= self.r
|
|
36
36
|
|
|
37
|
-
# Draws a circle of radius `r` centred on the origin of the transform `m`. Paints with
|
|
37
|
+
# Draws a circle of radius `r` centred on the origin of the transform `m`. Paints with
|
|
38
|
-
# `alpha` in place of the colour's alpha, as `
|
|
38
|
+
# `alpha` in place of the colour's alpha, as `Box.draw` does.
|
|
39
|
-
fun
|
|
39
|
+
fun draw(io: Io, m: Affine, alpha: Float, r: Float, color: Color) =
|
|
40
|
-
|
|
40
|
+
c := m.apply(Vec2(x: 0.0, y: 0.0))
|
|
41
|
-
|
|
41
|
+
Gfx.drawCircle(io, c.x, c.y, r * m.scaleX(), color.withAlpha(alpha))
|
std/game/Effects.plum
CHANGED
|
@@ -8,12 +8,12 @@ import std/game/Control
|
|
|
8
8
|
import std/game/ScreenSlice
|
|
9
9
|
|
|
10
10
|
# The actions dispatched in a tick, waiting for their effects. A delivery
|
|
11
|
-
# (std/game/ScreenSlice's `deliverTo`) queues each action of its slice as it is
|
|
11
|
+
# (std/game/ScreenSlice's `Delivery.deliverTo`) queues each action of its slice as it is
|
|
12
12
|
# dispatched, on a queue of the slice's action type (`ActionSink<A>`); after the tick each
|
|
13
13
|
# is held with the slice as the tick left it (`Queued<A>`), so one list holds the actions
|
|
14
14
|
# of every screen whatever their types, and the game runs each one's effect (the slice's
|
|
15
15
|
# `effect(io, a, game)`), in the order dispatched. A test reads the ones that ran with
|
|
16
|
-
# `sent<A>(game.ran)` (std/game/headless's `effects<A>()`).
|
|
16
|
+
# `Effects.sent<A>(game.ran)` (std/game/headless's `effects<A>()`).
|
|
17
17
|
#
|
|
18
18
|
# The queue of the dispatch running sits in one slot (`Running`): the game sets it around
|
|
19
19
|
# a screen's `deliver`, std/game/headless around its own `dispatch`, and a slice's own
|
|
@@ -27,16 +27,6 @@ trait Effect =
|
|
|
27
27
|
struct Queued<A>[Effect](slice: ScreenSlice<A>, action: A) =
|
|
28
28
|
fun apply(self, io: Io, game: Control) = self.slice.effect(io, self.action, game)
|
|
29
29
|
|
|
30
|
-
# `a`, to run with the effect of the slice `s`.
|
|
31
|
-
fun queued<A>(s: ScreenSlice<A>, a: A) -> Effect = Queued(slice: s, action: a)
|
|
32
|
-
|
|
33
|
-
# The actions of type `A` among `fx`, in order: `sent<PlayAction>(game.ran)`.
|
|
34
|
-
fun sent<A>(fx: List<Effect>) -> List<A> =
|
|
35
|
-
none: List<A> := Nil
|
|
36
|
-
fx.fold(none, |acc, e| match e
|
|
37
|
-
Queued<A> as q => Cons(q.action, acc)
|
|
38
|
-
_ => acc).reverse()
|
|
39
|
-
|
|
40
30
|
# A delivery's queue, of the slice's action type.
|
|
41
31
|
trait Sink =
|
|
42
32
|
fun size(self) -> Int
|
|
@@ -47,19 +37,31 @@ struct ActionSink<A>[Sink](queue: Slice<A>) =
|
|
|
47
37
|
# The queue of the dispatch running: empty outside any, one queue inside one.
|
|
48
38
|
Running = Slice<Sink>()
|
|
49
39
|
|
|
40
|
+
# The dispatch slot and reading what ran.
|
|
41
|
+
primitive Effects =
|
|
42
|
+
# `a`, to run with the effect of the slice `s`.
|
|
43
|
+
fun queued<A>(s: ScreenSlice<A>, a: A) -> Effect = Queued(slice: s, action: a)
|
|
44
|
+
|
|
45
|
+
# The actions of type `A` among `fx`, in order: `Effects.sent<PlayAction>(game.ran)`.
|
|
46
|
+
fun sent<A>(fx: List<Effect>) -> List<A> =
|
|
47
|
+
none: List<A> := Nil
|
|
48
|
+
fx.fold(none, |acc, e| match e
|
|
49
|
+
Queued<A> as q => Cons(q.action, acc)
|
|
50
|
+
_ => acc).reverse()
|
|
51
|
+
|
|
50
|
-
# From here to `endDispatch()`, what a slice dispatches is queued on `s`.
|
|
52
|
+
# From here to `endDispatch()`, what a slice dispatches is queued on `s`.
|
|
51
|
-
fun startDispatch(s: Sink) =
|
|
53
|
+
fun startDispatch(s: Sink) =
|
|
52
|
-
|
|
54
|
+
Running.clear()
|
|
53
|
-
|
|
55
|
+
Running.push(s)
|
|
54
56
|
|
|
55
|
-
fun endDispatch() = Running.clear()
|
|
57
|
+
fun endDispatch() = Running.clear()
|
|
56
58
|
|
|
57
|
-
# Whether a dispatch is running.
|
|
59
|
+
# Whether a dispatch is running.
|
|
58
|
-
fun dispatching() -> Bool = !Running.isEmpty()
|
|
60
|
+
fun dispatching() -> Bool = !Running.isEmpty()
|
|
59
61
|
|
|
60
|
-
# Queues `a` on the dispatch running, if it queues actions of `a`'s type.
|
|
62
|
+
# Queues `a` on the dispatch running, if it queues actions of `a`'s type.
|
|
61
|
-
fun queueAction<A>(a: A) =
|
|
63
|
+
fun queueAction<A>(a: A) =
|
|
62
|
-
|
|
64
|
+
if !Running.isEmpty()
|
|
63
|
-
|
|
65
|
+
match Running.at(0)
|
|
64
|
-
|
|
66
|
+
ActionSink<A> as k => k.queue.push(a)
|
|
65
|
-
|
|
67
|
+
_ => Unit
|
std/game/Game.plum
CHANGED
|
@@ -13,8 +13,8 @@ import std/math/Vec2
|
|
|
13
13
|
import std/math/Affine
|
|
14
14
|
import std/math/Aabb
|
|
15
15
|
import std/game/Color
|
|
16
|
-
import std/game/
|
|
16
|
+
import std/game/Gfx
|
|
17
|
-
import std/game/
|
|
17
|
+
import std/game/Input
|
|
18
18
|
import std/game/View
|
|
19
19
|
import std/game/Effects
|
|
20
20
|
import std/game/ScreenSlice
|
|
@@ -42,12 +42,12 @@ import std/game/History
|
|
|
42
42
|
# The window's size in pixels (`pixels`) comes from the host each tick (`update`), or from
|
|
43
43
|
# the test (`step`). Each screen is seen through a camera worked out from its `size()`,
|
|
44
44
|
# its `viewport()`, the window's size and its `camera(base)` (`camera(i)`,
|
|
45
|
-
#
|
|
45
|
+
# `Game.cameraOf`): the game maps a press through it to the world's
|
|
46
46
|
# coordinates, draws the screen's `view` through it and, for a screen that `culls()`,
|
|
47
47
|
# skips what it doesn't show.
|
|
48
48
|
#
|
|
49
49
|
# The compiler adds the frame entry points the host calls: they run `update` once per
|
|
50
|
-
# fixed 1/60 s tick of the frame (std/game/
|
|
50
|
+
# fixed 1/60 s tick of the frame (std/game/Input's `Clock`, which also records and replays
|
|
51
51
|
# the ticks' input, std/game/Replay) and `draw` once a frame.
|
|
52
52
|
#
|
|
53
53
|
# One tick (`update`, or `step` with the window's size given, as a test runs it): the
|
|
@@ -115,7 +115,7 @@ struct Game(screens: List<Screen>) =
|
|
|
115
115
|
self.moved: Bool = false
|
|
116
116
|
if self.screens.isEmpty()
|
|
117
117
|
crash "game: a game needs a screen"
|
|
118
|
-
if Some(dup) := duplicateName(self.screens, Nil)
|
|
118
|
+
if Some(dup) := Game.duplicateName(self.screens, Nil)
|
|
119
119
|
crash "game: two screens are named '{dup}'"
|
|
120
120
|
|
|
121
121
|
# The screen shown.
|
|
@@ -125,14 +125,13 @@ struct Game(screens: List<Screen>) =
|
|
|
125
125
|
fun showing(self) -> Str = self.screen().name()
|
|
126
126
|
|
|
127
127
|
# The index of the screen named `name`, -1 if there is none.
|
|
128
|
-
fun indexOf(self, name: Str) -> Int = indexIn(self.screens, name, 0)
|
|
128
|
+
fun indexOf(self, name: Str) -> Int = Game.indexIn(self.screens, name, 0)
|
|
129
129
|
|
|
130
130
|
# Whether a transition is drawing two screens.
|
|
131
131
|
fun shifting(self) -> Bool = self.shift.isSome()
|
|
132
132
|
|
|
133
|
-
# The camera the screen at index `i` is seen through now (
|
|
133
|
+
# The camera the screen at index `i` is seen through now (`cameraOf`).
|
|
134
|
-
# `cameraOf`).
|
|
135
|
-
fun camera(self, i: Int) -> Viewport =
|
|
134
|
+
fun camera(self, i: Int) -> Viewport = Game.cameraOf(self.screens.at(i), self.pixels)
|
|
136
135
|
|
|
137
136
|
# A window pixel in the shown screen's world coordinates, through its camera.
|
|
138
137
|
fun toWorld(self, x: Float, y: Float) -> Vec2 = self.camera(self.current).toWorld(x, y)
|
|
@@ -148,7 +147,7 @@ struct Game(screens: List<Screen>) =
|
|
|
148
147
|
|
|
149
148
|
# One tick of `dt` seconds with `input` on the host's screen: what the host runs.
|
|
150
149
|
fun update(self, io: Io, input: Input, dt: Float) -> Game =
|
|
151
|
-
self.step(io, input, dt, Vec2(x:
|
|
150
|
+
self.step(io, input, dt, Vec2(x: Gfx.screenWidth(io), y: Gfx.screenHeight(io)))
|
|
152
151
|
|
|
153
152
|
# One tick of `dt` seconds with `input` in a window of `pixels` (see the top of this
|
|
154
153
|
# file); a test runs ticks with it. With a history, the tick is added to it, and then
|
|
@@ -182,10 +181,10 @@ struct Game(screens: List<Screen>) =
|
|
|
182
181
|
# `acted` says whether the tick was a user action (std/game/History).
|
|
183
182
|
fun run(self, io: Io, input: Input, dt: Float, pixels: Vec2, replayed: Option<List<Ask>>) -> Game =
|
|
184
183
|
sized := if pixels.x > 0.0 && pixels.y > 0.0 && {pixels.x != self.pixels.x || pixels.y != self.pixels.y} then self.resized(pixels) else self
|
|
185
|
-
news := heardIn(input, sized.pointer)
|
|
184
|
+
news := Game.heardIn(input, sized.pointer)
|
|
186
185
|
s := sized.screen()
|
|
187
|
-
base := Game(..sized, pointer: lastPointer(input, sized.pointer), ticks: sized.ticks + 1, ran: Nil, asked: Nil, acted: false)
|
|
186
|
+
base := Game(..sized, pointer: Game.lastPointer(input, sized.pointer), ticks: sized.ticks + 1, ran: Nil, asked: Nil, acted: false)
|
|
188
|
-
if onDemand(s) && !sized.wake && sized.shift.isNone() && !news && !s.wants(input)
|
|
187
|
+
if Game.onDemand(s) && !sized.wake && sized.shift.isNone() && !news && !s.wants(input)
|
|
189
188
|
Game(..base, skipped: base.skipped + 1)
|
|
190
189
|
else
|
|
191
190
|
d := s.deliver(Delivery(input: input, dt: dt, before: base.helds.at(base.current), camera: base.camera(base.current), pixels: base.pixels, shifting: base.shift.isSome()))
|
|
@@ -193,7 +192,7 @@ struct Game(screens: List<Screen>) =
|
|
|
193
192
|
Some(sh) => sh.after(dt)
|
|
194
193
|
None => None
|
|
195
194
|
ticked := Game(
|
|
196
|
-
..base, screens: setAt(base.screens, base.current, d.screen), helds: setAt(base.helds, base.current, input.held), shift: shift,
|
|
195
|
+
..base, screens: Game.setAt(base.screens, base.current, d.screen), helds: Game.setAt(base.helds, base.current, input.held), shift: shift,
|
|
197
196
|
delivered: base.delivered + 1, dirty: true, acted: d.acted)
|
|
198
197
|
after := match replayed
|
|
199
198
|
Some(asks) => ticked.redoAsks(asks)
|
|
@@ -236,7 +235,7 @@ struct Game(screens: List<Screen>) =
|
|
|
236
235
|
fun goTo(self, name: Str) -> Game =
|
|
237
236
|
i := self.indexOf(name)
|
|
238
237
|
if i < 0
|
|
239
|
-
crash "game: no screen is named '{name}' (the screens are {namesOf(self.screens)})"
|
|
238
|
+
crash "game: no screen is named '{name}' (the screens are {Game.namesOf(self.screens)})"
|
|
240
239
|
if i == self.current
|
|
241
240
|
self
|
|
242
241
|
else
|
|
@@ -248,7 +247,7 @@ struct Game(screens: List<Screen>) =
|
|
|
248
247
|
# During a transition, the screen going out and then the one coming in, each moved and
|
|
249
248
|
# faded by the transition.
|
|
250
249
|
fun draw(self, io: Io) =
|
|
251
|
-
|
|
250
|
+
Gfx.clear(io, self.screen().background())
|
|
252
251
|
match self.shift
|
|
253
252
|
Some(sh) =>
|
|
254
253
|
self.drawScreen(io, sh.from, sh.frame(false, self.pixels))
|
|
@@ -265,18 +264,18 @@ struct Game(screens: List<Screen>) =
|
|
|
265
264
|
s := self.screens.at(i)
|
|
266
265
|
if self.shift.isSome()
|
|
267
266
|
bg := s.background()
|
|
268
|
-
|
|
267
|
+
Gfx.drawRect(io, f.x, f.y, self.pixels.x, self.pixels.y, bg.withAlpha(bg.a * f.alpha))
|
|
269
268
|
c := self.camera(i)
|
|
270
269
|
v := self.visible(i)
|
|
271
270
|
area := Aabb(min: Vec2(x: v.min.x - f.x / c.sx, y: v.min.y - f.y / c.sy), max: Vec2(x: v.max.x - f.x / c.sx, y: v.max.y - f.y / c.sy))
|
|
272
271
|
m := Affine(a: 1.0, b: 0.0, c: 0.0, d: 1.0, e: f.x, f: f.y).mul(c.transform())
|
|
273
272
|
if s.culls()
|
|
274
|
-
|
|
273
|
+
Views.paintCulled(io, s.view(), m, f.alpha, Views.through(area, m))
|
|
275
274
|
else
|
|
276
|
-
|
|
275
|
+
Views.paint(io, s.view(), m, f.alpha)
|
|
277
276
|
hud := s.hud(self.pixels)
|
|
278
277
|
if !hud.isEmpty()
|
|
279
|
-
|
|
278
|
+
Views.paint(io, hud, Affine(a: 1.0, b: 0.0, c: 0.0, d: 1.0, e: f.x, f: f.y), f.alpha)
|
|
280
279
|
|
|
281
280
|
# What the frame would draw of the shown screen now: each item of its view, and what is
|
|
282
281
|
# under it, with its transform (to the world) and alpha, culled if the screen `culls()`;
|
|
@@ -284,8 +283,8 @@ struct Game(screens: List<Screen>) =
|
|
|
284
283
|
fun drawList(self) -> List<Drawn> =
|
|
285
284
|
s := self.screen()
|
|
286
285
|
out := Slice<Drawn>()
|
|
287
|
-
|
|
286
|
+
Views.drawnIn(s.view(), Affine.Identity, 1.0, out, self.visible(self.current), s.culls())
|
|
288
|
-
|
|
287
|
+
Views.drawnIn(s.hud(self.pixels), Affine.Identity, 1.0, out, Aabb.of(0.0, 0.0, 0.0, 0.0), false)
|
|
289
288
|
List.fromSlice(out)
|
|
290
289
|
|
|
291
290
|
# The game after a frame was drawn: until the next tick runs, `idle` holds for an
|
|
@@ -296,7 +295,7 @@ struct Game(screens: List<Screen>) =
|
|
|
296
295
|
# Whether the last frame drawn shows what the shown screen is now: only for an
|
|
297
296
|
# `OnDemand` screen, and never during a transition (or, paused, until a move).
|
|
298
297
|
fun idle(self) -> Bool =
|
|
299
|
-
if self.paused then !self.dirty else onDemand(self.screen()) && !self.dirty && self.shift.isNone()
|
|
298
|
+
if self.paused then !self.dirty else Game.onDemand(self.screen()) && !self.dirty && self.shift.isNone()
|
|
300
299
|
|
|
301
300
|
# --- undo and time travel (std/game/History) ---
|
|
302
301
|
|
|
@@ -389,40 +388,57 @@ struct Game(screens: List<Screen>) =
|
|
|
389
388
|
played := recs.fold(self, |g, r| g.run(io, r.input, r.dt, r.pixels, Some(r.asks)))
|
|
390
389
|
Game(..played, rewound: recs.length(), ran: Nil, asked: Nil, dirty: true, moved: true)
|
|
391
390
|
|
|
391
|
+
# The camera the screen `s` is seen through in a window of `pixels` (0 by 0 while the
|
|
392
|
+
# game doesn't know it): its world (`size()`) put on the window by its `viewport()`, or,
|
|
393
|
+
# for a screen with no size, one unit a pixel with the world as large as the window; then
|
|
394
|
+
# as the screen's `camera` moves it.
|
|
395
|
+
fun cameraOf(s: Screen, pixels: Vec2) -> Viewport = s.camera(Game.baseCamera(s, pixels))
|
|
396
|
+
|
|
397
|
+
fun baseCamera(s: Screen, pixels: Vec2) -> Viewport =
|
|
398
|
+
size := s.size()
|
|
399
|
+
known := pixels.x > 0.0 && pixels.y > 0.0
|
|
400
|
+
if size.x > 0.0 && size.y > 0.0
|
|
401
|
+
v := Viewport.of(s.viewport(), size.x, size.y)
|
|
402
|
+
if known then v.update(pixels.x, pixels.y) else v
|
|
403
|
+
else if known
|
|
404
|
+
Viewport.of(Pixels, pixels.x, pixels.y)
|
|
405
|
+
else
|
|
406
|
+
Viewport.of(Pixels, 1.0, 1.0)
|
|
407
|
+
|
|
392
|
-
# Whether `s` sleeps through ticks with nothing to do (`frames()` is `OnDemand`).
|
|
408
|
+
# Whether `s` sleeps through ticks with nothing to do (`frames()` is `OnDemand`).
|
|
393
|
-
fun onDemand(s: Screen) -> Bool = match s.frames()
|
|
409
|
+
fun onDemand(s: Screen) -> Bool = match s.frames()
|
|
394
|
-
|
|
410
|
+
OnDemand => true
|
|
395
|
-
|
|
411
|
+
Always => false
|
|
396
|
-
|
|
412
|
+
|
|
397
|
-
fun setAt<T>(xs: List<T>, i: Int, s: T) -> List<T> = match xs
|
|
413
|
+
fun setAt<T>(xs: List<T>, i: Int, s: T) -> List<T> = match xs
|
|
398
|
-
|
|
414
|
+
Nil => Nil
|
|
399
|
-
|
|
415
|
+
Cons(x, rest) => if i == 0 then Cons(s, rest) else Cons(x, Game.setAt(rest, i - 1, s))
|
|
400
|
-
|
|
416
|
+
|
|
401
|
-
fun indexIn(xs: List<Screen>, name: Str, i: Int) -> Int = match xs
|
|
417
|
+
fun indexIn(xs: List<Screen>, name: Str, i: Int) -> Int = match xs
|
|
402
|
-
|
|
418
|
+
Nil => -1
|
|
403
|
-
|
|
419
|
+
Cons(x, rest) => if x.name() == name then i else Game.indexIn(rest, name, i + 1)
|
|
404
|
-
|
|
420
|
+
|
|
405
|
-
fun namesOf(xs: List<Screen>) -> Str = xs.fold("", |acc, s| if acc == "" then s.name() else "{acc}, {s.name()}")
|
|
421
|
+
fun namesOf(xs: List<Screen>) -> Str = xs.fold("", |acc, s| if acc == "" then s.name() else "{acc}, {s.name()}")
|
|
406
|
-
|
|
422
|
+
|
|
407
|
-
# A name two screens of `xs` share, the names before them being `seen`.
|
|
423
|
+
# A name two screens of `xs` share, the names before them being `seen`.
|
|
408
|
-
fun duplicateName(xs: List<Screen>, seen: List<Str>) -> Option<Str> = match xs
|
|
424
|
+
fun duplicateName(xs: List<Screen>, seen: List<Str>) -> Option<Str> = match xs
|
|
409
|
-
|
|
425
|
+
Nil => None
|
|
410
|
-
|
|
426
|
+
Cons(x, rest) => if seen.any(|n| n == x.name()) then Some(x.name()) else Game.duplicateName(rest, Cons(x.name(), seen))
|
|
411
|
-
|
|
427
|
+
|
|
412
|
-
# Whether the tick's input has an event, a pointer move to where the pointer already was
|
|
428
|
+
# Whether the tick's input has an event, a pointer move to where the pointer already was
|
|
413
|
-
# aside.
|
|
429
|
+
# aside.
|
|
414
|
-
fun heardIn(input: Input, pointer: Vec2) -> Bool =
|
|
430
|
+
fun heardIn(input: Input, pointer: Vec2) -> Bool =
|
|
415
|
-
|
|
431
|
+
evs := input.events
|
|
416
|
-
|
|
432
|
+
0.until(evs.length()).fold(Pair(first: false, second: pointer), |acc, i|
|
|
417
|
-
|
|
433
|
+
e := evs.at(i)
|
|
418
|
-
|
|
434
|
+
match e
|
|
419
|
-
|
|
435
|
+
PointerMove(x, y) => Pair(first: acc.first || x != acc.second.x || y != acc.second.y, second: Vec2(x: x, y: y))
|
|
420
|
-
|
|
436
|
+
_ => match e.position()
|
|
421
|
-
|
|
437
|
+
Some(p) => Pair(first: true, second: p)
|
|
422
|
-
|
|
438
|
+
None => Pair(first: true, second: acc.second)
|
|
423
|
-
|
|
439
|
+
).first
|
|
424
|
-
|
|
440
|
+
|
|
425
|
-
# Where the pointer is after the tick's events.
|
|
441
|
+
# Where the pointer is after the tick's events.
|
|
426
|
-
fun lastPointer(input: Input, pointer: Vec2) -> Vec2 =
|
|
442
|
+
fun lastPointer(input: Input, pointer: Vec2) -> Vec2 =
|
|
427
|
-
|
|
443
|
+
evs := input.events
|
|
428
|
-
|
|
444
|
+
0.until(evs.length()).fold(pointer, |p, i| evs.at(i).position().unwrapOr(p))
|
std/game/Gfx.plum
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
module std
|
|
2
|
+
|
|
3
|
+
import std/Int
|
|
4
|
+
import std/Float
|
|
5
|
+
import std/Str
|
|
6
|
+
import std/math/Vec2
|
|
7
|
+
import std/game/Color
|
|
8
|
+
import std/Io
|
|
9
|
+
|
|
10
|
+
# Drawing, textures and files: the game host's graphics API (`plum run`), as statics of
|
|
11
|
+
# `Gfx` that take the `Io` capability first: `Gfx.clear(io, Color.Black)`,
|
|
12
|
+
# `Texture.load(io, "sprites.png")`. A game is any type that `main(io: Io)` returns and
|
|
13
|
+
# that has `update(self, io: Io, input: Input, dt: Float) -> Self` and `draw(self, io: Io)`;
|
|
14
|
+
# `update` runs once per fixed 1/60 s tick (see `Clock` in std/game/Input), `draw` once
|
|
15
|
+
# per frame, and the state `update` returns is the next tick's `self`. Input is `std/game/Input`, sound is `std/game/Audio`. This file also
|
|
16
|
+
# holds the `Blend` constants and the `Texture` handle. A path that escapes the asset root
|
|
17
|
+
# fails like a missing file. Coordinates are world units once `Gfx.setCamera` is set.
|
|
18
|
+
#
|
|
19
|
+
# Texture handles are -1 on failure. File and texture paths are relative to the asset
|
|
20
|
+
# root. `Int` in an extern signature is narrowed to i32 at the host boundary.
|
|
21
|
+
|
|
22
|
+
primitive Gfx =
|
|
23
|
+
extern fun screenWidth(io: Io) -> Float
|
|
24
|
+
extern fun screenHeight(io: Io) -> Float
|
|
25
|
+
extern fun frameTime(io: Io) -> Float
|
|
26
|
+
extern fun clear(io: Io, c: Color)
|
|
27
|
+
extern fun drawRect(io: Io, x: Float, y: Float, w: Float, h: Float, c: Color)
|
|
28
|
+
# The rect (x, y, w, h) turned `rotation` radians clockwise about its centre.
|
|
29
|
+
extern fun drawRectEx(io: Io, x: Float, y: Float, w: Float, h: Float, rotation: Float, c: Color)
|
|
30
|
+
extern fun drawCircle(io: Io, x: Float, y: Float, r: Float, c: Color)
|
|
31
|
+
extern fun drawLine(io: Io, x1: Float, y1: Float, x2: Float, y2: Float, thickness: Float, c: Color)
|
|
32
|
+
extern fun drawText(io: Io, s: Str, x: Float, y: Float, size: Float, c: Color)
|
|
33
|
+
# `s` with its top-left at (x, y), turned `rotation` radians clockwise about that point.
|
|
34
|
+
extern fun drawTextEx(io: Io, s: Str, x: Float, y: Float, size: Float, rotation: Float, c: Color)
|
|
35
|
+
extern fun setCamera(io: Io, x: Float, y: Float, scale: Float)
|
|
36
|
+
extern fun resetCamera(io: Io)
|
|
37
|
+
# Loads any image the host decodes (PNG at least); -1 on failure.
|
|
38
|
+
extern fun textureLoad(io: Io, path: Str) -> Int
|
|
39
|
+
# Size in pixels; 0 for a bad handle.
|
|
40
|
+
extern fun textureWidth(io: Io, t: Int) -> Int
|
|
41
|
+
extern fun textureHeight(io: Io, t: Int) -> Int
|
|
42
|
+
# Linear (true) or nearest (false, the default) filtering.
|
|
43
|
+
extern fun textureSetSmooth(io: Io, t: Int, smooth: Bool)
|
|
44
|
+
# A blank, transparent texture of w x h pixels that can be drawn into with
|
|
45
|
+
# `renderTarget`; -1 if a side is below 1 or above 8192.
|
|
46
|
+
extern fun textureCreate(io: Io, w: Int, h: Int) -> Int
|
|
47
|
+
# Sends the following draws (and `clear`) into texture `t` made by `textureCreate`,
|
|
48
|
+
# or back to the screen with -1; any other handle is ignored. Each target has its
|
|
49
|
+
# own camera: selecting a target resets its camera to its pixels, and returning to
|
|
50
|
+
# the screen restores the screen's camera. The texture's contents persist between
|
|
51
|
+
# frames; draw it like any texture once the screen is selected again.
|
|
52
|
+
extern fun renderTarget(io: Io, t: Int)
|
|
53
|
+
# The blend mode of the following draws: a `Blend` constant (other values are
|
|
54
|
+
# ignored). Every frame starts with `Blend.Normal`.
|
|
55
|
+
extern fun blendMode(io: Io, m: Int)
|
|
56
|
+
# The texture at its natural size, top-left at (x, y).
|
|
57
|
+
extern fun drawTexture(io: Io, t: Int, x: Float, y: Float, tint: Color)
|
|
58
|
+
# The source rect (sx, sy, sw, sh) in texture pixels drawn into the dest rect
|
|
59
|
+
# (x, y, w, h), rotated `rotation` radians clockwise about the dest centre.
|
|
60
|
+
extern fun drawTextureEx(io: Io, t: Int, sx: Float, sy: Float, sw: Float, sh: Float, x: Float, y: Float, w: Float, h: Float, rotation: Float, flipX: Bool, tint: Color)
|
|
61
|
+
extern fun fileExists(io: Io, path: Str) -> Bool
|
|
62
|
+
# The whole file; "" if it is missing.
|
|
63
|
+
extern fun fileLoad(io: Io, path: Str) -> Str
|
|
64
|
+
|
|
65
|
+
# Sends the following draws back to the screen.
|
|
66
|
+
fun renderToScreen(io: Io) =
|
|
67
|
+
renderTarget(io, -1)
|
|
68
|
+
|
|
69
|
+
fun missingAsset(kind: Str, name: Str) -> Str =
|
|
70
|
+
"missing {kind}: {name}"
|
|
71
|
+
|
|
72
|
+
# The whole file as text, relative to the asset root; crashes naming the file if it is missing.
|
|
73
|
+
fun loadFile(io: Io, name: Str) -> Str =
|
|
74
|
+
if !fileExists(io, name)
|
|
75
|
+
crash missingAsset("file", name)
|
|
76
|
+
fileLoad(io, name)
|
|
77
|
+
|
|
78
|
+
# Blend modes for `blendMode`: alpha blending, additive (brightens), multiply
|
|
79
|
+
# (darkens, white leaves the target unchanged) and screen (lightens).
|
|
80
|
+
primitive Blend =
|
|
81
|
+
Normal = 0
|
|
82
|
+
Add = 1
|
|
83
|
+
Multiply = 2
|
|
84
|
+
Screen = 3
|
|
85
|
+
|
|
86
|
+
# A texture handle; the id is the host's. A handle of -1 is no texture.
|
|
87
|
+
struct Texture(id: Int) =
|
|
88
|
+
# No texture: what a test's assets hold.
|
|
89
|
+
fun none() -> Texture = Texture(id: -1)
|
|
90
|
+
|
|
91
|
+
# Loads an image relative to the asset root; crashes naming the file if it is missing.
|
|
92
|
+
fun load(io: Io, name: Str) -> Texture =
|
|
93
|
+
id := Gfx.textureLoad(io, name)
|
|
94
|
+
if id < 0
|
|
95
|
+
crash Gfx.missingAsset("texture", name)
|
|
96
|
+
Texture(id: id)
|
|
97
|
+
|
|
98
|
+
# A blank transparent texture to draw into with `renderTarget`; crashes if a side is
|
|
99
|
+
# below 1 or above 8192.
|
|
100
|
+
fun create(io: Io, w: Int, h: Int) -> Texture =
|
|
101
|
+
id := Gfx.textureCreate(io, w, h)
|
|
102
|
+
if id < 0
|
|
103
|
+
crash "cannot create a {w}x{h} texture"
|
|
104
|
+
Texture(id: id)
|
|
105
|
+
|
|
106
|
+
fun width(self, io: Io) -> Int = Gfx.textureWidth(io, self.id)
|
|
107
|
+
|
|
108
|
+
fun height(self, io: Io) -> Int = Gfx.textureHeight(io, self.id)
|
|
109
|
+
|
|
110
|
+
# Linear (true) or nearest (false, the default) filtering.
|
|
111
|
+
fun setSmooth(self, io: Io, smooth: Bool) =
|
|
112
|
+
Gfx.textureSetSmooth(io, self.id, smooth)
|
|
113
|
+
|
|
114
|
+
# The texture at its natural size, top-left at (x, y).
|
|
115
|
+
fun draw(self, io: Io, x: Float, y: Float, tint: Color = Color.White) =
|
|
116
|
+
Gfx.drawTexture(io, self.id, x, y, tint)
|
|
117
|
+
|
|
118
|
+
# Sends the following draws into this texture; `Gfx.renderToScreen` goes back to the screen.
|
|
119
|
+
# Each target has its own camera, which starts as its pixels.
|
|
120
|
+
fun renderTarget(self, io: Io) =
|
|
121
|
+
Gfx.renderTarget(io, self.id)
|
std/game/Group.plum
CHANGED
|
@@ -19,7 +19,7 @@ struct Group[View, Touch](x: Float, y: Float, touch: Touchable, children: List<V
|
|
|
19
19
|
# At (x, y), `ChildrenOnly`.
|
|
20
20
|
fun of(x: Float, y: Float, children: List<View>) -> Group = Group(x: x, y: y, touch: ChildrenOnly, children: children)
|
|
21
21
|
|
|
22
|
-
fun place(self) -> Place =
|
|
22
|
+
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
23
23
|
|
|
24
24
|
fun view(self) -> List<View> = self.children
|
|
25
25
|
|
std/game/History.plum
CHANGED
|
@@ -5,7 +5,7 @@ import std/Float
|
|
|
5
5
|
import std/Bool
|
|
6
6
|
import std/collections/List
|
|
7
7
|
import std/math/Vec2
|
|
8
|
-
import std/game/
|
|
8
|
+
import std/game/Input
|
|
9
9
|
import std/game/Control
|
|
10
10
|
|
|
11
11
|
# A game's own history, for undo and time travel (docs/game.md, "Undo and time travel").
|
|
@@ -66,63 +66,60 @@ struct History(cap: Int, pixels: Vec2, ticks: List<HistoryTick>, count: Int, fut
|
|
|
66
66
|
History(..self, ticks: Cons(r, self.ticks), count: count, future: future, fork: if future.isEmpty() then count else self.fork)
|
|
67
67
|
|
|
68
68
|
# The last user action tick of the timeline, -1 for none.
|
|
69
|
-
fun lastUser(self) -> Int = lastUserFrom(self.ticks, self.count - 1)
|
|
69
|
+
fun lastUser(self) -> Int = History.lastUserFrom(self.ticks, self.count - 1)
|
|
70
70
|
|
|
71
71
|
# How many ticks of the future a redo plays: up to the next user action after the
|
|
72
72
|
# first, or all of them.
|
|
73
|
-
fun nextUser(self) -> Int = nextUserFrom(self.future, 0)
|
|
73
|
+
fun nextUser(self) -> Int = History.nextUserFrom(self.future, 0)
|
|
74
74
|
|
|
75
75
|
# Back at tick `k` (< now): the timeline keeps its first `k` ticks and the rest go to
|
|
76
76
|
# the front of the future. Ticks played since an undo (after `fork`) are dropped when
|
|
77
77
|
# `k` is at or before the fork; a `k` among them starts a new future from `k`.
|
|
78
78
|
fun backTo(self, k: Int) -> History =
|
|
79
|
-
kept := dropN(self.ticks, self.count - k)
|
|
79
|
+
kept := History.dropN(self.ticks, self.count - k)
|
|
80
80
|
moved := if self.future.isEmpty() || k > self.fork
|
|
81
|
-
takeN(self.ticks, self.count - k).reverse()
|
|
81
|
+
History.takeN(self.ticks, self.count - k).reverse()
|
|
82
82
|
else
|
|
83
|
-
takeN(dropN(self.ticks, self.count - self.fork), self.fork - k).reverse().append(self.future)
|
|
83
|
+
History.takeN(History.dropN(self.ticks, self.count - self.fork), self.fork - k).reverse().append(self.future)
|
|
84
84
|
History(..self, ticks: kept, count: k, future: moved, fork: k)
|
|
85
85
|
|
|
86
86
|
# The first `j` ticks of the future moved onto the timeline at `fork`, dropping what was
|
|
87
87
|
# played since; `taken` is what is played next.
|
|
88
88
|
fun forward(self, j: Int) -> Forwarded =
|
|
89
|
-
taken := takeN(self.future, j)
|
|
89
|
+
taken := History.takeN(self.future, j)
|
|
90
|
-
base := dropN(self.ticks, self.count - self.fork)
|
|
90
|
+
base := History.dropN(self.ticks, self.count - self.fork)
|
|
91
91
|
ticks := taken.fold(base, |acc, t| Cons(t, acc))
|
|
92
92
|
count := self.fork + taken.length()
|
|
93
|
-
Forwarded(history: History(..self, ticks: ticks, count: count, future: dropN(self.future, j), fork: count), taken: taken)
|
|
93
|
+
Forwarded(history: History(..self, ticks: ticks, count: count, future: History.dropN(self.future, j), fork: count), taken: taken)
|
|
94
94
|
|
|
95
95
|
# With no future: what was undone can no longer be redone.
|
|
96
96
|
fun cut(self) -> History = History(..self, future: Nil, fork: self.count)
|
|
97
97
|
|
|
98
|
+
fun lastUserFrom(ts: List<HistoryTick>, i: Int) -> Int = match ts
|
|
99
|
+
Nil => -1
|
|
100
|
+
Cons(t, rest) => if t.user then i else History.lastUserFrom(rest, i - 1)
|
|
101
|
+
|
|
102
|
+
fun nextUserFrom(ts: List<HistoryTick>, j: Int) -> Int = match ts
|
|
103
|
+
Nil => j
|
|
104
|
+
Cons(t, rest) => if j > 0 && t.user then j else History.nextUserFrom(rest, j + 1)
|
|
105
|
+
|
|
106
|
+
fun takeN<T>(xs: List<T>, n: Int) -> List<T> = History.takeInto(xs, n, Nil).reverse()
|
|
107
|
+
|
|
108
|
+
fun takeInto<T>(xs: List<T>, n: Int, acc: List<T>) -> List<T> =
|
|
109
|
+
if n <= 0
|
|
110
|
+
acc
|
|
111
|
+
else
|
|
112
|
+
match xs
|
|
113
|
+
Nil => acc
|
|
114
|
+
Cons(x, rest) => History.takeInto(rest, n - 1, Cons(x, acc))
|
|
115
|
+
|
|
116
|
+
fun dropN<T>(xs: List<T>, n: Int) -> List<T> =
|
|
117
|
+
if n <= 0
|
|
118
|
+
xs
|
|
119
|
+
else
|
|
120
|
+
match xs
|
|
121
|
+
Nil => Nil
|
|
122
|
+
Cons(_, rest) => History.dropN(rest, n - 1)
|
|
123
|
+
|
|
98
124
|
# A history moved forward, and the ticks to play.
|
|
99
125
|
struct Forwarded(history: History, taken: List<HistoryTick>)
|
|
100
|
-
|
|
101
|
-
fun lastUserFrom(ts: List<HistoryTick>, i: Int) -> Int = match ts
|
|
102
|
-
Nil => -1
|
|
103
|
-
Cons(t, rest) => if t.user then i else lastUserFrom(rest, i - 1)
|
|
104
|
-
|
|
105
|
-
fun nextUserFrom(ts: List<HistoryTick>, j: Int) -> Int = match ts
|
|
106
|
-
Nil => j
|
|
107
|
-
Cons(t, rest) => if j > 0 && t.user then j else nextUserFrom(rest, j + 1)
|
|
108
|
-
|
|
109
|
-
fun takeN<T>(xs: List<T>, n: Int) -> List<T> = takeInto(xs, n, Nil).reverse()
|
|
110
|
-
|
|
111
|
-
fun takeInto<T>(xs: List<T>, n: Int, acc: List<T>) -> List<T> =
|
|
112
|
-
if n <= 0
|
|
113
|
-
acc
|
|
114
|
-
else
|
|
115
|
-
match xs
|
|
116
|
-
Nil => acc
|
|
117
|
-
Cons(x, rest) => takeInto(rest, n - 1, Cons(x, acc))
|
|
118
|
-
|
|
119
|
-
fun dropN<T>(xs: List<T>, n: Int) -> List<T> =
|
|
120
|
-
if n <= 0
|
|
121
|
-
xs
|
|
122
|
-
else
|
|
123
|
-
match xs
|
|
124
|
-
Nil => Nil
|
|
125
|
-
Cons(_, rest) => dropN(rest, n - 1)
|
|
126
|
-
|
|
127
|
-
fun histTick(user: Bool) -> HistoryTick =
|
|
128
|
-
HistoryTick(input: Input.create(), pixels: Vec2(x: 800.0, y: 600.0), dt: 0.1, user: user, asks: Nil)
|
std/game/{input.plum → Input.plum}
RENAMED
|
@@ -16,13 +16,6 @@ import std/game/Replay
|
|
|
16
16
|
# nothing else, so a recorded list of inputs replays the game exactly (see
|
|
17
17
|
# std/game/Replay and docs/game.md, "Record and replay").
|
|
18
18
|
|
|
19
|
-
extern fun isKeyDown(io: Io, k: Int) -> Bool
|
|
20
|
-
extern fun isKeyPressed(io: Io, k: Int) -> Bool
|
|
21
|
-
extern fun mousePos(io: Io) -> Vec2
|
|
22
|
-
extern fun isMouseDown(io: Io, b: Int) -> Bool
|
|
23
|
-
extern fun isMousePressed(io: Io, b: Int) -> Bool
|
|
24
|
-
extern fun mouseWheel(io: Io) -> Float
|
|
25
|
-
|
|
26
19
|
# Key codes (GLFW numbering) for `isKeyDown` / `isKeyPressed`.
|
|
27
20
|
# Letter keys are `KeyA`..`KeyZ` (a constant name needs two characters). Super is Cmd on a
|
|
28
21
|
# Mac, the Windows key elsewhere.
|
|
@@ -104,6 +97,14 @@ enum InputEvent =
|
|
|
104
97
|
| KeyDown(key: Int)
|
|
105
98
|
| KeyUp(key: Int)
|
|
106
99
|
|
|
100
|
+
# The host's input, read only by `poll` and `Input.poll`.
|
|
101
|
+
extern fun isKeyDown(io: Io, k: Int) -> Bool
|
|
102
|
+
extern fun isKeyPressed(io: Io, k: Int) -> Bool
|
|
103
|
+
extern fun mousePos(io: Io) -> Vec2
|
|
104
|
+
extern fun isMouseDown(io: Io, b: Int) -> Bool
|
|
105
|
+
extern fun isMousePressed(io: Io, b: Int) -> Bool
|
|
106
|
+
extern fun mouseWheel(io: Io) -> Float
|
|
107
|
+
|
|
107
108
|
# This frame's inputs: the host's mouse buttons and keys compared with `prev`, what was
|
|
108
109
|
# held after the last poll. One pointer input (down, up or move) comes first, then a
|
|
109
110
|
# ButtonDown or ButtonUp for the right and the middle button if it changed, then a
|
|
@@ -156,7 +157,7 @@ enum InputEvent =
|
|
|
156
157
|
out.push(ButtonDown(button: b, x: m.x, y: m.y))
|
|
157
158
|
out.push(ButtonUp(button: b, x: m.x, y: m.y))
|
|
158
159
|
|
|
159
|
-
# Every `Key` code in std/game/
|
|
160
|
+
# Every `Key` code in std/game/Input, in increasing order.
|
|
160
161
|
fun keyCodes() -> Slice<Int> =
|
|
161
162
|
out := [Key.Space].toSlice()
|
|
162
163
|
for i := range 10
|
|
@@ -234,15 +235,15 @@ struct Held(mouse: Bool, keys: Slice<Int>, buttons: Slice<Int>) =
|
|
|
234
235
|
ButtonUp(b, _, _) => Held(..self, buttons: withOrWithout(self.buttons, b, false))
|
|
235
236
|
_ => self
|
|
236
237
|
|
|
237
|
-
# A fresh copy of `xs` with `x` added (`add`, once) or removed.
|
|
238
|
+
# A fresh copy of `xs` with `x` added (`add`, once) or removed.
|
|
238
|
-
fun withOrWithout(xs: Slice<Int>, x: Int, add: Bool) -> Slice<Int> =
|
|
239
|
+
fun withOrWithout(xs: Slice<Int>, x: Int, add: Bool) -> Slice<Int> =
|
|
239
|
-
|
|
240
|
+
out := Slice<Int>()
|
|
240
|
-
|
|
241
|
+
for i := range xs.length()
|
|
241
|
-
|
|
242
|
+
if xs.at(i) != x
|
|
242
|
-
|
|
243
|
+
out.push(xs.at(i))
|
|
243
|
-
|
|
244
|
+
if add
|
|
244
|
-
|
|
245
|
+
out.push(x)
|
|
245
|
-
|
|
246
|
+
out
|
|
246
247
|
|
|
247
248
|
# A poll's inputs and what is held after them (pass it to the next poll).
|
|
248
249
|
struct Polled(inputs: Slice<InputEvent>, held: Held)
|
|
@@ -259,7 +260,7 @@ struct Input(held: Held, events: Slice<InputEvent>, pos: Vec2, wheel: Float) =
|
|
|
259
260
|
# The input of this frame: the host polled once and compared with what was held before.
|
|
260
261
|
fun poll(self, io: Io) -> Input =
|
|
261
262
|
p := InputEvent.poll(io, self.held)
|
|
262
|
-
Input(held: p.held, events: p.inputs, pos: mousePos(io), wheel: mouseWheel(io))
|
|
263
|
+
Input(held: p.held, events: p.inputs, pos: InputEvent.mousePos(io), wheel: InputEvent.mouseWheel(io))
|
|
263
264
|
|
|
264
265
|
# Held at the start of this frame (a `Key` code).
|
|
265
266
|
fun isKeyDown(self, key: Int) -> Bool =
|
|
@@ -323,13 +324,13 @@ struct Clock(pending: Input, acc: Float, n: Int, ticks: Int, mode: Int, played:
|
|
|
323
324
|
# Polls the host once for a frame `dt` seconds long. While the host records, the
|
|
324
325
|
# frame's ticks go to it; while it replays, the host's input isn't read.
|
|
325
326
|
fun frame(self, io: Io, dt: Float) -> Clock =
|
|
326
|
-
mode :=
|
|
327
|
+
mode := Replay.replayMode(io)
|
|
327
328
|
if mode == ReplayMode.Replaying
|
|
328
329
|
self.loaded(io).paced(dt)
|
|
329
330
|
else
|
|
330
331
|
c := self.accept(self.pending.poll(io), dt)
|
|
331
332
|
if mode == ReplayMode.Recording
|
|
332
|
-
|
|
333
|
+
Replay.replayAppend(io, Replay.ticksText(c.dueInputs()))
|
|
333
334
|
c
|
|
334
335
|
|
|
335
336
|
# The clock replaying the host's replay: this one once it is loaded.
|
|
@@ -337,7 +338,7 @@ struct Clock(pending: Input, acc: Float, n: Int, ticks: Int, mode: Int, played:
|
|
|
337
338
|
if self.mode == ReplayMode.Replaying
|
|
338
339
|
self
|
|
339
340
|
else
|
|
340
|
-
match Replay.decode(
|
|
341
|
+
match Replay.decode(Replay.replayText(io))
|
|
341
342
|
Ok(r) => Clock.playing(r)
|
|
342
343
|
Err(why) => Clock.unreadable(why)
|
|
343
344
|
|
|
@@ -390,6 +391,3 @@ struct Clock(pending: Input, acc: Float, n: Int, ticks: Int, mode: Int, played:
|
|
|
390
391
|
self.ticked().collectDue(out)
|
|
391
392
|
else
|
|
392
393
|
out
|
|
393
|
-
|
|
394
|
-
fun inHeld(keys: Slice<Int>) -> Held =
|
|
395
|
-
Held.keysOnly(keys)
|
std/game/Label.plum
CHANGED
|
@@ -11,7 +11,7 @@ import std/math/Vec2
|
|
|
11
11
|
import std/math/Affine
|
|
12
12
|
import std/math/Aabb
|
|
13
13
|
import std/game/Color
|
|
14
|
-
import std/game/
|
|
14
|
+
import std/game/Gfx
|
|
15
15
|
import std/game/Place
|
|
16
16
|
import std/game/View
|
|
17
17
|
import std/game/Sized
|
|
@@ -19,10 +19,19 @@ import std/game/Touch
|
|
|
19
19
|
|
|
20
20
|
# Where a `Label` sits on its x: its text starting there (`Left`, the default), centred
|
|
21
21
|
# on it (`Centre`) or ending there (`Right`).
|
|
22
|
+
enum Align =
|
|
23
|
+
| Left
|
|
24
|
+
| Centre
|
|
25
|
+
| Right
|
|
26
|
+
|
|
22
|
-
|
|
27
|
+
# How far left of the anchor text `w` wide starts.
|
|
28
|
+
fun lead(self, w: Float) -> Float = match self
|
|
29
|
+
Left => 0.0
|
|
30
|
+
Centre => w / 2.0
|
|
31
|
+
Right => w
|
|
23
32
|
|
|
24
33
|
# Text whose top is at its place's y, which starts at (x, y), placed on its x by `align`,
|
|
25
|
-
# `size` high, in `color` (painted at its alpha). Its width is an estimate (`textWidth`:
|
|
34
|
+
# `size` high, in `color` (painted at its alpha). Its width is an estimate (`Label.textWidth`:
|
|
26
35
|
# 0.6 of its size per letter), which its bounds and its alignment go by. It turns with its
|
|
27
36
|
# place. It is decoration unless `touch: Enabled` (std/game/Touch). In a view it is made
|
|
28
37
|
# again each frame, so its text is read from the state as it is made
|
|
@@ -32,32 +41,26 @@ struct Label[View, Sized, Touch](x: Float, y: Float, text: Str, size: Float, col
|
|
|
32
41
|
fun of(x: Float, y: Float, text: Str, size: Float, color: Color, align: Align = Left) -> Label =
|
|
33
42
|
Label(x: x, y: y, text: text, size: size, color: color, align: align, touch: Disabled)
|
|
34
43
|
|
|
35
|
-
fun place(self) -> Place =
|
|
44
|
+
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
36
45
|
|
|
37
46
|
# How far left of its place its text starts.
|
|
38
|
-
fun lead(self) -> Float =
|
|
47
|
+
fun lead(self) -> Float = self.align.lead(Label.textWidth(self.text, self.size))
|
|
39
48
|
|
|
40
|
-
fun bounds(self) -> Aabb = Aabb.of(0.0 - self.lead(), 0.0, textWidth(self.text, self.size), self.size)
|
|
49
|
+
fun bounds(self) -> Aabb = Aabb.of(0.0 - self.lead(), 0.0, Label.textWidth(self.text, self.size), self.size)
|
|
41
50
|
|
|
42
51
|
fun touch(self) -> Touchable = self.touch
|
|
43
52
|
|
|
44
|
-
fun paintSelf(self, io: Io, m: Affine, alpha: Float) =
|
|
53
|
+
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = Label.draw(io, m, alpha * self.color.a, self.text, self.size, self.color, self.align)
|
|
45
|
-
|
|
54
|
+
|
|
46
|
-
# An estimate of how wide `s` is at `size`: 0.6 of the size per letter.
|
|
55
|
+
# An estimate of how wide `s` is at `size`: 0.6 of the size per letter.
|
|
47
|
-
fun textWidth(s: Str, size: Float) -> Float = size * 0.6 * Float(s.length())
|
|
56
|
+
fun textWidth(s: Str, size: Float) -> Float = size * 0.6 * Float(s.length())
|
|
48
|
-
|
|
49
|
-
|
|
57
|
+
|
|
50
|
-
fun leadOf(align: Align, w: Float) -> Float = match align
|
|
51
|
-
Left => 0.0
|
|
52
|
-
Centre => w / 2.0
|
|
53
|
-
Right => w
|
|
54
|
-
|
|
55
|
-
# Draws text with its top on the transform `m`'s origin, placed on it by `align`, turned
|
|
58
|
+
# Draws text with its top on the transform `m`'s origin, placed on it by `align`, turned
|
|
56
|
-
# and scaled as `m` turns and scales it. Paints with `alpha` in place of the colour's
|
|
59
|
+
# and scaled as `m` turns and scales it. Paints with `alpha` in place of the colour's
|
|
57
|
-
# alpha, as `
|
|
60
|
+
# alpha, as `Box.draw` does. Unturned text is a `Gfx.drawText`, turned text a `Gfx.drawTextEx`.
|
|
58
|
-
fun
|
|
61
|
+
fun draw(io: Io, m: Affine, alpha: Float, s: Str, size: Float, color: Color, align: Align = Left) =
|
|
59
|
-
|
|
62
|
+
p := m.apply(Vec2(x: 0.0 - align.lead(Label.textWidth(s, size)), y: 0.0))
|
|
60
|
-
|
|
63
|
+
if m.b == 0.0 && m.a > 0.0
|
|
61
|
-
|
|
64
|
+
Gfx.drawText(io, s, p.x, p.y, size * m.scaleY(), color.withAlpha(alpha))
|
|
62
|
-
|
|
65
|
+
else
|
|
63
|
-
|
|
66
|
+
Gfx.drawTextEx(io, s, p.x, p.y, size * m.scaleY(), m.rotation(), color.withAlpha(alpha))
|
std/game/Overlay.plum
CHANGED
|
@@ -11,7 +11,7 @@ import std/math/Vec2
|
|
|
11
11
|
import std/math/Affine
|
|
12
12
|
import std/math/Aabb
|
|
13
13
|
import std/game/Color
|
|
14
|
-
import std/game/
|
|
14
|
+
import std/game/Gfx
|
|
15
15
|
import std/game/Place
|
|
16
16
|
import std/game/View
|
|
17
17
|
import std/game/Sized
|
|
@@ -36,7 +36,7 @@ struct Overlay[View, Sized, Touch](x: Float, y: Float, w: Float, h: Float, color
|
|
|
36
36
|
fun of(w: Float, h: Float, children: List<View>) -> Overlay =
|
|
37
37
|
Overlay(x: 0.0, y: 0.0, w: w, h: h, color: Color(r: 0.0, g: 0.0, b: 0.0, a: 0.6), touch: Enabled, children: children)
|
|
38
38
|
|
|
39
|
-
fun place(self) -> Place =
|
|
39
|
+
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
40
40
|
|
|
41
41
|
fun bounds(self) -> Aabb = Aabb.of(0.0, 0.0, self.w, self.h)
|
|
42
42
|
|
|
@@ -44,4 +44,4 @@ struct Overlay[View, Sized, Touch](x: Float, y: Float, w: Float, h: Float, color
|
|
|
44
44
|
|
|
45
45
|
fun touch(self) -> Touchable = self.touch
|
|
46
46
|
|
|
47
|
-
fun paintSelf(self, io: Io, m: Affine, alpha: Float) =
|
|
47
|
+
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = Box.draw(io, m, alpha * self.color.a, self.w, self.h, self.color)
|
std/game/Place.plum
CHANGED
|
@@ -10,9 +10,9 @@ import std/math/Affine
|
|
|
10
10
|
# is drawn under it. Every view has one (`View.place`); a view's draw turns it into the
|
|
11
11
|
# actor's transform with `local` (std/game/View).
|
|
12
12
|
#
|
|
13
|
-
# p :=
|
|
13
|
+
# p := Place.at(10.0, 20.0) # unscaled, unturned
|
|
14
|
-
# q :=
|
|
14
|
+
# q := Place.at(400.0, 300.0).rotateTo(0.5)
|
|
15
|
-
# r := Place(..
|
|
15
|
+
# r := Place(..Place.at(0.0, 0.0), scaleX: 2.0)
|
|
16
16
|
#
|
|
17
17
|
# (Struct fields have no defaults in the language yet, so `at` and `turned` stand in for
|
|
18
18
|
# them.)
|
|
@@ -39,6 +39,18 @@ struct Place(
|
|
|
39
39
|
e: self.x + self.originX - {a * self.originX + c * self.originY},
|
|
40
40
|
f: self.y + self.originY - {b * self.originX + d * self.originY})
|
|
41
41
|
|
|
42
|
+
# At (x, y), unscaled and unturned.
|
|
43
|
+
fun at(x: Float, y: Float) -> Place =
|
|
44
|
+
Place(x: x, y: y, scaleX: 1.0, scaleY: 1.0, rotation: 0.0, originX: 0.0, originY: 0.0, alpha: 1.0)
|
|
45
|
+
|
|
46
|
+
# At (x, y), turning and scaling about its own (ox, oy).
|
|
47
|
+
fun pivoted(x: Float, y: Float, ox: Float, oy: Float) -> Place =
|
|
48
|
+
Place(x: x, y: y, scaleX: 1.0, scaleY: 1.0, rotation: 0.0, originX: ox, originY: oy, alpha: 1.0)
|
|
49
|
+
|
|
50
|
+
# At (x, y), scaled by `s` and turned by `r` radians about its own (0, 0).
|
|
51
|
+
fun turned(x: Float, y: Float, s: Float, r: Float) -> Place =
|
|
52
|
+
Place(x: x, y: y, scaleX: s, scaleY: s, rotation: r, originX: 0.0, originY: 0.0, alpha: 1.0)
|
|
53
|
+
|
|
42
54
|
# Copies with one thing changed.
|
|
43
55
|
|
|
44
56
|
fun moveTo(self, x: Float, y: Float) -> Place = Place(..self, x: x, y: y)
|
|
@@ -62,15 +74,3 @@ struct Place(
|
|
|
62
74
|
|
|
63
75
|
# A point of the parent's coordinates in its own.
|
|
64
76
|
fun toLocal(self, p: Vec2) -> Vec2 = self.local().inverse().apply(p)
|
|
65
|
-
|
|
66
|
-
# At (x, y), unscaled and unturned.
|
|
67
|
-
fun at(x: Float, y: Float) -> Place =
|
|
68
|
-
Place(x: x, y: y, scaleX: 1.0, scaleY: 1.0, rotation: 0.0, originX: 0.0, originY: 0.0, alpha: 1.0)
|
|
69
|
-
|
|
70
|
-
# At (x, y), turning and scaling about its own (ox, oy).
|
|
71
|
-
fun pivoted(x: Float, y: Float, ox: Float, oy: Float) -> Place =
|
|
72
|
-
Place(x: x, y: y, scaleX: 1.0, scaleY: 1.0, rotation: 0.0, originX: ox, originY: oy, alpha: 1.0)
|
|
73
|
-
|
|
74
|
-
# At (x, y), scaled by `s` and turned by `r` radians about its own (0, 0).
|
|
75
|
-
fun turned(x: Float, y: Float, s: Float, r: Float) -> Place =
|
|
76
|
-
Place(x: x, y: y, scaleX: s, scaleY: s, rotation: r, originX: 0.0, originY: 0.0, alpha: 1.0)
|
std/game/Replay.plum
CHANGED
|
@@ -10,14 +10,14 @@ import std/Slice
|
|
|
10
10
|
import std/Buffer
|
|
11
11
|
import std/math/Vec2
|
|
12
12
|
import std/Io
|
|
13
|
-
import std/game/
|
|
13
|
+
import std/game/Input
|
|
14
14
|
|
|
15
15
|
# Record and replay. A game's run is its seed and one `Input` per tick: `main` seeds the
|
|
16
16
|
# game's `Random` from `io.randomSeed()` and each tick's `update` gets that tick's
|
|
17
17
|
# `Input`, so the same seed and inputs play the same game again, tick for tick.
|
|
18
18
|
#
|
|
19
19
|
# The game hosts do both (docs/game.md, "Record and replay"). While recording, the host
|
|
20
|
-
# remembers what `io.randomSeed()` returned and the game's `Clock` (std/game/
|
|
20
|
+
# remembers what `io.randomSeed()` returned and the game's `Clock` (std/game/Input) hands
|
|
21
21
|
# it each frame's ticks as lines of text. While replaying, `io.randomSeed()` returns the
|
|
22
22
|
# recorded seed and the ticks take their input from the replay, never from the host. A
|
|
23
23
|
# game's `main` doesn't change for either.
|
|
@@ -36,13 +36,6 @@ import std/game/input
|
|
|
36
36
|
# or `nan` (`toStr` rounds to six digits). The screen size is the host's at the start of
|
|
37
37
|
# the recording, and the replaying host reports it to the game; 0 means not recorded.
|
|
38
38
|
|
|
39
|
-
# Whether the host is recording or replaying this run (a `ReplayMode`, asked once a frame).
|
|
40
|
-
extern fun replayMode(io: Io) -> Int
|
|
41
|
-
# The text of the replay the host is replaying.
|
|
42
|
-
extern fun replayText(io: Io) -> Str
|
|
43
|
-
# Hands the recording host the text of one or more ticks.
|
|
44
|
-
extern fun replayAppend(io: Io, ticks: Str)
|
|
45
|
-
|
|
46
39
|
primitive ReplayMode =
|
|
47
40
|
Off = 0
|
|
48
41
|
Recording = 1
|
|
@@ -51,6 +44,13 @@ primitive ReplayMode =
|
|
|
51
44
|
# A recorded run: the seed `io.randomSeed()` gave `main`, the screen size at the start,
|
|
52
45
|
# and the input of every tick in order.
|
|
53
46
|
struct Replay(seed: Int, width: Int, height: Int, ticks: Slice<Input>) =
|
|
47
|
+
# Whether the host is recording or replaying this run (a `ReplayMode`, asked once a frame).
|
|
48
|
+
extern fun replayMode(io: Io) -> Int
|
|
49
|
+
# The text of the replay the host is replaying.
|
|
50
|
+
extern fun replayText(io: Io) -> Str
|
|
51
|
+
# Hands the recording host the text of one or more ticks.
|
|
52
|
+
extern fun replayAppend(io: Io, ticks: Str)
|
|
53
|
+
|
|
54
54
|
fun create(seed: Int) -> Replay =
|
|
55
55
|
Replay(seed: seed, width: 0, height: 0, ticks: Slice<Input>())
|
|
56
56
|
|
|
@@ -120,10 +120,10 @@ struct Replay(seed: Int, width: Int, height: Int, ticks: Slice<Input>) =
|
|
|
120
120
|
b.write("{keys.at(i)}")
|
|
121
121
|
mask := input.held.buttons.fold(if input.held.mouse then 1 else 0, |m, x| m | {1 << x})
|
|
122
122
|
b.write(" {mask} ")
|
|
123
|
-
b.write("{floatCode(input.pos.x)} {floatCode(input.pos.y)} {floatCode(input.wheel)}")
|
|
123
|
+
b.write("{Replay.floatCode(input.pos.x)} {Replay.floatCode(input.pos.y)} {Replay.floatCode(input.wheel)}")
|
|
124
124
|
for i := range input.events.length()
|
|
125
125
|
b.write(" ")
|
|
126
|
-
b.write(eventCode(input.events.at(i)))
|
|
126
|
+
b.write(Replay.eventCode(input.events.at(i)))
|
|
127
127
|
b.write("\n")
|
|
128
128
|
b.toStr()
|
|
129
129
|
|
|
@@ -134,12 +134,12 @@ struct Replay(seed: Int, width: Int, height: Int, ticks: Slice<Input>) =
|
|
|
134
134
|
if mask < 0 || mask > 7
|
|
135
135
|
None
|
|
136
136
|
else
|
|
137
|
-
keys := if t.at(0) == "-" then Some(Slice<Int>()) else parseKeys(t.at(0).split(",", 0), 0, Slice<Int>())
|
|
137
|
+
keys := if t.at(0) == "-" then Some(Slice<Int>()) else Replay.parseKeys(t.at(0).split(",", 0), 0, Slice<Int>())
|
|
138
138
|
held := keys?
|
|
139
|
-
x := parseFloatCode(t.at(2))?
|
|
139
|
+
x := Replay.parseFloatCode(t.at(2))?
|
|
140
|
-
y := parseFloatCode(t.at(3))?
|
|
140
|
+
y := Replay.parseFloatCode(t.at(3))?
|
|
141
|
-
wheel := parseFloatCode(t.at(4))?
|
|
141
|
+
wheel := Replay.parseFloatCode(t.at(4))?
|
|
142
|
-
events := parseEvents(t, 5, Slice<InputEvent>())?
|
|
142
|
+
events := Replay.parseEvents(t, 5, Slice<InputEvent>())?
|
|
143
143
|
buttons := Slice<Int>()
|
|
144
144
|
if {mask & 2} != 0
|
|
145
145
|
buttons.push(Mouse.Right)
|
|
@@ -147,118 +147,117 @@ struct Replay(seed: Int, width: Int, height: Int, ticks: Slice<Input>) =
|
|
|
147
147
|
buttons.push(Mouse.Middle)
|
|
148
148
|
Some(Input(held: Held(mouse: {mask & 1} != 0, keys: held, buttons: buttons), events: events, pos: Vec2(x: x, y: y), wheel: wheel))
|
|
149
149
|
|
|
150
|
-
fun parseKeys(ks: Slice<Str>, i: Int, out: Slice<Int>) -> Option<Slice<Int>> =
|
|
150
|
+
fun parseKeys(ks: Slice<Str>, i: Int, out: Slice<Int>) -> Option<Slice<Int>> =
|
|
151
|
-
|
|
151
|
+
if i >= ks.length()
|
|
152
|
-
|
|
152
|
+
Some(out)
|
|
153
|
-
|
|
153
|
+
else
|
|
154
|
-
|
|
154
|
+
match Int.parse(ks.at(i)).ok()
|
|
155
|
-
|
|
155
|
+
Some(k) =>
|
|
156
|
-
|
|
156
|
+
out.push(k)
|
|
157
|
-
|
|
157
|
+
Replay.parseKeys(ks, i + 1, out)
|
|
158
|
-
|
|
158
|
+
None => None
|
|
159
|
-
|
|
160
|
-
fun parseEvents(t: Slice<Str>, i: Int, out: Slice<InputEvent>) -> Option<Slice<InputEvent>> =
|
|
161
|
-
if i >= t.length()
|
|
162
|
-
Some(out)
|
|
163
|
-
else
|
|
164
|
-
match parseEvent(t.at(i))
|
|
165
|
-
Some(e) =>
|
|
166
|
-
out.push(e)
|
|
167
|
-
parseEvents(t, i + 1, out)
|
|
168
|
-
None => None
|
|
169
|
-
|
|
170
|
-
# A Float written as the integer `m` times 2^`e`.
|
|
171
|
-
struct Scaled(m: Float, e: Int)
|
|
172
|
-
|
|
173
|
-
# `x` exactly, as `m` or `m:e` for the integer m times 2^e, or as `-0`, `inf`, `-inf` or
|
|
174
|
-
# `nan`.
|
|
175
|
-
fun floatCode(x: Float) -> Str =
|
|
176
|
-
if x.isNaN()
|
|
177
|
-
"nan"
|
|
178
|
-
else if x.isInfinite()
|
|
179
|
-
if x > 0.0 then "inf" else "-inf"
|
|
180
|
-
else if x == 0.0 && 1.0 / x < 0.0
|
|
181
|
-
"-0"
|
|
182
|
-
else
|
|
183
|
-
s := wholeUp(halveBig(x, 0))
|
|
184
|
-
if s.e == 0 then "{Int(s.m)}" else "{Int(s.m)}:{s.e}"
|
|
185
159
|
|
|
186
|
-
# A Float this large is an even whole number, so halving it is exact.
|
|
187
|
-
fun halveBig(m: Float, e: Int) -> Scaled =
|
|
188
|
-
|
|
160
|
+
fun parseEvents(t: Slice<Str>, i: Int, out: Slice<InputEvent>) -> Option<Slice<InputEvent>> =
|
|
161
|
+
if i >= t.length()
|
|
162
|
+
Some(out)
|
|
163
|
+
else
|
|
164
|
+
match Replay.parseEvent(t.at(i))
|
|
165
|
+
Some(e) =>
|
|
166
|
+
out.push(e)
|
|
167
|
+
Replay.parseEvents(t, i + 1, out)
|
|
168
|
+
None => None
|
|
189
169
|
|
|
190
|
-
#
|
|
170
|
+
# `x` exactly, as `m` or `m:e` for the integer m times 2^e, or as `-0`, `inf`, `-inf` or
|
|
171
|
+
# `nan`.
|
|
191
|
-
fun
|
|
172
|
+
fun floatCode(x: Float) -> Str =
|
|
173
|
+
if x.isNaN()
|
|
174
|
+
"nan"
|
|
175
|
+
else if x.isInfinite()
|
|
176
|
+
if x > 0.0 then "inf" else "-inf"
|
|
177
|
+
else if x == 0.0 && 1.0 / x < 0.0
|
|
178
|
+
"-0"
|
|
179
|
+
else
|
|
180
|
+
s := Scaled.halveBig(x, 0).wholeUp()
|
|
192
|
-
|
|
181
|
+
if s.e == 0 then "{Int(s.m)}" else "{Int(s.m)}:{s.e}"
|
|
193
182
|
|
|
194
|
-
fun parseFloatCode(s: Str) -> Option<Float> =
|
|
183
|
+
fun parseFloatCode(s: Str) -> Option<Float> =
|
|
195
|
-
|
|
184
|
+
if s == "nan"
|
|
196
|
-
|
|
185
|
+
Some(0.0 / 0.0)
|
|
197
|
-
|
|
186
|
+
else if s == "inf"
|
|
198
|
-
|
|
187
|
+
Some(Float.Max * 2.0)
|
|
199
|
-
|
|
188
|
+
else if s == "-inf"
|
|
200
|
-
|
|
189
|
+
Some(Float.Max * -2.0)
|
|
201
|
-
|
|
190
|
+
else if s == "-0"
|
|
202
|
-
|
|
191
|
+
Some(0.0 * -1.0)
|
|
203
|
-
|
|
192
|
+
else
|
|
204
|
-
|
|
193
|
+
parts := s.split(":", 2)
|
|
205
|
-
|
|
194
|
+
m := Int.parse(parts.at(0)).ok()?
|
|
206
|
-
|
|
195
|
+
e := if parts.length() == 2 then Int.parse(parts.at(1)).ok() else Some(0)
|
|
207
|
-
|
|
196
|
+
exp := e?
|
|
208
|
-
|
|
197
|
+
# No Float needs more halvings or doublings than these.
|
|
209
|
-
|
|
198
|
+
if exp > 1100 || exp < -1200 then None else Some(Replay.scaleBy(Float(m), exp))
|
|
210
199
|
|
|
211
|
-
# `v` times 2^`e`, one exact halving or doubling at a time.
|
|
200
|
+
# `v` times 2^`e`, one exact halving or doubling at a time.
|
|
212
|
-
fun scaleBy(v: Float, e: Int) -> Float =
|
|
201
|
+
fun scaleBy(v: Float, e: Int) -> Float =
|
|
213
|
-
|
|
202
|
+
if e > 0
|
|
214
|
-
|
|
203
|
+
Replay.scaleBy(v * 2.0, e - 1)
|
|
215
|
-
|
|
204
|
+
else if e < 0
|
|
216
|
-
|
|
205
|
+
Replay.scaleBy(v / 2.0, e + 1)
|
|
217
|
-
|
|
206
|
+
else
|
|
218
|
-
|
|
207
|
+
v
|
|
219
208
|
|
|
220
|
-
fun eventCode(e: InputEvent) -> Str = match e
|
|
209
|
+
fun eventCode(e: InputEvent) -> Str = match e
|
|
221
|
-
|
|
210
|
+
KeyDown(k) => "K{k}"
|
|
222
|
-
|
|
211
|
+
KeyUp(k) => "k{k}"
|
|
223
|
-
|
|
212
|
+
PointerDown(x, y) => "D{Replay.floatCode(x)},{Replay.floatCode(y)}"
|
|
224
|
-
|
|
213
|
+
PointerUp(x, y) => "U{Replay.floatCode(x)},{Replay.floatCode(y)}"
|
|
225
|
-
|
|
214
|
+
PointerMove(x, y) => "M{Replay.floatCode(x)},{Replay.floatCode(y)}"
|
|
226
|
-
|
|
215
|
+
ButtonDown(n, x, y) => "B{n},{Replay.floatCode(x)},{Replay.floatCode(y)}"
|
|
227
|
-
|
|
216
|
+
ButtonUp(n, x, y) => "b{n},{Replay.floatCode(x)},{Replay.floatCode(y)}"
|
|
228
217
|
|
|
229
|
-
fun parseEvent(tok: Str) -> Option<InputEvent> =
|
|
218
|
+
fun parseEvent(tok: Str) -> Option<InputEvent> =
|
|
230
|
-
|
|
219
|
+
if tok.length() < 2
|
|
231
|
-
|
|
220
|
+
None
|
|
232
|
-
else
|
|
233
|
-
kind := tok.byteAt(0)
|
|
234
|
-
rest := tok.sub(1, tok.length())
|
|
235
|
-
if kind == 75 || kind == 107
|
|
236
|
-
Int.parse(rest).ok().map(|k| if kind == 75 then KeyDown(key: k) else KeyUp(key: k))
|
|
237
|
-
else if kind == 66 || kind == 98
|
|
238
|
-
parts := rest.split(",", 2)
|
|
239
|
-
if parts.length() != 2
|
|
240
|
-
None
|
|
241
|
-
else
|
|
242
|
-
n := Int.parse(parts.at(0)).ok()?
|
|
243
|
-
at := parseEvent("M{parts.at(1)}")?
|
|
244
|
-
p := at.position()?
|
|
245
|
-
Some(if kind == 66 then ButtonDown(button: n, x: p.x, y: p.y) else ButtonUp(button: n, x: p.x, y: p.y))
|
|
246
221
|
else
|
|
222
|
+
kind := tok.byteAt(0)
|
|
223
|
+
rest := tok.sub(1, tok.length())
|
|
224
|
+
if kind == 75 || kind == 107
|
|
225
|
+
Int.parse(rest).ok().map(|k| if kind == 75 then KeyDown(key: k) else KeyUp(key: k))
|
|
226
|
+
else if kind == 66 || kind == 98
|
|
247
|
-
|
|
227
|
+
parts := rest.split(",", 2)
|
|
248
|
-
|
|
228
|
+
if parts.length() != 2
|
|
249
|
-
|
|
229
|
+
None
|
|
250
|
-
else
|
|
251
|
-
x := parseFloatCode(xy.at(0))?
|
|
252
|
-
y := parseFloatCode(xy.at(1))?
|
|
253
|
-
if kind == 68
|
|
254
|
-
Some(PointerDown(x: x, y: y))
|
|
255
|
-
else if kind == 85
|
|
256
|
-
Some(PointerUp(x: x, y: y))
|
|
257
|
-
else if kind == 77
|
|
258
|
-
Some(PointerMove(x: x, y: y))
|
|
259
230
|
else
|
|
231
|
+
n := Int.parse(parts.at(0)).ok()?
|
|
232
|
+
at := Replay.parseEvent("M{parts.at(1)}")?
|
|
233
|
+
p := at.position()?
|
|
234
|
+
Some(if kind == 66 then ButtonDown(button: n, x: p.x, y: p.y) else ButtonUp(button: n, x: p.x, y: p.y))
|
|
235
|
+
else
|
|
236
|
+
xy := rest.split(",", 0)
|
|
237
|
+
if xy.length() != 2
|
|
260
238
|
None
|
|
239
|
+
else
|
|
240
|
+
x := Replay.parseFloatCode(xy.at(0))?
|
|
241
|
+
y := Replay.parseFloatCode(xy.at(1))?
|
|
242
|
+
if kind == 68
|
|
243
|
+
Some(PointerDown(x: x, y: y))
|
|
244
|
+
else if kind == 85
|
|
245
|
+
Some(PointerUp(x: x, y: y))
|
|
246
|
+
else if kind == 77
|
|
247
|
+
Some(PointerMove(x: x, y: y))
|
|
248
|
+
else
|
|
249
|
+
None
|
|
250
|
+
|
|
251
|
+
# The same input, bit for bit (a NaN equals a NaN): what a replay must give back.
|
|
252
|
+
fun sameInput(a: Input, b: Input) -> Bool =
|
|
253
|
+
Replay.line(a) == Replay.line(b)
|
|
254
|
+
|
|
255
|
+
# A Float written as the integer `m` times 2^`e`.
|
|
256
|
+
struct Scaled(m: Float, e: Int) =
|
|
257
|
+
# A Float this large is an even whole number, so halving it is exact.
|
|
258
|
+
fun halveBig(m: Float, e: Int) -> Scaled =
|
|
259
|
+
if m.abs() >= 4611686018427387904.0 then Scaled.halveBig(m / 2.0, e + 1) else Scaled(m: m, e: e)
|
|
261
260
|
|
|
262
|
-
#
|
|
261
|
+
# Doubling is exact too; it ends once m is whole (at most 1074 times).
|
|
263
|
-
fun
|
|
262
|
+
fun wholeUp(self) -> Scaled =
|
|
264
|
-
|
|
263
|
+
if self.m != Float(Int(self.m)) then Scaled(m: self.m * 2.0, e: self.e - 1).wholeUp() else self
|
std/game/Screen.plum
CHANGED
|
@@ -11,7 +11,7 @@ import std/math/Vec2
|
|
|
11
11
|
import std/math/Affine
|
|
12
12
|
import std/math/Aabb
|
|
13
13
|
import std/game/Color
|
|
14
|
-
import std/game/
|
|
14
|
+
import std/game/Input
|
|
15
15
|
import std/game/View
|
|
16
16
|
import std/game/Viewport
|
|
17
17
|
import std/game/Control
|
|
@@ -33,10 +33,10 @@ import std/game/ScreenSlice
|
|
|
33
33
|
# `Actor`s (std/game/View), unless it says `steps()` is false: it calls the screen's
|
|
34
34
|
# `stepped(dt)`, which returns the screen with each actor it holds stepped. The game can't
|
|
35
35
|
# step an actor where it finds it in the view, since the screen holding it is a value, so
|
|
36
|
-
# the screen maps its own fields (`
|
|
36
|
+
# the screen maps its own fields (`Views.stepAll` for a list of actors):
|
|
37
37
|
#
|
|
38
38
|
# fun stepped(self, dt: Float) -> PlayingScreen =
|
|
39
|
-
# PlayingScreen(..self, ship: self.ship.step(dt), mines:
|
|
39
|
+
# PlayingScreen(..self, ship: self.ship.step(dt), mines: Views.stepAll(self.mines, dt))
|
|
40
40
|
#
|
|
41
41
|
# A screen holding no actors leaves it out (the default returns the screen as it is).
|
|
42
42
|
#
|
|
@@ -127,23 +127,6 @@ trait Screen =
|
|
|
127
127
|
# gave one while what is held differs from the screen's tick before.
|
|
128
128
|
struct Delivered(screen: Screen, effects: List<Effect>, acted: Bool)
|
|
129
129
|
|
|
130
|
-
# The camera the screen `s` is seen through in a window of `pixels` (0 by 0 while the
|
|
131
|
-
# game doesn't know it): its world (`size()`) put on the window by its `viewport()`, or,
|
|
132
|
-
# for a screen with no size, one unit a pixel with the world as large as the window; then
|
|
133
|
-
# as the screen's `camera` moves it.
|
|
134
|
-
fun cameraOf(s: Screen, pixels: Vec2) -> Viewport = s.camera(baseCamera(s, pixels))
|
|
135
|
-
|
|
136
|
-
fun baseCamera(s: Screen, pixels: Vec2) -> Viewport =
|
|
137
|
-
size := s.size()
|
|
138
|
-
known := pixels.x > 0.0 && pixels.y > 0.0
|
|
139
|
-
if size.x > 0.0 && size.y > 0.0
|
|
140
|
-
v := Viewport.of(s.viewport(), size.x, size.y)
|
|
141
|
-
if known then v.update(pixels.x, pixels.y) else v
|
|
142
|
-
else if known
|
|
143
|
-
Viewport.of(Pixels, pixels.x, pixels.y)
|
|
144
|
-
else
|
|
145
|
-
Viewport.of(Pixels, 1.0, 1.0)
|
|
146
|
-
|
|
147
130
|
# How often a screen gets its ticks and is drawn: every frame (`Always`), or only when
|
|
148
131
|
# something happened (`OnDemand`).
|
|
149
132
|
enum Frames =
|
std/game/ScreenSlice.plum
CHANGED
|
@@ -9,7 +9,7 @@ import std/Option
|
|
|
9
9
|
import std/Slice
|
|
10
10
|
import std/collections/List
|
|
11
11
|
import std/math/Vec2
|
|
12
|
-
import std/game/
|
|
12
|
+
import std/game/Input
|
|
13
13
|
import std/game/View
|
|
14
14
|
import std/game/Effects
|
|
15
15
|
import std/game/Viewport
|
|
@@ -44,7 +44,7 @@ import std/game/Screen
|
|
|
44
44
|
# a key `onKey(e)`
|
|
45
45
|
# a press `onPress(target, at)`, with the top-most item of the screen's
|
|
46
46
|
# `hud`, or else of its view, under the press that takes presses
|
|
47
|
-
# (`Sized` and `Enabled`, std/game/View's `hitIn`) and the press
|
|
47
|
+
# (`Sized` and `Enabled`, std/game/View's `Views.hitIn`) and the press
|
|
48
48
|
# in the world's coordinates; if it gives None, or the press is on
|
|
49
49
|
# no such item, `onPointer` is asked as for any pointer event
|
|
50
50
|
# a pointer event `onPointer(e, at)`, with the event's point in the world
|
|
@@ -71,7 +71,7 @@ trait ScreenSlice<A>[Screen] =
|
|
|
71
71
|
# reduced. Called outside a dispatch (a test calling `reduce` itself), it only reduces.
|
|
72
72
|
fun dispatch(self, a: A) -> Self =
|
|
73
73
|
next := self.reduce(a)
|
|
74
|
-
|
|
74
|
+
Effects.queueAction(a)
|
|
75
75
|
next
|
|
76
76
|
|
|
77
77
|
# The effects of `a`, after the tick: a sound, a change of screen. Nothing by default.
|
|
@@ -92,7 +92,7 @@ trait ScreenSlice<A>[Screen] =
|
|
|
92
92
|
fun wants(self, input: Input) -> Bool = self.held(input).isSome()
|
|
93
93
|
|
|
94
94
|
# std's: one tick of the screen, delivered as actions (see the top of this file).
|
|
95
|
-
fun deliver(self, d: Delivery) -> Delivered = deliverTo<A, Self>(self, d)
|
|
95
|
+
fun deliver(self, d: Delivery) -> Delivered = Delivery.deliverTo<A, Self>(self, d)
|
|
96
96
|
|
|
97
97
|
# One tick as the game hands it to a screen's `deliver`: the tick's input and length, what
|
|
98
98
|
# was held at the screen's tick before (`before`), the camera the screen is seen through
|
|
@@ -102,60 +102,60 @@ struct Delivery(input: Input, dt: Float, before: Held, camera: Viewport, pixels:
|
|
|
102
102
|
# The item a press at the window pixel `p`, the world point `at`, lands on: in the
|
|
103
103
|
# screen's `hud`, else in its view; None for none.
|
|
104
104
|
fun pressed(self, s: Screen, p: Vec2, at: Vec2) -> Option<View> =
|
|
105
|
-
h :=
|
|
105
|
+
h := Views.hitIn(s.hud(self.pixels), p)
|
|
106
|
-
if h.isSome() then h else
|
|
106
|
+
if h.isSome() then h else Views.hitIn(s.view(), at)
|
|
107
107
|
|
|
108
108
|
# `s` with its actors stepped (`stepped(dt)`), unless it says it `steps()` none this
|
|
109
109
|
# tick.
|
|
110
110
|
fun stepScreen<S: Screen>(self, s: S) -> S = if s.steps() then s.stepped(self.dt) else s
|
|
111
111
|
|
|
112
|
-
# One tick of the slice `s`, as `deliver` describes.
|
|
112
|
+
# One tick of the slice `s`, as `deliver` describes.
|
|
113
|
-
fun deliverTo<A, S: ScreenSlice<A>>(s: S, d: Delivery) -> Delivered =
|
|
113
|
+
fun deliverTo<A, S: ScreenSlice<A>>(s: S, d: Delivery) -> Delivered =
|
|
114
|
-
|
|
114
|
+
q := Slice<A>()
|
|
115
|
-
|
|
115
|
+
acted := Slice<Bool>()
|
|
116
|
-
|
|
116
|
+
Effects.startDispatch(ActionSink(queue: q))
|
|
117
|
-
|
|
117
|
+
evs := d.input.events
|
|
118
|
-
|
|
118
|
+
afterEvents := 0.until(evs.length()).fold(s, |cur, i| Delivery.eventInto<A, S>(cur, d, evs.at(i), q, acted))
|
|
119
|
-
|
|
119
|
+
h := afterEvents.held(d.input)
|
|
120
|
-
|
|
120
|
+
afterHeld := match h
|
|
121
|
+
Some(a) =>
|
|
122
|
+
if !d.before.same(d.input.held)
|
|
123
|
+
acted.push(true)
|
|
124
|
+
Delivery.reduced<A, S>(afterEvents, a, q)
|
|
125
|
+
None => afterEvents
|
|
126
|
+
stepped := d.stepScreen(afterHeld)
|
|
127
|
+
afterTick := match stepped.tick(d.dt)
|
|
128
|
+
Some(a) => Delivery.reduced<A, S>(stepped, a, q)
|
|
129
|
+
None => stepped
|
|
130
|
+
Effects.endDispatch()
|
|
131
|
+
final: ScreenSlice<A> := afterTick
|
|
132
|
+
fx: List<Effect> := List.fromSlice(q).map(|a| Effects.queued(final, a))
|
|
133
|
+
Delivered(screen: afterTick, effects: fx, acted: acted.length() > 0)
|
|
134
|
+
|
|
135
|
+
# `s` after the event `e`, mapped to its action; a pointer event while a transition runs
|
|
136
|
+
# is dropped.
|
|
137
|
+
fun eventInto<A, S: ScreenSlice<A>>(s: S, d: Delivery, e: InputEvent, q: Slice<A>, acted: Slice<Bool>) -> S = match e.position()
|
|
138
|
+
Some(p) =>
|
|
139
|
+
if d.shifting
|
|
140
|
+
s
|
|
141
|
+
else
|
|
142
|
+
at := d.camera.toWorld(p.x, p.y)
|
|
143
|
+
target := if e.isPointerDown() then d.pressed(s, p, at) else None
|
|
144
|
+
pressed := match target
|
|
145
|
+
Some(t) => s.onPress(t, at)
|
|
146
|
+
None => None
|
|
147
|
+
act := if pressed.isSome() then pressed else s.onPointer(e, at)
|
|
148
|
+
Delivery.actedOn<A, S>(s, act, q, acted)
|
|
149
|
+
None => Delivery.actedOn<A, S>(s, s.onKey(e), q, acted)
|
|
150
|
+
|
|
151
|
+
fun actedOn<A, S: ScreenSlice<A>>(s: S, act: Option<A>, q: Slice<A>, acted: Slice<Bool>) -> S = match act
|
|
121
152
|
Some(a) =>
|
|
122
|
-
if !d.before.same(d.input.held)
|
|
123
|
-
|
|
153
|
+
acted.push(true)
|
|
124
|
-
reduced<A, S>(afterEvents, a, q)
|
|
125
|
-
None => afterEvents
|
|
126
|
-
stepped := d.stepScreen(afterHeld)
|
|
127
|
-
afterTick := match stepped.tick(d.dt)
|
|
128
|
-
Some(a) => reduced<A, S>(stepped, a, q)
|
|
129
|
-
None => stepped
|
|
130
|
-
effects.endDispatch()
|
|
131
|
-
final: ScreenSlice<A> := afterTick
|
|
132
|
-
fx: List<Effect> := List.fromSlice(q).map(|a| effects.queued(final, a))
|
|
133
|
-
Delivered(screen: afterTick, effects: fx, acted: acted.length() > 0)
|
|
134
|
-
|
|
135
|
-
# `s` after the event `e`, mapped to its action; a pointer event while a transition runs
|
|
136
|
-
# is dropped.
|
|
137
|
-
fun eventInto<A, S: ScreenSlice<A>>(s: S, d: Delivery, e: InputEvent, q: Slice<A>, acted: Slice<Bool>) -> S = match e.position()
|
|
138
|
-
Some(p) =>
|
|
139
|
-
if d.shifting
|
|
140
|
-
s
|
|
141
|
-
else
|
|
142
|
-
at := d.camera.toWorld(p.x, p.y)
|
|
143
|
-
target := if e.isPointerDown() then d.pressed(s, p, at) else None
|
|
144
|
-
pressed := match target
|
|
145
|
-
Some(t) => s.onPress(t, at)
|
|
146
|
-
None => None
|
|
147
|
-
act := if pressed.isSome() then pressed else s.onPointer(e, at)
|
|
148
|
-
actedOn<A, S>(s, act, q, acted)
|
|
149
|
-
None => actedOn<A, S>(s, s.onKey(e), q, acted)
|
|
150
|
-
|
|
151
|
-
fun actedOn<A, S: ScreenSlice<A>>(s: S, act: Option<A>, q: Slice<A>, acted: Slice<Bool>) -> S = match act
|
|
152
|
-
Some(a) =>
|
|
153
|
-
acted.push(true)
|
|
154
|
-
|
|
154
|
+
Delivery.reduced<A, S>(s, a, q)
|
|
155
|
-
|
|
155
|
+
None => s
|
|
156
|
-
|
|
156
|
+
|
|
157
|
-
# `s` with `a` reduced into it, and `a` queued after what its reducer dispatched.
|
|
157
|
+
# `s` with `a` reduced into it, and `a` queued after what its reducer dispatched.
|
|
158
|
-
fun reduced<A, S: ScreenSlice<A>>(s: S, a: A, q: Slice<A>) -> S =
|
|
158
|
+
fun reduced<A, S: ScreenSlice<A>>(s: S, a: A, q: Slice<A>) -> S =
|
|
159
|
-
|
|
159
|
+
next := s.reduce(a)
|
|
160
|
-
|
|
160
|
+
q.push(a)
|
|
161
|
-
|
|
161
|
+
next
|
std/game/Sprite.plum
CHANGED
|
@@ -11,7 +11,7 @@ import std/math/Vec2
|
|
|
11
11
|
import std/math/Affine
|
|
12
12
|
import std/math/Aabb
|
|
13
13
|
import std/game/Color
|
|
14
|
-
import std/game/
|
|
14
|
+
import std/game/Gfx
|
|
15
15
|
import std/game/Place
|
|
16
16
|
import std/game/View
|
|
17
17
|
import std/game/Sized
|
|
@@ -32,7 +32,7 @@ struct Sprite[View, Sized, Touch](x: Float, y: Float, tex: Texture, w: Float, h:
|
|
|
32
32
|
fun cell(x: Float, y: Float, tex: Texture, w: Float, h: Float, srcX: Float, srcY: Float, srcW: Float, srcH: Float, frame: Int) -> Sprite =
|
|
33
33
|
Sprite(x: x, y: y, tex: tex, w: w, h: h, srcX: srcX, srcY: srcY, srcW: srcW, srcH: srcH, frame: frame, tint: Color.White, touch: Disabled)
|
|
34
34
|
|
|
35
|
-
fun place(self) -> Place =
|
|
35
|
+
fun place(self) -> Place = Place.at(self.x, self.y)
|
|
36
36
|
|
|
37
37
|
# The frame of a strip of `count` frames played at `fps` frames a second, `clock`
|
|
38
38
|
# seconds in, looping: `Sprite.cell(.., Sprite.frameAt(self.clock, 8.0, 4))`.
|
|
@@ -46,13 +46,13 @@ struct Sprite[View, Sized, Touch](x: Float, y: Float, tex: Texture, w: Float, h:
|
|
|
46
46
|
fun touch(self) -> Touchable = self.touch
|
|
47
47
|
|
|
48
48
|
fun paintSelf(self, io: Io, m: Affine, alpha: Float) =
|
|
49
|
-
|
|
49
|
+
Sprite.draw(io, m, alpha * self.tint.a, self.w, self.h, self.tint, self.tex, self.frameX(), self.srcY, self.srcW, self.srcH)
|
|
50
|
-
|
|
50
|
+
|
|
51
|
-
# Draws a texture region `w` by `h` at the transform `m`, turned by `m`'s rotation: for
|
|
51
|
+
# Draws a texture region `w` by `h` at the transform `m`, turned by `m`'s rotation: for
|
|
52
|
-
# your own sprite-like kinds. Paints with `alpha` in place of the colour's alpha, as
|
|
52
|
+
# your own sprite-like kinds. Paints with `alpha` in place of the colour's alpha, as
|
|
53
|
-
# `
|
|
53
|
+
# `Box.draw` does.
|
|
54
|
-
fun
|
|
54
|
+
fun draw(io: Io, m: Affine, alpha: Float, w: Float, h: Float, color: Color, tex: Texture, srcX: Float, srcY: Float, srcW: Float, srcH: Float) =
|
|
55
|
-
|
|
55
|
+
sx := w * m.scaleX()
|
|
56
|
-
|
|
56
|
+
sy := h * m.scaleY()
|
|
57
|
-
|
|
57
|
+
c := m.apply(Vec2(x: w / 2.0, y: h / 2.0))
|
|
58
|
-
|
|
58
|
+
Gfx.drawTextureEx(io, tex.id, srcX, srcY, srcW, srcH, c.x - sx / 2.0, c.y - sy / 2.0, sx, sy, m.rotation(), false, color.withAlpha(alpha))
|
std/game/Tween.plum
CHANGED
|
@@ -57,27 +57,25 @@ enum Easing =
|
|
|
57
57
|
fun apply(self, t: Float) -> Float = match self
|
|
58
58
|
Linear => t
|
|
59
59
|
Smooth => t * t * {3.0 - 2.0 * t}
|
|
60
|
-
QuadIn => powIn(t, 2)
|
|
60
|
+
QuadIn => Easing.powIn(t, 2)
|
|
61
|
-
QuadOut => powOut(t, 2)
|
|
61
|
+
QuadOut => Easing.powOut(t, 2)
|
|
62
|
-
QuadInOut => powInOut(t, 2)
|
|
62
|
+
QuadInOut => Easing.powInOut(t, 2)
|
|
63
|
-
CubicIn => powIn(t, 3)
|
|
63
|
+
CubicIn => Easing.powIn(t, 3)
|
|
64
|
-
CubicOut => powOut(t, 3)
|
|
64
|
+
CubicOut => Easing.powOut(t, 3)
|
|
65
|
-
CubicInOut => powInOut(t, 3)
|
|
65
|
+
CubicInOut => Easing.powInOut(t, 3)
|
|
66
66
|
|
|
67
|
-
# `t` to the power `p`.
|
|
67
|
+
# `t` to the power `p`.
|
|
68
|
-
fun powIn(t: Float, p: Int) -> Float = 0.until(p).fold(1.0, |r, i| r * t)
|
|
68
|
+
fun powIn(t: Float, p: Int) -> Float = 0.until(p).fold(1.0, |r, i| r * t)
|
|
69
69
|
|
|
70
|
-
# scene2d's `PowOut`: 1 - (1 - t)^p, written as it does.
|
|
70
|
+
# scene2d's `PowOut`: 1 - (1 - t)^p, written as it does.
|
|
71
|
-
fun powOut(t: Float, p: Int) -> Float =
|
|
71
|
+
fun powOut(t: Float, p: Int) -> Float =
|
|
72
|
-
|
|
72
|
+
if p % 2 == 0 then 1.0 - Easing.powIn(t - 1.0, p) else Easing.powIn(t - 1.0, p) + 1.0
|
|
73
73
|
|
|
74
|
-
# scene2d's `Pow`: the first half `PowIn`, the second `PowOut`, each squeezed into it.
|
|
74
|
+
# scene2d's `Pow`: the first half `PowIn`, the second `PowOut`, each squeezed into it.
|
|
75
|
-
fun powInOut(t: Float, p: Int) -> Float =
|
|
75
|
+
fun powInOut(t: Float, p: Int) -> Float =
|
|
76
|
-
|
|
76
|
+
if t <= 0.5
|
|
77
|
-
|
|
77
|
+
Easing.powIn(t * 2.0, p) / 2.0
|
|
78
|
-
|
|
78
|
+
else if p % 2 == 0
|
|
79
|
-
|
|
79
|
+
1.0 - Easing.powIn({t - 1.0} * 2.0, p) / 2.0
|
|
80
|
-
|
|
80
|
+
else
|
|
81
|
-
|
|
81
|
+
Easing.powIn({t - 1.0} * 2.0, p) / 2.0 + 1.0
|
|
82
|
-
|
|
83
|
-
# --- tests ---
|
std/game/View.plum
CHANGED
|
@@ -36,15 +36,15 @@ import std/game/Circle
|
|
|
36
36
|
# paintSelf what it draws itself, before its view, seen through `m` (its own coordinates
|
|
37
37
|
# to the screen's world) at `alpha` (the accumulated alpha of its places, its
|
|
38
38
|
# own included): a ready-made shape, or what no ready-made view draws (an
|
|
39
|
-
# additive glow, a line), with `
|
|
39
|
+
# additive glow, a line), with `Gfx` through `m`. Nothing by default
|
|
40
40
|
#
|
|
41
41
|
# A view whose state advances each tick (its place, its clock) is an `Actor`, which
|
|
42
42
|
# extends `View` with `step(self, dt) -> Self`, its next version `dt` seconds on. The game
|
|
43
43
|
# steps the shown screen's actors each tick it gets, before its tick action is reduced
|
|
44
44
|
# (std/game/Screen's `steps` and `stepped`): a screen is a value, so the game cannot step
|
|
45
45
|
# an actor where it finds it in the view, and the screen's `stepped` puts each list of
|
|
46
|
-
# actors it holds through `stepAll` (`ship: self.ship.step(dt)`, `mines:
|
|
46
|
+
# actors it holds through `Views.stepAll` (`ship: self.ship.step(dt)`, `mines:
|
|
47
|
-
#
|
|
47
|
+
# Views.stepAll(self.mines, dt)`).
|
|
48
48
|
#
|
|
49
49
|
# struct Walker[Actor](at: Place, speed: Float) =
|
|
50
50
|
# fun place(self) -> Place = self.at
|
|
@@ -68,10 +68,10 @@ import std/game/Circle
|
|
|
68
68
|
# walked: a press on it reaches the screen as that actor (std/game/ScreenSlice's
|
|
69
69
|
# `onPress`).
|
|
70
70
|
#
|
|
71
|
-
# The walks over a list of views are
|
|
71
|
+
# The walks over a list of views are statics of `Views`, each going where the draw goes:
|
|
72
|
-
# its own view.
|
|
72
|
+
# an item, then its own view.
|
|
73
73
|
#
|
|
74
|
-
#
|
|
74
|
+
# paint draws the visible items
|
|
75
75
|
# paintCulled draws them, skipping each `Sized` item whose bounds miss an area, with
|
|
76
76
|
# what is under it (a screen that `culls()`)
|
|
77
77
|
# drawnIn lists what they draw, each item with its transform and alpha (`Drawn`):
|
|
@@ -98,176 +98,130 @@ trait View =
|
|
|
98
98
|
trait Actor[View] =
|
|
99
99
|
fun step(self, dt: Float) -> Self
|
|
100
100
|
|
|
101
|
-
#
|
|
101
|
+
# One entry of a draw list: an item drawn, its transform (its own coordinates to the
|
|
102
|
-
#
|
|
102
|
+
# world's) and its alpha.
|
|
103
|
-
|
|
103
|
+
struct Drawn(actor: View, m: Affine, alpha: Float)
|
|
104
104
|
|
|
105
|
+
# The walks over a list of views and the steps a screen's `stepped` uses (see `View`).
|
|
106
|
+
primitive Views =
|
|
105
|
-
# Each
|
|
107
|
+
# Each of `xs` stepped by `dt`, in order: what a screen's `stepped` does with a list of
|
|
108
|
+
# actors it holds.
|
|
106
|
-
fun
|
|
109
|
+
fun stepAll<T: Actor>(xs: List<T>, dt: Float) -> List<T> = xs.map(|x| x.step(dt))
|
|
107
110
|
|
|
111
|
+
# Each `Actor` of `items` stepped by `dt`, in order, and the other views as they are.
|
|
108
|
-
fun
|
|
112
|
+
fun stepIn(items: List<View>, dt: Float) -> List<View> = items.map(|x| Views.stepOne(x, dt))
|
|
109
|
-
Actor as a => a.step(dt)
|
|
110
|
-
_ => x
|
|
111
113
|
|
|
112
|
-
# Each of `xs` as a `View`: a screen's `List<Mine>` in its view (a `List<Mine>` is not a
|
|
113
|
-
# `List<View>`).
|
|
114
|
-
fun
|
|
114
|
+
fun stepOne(x: View, dt: Float) -> View = match x
|
|
115
|
+
Actor as a => a.step(dt)
|
|
116
|
+
_ => x
|
|
115
117
|
|
|
118
|
+
# Each of `xs` as a `View`: a screen's `List<Mine>` in its view (a `List<Mine>` is not a
|
|
119
|
+
# `List<View>`).
|
|
116
|
-
fun
|
|
120
|
+
fun all<T: View>(xs: List<T>) -> List<View> = xs.map(|x| Views.one(x))
|
|
117
121
|
|
|
118
|
-
# Draws the visible `items`, `m` and `alpha` being those of what holds them (the screen's
|
|
119
|
-
# camera, or the view whose view they are): each through its own place under `m`, at
|
|
120
|
-
|
|
122
|
+
fun one<T: View>(x: T) -> View = x
|
|
121
|
-
fun paintView(io: Io, items: List<View>, m: Affine, alpha: Float) = items.each(|k| paintItem(io, k, m, alpha))
|
|
122
123
|
|
|
124
|
+
# Draws the visible `items`, `m` and `alpha` being those of what holds them (the screen's
|
|
125
|
+
# camera, or the view whose view they are): each through its own place under `m`, at
|
|
126
|
+
# `alpha` times its place's alpha, its `paintSelf` and then its view.
|
|
123
|
-
fun
|
|
127
|
+
fun paint(io: Io, items: List<View>, m: Affine, alpha: Float) = items.each(|k| Views.paintItem(io, k, m, alpha))
|
|
124
|
-
if a.visible()
|
|
125
|
-
at := a.place()
|
|
126
|
-
mk := at.under(m)
|
|
127
|
-
ak := alpha * at.alpha
|
|
128
|
-
a.paintSelf(io, mk, ak)
|
|
129
|
-
paintView(io, a.view(), mk, ak)
|
|
130
128
|
|
|
131
|
-
# `paintView`, skipping each `Sized` item whose bounds, through its transform, miss
|
|
132
|
-
# `area` (in the coordinates `m` maps to), with what is under it.
|
|
133
|
-
fun
|
|
129
|
+
fun paintItem(io: Io, a: View, m: Affine, alpha: Float) =
|
|
134
|
-
items.each(|a|
|
|
135
130
|
if a.visible()
|
|
136
131
|
at := a.place()
|
|
137
132
|
mk := at.under(m)
|
|
138
|
-
if !missesArea(a, mk, area)
|
|
139
|
-
|
|
133
|
+
ak := alpha * at.alpha
|
|
140
|
-
|
|
134
|
+
a.paintSelf(io, mk, ak)
|
|
135
|
+
Views.paint(io, a.view(), mk, ak)
|
|
136
|
+
|
|
137
|
+
# `paint`, skipping each `Sized` item whose bounds, through its transform, miss
|
|
138
|
+
# `area` (in the coordinates `m` maps to), with what is under it.
|
|
139
|
+
fun paintCulled(io: Io, items: List<View>, m: Affine, alpha: Float, area: Aabb) =
|
|
140
|
+
items.each(|a|
|
|
141
|
+
if a.visible()
|
|
142
|
+
at := a.place()
|
|
143
|
+
mk := at.under(m)
|
|
144
|
+
if !Views.missesArea(a, mk, area)
|
|
145
|
+
ak := alpha * at.alpha
|
|
146
|
+
a.paintSelf(io, mk, ak)
|
|
141
|
-
|
|
147
|
+
Views.paintCulled(io, a.view(), mk, ak, area)
|
|
142
|
-
|
|
148
|
+
)
|
|
143
|
-
|
|
144
|
-
|
|
149
|
+
|
|
145
|
-
# world's) and its alpha.
|
|
146
|
-
struct Drawn(actor: View, m: Affine, alpha: Float)
|
|
147
|
-
|
|
148
|
-
# Pushes onto `out` what `items` draw, in the order drawn: each visible item with its
|
|
150
|
+
# Pushes onto `out` what `items` draw, in the order drawn: each visible item with its
|
|
149
|
-
# transform and its alpha, then what its view draws. With `cull`, a `Sized` item whose
|
|
151
|
+
# transform and its alpha, then what its view draws. With `cull`, a `Sized` item whose
|
|
150
|
-
# bounds miss `area` is left out with what is under it.
|
|
152
|
+
# bounds miss `area` is left out with what is under it.
|
|
151
|
-
fun drawnIn(items: List<View>, m: Affine, alpha: Float, out: Slice<Drawn>, area: Aabb, cull: Bool) =
|
|
153
|
+
fun drawnIn(items: List<View>, m: Affine, alpha: Float, out: Slice<Drawn>, area: Aabb, cull: Bool) =
|
|
152
|
-
|
|
154
|
+
items.each(|a|
|
|
153
|
-
|
|
155
|
+
if a.visible()
|
|
154
|
-
|
|
156
|
+
at := a.place()
|
|
155
|
-
|
|
157
|
+
mk := at.under(m)
|
|
156
|
-
|
|
158
|
+
if !cull || !Views.missesArea(a, mk, area)
|
|
157
|
-
|
|
159
|
+
ak := alpha * at.alpha
|
|
158
|
-
|
|
160
|
+
out.push(Drawn(actor: a, m: mk, alpha: ak))
|
|
159
|
-
|
|
161
|
+
Views.drawnIn(a.view(), mk, ak, out, area, cull)
|
|
160
|
-
|
|
162
|
+
)
|
|
161
|
-
|
|
163
|
+
|
|
162
|
-
# The item of `items` that a press at `p` (in the coordinates `items` are placed in) is
|
|
164
|
+
# The item of `items` that a press at `p` (in the coordinates `items` are placed in) is
|
|
163
|
-
# on: the top-most visible item that takes presses (`Sized`, and `Enabled`,
|
|
165
|
+
# on: the top-most visible item that takes presses (`Sized`, and `Enabled`,
|
|
164
|
-
# std/game/Touch) and whose `hit` says the point is on it, what is drawn later before what
|
|
166
|
+
# std/game/Touch) and whose `hit` says the point is on it, what is drawn later before what
|
|
165
|
-
# is drawn earlier, what is under an item before the item. A `Disabled` or invisible item
|
|
167
|
+
# is drawn earlier, what is under an item before the item. A `Disabled` or invisible item
|
|
166
|
-
# is passed over with what is under it. None for none.
|
|
168
|
+
# is passed over with what is under it. None for none.
|
|
167
|
-
fun hitIn(items: List<View>, p: Vec2) -> Option<View> = hitAmong(items.reverse(), p)
|
|
169
|
+
fun hitIn(items: List<View>, p: Vec2) -> Option<View> = Views.hitAmong(items.reverse(), p)
|
|
168
|
-
|
|
170
|
+
|
|
169
|
-
fun hitAmong(rev: List<View>, p: Vec2) -> Option<View> = match rev
|
|
171
|
+
fun hitAmong(rev: List<View>, p: Vec2) -> Option<View> = match rev
|
|
170
|
-
|
|
172
|
+
Nil => None
|
|
171
|
-
|
|
173
|
+
Cons(k, rest) => match Views.hitItem(k, p)
|
|
172
|
-
|
|
174
|
+
Some(a) => Some(a)
|
|
173
|
-
|
|
175
|
+
None => Views.hitAmong(rest, p)
|
|
174
|
-
|
|
176
|
+
|
|
175
|
-
fun hitItem(a: View, p: Vec2) -> Option<View> =
|
|
177
|
+
fun hitItem(a: View, p: Vec2) -> Option<View> =
|
|
176
|
-
|
|
178
|
+
t := Views.touchOf(a)
|
|
177
|
-
|
|
179
|
+
if t.disabled() || !a.visible()
|
|
178
|
-
None
|
|
179
|
-
else
|
|
180
|
-
q := a.place().toLocal(p)
|
|
181
|
-
under := hitIn(a.view(), q)
|
|
182
|
-
if under.isSome()
|
|
183
|
-
under
|
|
184
|
-
else if t.enabled() && sizedHit(a, q)
|
|
185
|
-
Some(a)
|
|
186
|
-
else
|
|
187
180
|
None
|
|
188
|
-
|
|
189
|
-
# How `a` takes presses: its own `touch`, or `ChildrenOnly` if it doesn't say.
|
|
190
|
-
fun touchOf(a: View) -> Touchable = match a
|
|
191
|
-
Touch as t => t.touch()
|
|
192
|
-
_ => ChildrenOnly
|
|
193
|
-
|
|
194
|
-
# Whether `a` is `Sized` and says the point `q` (in its own coordinates) is on it.
|
|
195
|
-
fun sizedHit(a: View, q: Vec2) -> Bool = match a
|
|
196
|
-
Sized as s => s.hit(q)
|
|
197
|
-
_ => false
|
|
198
|
-
|
|
199
|
-
# The transform of the view `a` (its own coordinates to those `m` maps to) where `items`
|
|
200
|
-
# draw it, found by identity through each item's view, visible or not; None if it is not
|
|
201
|
-
# there. std/game/headless's `worldOf` and `click` find a view this way.
|
|
202
|
-
fun worldIn(items: List<View>, a: View, m: Affine) -> Option<Affine> = match items
|
|
203
|
-
Nil => None
|
|
204
|
-
Cons(x, rest) =>
|
|
205
|
-
mk := x.place().under(m)
|
|
206
|
-
if @sameRef(x, a)
|
|
207
|
-
Some(mk)
|
|
208
181
|
else
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
#
|
|
230
|
-
|
|
231
|
-
fun
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
fun
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
256
|
-
|
|
257
|
-
fun visible(self) -> Bool = self.shows
|
|
258
|
-
|
|
259
|
-
fun view(self) -> List<View> = [probeAt(3.0, self.log)]
|
|
260
|
-
|
|
261
|
-
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.log.push(-1.0)
|
|
262
|
-
|
|
263
|
-
# An actor that adds up its steps.
|
|
264
|
-
struct Stepper[Actor](time: Float) =
|
|
265
|
-
fun place(self) -> Place = place.at(0.0, 0.0)
|
|
266
|
-
|
|
267
|
-
fun step(self, dt: Float) -> Stepper = Stepper(time: self.time + dt)
|
|
268
|
-
|
|
269
|
-
# A group turned a quarter.
|
|
270
|
-
struct TurnedGroup[View](x: Float, y: Float, children: List<View>) =
|
|
271
|
-
fun place(self) -> Place = place.at(self.x, self.y).rotateTo(Float.HalfPi)
|
|
272
|
-
|
|
273
|
-
fun view(self) -> List<View> = self.children
|
|
182
|
+
q := a.place().toLocal(p)
|
|
183
|
+
under := Views.hitIn(a.view(), q)
|
|
184
|
+
if under.isSome()
|
|
185
|
+
under
|
|
186
|
+
else if t.enabled() && Views.sizedHit(a, q)
|
|
187
|
+
Some(a)
|
|
188
|
+
else
|
|
189
|
+
None
|
|
190
|
+
|
|
191
|
+
# How `a` takes presses: its own `touch`, or `ChildrenOnly` if it doesn't say.
|
|
192
|
+
fun touchOf(a: View) -> Touchable = match a
|
|
193
|
+
Touch as t => t.touch()
|
|
194
|
+
_ => ChildrenOnly
|
|
195
|
+
|
|
196
|
+
# Whether `a` is `Sized` and says the point `q` (in its own coordinates) is on it.
|
|
197
|
+
fun sizedHit(a: View, q: Vec2) -> Bool = match a
|
|
198
|
+
Sized as s => s.hit(q)
|
|
199
|
+
_ => false
|
|
200
|
+
|
|
201
|
+
# The transform of the view `a` (its own coordinates to those `m` maps to) where `items`
|
|
202
|
+
# draw it, found by identity through each item's view, visible or not; None if it is not
|
|
203
|
+
# there. std/game/headless's `worldOf` and `click` find a view this way.
|
|
204
|
+
fun worldIn(items: List<View>, a: View, m: Affine) -> Option<Affine> = match items
|
|
205
|
+
Nil => None
|
|
206
|
+
Cons(x, rest) =>
|
|
207
|
+
mk := x.place().under(m)
|
|
208
|
+
if @sameRef(x, a)
|
|
209
|
+
Some(mk)
|
|
210
|
+
else
|
|
211
|
+
inView := Views.worldIn(x.view(), a, mk)
|
|
212
|
+
if inView.isSome() then inView else Views.worldIn(rest, a, m)
|
|
213
|
+
|
|
214
|
+
# Whether `a` is `Sized` and its bounds, seen through `m`, miss `area`.
|
|
215
|
+
fun missesArea(a: View, m: Affine, area: Aabb) -> Bool = match a
|
|
216
|
+
Sized as s => !Views.through(s.bounds(), m).overlaps(area)
|
|
217
|
+
_ => false
|
|
218
|
+
|
|
219
|
+
# The box around `b` seen through `m`.
|
|
220
|
+
fun through(b: Aabb, m: Affine) -> Aabb =
|
|
221
|
+
p1 := m.apply(b.min)
|
|
222
|
+
p2 := m.apply(Vec2(x: b.max.x, y: b.min.y))
|
|
223
|
+
p3 := m.apply(b.max)
|
|
224
|
+
p4 := m.apply(Vec2(x: b.min.x, y: b.max.y))
|
|
225
|
+
Aabb(
|
|
226
|
+
min: Vec2(x: p1.x.min(p2.x).min(p3.x).min(p4.x), y: p1.y.min(p2.y).min(p3.y).min(p4.y)),
|
|
227
|
+
max: Vec2(x: p1.x.max(p2.x).max(p3.x).max(p4.x), y: p1.y.max(p2.y).max(p3.y).max(p4.y)))
|
std/game/Viewport.plum
CHANGED
|
@@ -23,7 +23,7 @@ enum Scaling = Fit | Fill | Stretch | Extend | Pixels
|
|
|
23
23
|
# point at the screen's top-left (`x`, `y`) and the pixels per world unit along each axis
|
|
24
24
|
# (`sx`, `sy`, the same but for `Stretch`). The game makes one for the shown screen from
|
|
25
25
|
# its `size()` and `viewport()` (std/game/Screen) and the screen's size in pixels, then
|
|
26
|
-
# the screen's `camera(base)` (
|
|
26
|
+
# the screen's `camera(base)` (`Game.cameraOf`), and draws and hit-tests the
|
|
27
27
|
# screen's view through it. `moved` is whether a screen's `camera` moved or zoomed it away
|
|
28
28
|
# from where its scaling puts it (`lookAt`): then what it shows is what is on the window.
|
|
29
29
|
#
|
std/game/audio.plum
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
module std
|
|
2
|
-
|
|
3
|
-
import std/Int
|
|
4
|
-
import std/Float
|
|
5
|
-
import std/Str
|
|
6
|
-
import std/Io
|
|
7
|
-
import std/game/gfx
|
|
8
|
-
|
|
9
|
-
# Sound: module functions that take the `Io` capability first, and the `Sound` handle.
|
|
10
|
-
# `audio.loadSound(io, "boop.wav")`. Volume is 0..1. A sound handle is -1 on failure. Paths
|
|
11
|
-
# are relative to the asset root. The host decodes every sound file in the asset root
|
|
12
|
-
# before `init` runs.
|
|
13
|
-
|
|
14
|
-
# Loads a WAV or OGG file relative to the asset root; -1 on failure.
|
|
15
|
-
extern fun soundLoad(io: Io, path: Str) -> Int
|
|
16
|
-
# A one-shot play; plays of the same sound may overlap. Volume is 0..1.
|
|
17
|
-
extern fun soundPlay(io: Io, s: Int, volume: Float)
|
|
18
|
-
# Starts the sound looping. Call `soundStop` first to restart it cleanly.
|
|
19
|
-
extern fun soundLoop(io: Io, s: Int, volume: Float)
|
|
20
|
-
# Stops every play and loop of the sound.
|
|
21
|
-
extern fun soundStop(io: Io, s: Int)
|
|
22
|
-
# Changes the volume of a sound that is playing or looping.
|
|
23
|
-
extern fun soundSetVolume(io: Io, s: Int, volume: Float)
|
|
24
|
-
|
|
25
|
-
# A sound handle; the id is the host's. `Sound.Silent` (id -1) is no sound: it plays
|
|
26
|
-
# nothing and never calls the host, so a test can run a game's effects.
|
|
27
|
-
struct Sound(id: Int) =
|
|
28
|
-
Silent = Sound(id: -1)
|
|
29
|
-
|
|
30
|
-
# No sound: `Sound.Silent`, what a test's assets hold.
|
|
31
|
-
fun none() -> Sound = Sound.Silent
|
|
32
|
-
|
|
33
|
-
fun play(self, io: Io, volume: Float = 1.0) =
|
|
34
|
-
if self.id >= 0
|
|
35
|
-
soundPlay(io, self.id, volume)
|
|
36
|
-
|
|
37
|
-
fun loop(self, io: Io, volume: Float = 1.0) =
|
|
38
|
-
if self.id >= 0
|
|
39
|
-
soundLoop(io, self.id, volume)
|
|
40
|
-
|
|
41
|
-
fun stop(self, io: Io) =
|
|
42
|
-
if self.id >= 0
|
|
43
|
-
soundStop(io, self.id)
|
|
44
|
-
|
|
45
|
-
fun setVolume(self, io: Io, volume: Float) =
|
|
46
|
-
if self.id >= 0
|
|
47
|
-
soundSetVolume(io, self.id, volume)
|
|
48
|
-
|
|
49
|
-
# Loads a sound relative to the asset root; crashes naming the file if it is missing.
|
|
50
|
-
fun loadSound(io: Io, name: Str) -> Sound =
|
|
51
|
-
id := soundLoad(io, name)
|
|
52
|
-
if id < 0
|
|
53
|
-
crash gfx.missingAsset("sound", name)
|
|
54
|
-
Sound(id: id)
|
std/game/gfx.plum
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
module std
|
|
2
|
-
|
|
3
|
-
import std/Int
|
|
4
|
-
import std/Float
|
|
5
|
-
import std/Str
|
|
6
|
-
import std/math/Vec2
|
|
7
|
-
import std/game/Color
|
|
8
|
-
import std/Io
|
|
9
|
-
|
|
10
|
-
# Drawing, textures and files: the game host's graphics API (`plum run`), as module
|
|
11
|
-
# functions that take the `Io` capability first: `gfx.clear(io, Color.Black)`,
|
|
12
|
-
# `gfx.loadTexture(io, "sprites.png")`. A game is any type that `main(io: Io)` returns and
|
|
13
|
-
# that has `update(self, io: Io, input: Input, dt: Float) -> Self` and `draw(self, io: Io)`;
|
|
14
|
-
# `update` runs once per fixed 1/60 s tick (see `Clock` in std/game/input), `draw` once
|
|
15
|
-
# per frame, and the state `update` returns is the next tick's `self`. Input is `std/game/input`, sound is `std/game/audio`. This file also
|
|
16
|
-
# holds the `Blend` constants and the `Texture` handle. A path that escapes the asset root
|
|
17
|
-
# fails like a missing file. Coordinates are world units once `gfx.setCamera` is set.
|
|
18
|
-
#
|
|
19
|
-
# Texture handles are -1 on failure. File and texture paths are relative to the asset
|
|
20
|
-
# root. `Int` in an extern signature is narrowed to i32 at the host boundary.
|
|
21
|
-
|
|
22
|
-
extern fun screenWidth(io: Io) -> Float
|
|
23
|
-
extern fun screenHeight(io: Io) -> Float
|
|
24
|
-
extern fun frameTime(io: Io) -> Float
|
|
25
|
-
extern fun clear(io: Io, c: Color)
|
|
26
|
-
extern fun drawRect(io: Io, x: Float, y: Float, w: Float, h: Float, c: Color)
|
|
27
|
-
# The rect (x, y, w, h) turned `rotation` radians clockwise about its centre.
|
|
28
|
-
extern fun drawRectEx(io: Io, x: Float, y: Float, w: Float, h: Float, rotation: Float, c: Color)
|
|
29
|
-
extern fun drawCircle(io: Io, x: Float, y: Float, r: Float, c: Color)
|
|
30
|
-
extern fun drawLine(io: Io, x1: Float, y1: Float, x2: Float, y2: Float, thickness: Float, c: Color)
|
|
31
|
-
extern fun drawText(io: Io, s: Str, x: Float, y: Float, size: Float, c: Color)
|
|
32
|
-
# `s` with its top-left at (x, y), turned `rotation` radians clockwise about that point.
|
|
33
|
-
extern fun drawTextEx(io: Io, s: Str, x: Float, y: Float, size: Float, rotation: Float, c: Color)
|
|
34
|
-
extern fun setCamera(io: Io, x: Float, y: Float, scale: Float)
|
|
35
|
-
extern fun resetCamera(io: Io)
|
|
36
|
-
# Loads any image the host decodes (PNG at least); -1 on failure.
|
|
37
|
-
extern fun textureLoad(io: Io, path: Str) -> Int
|
|
38
|
-
# Size in pixels; 0 for a bad handle.
|
|
39
|
-
extern fun textureWidth(io: Io, t: Int) -> Int
|
|
40
|
-
extern fun textureHeight(io: Io, t: Int) -> Int
|
|
41
|
-
# Linear (true) or nearest (false, the default) filtering.
|
|
42
|
-
extern fun textureSetSmooth(io: Io, t: Int, smooth: Bool)
|
|
43
|
-
# A blank, transparent texture of w x h pixels that can be drawn into with
|
|
44
|
-
# `renderTarget`; -1 if a side is below 1 or above 8192.
|
|
45
|
-
extern fun textureCreate(io: Io, w: Int, h: Int) -> Int
|
|
46
|
-
# Sends the following draws (and `clear`) into texture `t` made by `textureCreate`,
|
|
47
|
-
# or back to the screen with -1; any other handle is ignored. Each target has its
|
|
48
|
-
# own camera: selecting a target resets its camera to its pixels, and returning to
|
|
49
|
-
# the screen restores the screen's camera. The texture's contents persist between
|
|
50
|
-
# frames; draw it like any texture once the screen is selected again.
|
|
51
|
-
extern fun renderTarget(io: Io, t: Int)
|
|
52
|
-
# The blend mode of the following draws: a `Blend` constant (other values are
|
|
53
|
-
# ignored). Every frame starts with `Blend.Normal`.
|
|
54
|
-
extern fun blendMode(io: Io, m: Int)
|
|
55
|
-
# The texture at its natural size, top-left at (x, y).
|
|
56
|
-
extern fun drawTexture(io: Io, t: Int, x: Float, y: Float, tint: Color)
|
|
57
|
-
# The source rect (sx, sy, sw, sh) in texture pixels drawn into the dest rect
|
|
58
|
-
# (x, y, w, h), rotated `rotation` radians clockwise about the dest centre.
|
|
59
|
-
extern fun drawTextureEx(io: Io, t: Int, sx: Float, sy: Float, sw: Float, sh: Float, x: Float, y: Float, w: Float, h: Float, rotation: Float, flipX: Bool, tint: Color)
|
|
60
|
-
extern fun fileExists(io: Io, path: Str) -> Bool
|
|
61
|
-
# The whole file; "" if it is missing.
|
|
62
|
-
extern fun fileLoad(io: Io, path: Str) -> Str
|
|
63
|
-
|
|
64
|
-
# Blend modes for `blendMode`: alpha blending, additive (brightens), multiply
|
|
65
|
-
# (darkens, white leaves the target unchanged) and screen (lightens).
|
|
66
|
-
primitive Blend =
|
|
67
|
-
Normal = 0
|
|
68
|
-
Add = 1
|
|
69
|
-
Multiply = 2
|
|
70
|
-
Screen = 3
|
|
71
|
-
|
|
72
|
-
# A texture handle; the id is the host's. A handle of -1 is no texture.
|
|
73
|
-
struct Texture(id: Int) =
|
|
74
|
-
# No texture: what a test's assets hold.
|
|
75
|
-
fun none() -> Texture = Texture(id: -1)
|
|
76
|
-
|
|
77
|
-
fun width(self, io: Io) -> Int = textureWidth(io, self.id)
|
|
78
|
-
|
|
79
|
-
fun height(self, io: Io) -> Int = textureHeight(io, self.id)
|
|
80
|
-
|
|
81
|
-
# Linear (true) or nearest (false, the default) filtering.
|
|
82
|
-
fun setSmooth(self, io: Io, smooth: Bool) =
|
|
83
|
-
textureSetSmooth(io, self.id, smooth)
|
|
84
|
-
|
|
85
|
-
# The texture at its natural size, top-left at (x, y).
|
|
86
|
-
fun draw(self, io: Io, x: Float, y: Float, tint: Color = Color.White) =
|
|
87
|
-
drawTexture(io, self.id, x, y, tint)
|
|
88
|
-
|
|
89
|
-
# Sends the following draws into this texture; `gfx.renderToScreen` goes back to the screen.
|
|
90
|
-
# Each target has its own camera, which starts as its pixels.
|
|
91
|
-
fun renderTarget(self, io: Io) =
|
|
92
|
-
renderTarget(io, self.id)
|
|
93
|
-
|
|
94
|
-
# Loads an image relative to the asset root; crashes naming the file if it is missing.
|
|
95
|
-
fun loadTexture(io: Io, name: Str) -> Texture =
|
|
96
|
-
id := textureLoad(io, name)
|
|
97
|
-
if id < 0
|
|
98
|
-
crash missingAsset("texture", name)
|
|
99
|
-
Texture(id: id)
|
|
100
|
-
|
|
101
|
-
# A blank transparent texture to draw into with `renderTarget`; crashes if a side is
|
|
102
|
-
# below 1 or above 8192.
|
|
103
|
-
fun createTexture(io: Io, w: Int, h: Int) -> Texture =
|
|
104
|
-
id := textureCreate(io, w, h)
|
|
105
|
-
if id < 0
|
|
106
|
-
crash "cannot create a {w}x{h} texture"
|
|
107
|
-
Texture(id: id)
|
|
108
|
-
|
|
109
|
-
# Sends the following draws back to the screen.
|
|
110
|
-
fun renderToScreen(io: Io) =
|
|
111
|
-
renderTarget(io, -1)
|
|
112
|
-
|
|
113
|
-
fun missingAsset(kind: Str, name: Str) -> Str =
|
|
114
|
-
"missing {kind}: {name}"
|
|
115
|
-
|
|
116
|
-
# The whole file as text, relative to the asset root; crashes naming the file if it is missing.
|
|
117
|
-
fun loadFile(io: Io, name: Str) -> Str =
|
|
118
|
-
if !fileExists(io, name)
|
|
119
|
-
crash missingAsset("file", name)
|
|
120
|
-
fileLoad(io, name)
|
std/game/headless.plum
CHANGED
|
@@ -11,7 +11,7 @@ import std/collections/List
|
|
|
11
11
|
import std/math/Vec2
|
|
12
12
|
import std/math/Affine
|
|
13
13
|
import std/math/Aabb
|
|
14
|
-
import std/game/
|
|
14
|
+
import std/game/Input
|
|
15
15
|
import std/game/Replay
|
|
16
16
|
import std/game/View
|
|
17
17
|
import std/game/Effects
|
|
@@ -27,7 +27,7 @@ import std/game/Game
|
|
|
27
27
|
# A screen or a whole game run headless, for tests: input in, ticks through the game's own
|
|
28
28
|
# tick, and what came out read back.
|
|
29
29
|
#
|
|
30
|
-
# t :=
|
|
30
|
+
# t := TestRig.mount(io, PlayingScreen.start(Random.seeded(7), Assets.none()))
|
|
31
31
|
# u := t.hold(Key.Right).ticks(30)
|
|
32
32
|
# assert u.screen.score == 10
|
|
33
33
|
# assert u.text(u.first<Label>()) == "Score 10"
|
|
@@ -35,7 +35,8 @@ import std/game/Game
|
|
|
35
35
|
#
|
|
36
36
|
# A rig is a value, like the game in it: each input and each tick returns the next rig.
|
|
37
37
|
#
|
|
38
|
-
# `mount(io, screen)` makes a game of the one screen; `play(io, game)` takes a
|
|
38
|
+
# `TestRig.mount(io, screen)` makes a game of the one screen; `TestRig.play(io, game)` takes a
|
|
39
|
+
# whole game.
|
|
39
40
|
# Either sizes the window to `DefaultW` by `DefaultH` pixels (800 by 600; `resize(w, h)`
|
|
40
41
|
# changes it), as the host's first frame would. The rig is generic over the screen's type, so
|
|
41
42
|
# `t.screen` is the screen itself as the last tick left it (for `play`, the game's first
|
|
@@ -78,36 +79,14 @@ import std/game/Game
|
|
|
78
79
|
# `showing()` names the screen shown. `record(seed)`, `recorded()` and `replay(r)` record
|
|
79
80
|
# and replay the game (std/game/Replay).
|
|
80
81
|
#
|
|
81
|
-
# Undo and time travel (std/game/History): `mountWith(io, screen)` and
|
|
82
|
+
# Undo and time travel (std/game/History): `TestRig.mountWith(io, screen)` and
|
|
82
|
-
# game)` switch the game's history on. Then `rewindTo(k)`, `undo()`, `redo()`,
|
|
83
|
+
# `TestRig.playWith(io, game)` switch the game's history on. Then `rewindTo(k)`, `undo()`, `redo()`,
|
|
83
84
|
# `stepBack()`, `stepForward()`, `pause()` and `resume()` move the game through it, and
|
|
84
85
|
# `moved()` says whether the last move could.
|
|
85
86
|
DefaultW = 800.0
|
|
86
87
|
|
|
87
88
|
DefaultH = 600.0
|
|
88
89
|
|
|
89
|
-
# A game of `screen` alone, run headless.
|
|
90
|
-
fun mount<S: Screen>(io: Io, screen: S) -> TestRig<S> =
|
|
91
|
-
only: Screen := screen
|
|
92
|
-
rigOf(io, Game(screens: [only]), screen)
|
|
93
|
-
|
|
94
|
-
# A game of `screen` alone, run headless, with its history kept.
|
|
95
|
-
fun mountWith<S: Screen>(io: Io, screen: S) -> TestRig<S> =
|
|
96
|
-
only: Screen := screen
|
|
97
|
-
rigOf(io, Game(screens: [only]).timeTravel(History.Cap), screen)
|
|
98
|
-
|
|
99
|
-
# The whole `game`, run headless; `screen` is its first screen.
|
|
100
|
-
fun play(io: Io, game: Game) -> TestRig<Screen> = rigOf(io, game, game.screens.at(0))
|
|
101
|
-
|
|
102
|
-
# The whole `game`, run headless, with its history kept.
|
|
103
|
-
fun playWith(io: Io, game: Game) -> TestRig<Screen> = rigOf(io, game.timeTravel(History.Cap), game.screens.at(0))
|
|
104
|
-
|
|
105
|
-
fun rigOf<S>(io: Io, game: Game, screen: S) -> TestRig<S> =
|
|
106
|
-
pixels := Vec2(x: DefaultW, y: DefaultH)
|
|
107
|
-
TestRig(
|
|
108
|
-
io: io, game: game.resized(pixels), screen: screen, pixels: pixels, held: Held.none(), events: Nil, pos: Vec2(x: 0.0, y: 0.0),
|
|
109
|
-
ran: Nil, asks: Nil, clock: Clock.create(), recording: false, log: Replay.create(0))
|
|
110
|
-
|
|
111
90
|
# A game run headless (see the top of this file): the window's size, what is held, the input
|
|
112
91
|
# waiting for the next tick, the pointer, what the last ticks gave (the effects that ran,
|
|
113
92
|
# what they asked of the game), and the clock and recording of `frame` and `record`.
|
|
@@ -115,6 +94,32 @@ struct TestRig<S>(
|
|
|
115
94
|
io: Io, game: Game, screen: S, pixels: Vec2, held: Held, events: List<InputEvent>, pos: Vec2,
|
|
116
95
|
ran: List<Effect>, asks: List<Ask>, clock: Clock, recording: Bool, log: Replay) =
|
|
117
96
|
|
|
97
|
+
# A game of `screen` alone, run headless.
|
|
98
|
+
fun mount<T: Screen>(io: Io, screen: T) -> TestRig<T> =
|
|
99
|
+
only: Screen := screen
|
|
100
|
+
TestRig.rigOf(io, Game(screens: [only]), screen)
|
|
101
|
+
|
|
102
|
+
# A game of `screen` alone, run headless, with its history kept.
|
|
103
|
+
fun mountWith<T: Screen>(io: Io, screen: T) -> TestRig<T> =
|
|
104
|
+
only: Screen := screen
|
|
105
|
+
TestRig.rigOf(io, Game(screens: [only]).timeTravel(History.Cap), screen)
|
|
106
|
+
|
|
107
|
+
# The whole `game`, run headless; `screen` is its first screen.
|
|
108
|
+
fun play(io: Io, game: Game) -> TestRig<Screen> = TestRig.rigOf(io, game, game.screens.at(0))
|
|
109
|
+
|
|
110
|
+
# The whole `game`, run headless, with its history kept.
|
|
111
|
+
fun playWith(io: Io, game: Game) -> TestRig<Screen> = TestRig.rigOf(io, game.timeTravel(History.Cap), game.screens.at(0))
|
|
112
|
+
|
|
113
|
+
fun rigOf<T>(io: Io, game: Game, screen: T) -> TestRig<T> =
|
|
114
|
+
pixels := Vec2(x: DefaultW, y: DefaultH)
|
|
115
|
+
TestRig(
|
|
116
|
+
io: io, game: game.resized(pixels), screen: screen, pixels: pixels, held: Held.none(), events: Nil, pos: Vec2(x: 0.0, y: 0.0),
|
|
117
|
+
ran: Nil, asks: Nil, clock: Clock.create(), recording: false, log: Replay.create(0))
|
|
118
|
+
|
|
119
|
+
# Stops a test that clicks an actor in neither the view nor the hud.
|
|
120
|
+
fun notInView() -> Vec2 =
|
|
121
|
+
crash "headless: click: the actor is not in the view"
|
|
122
|
+
|
|
118
123
|
# The window is `w` by `h` pixels from now on.
|
|
119
124
|
fun resize(self, w: Float, h: Float) -> TestRig<S> =
|
|
120
125
|
v := Vec2(x: w, y: h)
|
|
@@ -153,9 +158,9 @@ struct TestRig<S>(
|
|
|
153
158
|
_ => Vec2(x: 0.0, y: 0.0)
|
|
154
159
|
match self.worldOf(a)
|
|
155
160
|
Some(m) => self.game.toScreen(m.apply(p))
|
|
156
|
-
None => match
|
|
161
|
+
None => match Views.worldIn(self.game.screen().hud(self.pixels), a, Affine.Identity)
|
|
157
162
|
Some(h) => h.apply(p)
|
|
158
|
-
None => notInView()
|
|
163
|
+
None => TestRig.notInView()
|
|
159
164
|
|
|
160
165
|
fun tick(self, dt: Float = Clock.Tick) -> TestRig<S> = self.forget().step(dt)
|
|
161
166
|
|
|
@@ -184,13 +189,13 @@ struct TestRig<S>(
|
|
|
184
189
|
match g.screen()
|
|
185
190
|
ScreenSlice<A> as sl =>
|
|
186
191
|
q := Slice<A>()
|
|
187
|
-
|
|
192
|
+
Effects.startDispatch(ActionSink(queue: q))
|
|
188
193
|
next := sl.reduce(a)
|
|
189
194
|
q.push(a)
|
|
190
|
-
|
|
195
|
+
Effects.endDispatch()
|
|
191
196
|
final: ScreenSlice<A> := next
|
|
192
|
-
fx: List<Effect> := List.fromSlice(q).map(|x|
|
|
197
|
+
fx: List<Effect> := List.fromSlice(q).map(|x| Effects.queued(final, x))
|
|
193
|
-
withScreen := Game(..g, screens:
|
|
198
|
+
withScreen := Game(..g, screens: Game.setAt(g.screens, g.current, next), wake: true, dirty: true, ran: Nil, asked: Nil)
|
|
194
199
|
TestRig(..self.forget(), game: withScreen).kept(withScreen.apply(self.io, fx).travelAsked(self.io))
|
|
195
200
|
_ =>
|
|
196
201
|
crash "headless: dispatch: the screen shown takes no actions of that type"
|
|
@@ -248,8 +253,8 @@ struct TestRig<S>(
|
|
|
248
253
|
ds := Slice<Drawn>()
|
|
249
254
|
s := self.game.screen()
|
|
250
255
|
none := Aabb.of(0.0, 0.0, 0.0, 0.0)
|
|
251
|
-
|
|
256
|
+
Views.drawnIn(s.view(), Affine.Identity, 1.0, ds, none, false)
|
|
252
|
-
|
|
257
|
+
Views.drawnIn(s.hud(self.pixels), Affine.Identity, 1.0, ds, none, false)
|
|
253
258
|
List.fromSlice(ds)
|
|
254
259
|
|
|
255
260
|
# What the frame would draw now: each item of the shown screen's view, and what is under
|
|
@@ -276,19 +281,19 @@ struct TestRig<S>(
|
|
|
276
281
|
fun shown(self, a: View) -> Bool = self.viewed().any(|d| @sameRef(d.actor, a))
|
|
277
282
|
|
|
278
283
|
# Where the shown screen's view draws `a`: its transform to the world.
|
|
279
|
-
fun worldOf(self, a: View) -> Option<Affine> =
|
|
284
|
+
fun worldOf(self, a: View) -> Option<Affine> = Views.worldIn(self.game.screen().view(), a, Affine.Identity)
|
|
280
285
|
|
|
281
286
|
# What a press at the screen pixel (x, y) lands on: the item of the shown screen's hud,
|
|
282
287
|
# or else of its view, there that takes presses (std/game/View's `hitIn`).
|
|
283
288
|
fun hit(self, x: Float, y: Float) -> Option<View> =
|
|
284
289
|
s := self.game.screen()
|
|
285
|
-
h :=
|
|
290
|
+
h := Views.hitIn(s.hud(self.pixels), Vec2(x: x, y: y))
|
|
286
|
-
if h.isSome() then h else
|
|
291
|
+
if h.isSome() then h else Views.hitIn(s.view(), self.toWorld(x, y))
|
|
287
292
|
|
|
288
293
|
# A screen pixel in the world's coordinates, through the screen's camera.
|
|
289
294
|
fun toWorld(self, x: Float, y: Float) -> Vec2 = self.game.toWorld(x, y)
|
|
290
295
|
|
|
291
|
-
fun effects<A>(self) -> List<A> =
|
|
296
|
+
fun effects<A>(self) -> List<A> = Effects.sent<A>(self.ran)
|
|
292
297
|
|
|
293
298
|
fun wentTo(self) -> Option<Str> =
|
|
294
299
|
none: Option<Str> := None
|
|
@@ -309,7 +314,3 @@ struct TestRig<S>(
|
|
|
309
314
|
# Every tick of `r` played now (a game made from `r.seed`).
|
|
310
315
|
fun replay(self, r: Replay) -> TestRig<S> =
|
|
311
316
|
0.until(r.length()).fold(self.forget(), |t, i| t.ran1(r.at(i), Clock.Tick))
|
|
312
|
-
|
|
313
|
-
# Stops a test that clicks an actor in neither the view nor the hud.
|
|
314
|
-
fun notInView() -> Vec2 =
|
|
315
|
-
crash "headless: click: the actor is not in the view"
|
tests/benchmarks/screen_views/orbit.plum
CHANGED
|
@@ -6,7 +6,7 @@ import std/Io
|
|
|
6
6
|
import std/Slice
|
|
7
7
|
import std/collections/List
|
|
8
8
|
import std/math/Random
|
|
9
|
-
import std/game/
|
|
9
|
+
import std/game/Input
|
|
10
10
|
import std/math/Vec2
|
|
11
11
|
import std/game/Screen
|
|
12
12
|
import std/game/Game
|
tests/benchmarks/screen_views/scale.plum
CHANGED
|
@@ -10,7 +10,7 @@ import std/math/Affine
|
|
|
10
10
|
import std/math/Vec2
|
|
11
11
|
import std/math/Aabb
|
|
12
12
|
import std/game/Color
|
|
13
|
-
import std/game/
|
|
13
|
+
import std/game/Input
|
|
14
14
|
import std/game/Place
|
|
15
15
|
import std/game/View
|
|
16
16
|
import std/game/Sprite
|
|
@@ -28,5 +28,5 @@ primitive ScaleTest[UnitTest] =
|
|
|
28
28
|
v := scale.run(io, CrowdScreen.of(false, 50), 30)
|
|
29
29
|
h := scale.run(io, CrowdScreen.of(true, 50), 30)
|
|
30
30
|
assert v.frameSum() == h.frameSum()
|
|
31
|
-
assert
|
|
31
|
+
assert TestRig.mount(io, v).drawn().length() == 100
|
|
32
|
-
assert
|
|
32
|
+
assert TestRig.mount(io, h).drawn().length() == 50
|
tests/benchmarks/screen_views/timing.plum
CHANGED
|
@@ -4,7 +4,7 @@ import std/Bool
|
|
|
4
4
|
import std/Slice
|
|
5
5
|
import std/collections/List
|
|
6
6
|
import std/math/Vec2
|
|
7
|
-
import std/game/
|
|
7
|
+
import std/game/Input
|
|
8
8
|
import benchmarks/screen_views/timing
|
|
9
9
|
import std/Test
|
|
10
10
|
|
tests/examples/games/balloonpop/actors/Balloon.plum
CHANGED
|
@@ -10,7 +10,7 @@ import std/math/Random
|
|
|
10
10
|
import std/math/Vec2
|
|
11
11
|
import std/math/Math
|
|
12
12
|
import std/game/Color
|
|
13
|
-
import std/game/
|
|
13
|
+
import std/game/Gfx
|
|
14
14
|
import std/game/Place
|
|
15
15
|
import std/game/View
|
|
16
16
|
import std/game/Circle
|
tests/examples/games/balloonpop/screens/PlaygroundScreen.plum
CHANGED
|
@@ -10,7 +10,7 @@ import std/math/Random
|
|
|
10
10
|
import std/math/Vec2
|
|
11
11
|
import std/math/Math
|
|
12
12
|
import std/game/Color
|
|
13
|
-
import std/game/
|
|
13
|
+
import std/game/Input
|
|
14
14
|
import std/game/Replay
|
|
15
15
|
import std/game/View
|
|
16
16
|
import std/game/Box
|
tests/examples/games/clicks.plum
CHANGED
|
@@ -6,7 +6,7 @@ import std/Io
|
|
|
6
6
|
import std/collections/List
|
|
7
7
|
import std/math/Vec2
|
|
8
8
|
import std/game/Color
|
|
9
|
-
import std/game/
|
|
9
|
+
import std/game/Input
|
|
10
10
|
import std/game/Place
|
|
11
11
|
import std/game/View
|
|
12
12
|
import std/game/Box
|
|
@@ -23,7 +23,7 @@ import std/Test
|
|
|
23
23
|
primitive ClicksTest[UnitTest] =
|
|
24
24
|
# a click on the box counts, one beside it does not, and Right rolls the ball
|
|
25
25
|
fun aClickOnTheBoxCountsOneBeside() =
|
|
26
|
-
t :=
|
|
26
|
+
t := TestRig.mount(io, TapScreen.start())
|
|
27
27
|
u := t.click(t.screen.box).tick().clickAt(10.0, 10.0).tick()
|
|
28
28
|
assert u.screen.taps == 1
|
|
29
29
|
assert u.text(u.first<Label>()) == "Taps: 1"
|
tests/examples/games/orbitrunner/actors/Orb.plum
CHANGED
|
@@ -6,7 +6,7 @@ import std/collections/List
|
|
|
6
6
|
import std/math/Vec2
|
|
7
7
|
import std/math/Affine
|
|
8
8
|
import std/game/Color
|
|
9
|
-
import std/game/
|
|
9
|
+
import std/game/Gfx
|
|
10
10
|
import std/game/Place
|
|
11
11
|
import std/game/View
|
|
12
12
|
import std/game/Sprite
|
tests/examples/games/orbitrunner/screens/PlayingScreen.plum
CHANGED
|
@@ -10,7 +10,7 @@ import std/math/Vec2
|
|
|
10
10
|
import std/math/Random
|
|
11
11
|
import std/math/Math
|
|
12
12
|
import std/game/Color
|
|
13
|
-
import std/game/
|
|
13
|
+
import std/game/Input
|
|
14
14
|
import std/game/Replay
|
|
15
15
|
import std/game/View
|
|
16
16
|
import std/game/Box
|
|
@@ -336,7 +336,7 @@ primitive PlayingScreenTest[UnitTest] =
|
|
|
336
336
|
|
|
337
337
|
# a run through game over and a restart, rewound to its middle and played on again, reaches the same end
|
|
338
338
|
fun aRunThroughGameOverAndARestart() =
|
|
339
|
-
a := playingscreen.counted(Run(rig:
|
|
339
|
+
a := playingscreen.counted(Run(rig: TestRig.mountWith(io, PlayingScreen.at(Random.seeded(42), Assets.none(), 0, 1)), overs: 0, restarts: 0), 0, 900, Clock.Tick)
|
|
340
340
|
b := playingscreen.counted(Run(rig: playingscreen.mounted(io, 42, 0, 1), overs: 0, restarts: 0), 0, 900, Clock.Tick)
|
|
341
341
|
assert a.restarts > 0
|
|
342
342
|
assert playingscreen.sameRun(a.rig.screen, b.rig.screen)
|
tests/examples/games/two_screens.plum
CHANGED
|
@@ -5,7 +5,7 @@ import std/Option
|
|
|
5
5
|
import std/Io
|
|
6
6
|
import std/collections/List
|
|
7
7
|
import std/game/Color
|
|
8
|
-
import std/game/
|
|
8
|
+
import std/game/Input
|
|
9
9
|
import std/game/View
|
|
10
10
|
import std/game/Label
|
|
11
11
|
import std/game/Control
|
|
@@ -19,7 +19,7 @@ import std/Test
|
|
|
19
19
|
primitive TwoScreensTest[UnitTest] =
|
|
20
20
|
# Enter fades in the play; Escape counts a run and goes back to the title as it was left
|
|
21
21
|
fun enterFadesInThePlayEscapeCountsA() =
|
|
22
|
-
t :=
|
|
22
|
+
t := TestRig.play(io, two_screens.twoScreens()).press(Key.Enter).tick()
|
|
23
23
|
assert t.showing() == "play"
|
|
24
24
|
assert t.game.shifting()
|
|
25
25
|
assert t.text(t.first<Label>()) == "run 0"
|
|
@@ -31,7 +31,7 @@ primitive TwoScreensTest[UnitTest] =
|
|
|
31
31
|
|
|
32
32
|
# a press while the play slides in is dropped; keys still come
|
|
33
33
|
fun aPressWhileThePlaySlidesInIs() =
|
|
34
|
-
t :=
|
|
34
|
+
t := TestRig.play(io, two_screens.twoScreens()).press(Key.Enter).tick()
|
|
35
35
|
assert t.game.shifting()
|
|
36
36
|
u := t.clickAt(20.0, 20.0).press(Key.Escape).tick()
|
|
37
37
|
assert u.showing() == "title"
|
tests/port/game/determinism.plum
CHANGED
|
@@ -6,7 +6,7 @@ import std/Slice
|
|
|
6
6
|
import std/Io
|
|
7
7
|
import std/math/Random
|
|
8
8
|
import std/math/Vec2
|
|
9
|
-
import std/game/
|
|
9
|
+
import std/game/Input
|
|
10
10
|
import std/Option
|
|
11
11
|
import std/collections/List
|
|
12
12
|
import std/game/Screen
|
|
@@ -14,7 +14,7 @@ import std/game/ScreenSlice
|
|
|
14
14
|
import std/game/Game
|
|
15
15
|
import std/Test
|
|
16
16
|
|
|
17
|
-
# Deterministic games: the game's `Clock` (std/game/
|
|
17
|
+
# Deterministic games: the game's `Clock` (std/game/Input) runs fixed 1/60 s ticks, at
|
|
18
18
|
# most 8 a frame, and hands each tick its input as a value; randomness is a `Random`
|
|
19
19
|
# held by the screen. So one seed and one list of inputs give the same game.
|
|
20
20
|
|
|
@@ -82,7 +82,7 @@ fun space(down: Bool) -> Input =
|
|
|
82
82
|
events := Slice<InputEvent>()
|
|
83
83
|
if down
|
|
84
84
|
events.push(KeyDown(key: Key.Space))
|
|
85
|
-
Input(held:
|
|
85
|
+
Input(held: Held.keysOnly([Key.Space].toSlice()), events: events, pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0)
|
|
86
86
|
|
|
87
87
|
# Frame `i` of a scripted run: Space goes down on frames 3 and 20 and up on 10 and 25.
|
|
88
88
|
fun scripted(i: Int) -> Input =
|
|
@@ -94,7 +94,7 @@ fun scripted(i: Int) -> Input =
|
|
|
94
94
|
keys := Slice<Int>()
|
|
95
95
|
if {i >= 3 && i < 10} || {i >= 20 && i < 25}
|
|
96
96
|
keys.push(Key.Space)
|
|
97
|
-
Input(held:
|
|
97
|
+
Input(held: Held.keysOnly(keys), events: events, pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0)
|
|
98
98
|
|
|
99
99
|
# A jittery frame time around 1/60 s for frame `i`.
|
|
100
100
|
fun jitter(i: Int) -> Float =
|
tests/port/game/replay.plum
CHANGED
|
@@ -9,7 +9,7 @@ import std/Io
|
|
|
9
9
|
import std/math/Random
|
|
10
10
|
import std/math/Math
|
|
11
11
|
import std/math/Vec2
|
|
12
|
-
import std/game/
|
|
12
|
+
import std/game/Input
|
|
13
13
|
import std/game/Replay
|
|
14
14
|
import std/collections/List
|
|
15
15
|
import std/game/Screen
|
|
@@ -133,7 +133,7 @@ primitive ReplayTest[UnitTest] =
|
|
|
133
133
|
assert back.at(3).held.isButtonDown(Mouse.Middle) && back.at(3).held.isButtonDown(Mouse.Right)
|
|
134
134
|
assert back.at(3).held.same(r.at(3).held)
|
|
135
135
|
for i := range 4
|
|
136
|
-
assert
|
|
136
|
+
assert Replay.sameInput(back.at(i), r.at(i))
|
|
137
137
|
assert back.at(1).pos.x == 0.1
|
|
138
138
|
assert back.at(1).pos.y == 1.0e-300
|
|
139
139
|
assert back.at(2).wheel == 0.1
|
|
@@ -185,40 +185,40 @@ primitive ReplayTest[UnitTest] =
|
|
|
185
185
|
xs := [0.0, 1.0, -3.0, 0.1, 100.25, -0.3333333333333333, 1234567.891, 1.0e-300, 3.0e300, 0.016666666666666666, Float.Pi].toSlice()
|
|
186
186
|
for i := range xs.length()
|
|
187
187
|
x := xs.at(i)
|
|
188
|
-
if Some(back) :=
|
|
188
|
+
if Some(back) := Replay.parseFloatCode(Replay.floatCode(x))
|
|
189
189
|
assert back == x
|
|
190
190
|
else
|
|
191
191
|
assert false
|
|
192
|
-
assert
|
|
192
|
+
assert Replay.floatCode(3.0) == "3"
|
|
193
|
-
assert
|
|
193
|
+
assert Replay.floatCode(0.5) == "1:-1"
|
|
194
|
-
assert
|
|
194
|
+
assert Replay.floatCode(-2.75) == "-11:-2"
|
|
195
|
-
assert
|
|
195
|
+
assert Replay.parseFloatCode("x").isNone()
|
|
196
|
-
assert
|
|
196
|
+
assert Replay.parseFloatCode("1:y").isNone()
|
|
197
|
-
assert
|
|
197
|
+
assert Replay.parseFloatCode("1:99999999999").isNone()
|
|
198
198
|
|
|
199
199
|
# negative zero, the smallest Floats, NaN and the infinities are written exactly
|
|
200
200
|
fun negativeZeroTheSmallestFloatsNaNAndThe() =
|
|
201
201
|
negZero := 0.0 * -1.0
|
|
202
|
-
assert
|
|
202
|
+
assert Replay.floatCode(negZero) == "-0"
|
|
203
|
-
assert
|
|
203
|
+
assert Replay.floatCode(0.0) == "0"
|
|
204
|
-
if Some(z) :=
|
|
204
|
+
if Some(z) := Replay.parseFloatCode(Replay.floatCode(negZero))
|
|
205
205
|
assert z == 0.0
|
|
206
206
|
assert 1.0 / z < 0.0
|
|
207
207
|
else
|
|
208
208
|
assert false
|
|
209
209
|
tiny := [Float.Min, -Float.Min, Float.Min * 3.0, 2.2250738585072014e-308, 2.2250738585072009e-308, Float.Max, -Float.Max].toSlice()
|
|
210
210
|
for i := range tiny.length()
|
|
211
|
-
if Some(back) :=
|
|
211
|
+
if Some(back) := Replay.parseFloatCode(Replay.floatCode(tiny.at(i)))
|
|
212
212
|
assert back == tiny.at(i)
|
|
213
213
|
else
|
|
214
214
|
assert false
|
|
215
215
|
inf := Float.Max * 2.0
|
|
216
|
-
assert
|
|
216
|
+
assert Replay.floatCode(inf) == "inf"
|
|
217
|
-
assert
|
|
217
|
+
assert Replay.floatCode(-inf) == "-inf"
|
|
218
|
-
assert
|
|
218
|
+
assert Replay.floatCode(0.0 / 0.0) == "nan"
|
|
219
|
-
assert
|
|
219
|
+
assert Replay.parseFloatCode("inf").unwrapOr(0.0) == inf
|
|
220
|
-
assert
|
|
220
|
+
assert Replay.parseFloatCode("-inf").unwrapOr(0.0) == -inf
|
|
221
|
-
assert
|
|
221
|
+
assert Replay.parseFloatCode("nan").unwrapOr(0.0).isNaN()
|
|
222
222
|
odd := Input(held: Held.none(), events: [PointerMove(x: negZero, y: inf)].toSlice(), pos: Vec2(x: negZero, y: Float.Min), wheel: 0.0 / 0.0)
|
|
223
223
|
if Some(back) := Replay.parseLine(Replay.line(odd).trim())
|
|
224
224
|
assert 1.0 / back.pos.x < 0.0
|
|
@@ -270,7 +270,7 @@ primitive ReplayTest[UnitTest] =
|
|
|
270
270
|
# the same bits. `sqrt` is correctly rounded: sqrt(2) is `Float.Sqrt2`, 0x3FF6A09E667F3BCD.
|
|
271
271
|
# std maths gives the same results on every host
|
|
272
272
|
fun stdMathsGivesTheSameResultsOnEvery() =
|
|
273
|
-
assert
|
|
273
|
+
assert Replay.floatCode(Math.sin(1.0)) == "3789648413623927:-52"
|
|
274
|
-
assert
|
|
274
|
+
assert Replay.floatCode(Math.sqrt(2.0)) == "6369051672525773:-52"
|
|
275
275
|
assert Math.sqrt(2.0) == Float.Sqrt2
|
|
276
276
|
assert Math.hypot(3.0, 4.0) == 5.0
|
tests/port/game/routing.plum
CHANGED
|
@@ -63,11 +63,11 @@ fun placeOf(g: Gen) -> Place =
|
|
|
63
63
|
y := Float(g.below(200) - 60) + g.unit()
|
|
64
64
|
kind := g.below(8)
|
|
65
65
|
if kind < 4
|
|
66
|
-
|
|
66
|
+
Place.at(x, y)
|
|
67
67
|
else if kind < 6
|
|
68
|
-
|
|
68
|
+
Place.at(x, y).scaleTo(scaleOf(g), scaleOf(g))
|
|
69
69
|
else
|
|
70
|
-
|
|
70
|
+
Place.at(x, y).pivotAt(Float(g.below(40)), Float(g.below(40))).rotateTo(g.unit() * 6.0).scaleTo(scaleOf(g), scaleOf(g))
|
|
71
71
|
|
|
72
72
|
fun touchOf(g: Gen) -> Touchable =
|
|
73
73
|
k := g.below(10)
|
|
@@ -119,7 +119,7 @@ fun reference(specs: Slice<Spec>, p: Vec2) -> Int =
|
|
|
119
119
|
if near then -2 else if inside then i else best
|
|
120
120
|
)
|
|
121
121
|
|
|
122
|
-
fun hitId(items: List<View>, p: Vec2) -> Int = match
|
|
122
|
+
fun hitId(items: List<View>, p: Vec2) -> Int = match Views.hitIn(items, p)
|
|
123
123
|
Some(a) => match a
|
|
124
124
|
RNode as n => n.id
|
|
125
125
|
_ => -3
|
|
@@ -154,11 +154,11 @@ primitive RoutingTest[UnitTest] =
|
|
|
154
154
|
# the reference itself: a hidden parent hides its children, a Disabled one takes them out, the last drawn wins
|
|
155
155
|
fun theReferenceItselfAHiddenParentHidesIts() =
|
|
156
156
|
specs := [
|
|
157
|
-
Spec(parent: -1, at:
|
|
157
|
+
Spec(parent: -1, at: Place.at(0.0, 0.0), w: 100.0, h: 100.0, touch: Enabled, visible: true),
|
|
158
|
-
Spec(parent: 0, at:
|
|
158
|
+
Spec(parent: 0, at: Place.at(10.0, 10.0), w: 10.0, h: 10.0, touch: Enabled, visible: true),
|
|
159
|
-
Spec(parent: -1, at:
|
|
159
|
+
Spec(parent: -1, at: Place.at(50.0, 50.0), w: 10.0, h: 10.0, touch: Enabled, visible: false),
|
|
160
|
-
Spec(parent: -1, at:
|
|
160
|
+
Spec(parent: -1, at: Place.at(0.0, 0.0), w: 30.0, h: 30.0, touch: Disabled, visible: true),
|
|
161
|
-
Spec(parent: 3, at:
|
|
161
|
+
Spec(parent: 3, at: Place.at(0.0, 0.0), w: 30.0, h: 30.0, touch: Enabled, visible: true)].toSlice()
|
|
162
162
|
assert reference(specs, Vec2(x: 15.5, y: 15.5)) == 1
|
|
163
163
|
assert reference(specs, Vec2(x: 55.5, y: 55.5)) == 0
|
|
164
164
|
assert reference(specs, Vec2(x: 80.5, y: 80.5)) == 0
|
tests/port/game/screens.plum
CHANGED
|
@@ -15,7 +15,7 @@ import std/math/Affine
|
|
|
15
15
|
import std/math/Random
|
|
16
16
|
import std/math/Math
|
|
17
17
|
import std/game/Color
|
|
18
|
-
import std/game/
|
|
18
|
+
import std/game/Input
|
|
19
19
|
import std/game/Replay
|
|
20
20
|
import std/game/Place
|
|
21
21
|
import std/game/View
|
|
@@ -124,7 +124,7 @@ fun turnedBy(g: View) -> View = match g
|
|
|
124
124
|
|
|
125
125
|
# A group turned a quarter about its origin.
|
|
126
126
|
struct Turned[View, Touch](group: Group) =
|
|
127
|
-
fun place(self) -> Place =
|
|
127
|
+
fun place(self) -> Place = Place.at(self.group.x, self.group.y).rotateTo(Float.HalfPi)
|
|
128
128
|
|
|
129
129
|
fun view(self) -> List<View> = self.group.children
|
|
130
130
|
|
|
@@ -163,7 +163,7 @@ fun probeOf(s: Screen) -> ProbeScreen = match s
|
|
|
163
163
|
|
|
164
164
|
fun names(xs: List<ProbeAct>) -> Str = xs.fold("", |s, a| if s == "" then a.toStr() else "{s} {a.toStr()}")
|
|
165
165
|
|
|
166
|
-
fun probe(io: Io) -> TestRig<ProbeScreen> =
|
|
166
|
+
fun probe(io: Io) -> TestRig<ProbeScreen> = TestRig.mount(io, ProbeScreen.create(Always))
|
|
167
167
|
|
|
168
168
|
fun twoScreens() -> Game =
|
|
169
169
|
first: Screen := ProbeScreen.create(Always)
|
|
@@ -176,7 +176,7 @@ fun playOf(t: TestRig<Screen>) -> PlayProbe = match t.game.screens.at(1)
|
|
|
176
176
|
|
|
177
177
|
# A walker that moves right `speed` a second, stepped by the game.
|
|
178
178
|
struct Walker[Actor](x: Float, speed: Float) =
|
|
179
|
-
fun place(self) -> Place =
|
|
179
|
+
fun place(self) -> Place = Place.at(self.x, 0.0)
|
|
180
180
|
|
|
181
181
|
fun view(self) -> List<View> = [Box.of(0.0, 0.0, 4.0, 4.0, Color.White)]
|
|
182
182
|
|
|
@@ -252,7 +252,7 @@ primitive ScreensTest[UnitTest] =
|
|
|
252
252
|
# an overlay in the view takes the press meant for what is under it
|
|
253
253
|
fun anOverlayInTheViewTakesThePress() =
|
|
254
254
|
t := probe(io).tick()
|
|
255
|
-
covered :=
|
|
255
|
+
covered := TestRig.mount(io, ProbeScreen(..t.screen, covered: true)).clickAt(15.0, 15.0).tick()
|
|
256
256
|
assert covered.screen.hits.length() == 1
|
|
257
257
|
assert isOverlay(covered.screen.hits.at(0))
|
|
258
258
|
|
|
@@ -296,7 +296,7 @@ primitive ScreensTest[UnitTest] =
|
|
|
296
296
|
|
|
297
297
|
# effects go to another screen, which fades in, its presses dropped until it is in; the old one is kept and resumes
|
|
298
298
|
fun effectsGoToAnotherScreenWhichFadesIn() =
|
|
299
|
-
t :=
|
|
299
|
+
t := TestRig.play(io, twoScreens()).press(Key.KeyG).tick()
|
|
300
300
|
assert t.wentTo().unwrap() == "play"
|
|
301
301
|
assert t.showing() == "play"
|
|
302
302
|
assert t.game.shifting()
|
|
@@ -311,14 +311,14 @@ primitive ScreensTest[UnitTest] =
|
|
|
311
311
|
assert probeOf(back.game.screen()).ticks == 1
|
|
312
312
|
|
|
313
313
|
fun exitStopsTheTicks() =
|
|
314
|
-
t :=
|
|
314
|
+
t := TestRig.play(io, twoScreens()).press(Key.KeyA).tick()
|
|
315
315
|
q := t.press(Key.KeyQ).tick()
|
|
316
316
|
assert q.exited()
|
|
317
317
|
assert q.tick().game.ticks == q.game.ticks
|
|
318
318
|
|
|
319
319
|
# an OnDemand view screen ticks after an action and while it is busy, and sleeps otherwise
|
|
320
320
|
fun anOnDemandViewScreenTicksAfterAnAction() =
|
|
321
|
-
t :=
|
|
321
|
+
t := TestRig.mount(io, ProbeScreen.create(OnDemand)).ticks(10)
|
|
322
322
|
assert t.screen.ticks == 1
|
|
323
323
|
d := TestRig(..t, game: t.game.frameDrawn())
|
|
324
324
|
assert d.game.idle()
|
|
@@ -342,7 +342,7 @@ primitive ScreensTest[UnitTest] =
|
|
|
342
342
|
|
|
343
343
|
# undo goes back before the last user action, redo forward again, and a rewind replays the ticks
|
|
344
344
|
fun undoGoesBackBeforeTheLastUserAction() =
|
|
345
|
-
t :=
|
|
345
|
+
t := TestRig.mountWith(io, ProbeScreen.create(Always)).ticks(5).press(Key.KeyA).ticks(5).press(Key.Space).ticks(5)
|
|
346
346
|
assert t.screen.keys == 1 && t.screen.ticks == 15
|
|
347
347
|
rolled := t.screen.roll
|
|
348
348
|
u := t.undo()
|
|
@@ -357,7 +357,7 @@ primitive ScreensTest[UnitTest] =
|
|
|
357
357
|
assert r2.game.tickNow() == 15 && r2.screen.roll == rolled
|
|
358
358
|
m := r2.rewindTo(3)
|
|
359
359
|
assert m.screen.ticks == 3 && m.game.rewound == 3
|
|
360
|
-
assert !
|
|
360
|
+
assert !TestRig.mount(io, ProbeScreen.create(Always)).undo().moved()
|
|
361
361
|
s := m.stepForward()
|
|
362
362
|
assert s.game.tickNow() == 4
|
|
363
363
|
p := s.pause().tick()
|
|
@@ -366,7 +366,7 @@ primitive ScreensTest[UnitTest] =
|
|
|
366
366
|
|
|
367
367
|
# the game steps the screen's actors between held and the tick action; steps() false pauses it
|
|
368
368
|
fun theGameStepsTheScreensActorsBetweenHeld() =
|
|
369
|
-
t :=
|
|
369
|
+
t := TestRig.mount(io, WalkScreen.create()).tick(0.5)
|
|
370
370
|
assert t.screen.seen.length() == 2 && t.screen.seen.at(0) == 0.0 && t.screen.seen.at(1) == 30.0
|
|
371
371
|
u := t.press(Key.KeyP).tick(0.5)
|
|
372
372
|
assert u.screen.walker.x == 30.0
|
|
@@ -375,12 +375,12 @@ primitive ScreensTest[UnitTest] =
|
|
|
375
375
|
# a plain screen's actors are stepped too
|
|
376
376
|
fun aPlainScreensActorsAreSteppedToo() =
|
|
377
377
|
only: Screen := StillScreen(walker: Walker(x: 0.0, speed: 10.0))
|
|
378
|
-
t :=
|
|
378
|
+
t := TestRig.play(io, Game(screens: [only])).ticks(6)
|
|
379
379
|
assert Math.near(t.first<Walker>().x, 1.0, 0.000001)
|
|
380
380
|
|
|
381
381
|
# the hud sits on the window, one unit a pixel: hit before the view, and found by the kit
|
|
382
382
|
fun theHudSitsOnTheWindowOneUnit() =
|
|
383
|
-
t :=
|
|
383
|
+
t := TestRig.mount(io, WalkScreen.create()).resize(1600.0, 600.0)
|
|
384
384
|
assert t.count<Box>() == 2
|
|
385
385
|
assert t.hit(1590.0, 590.0).isSome()
|
|
386
386
|
u := t.clickAt(1590.0, 590.0).tick()
|
|
@@ -389,7 +389,7 @@ primitive ScreensTest[UnitTest] =
|
|
|
389
389
|
|
|
390
390
|
# a screen's camera looks at what it follows, zoomed, and presses and toWorld follow it
|
|
391
391
|
fun aScreensCameraLooksAtWhatItFollows() =
|
|
392
|
-
t :=
|
|
392
|
+
t := TestRig.mount(io, WalkScreen.create()).tick(1.0)
|
|
393
393
|
assert Math.near(t.toWorld(0.0, 0.0).x, 0.0, 0.000001)
|
|
394
394
|
u := t.press(Key.KeyF).tick(1.0)
|
|
395
395
|
assert u.screen.walker.x == 120.0
|
|
@@ -400,7 +400,7 @@ primitive ScreensTest[UnitTest] =
|
|
|
400
400
|
|
|
401
401
|
# the kit's dispatch does an undo its effect asks for at once
|
|
402
402
|
fun theKitsDispatchDoesAnUndoItsEffect() =
|
|
403
|
-
t :=
|
|
403
|
+
t := TestRig.mountWith(io, WalkScreen.create()).press(Key.KeyP).ticks(3)
|
|
404
404
|
assert t.screen.paused && t.game.tickNow() == 3
|
|
405
405
|
u := t.dispatch(Undo)
|
|
406
406
|
assert u.moved()
|
tests/std/game/Anim.plum
CHANGED
|
@@ -7,61 +7,65 @@ import std/Test
|
|
|
7
7
|
primitive AnimTest[UnitTest] =
|
|
8
8
|
# move_halfway
|
|
9
9
|
fun moveHalfway() =
|
|
10
|
-
p :=
|
|
10
|
+
p := MoveBy(dx: 10.0, dy: 0.0, secs: 2.0).at(1.0)
|
|
11
|
-
assert
|
|
11
|
+
assert AnimHelpers.near(p.dx, 5.0)
|
|
12
|
-
assert
|
|
12
|
+
assert AnimHelpers.near(p.dy, 0.0)
|
|
13
|
-
assert
|
|
13
|
+
assert AnimHelpers.near(p.rotation, 0.0)
|
|
14
|
-
assert
|
|
14
|
+
assert AnimHelpers.near(p.scaleX, 1.0)
|
|
15
|
-
assert
|
|
15
|
+
assert AnimHelpers.near(p.scaleY, 1.0)
|
|
16
16
|
|
|
17
17
|
# seq_second_step
|
|
18
18
|
fun seqSecondStep() =
|
|
19
19
|
a := Seq(steps: [MoveBy(dx: 10.0, dy: 0.0, secs: 1.0), RotateBy(radians: 1.0, secs: 1.0)])
|
|
20
|
-
p :=
|
|
20
|
+
p := a.at(1.5)
|
|
21
|
-
assert
|
|
21
|
+
assert AnimHelpers.near(p.dx, 10.0)
|
|
22
|
-
assert
|
|
22
|
+
assert AnimHelpers.near(p.rotation, 0.5)
|
|
23
|
-
assert
|
|
23
|
+
assert AnimHelpers.near(a.length(), 2.0)
|
|
24
24
|
|
|
25
25
|
# past_the_end_clamps
|
|
26
26
|
fun pastTheEndClamps() =
|
|
27
|
-
p :=
|
|
27
|
+
p := MoveBy(dx: 10.0, dy: 4.0, secs: 1.0).at(99.0)
|
|
28
|
-
assert
|
|
28
|
+
assert AnimHelpers.near(p.dx, 10.0)
|
|
29
|
-
assert
|
|
29
|
+
assert AnimHelpers.near(p.dy, 4.0)
|
|
30
30
|
|
|
31
31
|
# forever_wraps
|
|
32
32
|
fun foreverWraps() =
|
|
33
|
-
p :=
|
|
33
|
+
p := Forever(anim: RotateBy(radians: 1.0, secs: 2.0)).at(5.0)
|
|
34
|
-
assert
|
|
34
|
+
assert AnimHelpers.near(p.rotation, 0.5)
|
|
35
35
|
|
|
36
36
|
# forever_no_drift_after_many_laps
|
|
37
37
|
fun foreverNoDriftAfterManyLaps() =
|
|
38
|
-
p :=
|
|
38
|
+
p := Forever(anim: RotateBy(radians: 1.0, secs: 0.5)).at(1000.25)
|
|
39
|
-
assert
|
|
39
|
+
assert AnimHelpers.near(p.rotation, 0.5)
|
|
40
40
|
|
|
41
41
|
# forever_zero_lap_is_identity
|
|
42
42
|
fun foreverZeroLapIsIdentity() =
|
|
43
|
-
p :=
|
|
43
|
+
p := Forever(anim: Delay(secs: 0.0)).at(3.0)
|
|
44
|
-
assert
|
|
44
|
+
assert AnimHelpers.near(p.rotation, 0.0)
|
|
45
|
-
q :=
|
|
45
|
+
q := Forever(anim: MoveBy(dx: 5.0, dy: 0.0, secs: 0.0)).at(3.0)
|
|
46
|
-
assert
|
|
46
|
+
assert AnimHelpers.near(q.dx, 5.0)
|
|
47
47
|
|
|
48
48
|
# zero_length_move_applies_at_once
|
|
49
49
|
fun zeroLengthMoveAppliesAtOnce() =
|
|
50
|
-
p :=
|
|
50
|
+
p := MoveBy(dx: 10.0, dy: 0.0, secs: 0.0).at(0.0)
|
|
51
|
-
assert
|
|
51
|
+
assert AnimHelpers.near(p.dx, 10.0)
|
|
52
52
|
|
|
53
53
|
# negative_time_is_identity
|
|
54
54
|
fun negativeTimeIsIdentity() =
|
|
55
|
-
p :=
|
|
55
|
+
p := MoveBy(dx: 10.0, dy: 3.0, secs: 1.0).at(0.0 - 1.0)
|
|
56
|
-
assert
|
|
56
|
+
assert AnimHelpers.near(p.dx, 0.0)
|
|
57
|
-
assert
|
|
57
|
+
assert AnimHelpers.near(p.dy, 0.0)
|
|
58
|
-
assert
|
|
58
|
+
assert AnimHelpers.near(p.scaleX, 1.0)
|
|
59
59
|
|
|
60
60
|
# par_combines
|
|
61
61
|
fun parCombines() =
|
|
62
62
|
a := Par(steps: [MoveBy(dx: 2.0, dy: 0.0, secs: 1.0), MoveBy(dx: 3.0, dy: 0.0, secs: 2.0), ScaleTo(sx: 2.0, sy: 3.0, secs: 1.0), ScaleTo(sx: 2.0, sy: 1.0, secs: 1.0)])
|
|
63
|
-
p :=
|
|
63
|
+
p := a.at(1.0)
|
|
64
|
-
assert
|
|
64
|
+
assert AnimHelpers.near(p.dx, 3.5)
|
|
65
|
-
assert
|
|
65
|
+
assert AnimHelpers.near(p.scaleX, 4.0)
|
|
66
|
-
assert
|
|
66
|
+
assert AnimHelpers.near(p.scaleY, 3.0)
|
|
67
|
-
assert
|
|
67
|
+
assert AnimHelpers.near(a.length(), 2.0)
|
|
68
|
+
|
|
69
|
+
primitive AnimHelpers =
|
|
70
|
+
fun near(a: Float, b: Float) -> Bool =
|
|
71
|
+
{a - b}.abs() < 1e-6
|
tests/std/game/Box.plum
CHANGED
|
@@ -6,7 +6,7 @@ import std/math/Vec2
|
|
|
6
6
|
import std/math/Affine
|
|
7
7
|
import std/math/Aabb
|
|
8
8
|
import std/game/Color
|
|
9
|
-
import std/game/
|
|
9
|
+
import std/game/Gfx
|
|
10
10
|
import std/game/Place
|
|
11
11
|
import std/game/View
|
|
12
12
|
import std/game/Sized
|
|
@@ -23,4 +23,4 @@ primitive BoxTest[UnitTest] =
|
|
|
23
23
|
assert b.touch().disabled()
|
|
24
24
|
assert Box(x: 0.0, y: 0.0, w: 1.0, h: 1.0, color: Color.White, touch: Enabled).touch().enabled()
|
|
25
25
|
assert b.place().x == 1.0
|
|
26
|
-
assert !
|
|
26
|
+
assert !Box.contains(0.0, 3.0, Vec2(x: 0.0, y: 0.0))
|
tests/std/game/Circle.plum
CHANGED
|
@@ -9,7 +9,7 @@ import std/math/Vec2
|
|
|
9
9
|
import std/math/Affine
|
|
10
10
|
import std/math/Aabb
|
|
11
11
|
import std/game/Color
|
|
12
|
-
import std/game/
|
|
12
|
+
import std/game/Gfx
|
|
13
13
|
import std/game/Place
|
|
14
14
|
import std/game/View
|
|
15
15
|
import std/game/Sized
|
tests/std/game/{gfx.plum → Gfx.plum}
RENAMED
|
@@ -4,12 +4,12 @@ import std/Str
|
|
|
4
4
|
import std/math/Vec2
|
|
5
5
|
import std/game/Color
|
|
6
6
|
import std/Io
|
|
7
|
-
import std/game/
|
|
7
|
+
import std/game/Gfx
|
|
8
8
|
import std/Test
|
|
9
9
|
|
|
10
10
|
primitive GfxTest[UnitTest] =
|
|
11
11
|
fun aMissingAssetMessageNamesTheFile() =
|
|
12
|
-
assert
|
|
12
|
+
assert Gfx.missingAsset("texture", "ship.png") == "missing texture: ship.png"
|
|
13
13
|
|
|
14
14
|
fun blendConstants() =
|
|
15
15
|
assert Blend.Normal == 0
|
tests/std/game/History.plum
CHANGED
|
@@ -3,7 +3,7 @@ import std/Float
|
|
|
3
3
|
import std/Bool
|
|
4
4
|
import std/collections/List
|
|
5
5
|
import std/math/Vec2
|
|
6
|
-
import std/game/
|
|
6
|
+
import std/game/Input
|
|
7
7
|
import std/game/Control
|
|
8
8
|
import std/game/History
|
|
9
9
|
import std/Test
|
|
@@ -11,7 +11,7 @@ import std/Test
|
|
|
11
11
|
primitive HistoryTest[UnitTest] =
|
|
12
12
|
# a history goes back, keeps the future for a redo, and a user action cuts it
|
|
13
13
|
fun aHistoryGoesBackKeepsTheFutureFor() =
|
|
14
|
-
h := [true, false, true, false, false].fold(History.start(100, Vec2(x: 800.0, y: 600.0)), |acc, u| acc.push(
|
|
14
|
+
h := [true, false, true, false, false].fold(History.start(100, Vec2(x: 800.0, y: 600.0)), |acc, u| acc.push(HistoryHelpers.tick(u)))
|
|
15
15
|
assert h.now() == 5
|
|
16
16
|
assert h.lastUser() == 2
|
|
17
17
|
b := h.backTo(2)
|
|
@@ -21,11 +21,15 @@ primitive HistoryTest[UnitTest] =
|
|
|
21
21
|
assert b2.future.length() == 5 && b2.nextUser() == 2
|
|
22
22
|
f := b2.forward(2)
|
|
23
23
|
assert f.taken.length() == 2 && f.history.now() == 2 && f.history.future.length() == 3
|
|
24
|
-
assert f.history.push(
|
|
24
|
+
assert f.history.push(HistoryHelpers.tick(false)).future.length() == 3
|
|
25
|
-
assert f.history.push(
|
|
25
|
+
assert f.history.push(HistoryHelpers.tick(true)).future.isEmpty()
|
|
26
26
|
assert h.backTo(3).cut().future.isEmpty()
|
|
27
27
|
|
|
28
28
|
# a full history lets its ticks go and counts on
|
|
29
29
|
fun aFullHistoryLetsItsTicksGoAnd() =
|
|
30
|
-
h := 0.until(4).fold(History.start(3, Vec2(x: 1.0, y: 1.0)), |acc, i| acc.push(
|
|
30
|
+
h := 0.until(4).fold(History.start(3, Vec2(x: 1.0, y: 1.0)), |acc, i| acc.push(HistoryHelpers.tick(false)))
|
|
31
31
|
assert h.full && h.now() == 4
|
|
32
|
+
|
|
33
|
+
primitive HistoryHelpers =
|
|
34
|
+
fun tick(user: Bool) -> HistoryTick =
|
|
35
|
+
HistoryTick(input: Input.create(), pixels: Vec2(x: 800.0, y: 600.0), dt: 0.1, user: user, asks: Nil)
|
tests/std/game/{input.plum → Input.plum}
RENAMED
|
@@ -8,7 +8,7 @@ import std/collections/List
|
|
|
8
8
|
import std/Str
|
|
9
9
|
import std/Result
|
|
10
10
|
import std/game/Replay
|
|
11
|
-
import std/game/
|
|
11
|
+
import std/game/Input
|
|
12
12
|
import std/Test
|
|
13
13
|
|
|
14
14
|
primitive InputTest[UnitTest] =
|
|
@@ -43,14 +43,14 @@ primitive InputTest[UnitTest] =
|
|
|
43
43
|
fun keyCodesCoverEveryKeyConstant() =
|
|
44
44
|
codes := InputEvent.keyCodes()
|
|
45
45
|
assert codes.length() == 56
|
|
46
|
-
assert
|
|
46
|
+
assert Held.keysOnly(codes).isDown(Key.Space)
|
|
47
|
-
assert
|
|
47
|
+
assert Held.keysOnly(codes).isDown(Key.Num9)
|
|
48
|
-
assert
|
|
48
|
+
assert Held.keysOnly(codes).isDown(Key.KeyZ)
|
|
49
|
-
assert
|
|
49
|
+
assert Held.keysOnly(codes).isDown(Key.Backslash)
|
|
50
|
-
assert
|
|
50
|
+
assert Held.keysOnly(codes).isDown(Key.Backspace)
|
|
51
|
-
assert
|
|
51
|
+
assert Held.keysOnly(codes).isDown(Key.Up)
|
|
52
|
-
assert
|
|
52
|
+
assert Held.keysOnly(codes).isDown(Key.LeftControl)
|
|
53
|
-
assert
|
|
53
|
+
assert Held.keysOnly(codes).isDown(Key.RightSuper)
|
|
54
54
|
assert 1.until(codes.length()).all(|i| codes.at(i - 1) < codes.at(i))
|
|
55
55
|
|
|
56
56
|
# pointer events have a position, keys don't
|
|
@@ -80,7 +80,7 @@ primitive InputTest[UnitTest] =
|
|
|
80
80
|
|
|
81
81
|
# an axis is the high key held against the low one
|
|
82
82
|
fun anAxisIsTheHighKeyHeldAgainst() =
|
|
83
|
-
i := |keys: List<Int>| Input(held:
|
|
83
|
+
i := |keys: List<Int>| Input(held: Held.keysOnly(keys.toSlice()), events: Slice<InputEvent>(), pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0)
|
|
84
84
|
assert i([Key.Right]).axis(Key.Left, Key.Right) == 1.0
|
|
85
85
|
assert i([Key.Left]).axis(Key.Left, Key.Right) == -1.0
|
|
86
86
|
assert i([Key.Left, Key.Right]).axis(Key.Left, Key.Right) == 0.0
|
|
@@ -89,7 +89,7 @@ primitive InputTest[UnitTest] =
|
|
|
89
89
|
# only this frame's events count, not what lies past them
|
|
90
90
|
fun onlyThisFramesEventsCountNotWhatLies() =
|
|
91
91
|
all := [KeyDown(key: Key.KeyA), KeyDown(key: Key.KeyB), PointerDown(x: 1.0, y: 1.0), PointerUp(x: 1.0, y: 1.0)].toSlice()
|
|
92
|
-
held :=
|
|
92
|
+
held := Held.keysOnly([Key.KeyA].toSlice())
|
|
93
93
|
one := Input(held: held, events: all.slice(0, 1), pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0)
|
|
94
94
|
assert one.isKeyPressed(Key.KeyA)
|
|
95
95
|
assert !one.isKeyPressed(Key.KeyB)
|
tests/std/game/Label.plum
CHANGED
|
@@ -9,7 +9,7 @@ import std/math/Vec2
|
|
|
9
9
|
import std/math/Affine
|
|
10
10
|
import std/math/Aabb
|
|
11
11
|
import std/game/Color
|
|
12
|
-
import std/game/
|
|
12
|
+
import std/game/Gfx
|
|
13
13
|
import std/game/Place
|
|
14
14
|
import std/game/View
|
|
15
15
|
import std/game/Sized
|
tests/std/game/Overlay.plum
CHANGED
|
@@ -9,7 +9,7 @@ import std/math/Vec2
|
|
|
9
9
|
import std/math/Affine
|
|
10
10
|
import std/math/Aabb
|
|
11
11
|
import std/game/Color
|
|
12
|
-
import std/game/
|
|
12
|
+
import std/game/Gfx
|
|
13
13
|
import std/game/Place
|
|
14
14
|
import std/game/View
|
|
15
15
|
import std/game/Sized
|
|
@@ -26,9 +26,9 @@ primitive OverlayTest[UnitTest] =
|
|
|
26
26
|
button := Box(x: 40.0, y: 40.0, w: 10.0, h: 10.0, color: Color.White, touch: Enabled)
|
|
27
27
|
over := Overlay.of(100.0, 100.0, [button, Label.of(0.0, 0.0, "Paused", 24.0, Color.White)])
|
|
28
28
|
items: List<View> := [under, over]
|
|
29
|
-
assert @sameRef(
|
|
29
|
+
assert @sameRef(Views.hitIn(items, Vec2(x: 5.0, y: 5.0)).unwrap(), over)
|
|
30
|
-
assert
|
|
30
|
+
assert Views.hitIn(items, Vec2(x: 5000.0, y: -20.0)).isNone()
|
|
31
|
-
assert @sameRef(
|
|
31
|
+
assert @sameRef(Views.hitIn(items, Vec2(x: 45.0, y: 45.0)).unwrap(), button)
|
|
32
|
-
assert @sameRef(
|
|
32
|
+
assert @sameRef(Views.hitIn([under], Vec2(x: 5.0, y: 5.0)).unwrap(), under)
|
|
33
33
|
off := Overlay(..Overlay.of(10.0, 10.0, Nil), touch: Disabled)
|
|
34
|
-
assert @sameRef(
|
|
34
|
+
assert @sameRef(Views.hitIn([under, off], Vec2(x: 5.0, y: 5.0)).unwrap(), under)
|
tests/std/game/Place.plum
CHANGED
|
@@ -8,7 +8,7 @@ import std/Test
|
|
|
8
8
|
primitive PlaceTest[UnitTest] =
|
|
9
9
|
# local translates, then turns and scales about the origin
|
|
10
10
|
fun localTranslatesThenTurnsAndScalesAboutThe() =
|
|
11
|
-
b :=
|
|
11
|
+
b := Place.turned(10.0, 20.0, 2.0, 0.0).pivotAt(1.0, 1.0)
|
|
12
12
|
p := b.local().apply(Vec2(x: 1.0, y: 1.0))
|
|
13
13
|
assert p.x == 11.0 && p.y == 21.0
|
|
14
14
|
q := b.local().apply(Vec2(x: 2.0, y: 1.0))
|
|
@@ -26,7 +26,7 @@ primitive PlaceTest[UnitTest] =
|
|
|
26
26
|
assert q.a == Math.cos(0.25) * -2.0 && q.b == Math.sin(0.25) * -2.0
|
|
27
27
|
|
|
28
28
|
fun theCopiesChangeOneThing() =
|
|
29
|
-
p :=
|
|
29
|
+
p := Place.at(1.0, 2.0)
|
|
30
30
|
assert p.scaleX == 1.0 && p.rotation == 0.0 && p.originX == 0.0
|
|
31
31
|
assert p.moveBy(1.0, 2.0).x == 2.0 && p.moveBy(1.0, 2.0).y == 4.0
|
|
32
32
|
assert p.moveTo(5.0, 6.0).y == 6.0
|
|
@@ -36,8 +36,8 @@ primitive PlaceTest[UnitTest] =
|
|
|
36
36
|
|
|
37
37
|
# a place maps a point into its own coordinates and back
|
|
38
38
|
fun aPlaceMapsAPointIntoItsOwn() =
|
|
39
|
-
p :=
|
|
39
|
+
p := Place.pivoted(10.0, 20.0, 5.0, 5.0).rotateTo(1.0).fade(0.5)
|
|
40
40
|
q := p.toLocal(p.local().apply(Vec2(x: 3.0, y: 4.0)))
|
|
41
41
|
assert {q.x - 3.0}.abs() < 0.000001 && {q.y - 4.0}.abs() < 0.000001
|
|
42
42
|
assert p.alpha == 0.5
|
|
43
|
-
assert
|
|
43
|
+
assert Place.at(1.0, 2.0).under(Affine.Identity).e == 1.0
|
tests/std/game/Screen.plum
CHANGED
|
@@ -9,7 +9,7 @@ import std/math/Vec2
|
|
|
9
9
|
import std/math/Affine
|
|
10
10
|
import std/math/Aabb
|
|
11
11
|
import std/game/Color
|
|
12
|
-
import std/game/
|
|
12
|
+
import std/game/Input
|
|
13
13
|
import std/game/View
|
|
14
14
|
import std/game/Viewport
|
|
15
15
|
import std/game/Control
|
tests/std/game/Sprite.plum
CHANGED
|
@@ -9,7 +9,7 @@ import std/math/Vec2
|
|
|
9
9
|
import std/math/Affine
|
|
10
10
|
import std/math/Aabb
|
|
11
11
|
import std/game/Color
|
|
12
|
-
import std/game/
|
|
12
|
+
import std/game/Gfx
|
|
13
13
|
import std/game/Place
|
|
14
14
|
import std/game/View
|
|
15
15
|
import std/game/Sized
|
tests/std/game/View.plum
CHANGED
|
@@ -28,38 +28,38 @@ primitive ViewTest[UnitTest] =
|
|
|
28
28
|
views := Slice<Int>()
|
|
29
29
|
v := Viewed(x: 1000.0, log: log, views: views, lit: true, inner: true)
|
|
30
30
|
assert views.length() == 0
|
|
31
|
-
|
|
31
|
+
Views.paint(io, [v], Affine.Identity, 1.0)
|
|
32
32
|
assert views.length() == 2
|
|
33
33
|
# x then alpha of each probe painted, in order: the inner Viewed's (at 1100, alpha
|
|
34
34
|
# 0.25) come after its parent's own.
|
|
35
35
|
expect := [1001.0, 0.5, 1002.0, 0.5, 1014.0, 0.25, 1101.0, 0.25, 1102.0, 0.25, 1114.0, 0.125]
|
|
36
36
|
assert log.length() == expect.length()
|
|
37
37
|
assert 0.until(expect.length()).all(|i| log.at(i) == expect.at(i))
|
|
38
|
-
|
|
38
|
+
Views.paint(io, [v], Affine.Identity, 1.0)
|
|
39
39
|
assert views.length() == 4
|
|
40
40
|
|
|
41
41
|
# an item left out is not drawn; a view paints itself, then its view; an invisible one draws nothing
|
|
42
42
|
fun anItemLeftOutIsNotDrawnA() =
|
|
43
43
|
log := Slice<Float>()
|
|
44
44
|
views := Slice<Int>()
|
|
45
|
-
|
|
45
|
+
Views.paint(io, [Viewed(x: 0.0, log: log, views: views, lit: false, inner: false)], Affine.Identity, 1.0)
|
|
46
46
|
assert log.length() == 4
|
|
47
47
|
log.clear()
|
|
48
|
-
|
|
48
|
+
Views.paint(io, [SelfPainted(log: log, shows: true)], Affine.Identity, 1.0)
|
|
49
49
|
assert log.length() == 3 && log.at(0) == -1.0 && log.at(1) == 3.0
|
|
50
50
|
log.clear()
|
|
51
|
-
|
|
51
|
+
Views.paint(io, [SelfPainted(log: log, shows: false)], Affine.Identity, 1.0)
|
|
52
52
|
assert log.isEmpty()
|
|
53
53
|
found := Slice<Drawn>()
|
|
54
|
-
|
|
54
|
+
Views.drawnIn([SelfPainted(log: log, shows: false), Box.of(0.0, 0.0, 1.0, 1.0, Color.White)], Affine.Identity, 1.0, found, Aabb.of(0.0, 0.0, 0.0, 0.0), false)
|
|
55
55
|
assert found.length() == 1
|
|
56
56
|
|
|
57
57
|
# stepping a list of actors steps each in order; stepping a view's items steps only its actors
|
|
58
58
|
fun steppingAListOfActorsStepsEachIn() =
|
|
59
|
-
steppers :=
|
|
59
|
+
steppers := Views.stepAll([Stepper(time: 0.0), Stepper(time: 1.0)], 0.5)
|
|
60
60
|
assert steppers.at(0).time == 0.5 && steppers.at(1).time == 1.5
|
|
61
61
|
items: List<View> := [Stepper(time: 0.0), Box.of(0.0, 0.0, 1.0, 1.0, Color.White), Stepper(time: 2.0)]
|
|
62
|
-
stepped :=
|
|
62
|
+
stepped := Views.stepIn(Views.stepIn(items, 0.5), 0.25)
|
|
63
63
|
times := stepped.map(|v| match v
|
|
64
64
|
Stepper as s => s.time
|
|
65
65
|
_ => -1.0)
|
|
@@ -73,27 +73,74 @@ primitive ViewTest[UnitTest] =
|
|
|
73
73
|
turned := TurnedGroup(x: 200.0, y: 100.0, children: [Group.of(50.0, 0.0, [inner])])
|
|
74
74
|
hidden := Group(x: 0.0, y: 0.0, touch: Disabled, children: [Box(x: 400.0, y: 400.0, w: 50.0, h: 50.0, color: Color.White, touch: Enabled)])
|
|
75
75
|
items: List<View> := [under, top, turned, hidden, Label.of(0.0, 0.0, "deco", 24.0, Color.White)]
|
|
76
|
-
assert @sameRef(
|
|
76
|
+
assert @sameRef(Views.hitIn(items, Vec2(x: 15.0, y: 15.0)).unwrap(), top)
|
|
77
77
|
# The inner box is 50 along the group's x, which is turned a quarter: 50 down from it.
|
|
78
|
-
assert @sameRef(
|
|
78
|
+
assert @sameRef(Views.hitIn(items, Vec2(x: 195.0, y: 155.0)).unwrap(), inner)
|
|
79
|
-
assert
|
|
79
|
+
assert Views.hitIn(items, Vec2(x: 420.0, y: 420.0)).isNone()
|
|
80
|
-
assert
|
|
80
|
+
assert Views.hitIn(items, Vec2(x: 2.0, y: 2.0)).isNone()
|
|
81
|
-
c :=
|
|
81
|
+
c := Views.worldIn(items, inner, Affine.Identity).unwrap().apply(Vec2(x: 0.0, y: 0.0))
|
|
82
82
|
assert Math.near(c.x, 200.0, 0.001) && Math.near(c.y, 150.0, 0.001)
|
|
83
|
-
assert
|
|
83
|
+
assert Views.worldIn(items, Box.of(0.0, 0.0, 1.0, 1.0, Color.White), Affine.Identity).isNone()
|
|
84
84
|
|
|
85
85
|
# the draw list holds every item and what is under it, and culling leaves out what misses the area
|
|
86
86
|
fun theDrawListHoldsEveryItemAndWhat() =
|
|
87
87
|
items: List<View> := [Box.of(0.0, 0.0, 10.0, 10.0, Color.White), Group.of(5000.0, 0.0, [Circle.of(0.0, 0.0, 2.0, Color.White), Label.of(0.0, 0.0, "far", 24.0, Color.White)]), Box.of(5000.0, 0.0, 10.0, 10.0, Color.White)]
|
|
88
88
|
all := Slice<Drawn>()
|
|
89
|
-
|
|
89
|
+
Views.drawnIn(items, Affine.Identity, 1.0, all, Aabb.of(0.0, 0.0, 0.0, 0.0), false)
|
|
90
90
|
assert all.length() == 5
|
|
91
91
|
culled := Slice<Drawn>()
|
|
92
|
-
|
|
92
|
+
Views.drawnIn(items, Affine.Identity, 1.0, culled, Aabb.of(0.0, 0.0, 800.0, 600.0), true)
|
|
93
93
|
# The group is not Sized, so it stays; its sized children and the far box go.
|
|
94
94
|
assert culled.length() == 2
|
|
95
95
|
|
|
96
96
|
# a box seen through a turn is bounded by its turned corners
|
|
97
97
|
fun aBoxSeenThroughATurnIsBounded() =
|
|
98
|
-
b :=
|
|
98
|
+
b := Views.through(Aabb.of(0.0, 0.0, 10.0, 20.0), Affine(a: 0.0, b: 1.0, c: -1.0, d: 0.0, e: 100.0, f: 0.0))
|
|
99
99
|
assert b.min.x == 80.0 && b.max.x == 100.0 && b.min.y == 0.0 && b.max.y == 10.0
|
|
100
|
+
|
|
101
|
+
# A view that logs where it is painted (the x of its origin) and at what alpha.
|
|
102
|
+
struct ViewProbe[View](x: Float, log: Slice<Float>, fade: Float) =
|
|
103
|
+
fun place(self) -> Place = Place.at(self.x, 0.0).fade(self.fade)
|
|
104
|
+
|
|
105
|
+
fun paintSelf(self, io: Io, m: Affine, alpha: Float) =
|
|
106
|
+
self.log.push(m.e)
|
|
107
|
+
self.log.push(alpha)
|
|
108
|
+
|
|
109
|
+
# A view drawn by its view, which counts each call into `views`: its probes, one shown
|
|
110
|
+
# only while `lit`, one in a group, and a nested viewed one.
|
|
111
|
+
struct Viewed[View](x: Float, log: Slice<Float>, views: Slice<Int>, lit: Bool, inner: Bool) =
|
|
112
|
+
fun place(self) -> Place = Place.at(self.x, 0.0).fade(0.5)
|
|
113
|
+
|
|
114
|
+
fun view(self) -> List<View> =
|
|
115
|
+
self.views.push(1)
|
|
116
|
+
first: List<View> := [ViewHelpers.probeAt(1.0, self.log)]
|
|
117
|
+
lit: List<View> := if self.lit then [ViewHelpers.probeAt(2.0, self.log)] else Nil
|
|
118
|
+
grouped: List<View> := [Group.of(10.0, 0.0, [ViewProbe(x: 4.0, log: self.log, fade: 0.5)])]
|
|
119
|
+
inner: List<View> := if self.inner then [Viewed(x: 100.0, log: self.log, views: self.views, lit: true, inner: false)] else Nil
|
|
120
|
+
first.append(lit).append(grouped).append(inner)
|
|
121
|
+
|
|
122
|
+
# A view that paints itself (-1) and then has its view drawn, and is invisible unless
|
|
123
|
+
# `shows`.
|
|
124
|
+
struct SelfPainted[View](log: Slice<Float>, shows: Bool) =
|
|
125
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
126
|
+
|
|
127
|
+
fun visible(self) -> Bool = self.shows
|
|
128
|
+
|
|
129
|
+
fun view(self) -> List<View> = [ViewHelpers.probeAt(3.0, self.log)]
|
|
130
|
+
|
|
131
|
+
fun paintSelf(self, io: Io, m: Affine, alpha: Float) = self.log.push(-1.0)
|
|
132
|
+
|
|
133
|
+
# An actor that adds up its steps.
|
|
134
|
+
struct Stepper[Actor](time: Float) =
|
|
135
|
+
fun place(self) -> Place = Place.at(0.0, 0.0)
|
|
136
|
+
|
|
137
|
+
fun step(self, dt: Float) -> Stepper = Stepper(time: self.time + dt)
|
|
138
|
+
|
|
139
|
+
# A group turned a quarter.
|
|
140
|
+
struct TurnedGroup[View](x: Float, y: Float, children: List<View>) =
|
|
141
|
+
fun place(self) -> Place = Place.at(self.x, self.y).rotateTo(Float.HalfPi)
|
|
142
|
+
|
|
143
|
+
fun view(self) -> List<View> = self.children
|
|
144
|
+
|
|
145
|
+
primitive ViewHelpers =
|
|
146
|
+
fun probeAt(x: Float, log: Slice<Float>) -> View = ViewProbe(x: x, log: log, fade: 1.0)
|