plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
b5b860b
— Peter John
2026-11-09T14:54:58+05:30
orb_run_tree and the scene_trees benchmark on trait Actor[M] (actor-trait task 7)
benchmarks/scene_trees/game_enum_tree.plum
CHANGED
|
@@ -12,6 +12,7 @@ import std/game/Vec2
|
|
|
12
12
|
import std/game/Affine
|
|
13
13
|
import std/game/Color
|
|
14
14
|
import std/collections/functional/FRoseTree
|
|
15
|
+
import std/game_enum_tree/Place
|
|
15
16
|
import std/game_enum_tree/Actor
|
|
16
17
|
import std/game_enum_tree/ECtx
|
|
17
18
|
import std/game_enum_tree/EStage
|
|
@@ -50,35 +51,38 @@ struct Rng(mut s: Int) =
|
|
|
50
51
|
|
|
51
52
|
fun float(self, k: Int) -> Float = Float(self.next(k * 10)) / 10.0
|
|
52
53
|
|
|
53
|
-
# The
|
|
54
|
+
# The scene's actors are `Actor[Int]` (the benchmark sends no messages): the groups are
|
|
55
|
+
# std's `Group`, which a step returns as it is, and the leaves are `Bod`s, master's `Leaf`
|
|
54
|
-
#
|
|
56
|
+
# (master's `EBody` fields and the velocity): a 20 by 20 red box that moves by (vx, vy) and
|
|
57
|
+
# bounces. A leaf keeps its position in its own fields and makes its `Place` when asked:
|
|
58
|
+
# with a `Place` field moved by a nested spread (`Bod(..self, at: self.at.moveTo(..))`)
|
|
55
|
-
#
|
|
59
|
+
# each step allocates a cell, which FBIP doesn't reuse, and the step workload is about
|
|
56
|
-
#
|
|
60
|
+
# 1.7 times slower.
|
|
57
|
-
struct Bod(
|
|
61
|
+
struct Bod(id: Int, x: Float, y: Float, vx: Float, vy: Float) is Actor[Int] =
|
|
62
|
+
fun key(self) -> Int = self.id
|
|
58
63
|
|
|
59
|
-
fun
|
|
64
|
+
fun place(self) -> Place = place.at(self.x, self.y)
|
|
60
65
|
|
|
61
|
-
fun
|
|
66
|
+
fun paint(self, io: Io, m: Affine, alpha: Float) = actor.boxPaint(io, m, alpha, 20.0, 20.0, Color.Red)
|
|
62
67
|
|
|
68
|
+
fun hit(self, p: Vec2) -> Bool = actor.boxHit(20.0, 20.0, p)
|
|
69
|
+
|
|
63
|
-
fun step(self,
|
|
70
|
+
fun step(self, ctx: ECtx[Int], dt: Float) -> Bod =
|
|
64
|
-
if !self.moves
|
|
65
|
-
a
|
|
66
|
-
else
|
|
67
|
-
|
|
71
|
+
nx := self.x + self.vx * dt
|
|
68
|
-
|
|
72
|
+
ny := self.y + self.vy * dt
|
|
69
|
-
|
|
73
|
+
vx := if nx < 0.0 || nx > 200.0 then 0.0 - self.vx else self.vx
|
|
70
|
-
|
|
74
|
+
vy := if ny < 0.0 || ny > 200.0 then 0.0 - self.vy else self.vy
|
|
71
|
-
|
|
75
|
+
Bod(..self, x: nx, y: ny, vx: vx, vy: vy)
|
|
72
76
|
|
|
73
|
-
fun group(id: Int, x: Float, y: Float, s: Float, r: Float) -> Actor[
|
|
77
|
+
fun group(id: Int, x: Float, y: Float, s: Float, r: Float) -> Actor[Int] =
|
|
74
|
-
|
|
78
|
+
Group(id: id, at: place.turned(x, y, s, r))
|
|
75
79
|
|
|
76
|
-
fun newLeaf(rng: Rng, id: Int) -> FRoseTree[Actor[
|
|
80
|
+
fun newLeaf(rng: Rng, id: Int) -> FRoseTree[Actor[Int]] =
|
|
77
81
|
x := rng.float(200)
|
|
78
82
|
y := rng.float(200)
|
|
79
83
|
vx := rng.float(100) - 50.0
|
|
80
84
|
vy := rng.float(100) - 50.0
|
|
81
|
-
FRoseTree.leaf(
|
|
85
|
+
FRoseTree.leaf(Bod(id: id, x: x, y: y, vx: vx, vy: vy))
|
|
82
86
|
|
|
83
87
|
# What the workloads change besides the stage: the live ids, the random numbers, the next
|
|
84
88
|
# id to give out.
|
|
@@ -88,42 +92,43 @@ fun bench(n: Int) -> Bench = Bench(live: Slice[Int](), rng: Rng(s: 12345), nextI
|
|
|
88
92
|
|
|
89
93
|
# The scene, built bottom up as master builds it: each group's children first, then the
|
|
90
94
|
# groups under the root. Ids: the root 0, the groups 1 to 10, the leaves from 11.
|
|
91
|
-
fun build(n: Int, sc: Bench) -> EStage[
|
|
95
|
+
fun build(n: Int, sc: Bench) -> EStage[Int] =
|
|
92
|
-
kids := Slice[Slice[FRoseTree[Actor[
|
|
96
|
+
kids := Slice[Slice[FRoseTree[Actor[Int]]]]()
|
|
93
97
|
for g := range 10
|
|
94
|
-
kids.push(Slice[FRoseTree[Actor[
|
|
98
|
+
kids.push(Slice[FRoseTree[Actor[Int]]]())
|
|
95
99
|
for i := range n - 11
|
|
96
100
|
kids.at(i % 10).push(newLeaf(sc.rng, 11 + i))
|
|
97
101
|
sc.live.push(11 + i)
|
|
98
|
-
groups := Slice[FRoseTree[Actor[
|
|
102
|
+
groups := Slice[FRoseTree[Actor[Int]]]()
|
|
99
103
|
for g := range 10
|
|
100
104
|
gb := group(g + 1, Float(100 + 300 * {g % 5}), Float(100 + 300 * {g / 5}), 1.0 + 0.05 * Float(g), 0.1 * Float(g))
|
|
101
|
-
empty: FRoseTree[Actor[
|
|
105
|
+
empty: FRoseTree[Actor[Int]] := FRoseEmpty
|
|
102
106
|
groups.push(FRoseNode(value: gb, firstChild: estage.echainFrom(kids.at(g), 0), nextSibling: empty))
|
|
103
|
-
empty: FRoseTree[Actor[
|
|
107
|
+
empty: FRoseTree[Actor[Int]] := FRoseEmpty
|
|
104
108
|
root := FRoseNode(value: group(0, 0.0, 0.0, 1.0, 0.0), firstChild: estage.echainFrom(groups, 0), nextSibling: empty)
|
|
105
109
|
EStage(root: root, camX: 0.0, camY: 0.0, camScale: 1.0)
|
|
106
110
|
|
|
107
111
|
# Every node's position, in preorder, folded into one number.
|
|
108
|
-
fun hashTree(t: FRoseTree[Actor[
|
|
112
|
+
fun hashTree(t: FRoseTree[Actor[Int]], h: Int) -> Int =
|
|
109
113
|
t.fold(h, |r0, b|
|
|
114
|
+
p := b.place()
|
|
110
|
-
r := {r0 * 31 + Int(
|
|
115
|
+
r := {r0 * 31 + Int(p.x * 1000.0)} % 1000000007
|
|
111
|
-
{r * 31 + Int(
|
|
116
|
+
{r * 31 + Int(p.y * 1000.0)} % 1000000007
|
|
112
117
|
)
|
|
113
118
|
|
|
114
119
|
# `t` ticks of `stage.step`.
|
|
115
|
-
fun steps(stage: EStage[
|
|
120
|
+
fun steps(stage: EStage[Int], ctx: ECtx[Int], dt: Float, t: Int) -> EStage[Int] =
|
|
116
121
|
if t == 0 then stage else steps(stage.step(ctx, dt), ctx, dt, t - 1)
|
|
117
122
|
|
|
118
123
|
# `steps` with each tick's stage also stored in `kept`, a ring of the last 60.
|
|
119
|
-
fun stepsKept(stage: EStage[
|
|
124
|
+
fun stepsKept(stage: EStage[Int], ctx: ECtx[Int], dt: Float, t: Int, kept: Slice[EStage[Int]]) -> EStage[Int] =
|
|
120
125
|
if t == 0
|
|
121
126
|
stage
|
|
122
127
|
else
|
|
123
128
|
keep(kept, stage, t)
|
|
124
129
|
stepsKept(stage.step(ctx, dt), ctx, dt, t - 1, kept)
|
|
125
130
|
|
|
126
|
-
fun keep(kept: Slice[EStage[
|
|
131
|
+
fun keep(kept: Slice[EStage[Int]], stage: EStage[Int], t: Int) =
|
|
127
132
|
if kept.length() < 60
|
|
128
133
|
kept.push(stage)
|
|
129
134
|
else
|
|
@@ -131,7 +136,7 @@ fun keep(kept: Slice[EStage[Bod]], stage: EStage[Bod], t: Int) =
|
|
|
131
136
|
|
|
132
137
|
# Pushes the tick's kills and spawns to `ctx.out`: `k` random live leaves die and `k` new
|
|
133
138
|
# ones are born under random groups.
|
|
134
|
-
fun pushChurn(sc: Bench, ctx: ECtx[
|
|
139
|
+
fun pushChurn(sc: Bench, ctx: ECtx[Int], k: Int) =
|
|
135
140
|
for j := range k
|
|
136
141
|
i := sc.rng.next(sc.live.length())
|
|
137
142
|
victim := sc.live.at(i)
|
|
@@ -147,7 +152,7 @@ fun pushChurn(sc: Bench, ctx: ECtx[Bod], k: Int) =
|
|
|
147
152
|
|
|
148
153
|
# `t` ticks of churn: the commands pushed, then a step that applies them in its walk, or
|
|
149
154
|
# (`after`) a step, then the commands pushed and applied with a second walk.
|
|
150
|
-
fun churns(stage: EStage[
|
|
155
|
+
fun churns(stage: EStage[Int], sc: Bench, ctx: ECtx[Int], dt: Float, k: Int, t: Int, after: Bool) -> EStage[Int] =
|
|
151
156
|
if t == 0
|
|
152
157
|
stage
|
|
153
158
|
else if after
|
|
@@ -161,7 +166,7 @@ fun churns(stage: EStage[Bod], sc: Bench, ctx: ECtx[Bod], dt: Float, k: Int, t:
|
|
|
161
166
|
churns(stage.step(ctx, dt), sc, ctx, dt, k, t - 1, after)
|
|
162
167
|
|
|
163
168
|
# `churns` (commands before the step) with each tick's stage also kept in a ring of 60.
|
|
164
|
-
fun churnsKept(stage: EStage[
|
|
169
|
+
fun churnsKept(stage: EStage[Int], sc: Bench, ctx: ECtx[Int], dt: Float, k: Int, t: Int, kept: Slice[EStage[Int]]) -> EStage[Int] =
|
|
165
170
|
if t == 0
|
|
166
171
|
stage
|
|
167
172
|
else
|
|
@@ -171,11 +176,11 @@ fun churnsKept(stage: EStage[Bod], sc: Bench, ctx: ECtx[Bod], dt: Float, k: Int,
|
|
|
171
176
|
|
|
172
177
|
# EStage's walk with no commands and the root's context for every body: no parent's world
|
|
173
178
|
# transform is computed.
|
|
174
|
-
fun stepFlat(t: FRoseTree[Actor[
|
|
179
|
+
fun stepFlat(t: FRoseTree[Actor[Int]], ctx: ECtx[Int], dt: Float) -> FRoseTree[Actor[Int]] = match t
|
|
175
180
|
FRoseEmpty => t
|
|
176
181
|
FRoseNode(b, k, n) =>
|
|
177
|
-
nb := b.
|
|
182
|
+
nb := b.step(ctx, dt)
|
|
178
|
-
if nb.
|
|
183
|
+
if nb.gone()
|
|
179
184
|
stepFlat(n, ctx, dt)
|
|
180
185
|
else
|
|
181
186
|
k2 := if k.isEmpty() then k else stepFlat(k, ctx, dt)
|
|
@@ -183,20 +188,20 @@ fun stepFlat(t: FRoseTree[Actor[Bod]], ctx: ECtx[Bod], dt: Float) -> FRoseTree[A
|
|
|
183
188
|
|
|
184
189
|
# EStage's walk with no commands, every body's world transform computed (and checked, so
|
|
185
190
|
# it is used).
|
|
186
|
-
fun stepEager(t: FRoseTree[Actor[
|
|
191
|
+
fun stepEager(t: FRoseTree[Actor[Int]], ctx: ECtx[Int], dt: Float) -> FRoseTree[Actor[Int]] = match t
|
|
187
192
|
FRoseEmpty => t
|
|
188
193
|
FRoseNode(b, k, n) =>
|
|
189
|
-
m := ctx.worldOf(b.local())
|
|
194
|
+
m := ctx.worldOf(b.place().local())
|
|
190
195
|
assert m.e > -1.0e30
|
|
191
|
-
cc := ctx.under(b.local(), b.opacity())
|
|
196
|
+
cc := ctx.under(b.place().local(), b.opacity())
|
|
192
|
-
nb := b.
|
|
197
|
+
nb := b.step(ctx, dt)
|
|
193
|
-
if nb.
|
|
198
|
+
if nb.gone()
|
|
194
199
|
stepEager(n, ctx, dt)
|
|
195
200
|
else
|
|
196
201
|
k2 := if k.isEmpty() then k else stepEager(k, cc, dt)
|
|
197
202
|
FRoseNode(value: nb, firstChild: k2, nextSibling: stepEager(n, ctx, dt))
|
|
198
203
|
|
|
199
|
-
fun walks(stage: EStage[
|
|
204
|
+
fun walks(stage: EStage[Int], ctx: ECtx[Int], dt: Float, t: Int, eager: Bool) -> EStage[Int] =
|
|
200
205
|
if t == 0
|
|
201
206
|
stage
|
|
202
207
|
else if eager
|
|
@@ -205,7 +210,7 @@ fun walks(stage: EStage[Bod], ctx: ECtx[Bod], dt: Float, t: Int, eager: Bool) ->
|
|
|
205
210
|
walks(stage.withRoot(stepFlat(stage.root, ctx, dt)), ctx, dt, t - 1, eager)
|
|
206
211
|
|
|
207
212
|
# 1000 flattens: the entries over all of them plus the summed translations of the last.
|
|
208
|
-
fun draws(stage: EStage[
|
|
213
|
+
fun draws(stage: EStage[Int], r: Int, total: Int) -> Int =
|
|
209
214
|
d := stage.flatten()
|
|
210
215
|
if r == 999
|
|
211
216
|
last := lastSum(d, 0, 0.0)
|
|
@@ -213,25 +218,27 @@ fun draws(stage: EStage[Bod], r: Int, total: Int) -> Int =
|
|
|
213
218
|
else
|
|
214
219
|
draws(stage, r + 1, total + d.length())
|
|
215
220
|
|
|
216
|
-
fun lastSum(d: Slice[EDrawn[
|
|
221
|
+
fun lastSum(d: Slice[EDrawn[Int]], i: Int, acc: Float) -> Float =
|
|
217
222
|
if i >= d.length() then acc else lastSum(d, i + 1, acc + d.at(i).m.e + d.at(i).m.f)
|
|
218
223
|
|
|
219
224
|
# 1000 hit tests at random stage points: the positions of the bodies hit, in order.
|
|
220
|
-
fun hits(stage: EStage[
|
|
225
|
+
fun hits(stage: EStage[Int], rng: Rng, q: Int, sum: Int) -> Int =
|
|
221
226
|
if q == 1000
|
|
222
227
|
sum
|
|
223
228
|
else
|
|
224
229
|
x := rng.float(1600)
|
|
225
230
|
y := rng.float(800)
|
|
226
231
|
next := match stage.hit(x, y)
|
|
232
|
+
Some(a) =>
|
|
233
|
+
p := a.place()
|
|
227
|
-
|
|
234
|
+
{sum * 31 + Int(p.x * 100.0) + 7 * Int(p.y * 100.0)} % 1000000007
|
|
228
235
|
None => {sum * 31 + 1} % 1000000007
|
|
229
236
|
hits(stage, rng, q + 1, next)
|
|
230
237
|
|
|
231
238
|
# Runs workload `w` on `stage`, a scene of `n` nodes, and returns its checksum.
|
|
232
|
-
fun work(stage: EStage[
|
|
239
|
+
fun work(stage: EStage[Int], sc: Bench, w: Int, n: Int) -> Int =
|
|
233
240
|
dt := 1.0 / 60.0
|
|
234
|
-
ctx := ECtx[
|
|
241
|
+
ctx := ECtx[Int]()
|
|
235
242
|
k := {{n - 11} / 100}.max(1)
|
|
236
243
|
if w == 1
|
|
237
244
|
hashTree(steps(stage, ctx, dt, 1000).root, 0)
|
|
@@ -244,9 +251,9 @@ fun work(stage: EStage[Bod], sc: Bench, w: Int, n: Int) -> Int =
|
|
|
244
251
|
else if w == 5 || w == 6
|
|
245
252
|
hashTree(walks(stage, ctx, dt, 1000, w == 6).root, 0)
|
|
246
253
|
else if w == 8
|
|
247
|
-
hashTree(stepsKept(stage, ctx, dt, 1000, Slice[EStage[
|
|
254
|
+
hashTree(stepsKept(stage, ctx, dt, 1000, Slice[EStage[Int]]()).root, 0)
|
|
248
255
|
else
|
|
249
|
-
hashTree(churnsKept(stage, sc, ctx, dt, k, 1000, Slice[EStage[
|
|
256
|
+
hashTree(churnsKept(stage, sc, ctx, dt, k, 1000, Slice[EStage[Int]]()).root, 0) + sc.live.length()
|
|
250
257
|
|
|
251
258
|
# Builds the scene, then times the workload alone with the wall clock.
|
|
252
259
|
fun run(io: Io, w: Int, n: Int) -> Int =
|
docs/benchmarks/2026-11-08-scene-trees-branch.md
CHANGED
|
@@ -105,6 +105,42 @@ So "with and without the branch's in-place reuse" measures the same program twic
|
|
|
105
105
|
|
|
106
106
|
With the first two, master's persistent tree should come close to the branch's (1.3 times std/game_tree on step, level on churn), though that is an expectation, not a measurement. It will stay behind std/game on step, because a persistent tree allocates every moving body every tick, and no reuse on either branch removes that.
|
|
107
107
|
|
|
108
|
+
## On the Actor trait (2026-11-09)
|
|
109
|
+
|
|
110
|
+
std/game_enum_tree moved from `Actor[K]` (one struct of shared fields plus the game's kind) to `trait Actor[M]` (actor-trait tasks 6 and 7). The benchmark's groups became std's `Group` and its leaves a `Bod` struct that `is Actor[Int]`, with `x`/`y` fields and a `place()` that builds the `Place`. Median ms of 5 rounds, each cell a fresh process, all three versions built with `plum build` on the same compiler; `Actor[K]` is the benchmark and std as of e797f07. "trait, `at: Place`" is the same port with the leaf keeping a `Place` field, moved by a nested spread (`Bod(..self, at: self.at.moveTo(..))`), default build only: that cell is not reused, so each step allocates. Checksums are the same in every version and mode.
|
|
111
|
+
|
|
112
|
+
| workload | N | Actor[K] | trait | trait, `at: Place` | Actor[K] --no-fbip | trait--no-fbip | Actor[K] --no-fusion | trait--no-fusion | checksum |
|
|
113
|
+
|---|---|---|---|---|---|---|---|---|---|
|
|
114
|
+
| step | 100 | 4 | 3 | 5 | 4 | 3 | 4 | 4 | 809683012 |
|
|
115
|
+
| step | 1000 | 26 | 22 | 36 | 25 | 22 | 26 | 23 | 459572469 |
|
|
116
|
+
| step | 10000 | 251 | 190 | 333 | 251 | 199 | 251 | 199 | 77718206 |
|
|
117
|
+
| churn | 100 | 6 | 6 | 7 | 6 | 6 | 6 | 6 | 998134459 |
|
|
118
|
+
| churn | 1000 | 40 | 40 | 53 | 40 | 41 | 40 | 41 | 411093292 |
|
|
119
|
+
| churn | 10000 | 411 | 373 | 508 | 411 | 377 | 411 | 377 | 850449880 |
|
|
120
|
+
| draw | 100 | 7 | 8 | 8 | 7 | 8 | 7 | 8 | 211827 |
|
|
121
|
+
| draw | 1000 | 75 | 74 | 78 | 74 | 73 | 73 | 77 | 2145249 |
|
|
122
|
+
| draw | 10000 | 742 | 721 | 752 | 746 | 718 | 727 | 757 | 21533361 |
|
|
123
|
+
| hit | 100 | 6 | 6 | 7 | 7 | 7 | 6 | 7 | 462421202 |
|
|
124
|
+
| hit | 1000 | 55 | 55 | 56 | 55 | 55 | 57 | 57 | 649980221 |
|
|
125
|
+
| hit | 10000 | 467 | 465 | 474 | 472 | 465 | 474 | 484 | 241346959 |
|
|
126
|
+
| step, no transforms | 100 | 3 | 2 | 3 | 3 | 2 | 3 | 2 | 809683012 |
|
|
127
|
+
| step, no transforms | 1000 | 22 | 16 | 31 | 22 | 17 | 22 | 17 | 459572469 |
|
|
128
|
+
| step, no transforms | 10000 | 218 | 145 | 301 | 218 | 154 | 218 | 154 | 77718206 |
|
|
129
|
+
| step, eager transforms | 100 | 14 | 13 | 16 | 14 | 13 | 15 | 14 | 809683012 |
|
|
130
|
+
| step, eager transforms | 1000 | 131 | 125 | 144 | 131 | 124 | 130 | 126 | 459572469 |
|
|
131
|
+
| step, eager transforms | 10000 | 1331 | 1222 | 1419 | 1332 | 1230 | 1332 | 1246 | 77718206 |
|
|
132
|
+
| churn, apply after | 100 | 7 | 6 | 8 | 6 | 7 | 6 | 7 | 998134459 |
|
|
133
|
+
| churn, apply after | 1000 | 47 | 45 | 59 | 47 | 45 | 47 | 45 | 411093292 |
|
|
134
|
+
| churn, apply after | 10000 | 474 | 413 | 559 | 475 | 416 | 474 | 416 | 850449880 |
|
|
135
|
+
| step, 60 kept | 100 | 4 | 3 | 5 | 4 | 4 | 4 | 4 | 809683012 |
|
|
136
|
+
| step, 60 kept | 1000 | 51 | 23 | 40 | 51 | 27 | 51 | 27 | 459572469 |
|
|
137
|
+
| step, 60 kept | 10000 | 3022 | 407 | 1161 | 3024 | 1659 | 3021 | 1661 | 77718206 |
|
|
138
|
+
| churn, 60 kept | 100 | 5 | 6 | 7 | 6 | 5 | 6 | 6 | 998134459 |
|
|
139
|
+
| churn, 60 kept | 1000 | 66 | 42 | 57 | 65 | 45 | 65 | 45 | 411093292 |
|
|
140
|
+
| churn, 60 kept | 10000 | 3299 | 641 | 1507 | 3290 | 1832 | 3283 | 1829 | 850449880 |
|
|
141
|
+
|
|
142
|
+
The trait version is level on draw and hit (dispatch through `place()`, `shown()`, `paint` and `hit` costs nothing measurable), 10 to 33% faster on the step and churn workloads (a leaf is 5 fields instead of 14), and 2.5 to 7 times faster with 60 stages kept, where every step copies every actor. FBIP and fusion make no difference to `Actor[K]` here; on the trait version `--no-fbip` and `--no-fusion` cost 4 to 6% on step and 3 to 4 times on the kept workloads, which is not explained yet (reuse can't happen while 60 stages are held).
|
|
143
|
+
|
|
108
144
|
## How to rerun
|
|
109
145
|
|
|
110
146
|
From this branch's root, with master's compiler built from a copy of master (a `git archive` keeps the build, and the runtime that `plum build` compiles, out of your own master checkout):
|
examples/games/orb_run_tree.plum
CHANGED
|
@@ -5,13 +5,14 @@
|
|
|
5
5
|
#
|
|
6
6
|
# orb_run.plum keeps the game in plain lists (`orbs: List[Orb]`) and builds a fresh
|
|
7
7
|
# `SceneNode` picture of it every frame. This version keeps the game *in* the scene: an
|
|
8
|
-
# `EStage[
|
|
8
|
+
# `EStage[Msg]` from std/game_enum_tree, a tree of actors that lives from frame to frame.
|
|
9
|
-
# Every body is an `Actor[Kind]`: the actor holds what every body has (its id, its place
|
|
10
|
-
# in its parent, its colour, whether it is shown), and `Kind` is what this game adds, one
|
|
11
|
-
#
|
|
9
|
+
# Every kind of body is its own struct (`Ship`, `Orb`, `Mine`, `Star`, `Label`, the
|
|
10
|
+
# `Button`, ..) with only its own fields, and `is Actor[Msg]`: it says which actor it is
|
|
11
|
+
# (`key`) and where (`place`), and writes only the behaviour it has: its next version
|
|
12
|
-
#
|
|
12
|
+
# (`step`), its drawing (`paint`), its hit test (`hit`). The backdrop and the dimmer are
|
|
13
|
+
# std's `Box`, and the root and the layers std's `Group`. The stage's `step` walks the
|
|
13
|
-
#
|
|
14
|
+
# tree once a tick and gives every actor its next version; `draw` and `hit` walk it again
|
|
14
|
-
#
|
|
15
|
+
# with the parents' transforms and alphas multiplied down.
|
|
15
16
|
#
|
|
16
17
|
# 0 root (the camera that fits the arena on screen)
|
|
17
18
|
# ├─ 1 backdrop
|
|
@@ -23,14 +24,15 @@
|
|
|
23
24
|
# └─ 7 panel (hidden while playing) ─ 13 dim, 11 title, 12 prompt, 14 "Again"
|
|
24
25
|
#
|
|
25
26
|
# What is between bodies (an orb is touched, a mine hits the ship, the score and the
|
|
26
|
-
# lives) stays in `State`, which
|
|
27
|
+
# lives) stays in `State`, which reads actors after the step with `EStage.findAs` and
|
|
28
|
+
# changes one kind's own fields with `updateAs` (`stage.updateAs[Ship](ShipId, |s|
|
|
27
|
-
#
|
|
29
|
+
# s.steered(ax, ay))`), and adds and removes them with `add` and `kill`. The stage is a
|
|
28
|
-
# of `State`: a kept old state still has its old tree.
|
|
30
|
+
# value like the rest of `State`: a kept old state still has its old tree.
|
|
29
31
|
#
|
|
30
32
|
# Differences from orb_run.plum: the game-over panel fades by multiplying its opacity
|
|
31
33
|
# into each of its children instead of drawing through a render-target filter (so
|
|
32
34
|
# overlapping children are each translucent), and the ship's hurt flash, the title's
|
|
33
|
-
# pulse and the panel's fade keep their own clocks in their
|
|
35
|
+
# pulse and the panel's fade keep their own clocks in their own structs.
|
|
34
36
|
module orb_run_tree
|
|
35
37
|
import std/Str
|
|
36
38
|
import std/Int
|
|
@@ -50,6 +52,7 @@ import std/game/Anim
|
|
|
50
52
|
import std/game/ui
|
|
51
53
|
import std/game/input
|
|
52
54
|
import std/collections/functional/FRoseTree
|
|
55
|
+
import std/game_enum_tree/Place
|
|
53
56
|
import std/game_enum_tree/Actor
|
|
54
57
|
import std/game_enum_tree/ECtx
|
|
55
58
|
import std/game_enum_tree/EStage
|
|
@@ -169,11 +172,6 @@ fun fitX(sw: Float, sh: Float) -> Float =
|
|
|
169
172
|
fun fitY(sw: Float, sh: Float) -> Float =
|
|
170
173
|
{sh - fitZoom(sw, sh) * ArenaH} / 2.0
|
|
171
174
|
|
|
172
|
-
# The root actor placed as the camera for a `sw` by `sh` screen.
|
|
173
|
-
fun fitted(a: Actor[Kind], sw: Float, sh: Float) -> Actor[Kind] =
|
|
174
|
-
z := fitZoom(sw, sh)
|
|
175
|
-
a.at(fitX(sw, sh), fitY(sw, sh)).scaleTo(z, z)
|
|
176
|
-
|
|
177
175
|
# --- painting helpers: an actor paints in its own coordinates, seen through `m` ---
|
|
178
176
|
|
|
179
177
|
# A `size` square cell (col, row) of the sprite sheet with its top-left at (x, y).
|
|
@@ -195,161 +193,199 @@ fun paintText(io: Io, m: Affine, text: Str, x: Float, y: Float, size: Float, c:
|
|
|
195
193
|
|
|
196
194
|
# --- the kinds ---
|
|
197
195
|
|
|
198
|
-
#
|
|
199
|
-
#
|
|
200
|
-
#
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
fun
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
fun
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
# The ship steered by the axes for its next step.
|
|
308
|
-
fun steered(self, ax: Float, ay: Float) -> Kind = match self
|
|
309
|
-
Ship(vx, vy, _, _, h, ha, c, sh) => Ship(vx: vx, vy: vy, ax: ax, ay: ay, hurt: h, hurtAge: ha, clock: c, sheet: sh)
|
|
310
|
-
_ => self
|
|
311
|
-
|
|
312
|
-
# The ship just hit: hurt for `HurtTime`, its flash from the start.
|
|
313
|
-
fun struck(self) -> Kind = match self
|
|
314
|
-
Ship(vx, vy, ax, ay, _, _, c, sh) => Ship(vx: vx, vy: vy, ax: ax, ay: ay, hurt: HurtTime, hurtAge: 0.0, clock: c, sheet: sh)
|
|
315
|
-
_ => self
|
|
316
|
-
|
|
317
|
-
fun relabel(self, t: Str) -> Kind = match self
|
|
318
|
-
Label(_, s) => Label(text: t, size: s)
|
|
319
|
-
_ => self
|
|
320
|
-
|
|
321
|
-
fun looked(self, k: Int) -> Kind = match self
|
|
322
|
-
Button(l, _) => Button(label: l, look: k)
|
|
323
|
-
_ => self
|
|
324
|
-
|
|
325
|
-
# The ship's next actor: it accelerates by the axes, drag slows it and the walls stop it.
|
|
326
|
-
fun shipStep(a: Actor[Kind], vx: Float, vy: Float, ax: Float, ay: Float, hurt: Float, hurtAge: Float, clock: Float, sheet: Texture, dt: Float) -> Actor[Kind] =
|
|
327
|
-
keep := {1.0 - Drag * dt}.max(0.0)
|
|
328
|
-
r := ShipRadius
|
|
329
|
-
vx0 := {vx + ax * Thrust * dt} * keep
|
|
330
|
-
vy0 := {vy + ay * Thrust * dt} * keep
|
|
331
|
-
x0 := a.x + vx0 * dt
|
|
332
|
-
y0 := a.y + vy0 * dt
|
|
333
|
-
x := x0.max(r).min(ArenaW - r)
|
|
334
|
-
y := y0.max(r).min(ArenaH - r)
|
|
335
|
-
nvx := if x != x0 then 0.0 else vx0
|
|
336
|
-
nvy := if y != y0 then 0.0 else vy0
|
|
337
|
-
rot := if nvx * nvx + nvy * nvy > 400.0 then math.atan2(nvx, 0.0 - nvy) else a.rotation
|
|
338
|
-
Actor(
|
|
339
|
-
..a, x: x, y: y, rotation: rot,
|
|
340
|
-
kind: Ship(vx: nvx, vy: nvy, ax: ax, ay: ay, hurt: {hurt - dt}.max(0.0), hurtAge: hurtAge + dt, clock: clock + dt, sheet: sheet))
|
|
196
|
+
# Each kind of body is its own struct with only its own fields, and `is Actor[Msg]`: the
|
|
197
|
+
# stage asks it for its id (`key`), its place in its parent (`place`), its next version
|
|
198
|
+
# (`step`), its drawing (`paint`, in its own coordinates seen through `m`) and its hit test
|
|
199
|
+
# (`hit`), and whether it is shown and how opaque it is. A kind writes only what differs
|
|
200
|
+
# from the trait's defaults. Orbs, mines and the ship are centred on their place's (x, y)
|
|
201
|
+
# and turn about it. The backdrop and the dimmer are std's `Box`, and the root, the layers
|
|
202
|
+
# and the hud are std's `Group`.
|
|
203
|
+
|
|
204
|
+
# A twinkling background star at its fixed spot.
|
|
205
|
+
struct Star(id: Int, i: Int, clock: Float) is Actor[Msg] =
|
|
206
|
+
fun key(self) -> Int = self.id
|
|
207
|
+
|
|
208
|
+
fun place(self) -> Place = place.at(Float({self.i * 7919} % 800), Float({self.i * 104729} % 600))
|
|
209
|
+
|
|
210
|
+
fun step(self, ctx: ECtx[Msg], dt: Float) -> Star = Star(..self, clock: self.clock + dt)
|
|
211
|
+
|
|
212
|
+
fun paint(self, io: Io, m: Affine, alpha: Float) =
|
|
213
|
+
twinkle := 0.25 + 0.2 * math.sin(self.clock * 2.0 + Float(self.i))
|
|
214
|
+
paintCircle(io, m, 0.0, 0.0, 1.0 + Float(self.i % 3) * 0.5, Color(r: 0.7, g: 0.8, b: 1.0, a: twinkle * alpha))
|
|
215
|
+
|
|
216
|
+
# A glowing orb drifting at (vx, vy). `phase` offsets its animation from the other orbs.
|
|
217
|
+
struct Orb(id: Int, at: Place, vx: Float, vy: Float, phase: Float, clock: Float, sheet: Texture) is Actor[Msg] =
|
|
218
|
+
fun key(self) -> Int = self.id
|
|
219
|
+
|
|
220
|
+
fun place(self) -> Place = self.at
|
|
221
|
+
|
|
222
|
+
fun step(self, ctx: ECtx[Msg], dt: Float) -> Orb =
|
|
223
|
+
m := driftOf(Motion(x: self.at.x, y: self.at.y, vx: self.vx, vy: self.vy), OrbRadius, dt)
|
|
224
|
+
Orb(..self, at: self.at.moveTo(m.x, m.y), vx: m.vx, vy: m.vy, clock: self.clock + dt)
|
|
225
|
+
|
|
226
|
+
# The glow is drawn additively, then the sprite on top.
|
|
227
|
+
fun paint(self, io: Io, m: Affine, alpha: Float) =
|
|
228
|
+
gfx.blendMode(io, Blend.Add)
|
|
229
|
+
paintCircle(io, m, 0.0, 0.0, 26.0, Color(r: 0.35, g: 0.22, b: 0.0, a: 0.5 * alpha))
|
|
230
|
+
paintCircle(io, m, 0.0, 0.0, 17.0, Color(r: 0.6, g: 0.4, b: 0.05, a: 0.5 * alpha))
|
|
231
|
+
gfx.blendMode(io, Blend.Normal)
|
|
232
|
+
paintCell(io, m, alpha, self.sheet, 0.0 - SpriteHalf, 0.0 - SpriteHalf, SpriteSize, frameOf(self.clock + self.phase, 4, 8.0), 1)
|
|
233
|
+
|
|
234
|
+
# Put at `to`, drifting with its velocity.
|
|
235
|
+
fun placed(self, to: Motion) -> Orb = Orb(..self, at: self.at.moveTo(to.x, to.y), vx: to.vx, vy: to.vy)
|
|
236
|
+
|
|
237
|
+
# Stopped; it still animates.
|
|
238
|
+
fun still(self) -> Orb = Orb(..self, vx: 0.0, vy: 0.0)
|
|
239
|
+
|
|
240
|
+
# A mine drifting at (vx, vy) and turning at `spin` radians per second.
|
|
241
|
+
struct Mine(id: Int, at: Place, vx: Float, vy: Float, spin: Float, clock: Float, sheet: Texture) is Actor[Msg] =
|
|
242
|
+
fun key(self) -> Int = self.id
|
|
243
|
+
|
|
244
|
+
fun place(self) -> Place = self.at
|
|
245
|
+
|
|
246
|
+
fun step(self, ctx: ECtx[Msg], dt: Float) -> Mine =
|
|
247
|
+
m := driftOf(Motion(x: self.at.x, y: self.at.y, vx: self.vx, vy: self.vy), MineRadius, dt)
|
|
248
|
+
at := Place(..self.at, x: m.x, y: m.y, rotation: self.at.rotation + self.spin * dt)
|
|
249
|
+
Mine(..self, at: at, vx: m.vx, vy: m.vy, clock: self.clock + dt)
|
|
250
|
+
|
|
251
|
+
fun paint(self, io: Io, m: Affine, alpha: Float) =
|
|
252
|
+
paintCell(io, m, alpha, self.sheet, 0.0 - SpriteHalf, 0.0 - SpriteHalf, SpriteSize, frameOf(self.clock, 4, 4.0), 2)
|
|
253
|
+
|
|
254
|
+
fun placed(self, to: Motion) -> Mine = Mine(..self, at: self.at.moveTo(to.x, to.y), vx: to.vx, vy: to.vy)
|
|
255
|
+
|
|
256
|
+
# Stopped and no longer turning; it still animates.
|
|
257
|
+
fun still(self) -> Mine = Mine(..self, vx: 0.0, vy: 0.0, spin: 0.0)
|
|
258
|
+
|
|
259
|
+
# The player. `ax`, `ay` are the steering axes for its next step, set by the game before
|
|
260
|
+
# it; `hurt` is the time left of its hurt flash and `hurtAge` the time since the hit.
|
|
261
|
+
# Hidden at game over, when it stops.
|
|
262
|
+
struct Ship(
|
|
263
|
+
id: Int, at: Place, vx: Float, vy: Float, ax: Float, ay: Float,
|
|
264
|
+
hurt: Float, hurtAge: Float, clock: Float, sheet: Texture, visible: Bool) is Actor[Msg] =
|
|
265
|
+
|
|
266
|
+
fun key(self) -> Int = self.id
|
|
267
|
+
|
|
268
|
+
fun place(self) -> Place = self.at
|
|
269
|
+
|
|
270
|
+
fun shown(self) -> Bool = self.visible
|
|
271
|
+
|
|
272
|
+
# It accelerates by the axes, drag slows it and the walls stop it.
|
|
273
|
+
fun step(self, ctx: ECtx[Msg], dt: Float) -> Ship =
|
|
274
|
+
if !self.visible
|
|
275
|
+
self
|
|
276
|
+
else
|
|
277
|
+
keep := {1.0 - Drag * dt}.max(0.0)
|
|
278
|
+
r := ShipRadius
|
|
279
|
+
vx0 := {self.vx + self.ax * Thrust * dt} * keep
|
|
280
|
+
vy0 := {self.vy + self.ay * Thrust * dt} * keep
|
|
281
|
+
x0 := self.at.x + vx0 * dt
|
|
282
|
+
y0 := self.at.y + vy0 * dt
|
|
283
|
+
x := x0.max(r).min(ArenaW - r)
|
|
284
|
+
y := y0.max(r).min(ArenaH - r)
|
|
285
|
+
nvx := if x != x0 then 0.0 else vx0
|
|
286
|
+
nvy := if y != y0 then 0.0 else vy0
|
|
287
|
+
rot := if nvx * nvx + nvy * nvy > 400.0 then math.atan2(nvx, 0.0 - nvy) else self.at.rotation
|
|
288
|
+
Ship(
|
|
289
|
+
..self, at: Place(..self.at, x: x, y: y, rotation: rot), vx: nvx, vy: nvy,
|
|
290
|
+
hurt: {self.hurt - dt}.max(0.0), hurtAge: self.hurtAge + dt, clock: self.clock + dt)
|
|
291
|
+
|
|
292
|
+
fun paint(self, io: Io, m: Affine, alpha: Float) =
|
|
293
|
+
if !self.blinking()
|
|
294
|
+
paintCircle(io, m, 0.0, 0.0, 17.0, Color(r: 0.4, g: 0.8, b: 1.0, a: 0.12 * alpha))
|
|
295
|
+
paintCell(io, m, alpha, self.sheet, 0.0 - SpriteHalf, 0.0 - SpriteHalf, SpriteSize, frameOf(self.clock, 4, 14.0), 0)
|
|
296
|
+
|
|
297
|
+
# It blinks off while hurt.
|
|
298
|
+
fun blinking(self) -> Bool = self.hurt > 0.0 && flashPose(self.hurtAge).scaleX < 0.7
|
|
299
|
+
|
|
300
|
+
# Steered by the axes for its next step.
|
|
301
|
+
fun steered(self, ax: Float, ay: Float) -> Ship = Ship(..self, ax: ax, ay: ay)
|
|
302
|
+
|
|
303
|
+
# Just hit: hurt for `HurtTime`, its flash from the start.
|
|
304
|
+
fun struck(self) -> Ship = Ship(..self, hurt: HurtTime, hurtAge: 0.0)
|
|
341
305
|
|
|
342
|
-
# An orb or a mine put at `to`, drifting with its velocity.
|
|
343
|
-
fun
|
|
306
|
+
fun hidden(self) -> Ship = Ship(..self, visible: false)
|
|
344
|
-
Actor(..a, x: to.x, y: to.y, kind: a.kind.drifting(to.vx, to.vy))
|
|
345
307
|
|
|
346
|
-
#
|
|
308
|
+
# A `size` square cell (col, row) of the sprite sheet: a life icon.
|
|
347
|
-
fun revealed(a: Actor[Kind]) -> Actor[Kind] = match a.kind
|
|
348
|
-
|
|
309
|
+
struct Icon(id: Int, at: Place, size: Float, col: Int, row: Int, sheet: Texture) is Actor[Msg] =
|
|
349
|
-
Panel(_) => Actor(..a, visible: true, color: a.color.withAlpha(0.0), kind: Panel(age: 0.0))
|
|
350
|
-
|
|
310
|
+
fun key(self) -> Int = self.id
|
|
351
311
|
|
|
312
|
+
fun place(self) -> Place = self.at
|
|
313
|
+
|
|
314
|
+
fun paint(self, io: Io, m: Affine, alpha: Float) = paintCell(io, m, alpha, self.sheet, 0.0, 0.0, self.size, self.col, self.row)
|
|
315
|
+
|
|
316
|
+
# A line of text in `color`, whose alpha is its opacity.
|
|
317
|
+
struct Label(id: Int, at: Place, text: Str, size: Float, color: Color) is Actor[Msg] =
|
|
318
|
+
fun key(self) -> Int = self.id
|
|
319
|
+
|
|
320
|
+
fun place(self) -> Place = self.at
|
|
321
|
+
|
|
322
|
+
fun opacity(self) -> Float = self.color.a
|
|
323
|
+
|
|
324
|
+
fun paint(self, io: Io, m: Affine, alpha: Float) =
|
|
325
|
+
paintText(io, m, self.text, 0.0, 0.0, self.size, self.color.withAlpha(alpha))
|
|
326
|
+
|
|
327
|
+
fun relabel(self, t: Str) -> Label = Label(..self, text: t)
|
|
328
|
+
|
|
329
|
+
# The game-over title: its scale is `titlePose` of its age.
|
|
330
|
+
struct Title(id: Int, at: Place, age: Float, color: Color) is Actor[Msg] =
|
|
331
|
+
fun key(self) -> Int = self.id
|
|
332
|
+
|
|
333
|
+
fun place(self) -> Place = self.at
|
|
334
|
+
|
|
335
|
+
fun step(self, ctx: ECtx[Msg], dt: Float) -> Title =
|
|
336
|
+
p := titlePose(self.age + dt)
|
|
337
|
+
Title(..self, at: self.at.scaleTo(p.scaleX, p.scaleY), age: self.age + dt)
|
|
338
|
+
|
|
339
|
+
fun paint(self, io: Io, m: Affine, alpha: Float) =
|
|
340
|
+
paintText(io, m, "Game over", -100.0, -24.0, 48.0, self.color.withAlpha(alpha))
|
|
341
|
+
|
|
342
|
+
# Unscaled, its clock from 0: as it comes up at game over.
|
|
343
|
+
fun revealed(self) -> Title = Title(..self, at: self.at.scaleTo(1.0, 1.0), age: 0.0)
|
|
344
|
+
|
|
345
|
+
# The game-over panel, hidden while playing: its opacity fades in over half a second of
|
|
346
|
+
# its age, and so do its children.
|
|
347
|
+
struct Panel(id: Int, at: Place, age: Float, alpha: Float, visible: Bool) is Actor[Msg] =
|
|
348
|
+
fun key(self) -> Int = self.id
|
|
349
|
+
|
|
350
|
+
fun place(self) -> Place = self.at
|
|
351
|
+
|
|
352
|
+
fun shown(self) -> Bool = self.visible
|
|
353
|
+
|
|
354
|
+
fun opacity(self) -> Float = self.alpha
|
|
355
|
+
|
|
356
|
+
fun step(self, ctx: ECtx[Msg], dt: Float) -> Panel =
|
|
357
|
+
Panel(..self, alpha: {{self.age + dt} * 2.0}.min(1.0), age: self.age + dt)
|
|
358
|
+
|
|
359
|
+
# Shown, faded out, its clock from 0.
|
|
360
|
+
fun revealed(self) -> Panel = Panel(..self, visible: true, alpha: 0.0, age: 0.0)
|
|
361
|
+
|
|
362
|
+
# The "Again" button, `w` by `h`. `look` is 0 idle, 1 hovered, 2 pressed (from the
|
|
363
|
+
# `UiState`).
|
|
364
|
+
struct Button(id: Int, at: Place, w: Float, h: Float, label: Str, look: Int) is Actor[Msg] =
|
|
365
|
+
fun key(self) -> Int = self.id
|
|
366
|
+
|
|
367
|
+
fun place(self) -> Place = self.at
|
|
368
|
+
|
|
369
|
+
fun paint(self, io: Io, m: Affine, alpha: Float) =
|
|
370
|
+
actor.boxPaint(io, m, alpha, self.w, self.h, buttonColor(self.look))
|
|
371
|
+
paintText(io, m, self.label, 4.0, 4.0, self.h * 0.5, Color.White.withAlpha(alpha))
|
|
372
|
+
|
|
373
|
+
# The only body that can be clicked.
|
|
374
|
+
fun hit(self, p: Vec2) -> Bool = actor.boxHit(self.w, self.h, p)
|
|
375
|
+
|
|
376
|
+
fun looked(self, k: Int) -> Button = Button(..self, look: k)
|
|
377
|
+
|
|
378
|
+
# An orb or a mine stopped (the others as they are): what game over does to every body.
|
|
352
|
-
fun
|
|
379
|
+
fun stilled(a: Actor[Msg]) -> Actor[Msg] = match a
|
|
380
|
+
Orb as o => o.still()
|
|
381
|
+
Mine as m => m.still()
|
|
382
|
+
_ => a
|
|
383
|
+
|
|
384
|
+
# An orb or a mine put at `to` (the others as they are).
|
|
385
|
+
fun placedAt(a: Actor[Msg], to: Motion) -> Actor[Msg] = match a
|
|
386
|
+
Orb as o => o.placed(to)
|
|
387
|
+
Mine as m => m.placed(to)
|
|
388
|
+
_ => a
|
|
353
389
|
|
|
354
390
|
fun buttonColor(look: Int) -> Color =
|
|
355
391
|
if look == 2
|
|
@@ -367,46 +403,51 @@ fun buttonLook(u: UiState) -> Int =
|
|
|
367
403
|
else
|
|
368
404
|
0
|
|
369
405
|
|
|
406
|
+
# The root's place as the camera for a `sw` by `sh` screen.
|
|
407
|
+
fun fitted(sw: Float, sh: Float) -> Place =
|
|
370
|
-
|
|
408
|
+
z := fitZoom(sw, sh)
|
|
409
|
+
place.at(fitX(sw, sh), fitY(sw, sh)).scaleTo(z, z)
|
|
371
410
|
|
|
372
|
-
|
|
411
|
+
# --- building the tree ---
|
|
373
412
|
|
|
374
|
-
fun group(id: Int, kids: List[FRoseTree[Actor[
|
|
413
|
+
fun group(id: Int, kids: List[FRoseTree[Actor[Msg]]]) -> FRoseTree[Actor[Msg]] =
|
|
414
|
+
FRoseTree.of(Group(id: id, at: place.at(0.0, 0.0)), kids)
|
|
375
415
|
|
|
376
|
-
fun label(id: Int, text: Str, x: Float, y: Float, size: Float, c: Color) -> FRoseTree[Actor[
|
|
416
|
+
fun label(id: Int, text: Str, x: Float, y: Float, size: Float, c: Color) -> FRoseTree[Actor[Msg]] =
|
|
377
|
-
FRoseTree.leaf(
|
|
417
|
+
FRoseTree.leaf(Label(id: id, at: place.at(x, y), text: text, size: size, color: c))
|
|
378
418
|
|
|
379
419
|
# Stars `i` and up to `StarCount`, each at its fixed spot.
|
|
380
|
-
fun starNodes(i: Int) -> List[FRoseTree[Actor[
|
|
420
|
+
fun starNodes(i: Int) -> List[FRoseTree[Actor[Msg]]] =
|
|
381
421
|
if i >= StarCount
|
|
382
422
|
Nil
|
|
383
423
|
else
|
|
384
|
-
star := Actor.group(StarId + i, Float({i * 7919} % 800), Float({i * 104729} % 600), Star(i: i, clock: 0.0))
|
|
385
|
-
Cons(head: FRoseTree.leaf(
|
|
424
|
+
Cons(head: FRoseTree.leaf(Star(id: StarId + i, i: i, clock: 0.0)), tail: starNodes(i + 1))
|
|
386
425
|
|
|
387
426
|
# Life icons `i` and up to `StartLives`, from the right edge going left.
|
|
388
|
-
fun lifeNodes(i: Int, sheet: Texture) -> List[FRoseTree[Actor[
|
|
427
|
+
fun lifeNodes(i: Int, sheet: Texture) -> List[FRoseTree[Actor[Msg]]] =
|
|
389
428
|
if i >= StartLives
|
|
390
429
|
Nil
|
|
391
430
|
else
|
|
392
|
-
icon :=
|
|
431
|
+
icon := Icon(id: LifeId + i, at: place.at(ArenaW - 40.0 - Float(i) * 36.0, 12.0), size: 32.0, col: 0, row: 0, sheet: sheet)
|
|
393
432
|
Cons(head: FRoseTree.leaf(icon), tail: lifeNodes(i + 1, sheet))
|
|
394
433
|
|
|
395
|
-
fun shipActor(x: Float, y: Float, sheet: Texture) ->
|
|
434
|
+
fun shipActor(x: Float, y: Float, sheet: Texture) -> Ship =
|
|
435
|
+
Ship(
|
|
396
|
-
|
|
436
|
+
id: ShipId, at: place.at(x, y), vx: 0.0, vy: 0.0, ax: 0.0, ay: 0.0,
|
|
437
|
+
hurt: 0.0, hurtAge: 0.0, clock: 0.0, sheet: sheet, visible: true)
|
|
397
438
|
|
|
398
439
|
# The stage of a fresh run: no orbs or mines yet (`State.start` adds them).
|
|
399
|
-
fun buildStage(sheet: Texture, sw: Float, sh: Float) -> EStage[
|
|
440
|
+
fun buildStage(sheet: Texture, sw: Float, sh: Float) -> EStage[Msg] =
|
|
400
441
|
hud := group(HudId, Cons(
|
|
401
442
|
head: label(ScoreId, "Score 0", 16.0, 12.0, 28.0, Color.White),
|
|
402
443
|
tail: Cons(head: label(BestId, "Best 0", 16.0, 44.0, 20.0, Color.Yellow), tail: lifeNodes(0, sheet))))
|
|
403
|
-
panel := FRoseTree.of(
|
|
444
|
+
panel: FRoseTree[Actor[Msg]] := FRoseTree.of(Panel(id: PanelId, at: place.at(0.0, 0.0), age: 0.0, alpha: 1.0, visible: false), [
|
|
404
|
-
|
|
445
|
+
FRoseTree.leaf(Box(id: DimId, at: place.at(0.0, 230.0), w: ArenaW, h: 170.0, color: Color(r: 0.0, g: 0.0, b: 0.0, a: 0.6))),
|
|
405
|
-
FRoseTree.leaf(
|
|
446
|
+
FRoseTree.leaf(Title(id: TitleId, at: place.at(400.0, 268.0), age: 0.0, color: Color.Red)),
|
|
406
447
|
label(PromptId, "Press Enter to play again", 270.0, 300.0, 24.0, Color.White),
|
|
407
|
-
FRoseTree.leaf(
|
|
448
|
+
FRoseTree.leaf(Button(id: RestartId, at: place.at(340.0, 342.0), w: 120.0, h: 44.0, label: "Again", look: 0))])
|
|
408
|
-
root := FRoseTree.of(
|
|
449
|
+
root: FRoseTree[Actor[Msg]] := FRoseTree.of(Group(id: RootId, at: fitted(sw, sh)), [
|
|
409
|
-
|
|
450
|
+
FRoseTree.leaf(Box(id: BackId, at: place.at(0.0, 0.0), w: ArenaW, h: ArenaH, color: Color.rgb(0.07, 0.08, 0.16))),
|
|
410
451
|
group(StarLayer, starNodes(0)),
|
|
411
452
|
group(MineLayer, Nil),
|
|
412
453
|
group(OrbLayer, Nil),
|
|
@@ -428,7 +469,7 @@ enum Msg =
|
|
|
428
469
|
# The scene and what is between its actors. `mines` is how many mines are out (the next
|
|
429
470
|
# one's id is `MineId + mines`).
|
|
430
471
|
struct State(
|
|
431
|
-
stage: EStage[
|
|
472
|
+
stage: EStage[Msg],
|
|
432
473
|
rng: Random,
|
|
433
474
|
score: Int,
|
|
434
475
|
best: Int,
|
|
@@ -463,7 +504,7 @@ struct State(
|
|
|
463
504
|
|
|
464
505
|
fun update(self, msg: Msg) -> State = match msg
|
|
465
506
|
Tick(dt, ax, ay) => self.tick(dt, ax, ay)
|
|
466
|
-
Resize(w, h) => State(..self, sw: w, sh: h, stage: self.stage.
|
|
507
|
+
Resize(w, h) => State(..self, sw: w, sh: h, stage: self.stage.updateAs[Group[Msg]](RootId, |g| Group(..g, at: fitted(w, h))))
|
|
467
508
|
Restart(seed) => self.restarted(seed)
|
|
468
509
|
Event(input) => self.onInput(input)
|
|
469
510
|
RestartClicked => if self.over then self.restarted(self.rng.nextInt().value) else self
|
|
@@ -471,38 +512,45 @@ struct State(
|
|
|
471
512
|
# One frame: the stage steps every actor (the ship with this frame's axes), then the
|
|
472
513
|
# game looks at what they did. Once the game is over the actors only animate.
|
|
473
514
|
fun tick(self, dt: Float, ax: Float, ay: Float) -> State =
|
|
474
|
-
stepped := self.stage.
|
|
515
|
+
stepped := self.stage.updateAs[Ship](ShipId, |sh| sh.steered(ax, ay)).step(ECtx[Msg](), dt)
|
|
475
516
|
moved := State(..self, stage: stepped, clock: self.clock + dt)
|
|
476
517
|
if self.over then moved else moved.collect().growMines().hurt().settle()
|
|
477
518
|
|
|
478
519
|
# The actor with this id (every fixed id is always on the stage).
|
|
479
|
-
fun body(self, id: Int) -> Actor[
|
|
520
|
+
fun body(self, id: Int) -> Actor[Msg] = self.stage.find(id).unwrap()
|
|
521
|
+
|
|
522
|
+
fun ship(self) -> Ship = self.stage.findAs[Ship](ShipId).unwrap()
|
|
480
523
|
|
|
524
|
+
# The orbs and the mines, in tree order.
|
|
481
|
-
fun
|
|
525
|
+
fun orbs(self) -> List[Orb] =
|
|
526
|
+
self.stage.root.fold(Nil, |acc, a| match a
|
|
527
|
+
Orb as o => Cons(head: o, tail: acc)
|
|
528
|
+
_ => acc).reverse()
|
|
482
529
|
|
|
483
|
-
# The actors whose kind passes `pick`, in tree order.
|
|
484
|
-
fun
|
|
530
|
+
fun mineList(self) -> List[Mine] =
|
|
485
|
-
self.stage.root.fold(Nil, |acc, a|
|
|
531
|
+
self.stage.root.fold(Nil, |acc, a| match a
|
|
532
|
+
Mine as m => Cons(head: m, tail: acc)
|
|
533
|
+
_ => acc).reverse()
|
|
486
534
|
|
|
487
|
-
fun touches(self,
|
|
535
|
+
fun touches(self, at: Place, radius: Float) -> Bool =
|
|
488
536
|
sh := self.ship()
|
|
489
|
-
overlaps(sh.x, sh.y, ShipRadius,
|
|
537
|
+
overlaps(sh.at.x, sh.at.y, ShipRadius, at.x, at.y, radius)
|
|
490
538
|
|
|
491
539
|
# The ship collects every orb it touches.
|
|
492
540
|
fun collect(self) -> State =
|
|
493
|
-
self.
|
|
541
|
+
self.orbs().fold(self, |s, o| if s.touches(o.at, OrbRadius) then s.respawnOrb(o.id) else s)
|
|
494
542
|
|
|
495
543
|
# Scores the orb `id` and puts it at a new random spot.
|
|
496
544
|
fun respawnOrb(self, id: Int) -> State =
|
|
497
545
|
sh := self.ship()
|
|
498
|
-
sp := spawn(self.rng, sh.x, sh.y, 120.0, 40.0)
|
|
546
|
+
sp := spawn(self.rng, sh.at.x, sh.at.y, 120.0, 40.0)
|
|
499
|
-
State(..self, stage: self.stage.
|
|
547
|
+
State(..self, stage: self.stage.updateAs[Orb](id, |o| o.placed(sp.at)), rng: sp.rng, score: self.score + OrbPoints).showScore()
|
|
500
548
|
|
|
501
549
|
# The hud's labels from the score.
|
|
502
550
|
fun showScore(self) -> State =
|
|
503
551
|
score := "Score {self.score}"
|
|
504
552
|
best := "Best {self.best.max(self.score)}"
|
|
505
|
-
State(..self, stage: self.stage.
|
|
553
|
+
State(..self, stage: self.stage.updateAs[Label](ScoreId, |l| l.relabel(score)).updateAs[Label](BestId, |l| l.relabel(best)))
|
|
506
554
|
|
|
507
555
|
# Orbs `i` and up to OrbCount, each at a random spot away from the ship.
|
|
508
556
|
fun fillOrbs(self, i: Int) -> State =
|
|
@@ -510,8 +558,8 @@ struct State(
|
|
|
510
558
|
|
|
511
559
|
fun addOrb(self, i: Int) -> State =
|
|
512
560
|
sh := self.ship()
|
|
513
|
-
sp := spawn(self.rng, sh.x, sh.y, 120.0, 40.0)
|
|
561
|
+
sp := spawn(self.rng, sh.at.x, sh.at.y, 120.0, 40.0)
|
|
514
|
-
orb :=
|
|
562
|
+
orb := Orb(id: OrbId + i, at: place.at(sp.at.x, sp.at.y), vx: sp.at.vx, vy: sp.at.vy, phase: Float(i) * 0.1, clock: 0.0, sheet: self.sheet)
|
|
515
563
|
State(..self, stage: self.stage.add(OrbLayer, FRoseTree.leaf(orb)), rng: sp.rng)
|
|
516
564
|
|
|
517
565
|
# Mines are added (on top of the others) until there are as many as the score asks for.
|
|
@@ -520,35 +568,35 @@ struct State(
|
|
|
520
568
|
|
|
521
569
|
fun addMine(self) -> State =
|
|
522
570
|
sh := self.ship()
|
|
523
|
-
sp := spawn(self.rng, sh.x, sh.y, 220.0, 90.0)
|
|
571
|
+
sp := spawn(self.rng, sh.at.x, sh.at.y, 220.0, 90.0)
|
|
524
|
-
mine :=
|
|
572
|
+
mine := Mine(id: MineId + self.mines, at: place.at(sp.at.x, sp.at.y), vx: sp.at.vx, vy: sp.at.vy, spin: 2.0, clock: 0.0, sheet: self.sheet)
|
|
525
573
|
State(..self, stage: self.stage.add(MineLayer, FRoseTree.leaf(mine)), rng: sp.rng, mines: self.mines + 1)
|
|
526
574
|
|
|
527
575
|
fun touchesMine(self) -> Bool =
|
|
528
|
-
self.
|
|
576
|
+
self.mineList().fold(false, |hit, m| hit || self.touches(m.at, MineRadius))
|
|
529
577
|
|
|
530
578
|
# A mine costs a life (and its icon), makes the ship flash and, at the last life, ends
|
|
531
579
|
# the game: the ship goes and the panel comes up.
|
|
532
580
|
fun hurt(self) -> State =
|
|
533
|
-
if self.ship().
|
|
581
|
+
if self.ship().hurt <= 0.0 && self.touchesMine() then self.struck() else self
|
|
534
582
|
|
|
535
583
|
fun struck(self) -> State =
|
|
536
584
|
lives := self.lives - 1
|
|
537
585
|
done := lives <= 0
|
|
538
|
-
hit := self.stage.
|
|
586
|
+
hit := self.stage.updateAs[Ship](ShipId, |sh| sh.struck()).kill(LifeId + lives)
|
|
539
|
-
stage := if done then
|
|
587
|
+
stage := if done then gameOver(hit) else hit
|
|
540
588
|
State(..self, stage: stage, lives: lives, over: done, best: if done then self.best.max(self.score) else self.best)
|
|
541
589
|
|
|
542
590
|
# After game over the orbs and mines stop: they only animate.
|
|
543
591
|
fun settle(self) -> State =
|
|
544
|
-
if self.over then State(..self, stage: self.stage.withRoot(self.stage.root.map(
|
|
592
|
+
if self.over then State(..self, stage: self.stage.withRoot(self.stage.root.map(stilled))) else self
|
|
545
593
|
|
|
546
594
|
# A fresh run that keeps the best score, the Ui and the held keys; the button shows the
|
|
547
595
|
# Ui's state.
|
|
548
596
|
fun restarted(self, seed: Int) -> State =
|
|
549
597
|
fresh := State.start(seed, self.best.max(self.score), self.sheet, self.sw, self.sh)
|
|
550
598
|
look := buttonLook(self.ui)
|
|
551
|
-
State(..fresh, ui: self.ui, held: self.held, stage: fresh.stage.
|
|
599
|
+
State(..fresh, ui: self.ui, held: self.held, stage: fresh.stage.updateAs[Button](RestartId, |b| b.looked(look)))
|
|
552
600
|
|
|
553
601
|
# A player input: keys update the held keys (Enter or R restarts a finished game),
|
|
554
602
|
# a pointer input goes through the Ui.
|
|
@@ -579,7 +627,7 @@ struct State(
|
|
|
579
627
|
pos := po.unwrap()
|
|
580
628
|
r := ui.step(self.ui, input, self.stage.hitId(pos.x, pos.y))
|
|
581
629
|
look := buttonLook(r.ui)
|
|
582
|
-
s := State(..self, ui: r.ui, stage: self.stage.
|
|
630
|
+
s := State(..self, ui: r.ui, stage: self.stage.updateAs[Button](RestartId, |b| b.looked(look)))
|
|
583
631
|
clicked := r.events.fold(false, |c, e| c || {e.kind == Click && e.id == RestartId})
|
|
584
632
|
if clicked then s.update(RestartClicked) else s
|
|
585
633
|
|
|
@@ -587,6 +635,10 @@ struct State(
|
|
|
587
635
|
fun onRestart(self, x: Float, y: Float) -> Bool =
|
|
588
636
|
self.stage.hitId(x, y).unwrapOr(-1) == RestartId
|
|
589
637
|
|
|
638
|
+
# The stage at game over: the ship hidden, the panel and its title coming up.
|
|
639
|
+
fun gameOver(s: EStage[Msg]) -> EStage[Msg] =
|
|
640
|
+
s.updateAs[Ship](ShipId, |sh| sh.hidden()).updateAs[Panel](PanelId, |p| p.revealed()).updateAs[Title](TitleId, |t| t.revealed())
|
|
641
|
+
|
|
590
642
|
# Enter or R going down.
|
|
591
643
|
fun isRestartKey(e: InputEvent) -> Bool = match e
|
|
592
644
|
KeyDown(k) => k == Key.Enter || k == Key.KeyR
|
|
@@ -670,38 +722,34 @@ fun newState(seed: Int) -> State =
|
|
|
670
722
|
State.start(seed, 0, Texture(id: -1), ArenaW, ArenaH)
|
|
671
723
|
|
|
672
724
|
fun moveBody(s: State, id: Int, m: Motion) -> State =
|
|
673
|
-
State(..s, stage: s.stage.update(id, |a|
|
|
725
|
+
State(..s, stage: s.stage.update(id, |a| placedAt(a, m)))
|
|
674
726
|
|
|
675
727
|
# A state with the orbs and mines moved out of the way.
|
|
676
728
|
fun emptyState() -> State =
|
|
677
729
|
s := newState(7)
|
|
678
|
-
State(..s, stage: s.stage.withRoot(s.stage.root.map(|a|
|
|
730
|
+
State(..s, stage: s.stage.withRoot(s.stage.root.map(|a| placedAt(a, away()))))
|
|
679
731
|
|
|
680
732
|
fun onShip(s: State) -> Motion =
|
|
681
|
-
Motion(x: s.ship().x, y: s.ship().y, vx: 0.0, vy: 0.0)
|
|
733
|
+
Motion(x: s.ship().at.x, y: s.ship().at.y, vx: 0.0, vy: 0.0)
|
|
682
734
|
|
|
683
735
|
fun forceOver(s: State) -> State =
|
|
684
|
-
stage := s.stage.update(ShipId, hidden).update(PanelId, revealed).update(TitleId, revealed)
|
|
685
|
-
State(..s, stage: stage, lives: 0, over: true, best: s.best.max(s.score))
|
|
736
|
+
State(..s, stage: gameOver(s.stage), lives: 0, over: true, best: s.best.max(s.score))
|
|
737
|
+
|
|
738
|
+
fun lone(sh: Ship) -> EStage[Msg] = EStage.of(sh)
|
|
739
|
+
|
|
740
|
+
fun shipAt(x: Float, y: Float) -> Ship = shipActor(x, y, Texture(id: -1))
|
|
686
741
|
|
|
687
|
-
fun
|
|
742
|
+
fun shipOf(s: EStage[Msg]) -> Ship = s.findAs[Ship](ShipId).unwrap()
|
|
688
743
|
|
|
689
|
-
fun
|
|
744
|
+
fun steer(s: EStage[Msg], ax: Float, ay: Float) -> EStage[Msg] = s.updateAs[Ship](ShipId, |sh| sh.steered(ax, ay))
|
|
690
745
|
|
|
691
|
-
fun
|
|
746
|
+
fun labelText(s: State, id: Int) -> Str = s.stage.findAs[Label](id).unwrap().text
|
|
692
|
-
Ship(vx, _, _, _, _, _, _, _) => vx
|
|
693
|
-
_ => 0.0
|
|
694
747
|
|
|
695
|
-
fun
|
|
748
|
+
fun buttonLookOf(s: State) -> Int = s.stage.findAs[Button](RestartId).unwrap().look
|
|
696
|
-
Label(t, _) => t
|
|
697
|
-
_ => ""
|
|
698
749
|
|
|
699
|
-
fun
|
|
750
|
+
fun mineAt(s: State, id: Int) -> Mine = s.stage.findAs[Mine](id).unwrap()
|
|
700
|
-
Button(_, k) => k
|
|
701
|
-
_ => -1
|
|
702
751
|
|
|
703
|
-
# One actor's step on its own.
|
|
704
|
-
fun
|
|
752
|
+
fun orbAt(s: State, id: Int) -> Orb = s.stage.findAs[Orb](id).unwrap()
|
|
705
753
|
|
|
706
754
|
test "the scene starts as a tree of 63 actors, 58 of them drawn"
|
|
707
755
|
s := newState(7)
|
|
@@ -711,42 +759,42 @@ test "the scene starts as a tree of 63 actors, 58 of them drawn"
|
|
|
711
759
|
assert over.stage.flatten().length() == 58 - 1 + 5
|
|
712
760
|
|
|
713
761
|
test "steering accelerates and drag slows the ship"
|
|
714
|
-
s := lone(shipAt(ArenaW / 2.0, ArenaH / 2.0))
|
|
762
|
+
s := steer(lone(shipAt(ArenaW / 2.0, ArenaH / 2.0)), 1.0, 0.0).step(ECtx[Msg](), 0.1)
|
|
715
|
-
sh :=
|
|
763
|
+
sh := shipOf(s)
|
|
716
|
-
assert sh.x > ArenaW / 2.0
|
|
764
|
+
assert sh.at.x > ArenaW / 2.0
|
|
717
|
-
assert
|
|
765
|
+
assert sh.vx > 0.0
|
|
718
|
-
coast :=
|
|
766
|
+
coast := steer(s, 0.0, 0.0)
|
|
719
|
-
t := 0.until(30).fold(coast, |cur, _| cur.step(ECtx[
|
|
767
|
+
t := 0.until(30).fold(coast, |cur, _| cur.step(ECtx[Msg](), 0.1))
|
|
720
|
-
assert
|
|
768
|
+
assert shipOf(t).vx < sh.vx / 4.0
|
|
721
|
-
assert
|
|
769
|
+
assert shipOf(t).at.x - sh.at.x < 50.0
|
|
722
770
|
|
|
723
771
|
test "the ship stops at the arena walls"
|
|
724
|
-
start := lone(shipAt(20.0, 20.0))
|
|
772
|
+
start := steer(lone(shipAt(20.0, 20.0)), -1.0, -1.0)
|
|
725
|
-
s := 0.until(20).fold(start, |cur, _| cur.step(ECtx[
|
|
773
|
+
s := 0.until(20).fold(start, |cur, _| cur.step(ECtx[Msg](), 0.1))
|
|
726
|
-
sh :=
|
|
774
|
+
sh := shipOf(s)
|
|
727
|
-
assert sh.x == ShipRadius
|
|
775
|
+
assert sh.at.x == ShipRadius
|
|
728
|
-
assert sh.y == ShipRadius
|
|
776
|
+
assert sh.at.y == ShipRadius
|
|
729
|
-
assert
|
|
777
|
+
assert sh.vx == 0.0
|
|
730
778
|
|
|
731
779
|
test "orbs bounce off the walls and mines spin as they drift"
|
|
732
|
-
orb :=
|
|
780
|
+
orb := Orb(id: OrbId, at: place.at(795.0, 300.0), vx: 100.0, vy: 0.0, phase: 0.0, clock: 0.0, sheet: Texture(id: -1))
|
|
733
|
-
o :=
|
|
781
|
+
o := orb.step(ECtx[Msg](), 0.1)
|
|
734
|
-
assert near(o.x, ArenaW - 10.0)
|
|
782
|
+
assert near(o.at.x, ArenaW - 10.0)
|
|
735
|
-
mine :=
|
|
783
|
+
mine := Mine(id: MineId, at: place.at(400.0, 300.0), vx: 10.0, vy: 0.0, spin: 2.0, clock: 0.0, sheet: Texture(id: -1))
|
|
736
|
-
k :=
|
|
784
|
+
k := mine.step(ECtx[Msg](), 0.5)
|
|
737
|
-
assert near(k.rotation, 1.0)
|
|
785
|
+
assert near(k.at.rotation, 1.0)
|
|
738
|
-
assert near(k.x, 405.0)
|
|
786
|
+
assert near(k.at.x, 405.0)
|
|
739
|
-
assert near(mine.rotation, 0.0)
|
|
787
|
+
assert near(mine.at.rotation, 0.0)
|
|
740
788
|
|
|
741
789
|
test "collecting an orb scores, respawns it elsewhere and updates the hud"
|
|
742
790
|
e := emptyState()
|
|
743
791
|
s := moveBody(e, OrbId, onShip(e))
|
|
744
792
|
t := tickOf(s, 0.0, 0.0, 0.016)
|
|
745
|
-
orb :=
|
|
793
|
+
orb := orbAt(t, OrbId)
|
|
746
794
|
assert t.score == OrbPoints
|
|
747
|
-
assert math.hypot(orb.x - t.ship().x, orb.y - t.ship().y) >= 120.0
|
|
795
|
+
assert math.hypot(orb.at.x - t.ship().at.x, orb.at.y - t.ship().at.y) >= 120.0
|
|
748
|
-
assert orb.x < 5000.0
|
|
796
|
+
assert orb.at.x < 5000.0
|
|
749
|
-
assert t.
|
|
797
|
+
assert t.orbs().length() == OrbCount
|
|
750
798
|
assert labelText(t, ScoreId) == "Score 10"
|
|
751
799
|
assert labelText(s, ScoreId) == "Score 0"
|
|
752
800
|
|
|
@@ -755,21 +803,21 @@ test "a mine costs one life and its icon, and makes the ship flash"
|
|
|
755
803
|
s := moveBody(e, MineId, onShip(e))
|
|
756
804
|
hit := tickOf(s, 0.0, 0.0, 0.016)
|
|
757
805
|
assert hit.lives == StartLives - 1
|
|
758
|
-
assert hit.ship().
|
|
806
|
+
assert hit.ship().hurt > 0.0
|
|
759
807
|
assert hit.stage.find(LifeId + 2).isNone()
|
|
760
808
|
assert hit.stage.find(LifeId + 1).isSome()
|
|
761
809
|
start := Blinks(state: moveBody(hit, MineId, away()), hidden: 0, shown: 0)
|
|
762
810
|
blinks := 0.until(100).fold(start, |b, _|
|
|
763
811
|
next := tickOf(b.state, 0.0, 0.0, 0.016)
|
|
764
|
-
if next.ship().
|
|
812
|
+
if next.ship().blinking()
|
|
765
813
|
Blinks(state: next, hidden: b.hidden + 1, shown: b.shown)
|
|
766
814
|
else
|
|
767
815
|
Blinks(state: next, hidden: b.hidden, shown: b.shown + 1)
|
|
768
816
|
)
|
|
769
817
|
assert blinks.hidden > 0
|
|
770
818
|
assert blinks.shown > 0
|
|
771
|
-
assert blinks.state.ship().
|
|
819
|
+
assert blinks.state.ship().hurt == 0.0
|
|
772
|
-
assert !blinks.state.ship().
|
|
820
|
+
assert !blinks.state.ship().blinking()
|
|
773
821
|
assert blinks.state.lives == StartLives - 1
|
|
774
822
|
|
|
775
823
|
test "the ship is briefly safe after a hit"
|
|
@@ -786,22 +834,22 @@ test "the game ends at zero lives: the ship goes, the panel comes up, steps are
|
|
|
786
834
|
assert t.best == 40
|
|
787
835
|
assert !t.ship().shown()
|
|
788
836
|
assert t.body(PanelId).shown()
|
|
789
|
-
x := t.ship().x
|
|
837
|
+
x := t.ship().at.x
|
|
790
|
-
assert tickOf(t, 1.0, 0.0, 0.5).ship().x == x
|
|
838
|
+
assert tickOf(t, 1.0, 0.0, 0.5).ship().at.x == x
|
|
791
839
|
|
|
792
840
|
test "after game over nothing moves but the animations keep running"
|
|
793
841
|
e := emptyState()
|
|
794
842
|
s := State(..e, lives: 1)
|
|
795
|
-
ended := tickOf(moveBody(s, MineId, Motion(x: s.ship().x, y: s.ship().y, vx: 10.0, vy: 0.0)), 0.0, 0.0, 0.016)
|
|
843
|
+
ended := tickOf(moveBody(s, MineId, Motion(x: s.ship().at.x, y: s.ship().at.y, vx: 10.0, vy: 0.0)), 0.0, 0.0, 0.016)
|
|
796
844
|
assert ended.over
|
|
797
|
-
mine :=
|
|
845
|
+
mine := mineAt(ended, MineId)
|
|
798
846
|
t := ticksOf(ended, 10, 1.0, 0.0, 0.1)
|
|
799
|
-
after :=
|
|
847
|
+
after := mineAt(t, MineId)
|
|
800
|
-
assert after.x == mine.x
|
|
848
|
+
assert after.at.x == mine.at.x
|
|
801
|
-
assert after.rotation == mine.rotation
|
|
849
|
+
assert after.at.rotation == mine.at.rotation
|
|
802
|
-
assert t.ship().x == ArenaW / 2.0
|
|
850
|
+
assert t.ship().at.x == ArenaW / 2.0
|
|
803
|
-
assert after.
|
|
851
|
+
assert after.clock > mine.clock + 0.9
|
|
804
|
-
assert
|
|
852
|
+
assert orbAt(t, OrbId).clock > 0.9
|
|
805
853
|
assert t.clock > 0.9
|
|
806
854
|
assert t.score == 0
|
|
807
855
|
|
|
@@ -813,10 +861,10 @@ test "restart builds a fresh tree and keeps the best score"
|
|
|
813
861
|
assert r.score == 0
|
|
814
862
|
assert r.best == 90
|
|
815
863
|
assert r.lives == StartLives
|
|
816
|
-
assert r.ship().x == ArenaW / 2.0
|
|
864
|
+
assert r.ship().at.x == ArenaW / 2.0
|
|
817
865
|
assert r.ship().shown()
|
|
818
866
|
assert !r.body(PanelId).shown()
|
|
819
|
-
assert r.
|
|
867
|
+
assert r.mineList().length() == mineCount(0)
|
|
820
868
|
assert labelText(r, BestId) == "Best 90"
|
|
821
869
|
|
|
822
870
|
test "more mines are added to the tree as the score grows"
|
|
@@ -825,28 +873,28 @@ test "more mines are added to the tree as the score grows"
|
|
|
825
873
|
assert mineCount(10000) == 8
|
|
826
874
|
e := emptyState()
|
|
827
875
|
t := tickOf(State(..e, score: 100), 0.0, 0.0, 0.016)
|
|
828
|
-
assert t.
|
|
876
|
+
assert t.mineList().length() == 5
|
|
829
877
|
assert t.stage.find(MineId + 4).isSome()
|
|
830
|
-
assert t.update(Restart(seed: 3)).
|
|
878
|
+
assert t.update(Restart(seed: 3)).mineList().length() == 3
|
|
831
879
|
|
|
832
880
|
test "the same seed plays out the same way"
|
|
833
881
|
a := ticksOf(newState(11), 120, 1.0, -1.0, 0.016)
|
|
834
882
|
b := ticksOf(newState(11), 120, 1.0, -1.0, 0.016)
|
|
835
|
-
assert a.ship().x == b.ship().x
|
|
883
|
+
assert a.ship().at.x == b.ship().at.x
|
|
836
884
|
assert a.score == b.score
|
|
837
|
-
assert
|
|
885
|
+
assert mineAt(a, MineId).at.x == mineAt(b, MineId).at.x
|
|
838
|
-
assert newState(12)
|
|
886
|
+
assert orbAt(newState(12), OrbId).at.x != orbAt(a, OrbId).at.x
|
|
839
887
|
|
|
840
888
|
test "a kept old state still has its old tree"
|
|
841
889
|
s := emptyState()
|
|
842
|
-
x := s.ship().x
|
|
890
|
+
x := s.ship().at.x
|
|
843
891
|
t := tickOf(s, 1.0, 0.0, 0.1)
|
|
844
|
-
assert t.ship().x > x
|
|
892
|
+
assert t.ship().at.x > x
|
|
845
|
-
assert s.ship().x == x
|
|
893
|
+
assert s.ship().at.x == x
|
|
846
894
|
|
|
847
895
|
test "the root fits the arena on the screen"
|
|
848
896
|
s := emptyState().update(Resize(w: 1600.0, h: 600.0))
|
|
849
|
-
root := s.body(RootId).local()
|
|
897
|
+
root := s.body(RootId).place().local()
|
|
850
898
|
assert near(root.a, 1.0)
|
|
851
899
|
assert near(root.e, 400.0)
|
|
852
900
|
assert near(root.f, 0.0)
|
|
@@ -880,7 +928,7 @@ test "a click on the button restarts through the ui"
|
|
|
880
928
|
test "the title pulses and the panel fades in by their own clocks"
|
|
881
929
|
t := ticksOf(forceOver(emptyState()), 15, 0.0, 0.0, 0.02)
|
|
882
930
|
assert near(t.body(PanelId).opacity(), 0.6)
|
|
883
|
-
assert near(t.body(TitleId).local().a, 1.3)
|
|
931
|
+
assert near(t.body(TitleId).place().local().a, 1.3)
|
|
884
932
|
assert near(titlePose(0.8).scaleX, 1.17)
|
|
885
933
|
assert near(flashPose(0.05).scaleX, 0.4)
|
|
886
934
|
|