plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
17736d7
— Peter John
2026-11-09T21:26:05+05:30
Balloon Pop: hit tests the body first and drops the shine circle
examples/games/kids/balloon_pop.plum
CHANGED
|
@@ -157,14 +157,14 @@ struct Balloon(id: Int, x: Float, y: Float, speed: Float, hue: Int, phase: Float
|
|
|
157
157
|
fun paint(self, io: Io, m: Affine, alpha: Float) =
|
|
158
158
|
if self.popped() then self.paintBurst(io, m, self.clock - self.poppedAt) else self.paintWhole(io, m)
|
|
159
159
|
|
|
160
|
-
# A whole balloon is hit on
|
|
160
|
+
# A whole balloon is hit on itself, its knot or its string (its shine is inside it, so
|
|
161
|
+
# it needs no test of its own). The cheapest tests come first: the string needs a sine.
|
|
161
162
|
fun hit(self, p: Vec2) -> Bool =
|
|
162
163
|
r := Radius
|
|
163
164
|
!self.popped() && {
|
|
164
|
-
actor.
|
|
165
|
+
actor.circleHit(0.0, 0.0, r, p)
|
|
165
166
|
|| actor.circleHit(0.0, r + 2.0, 6.0, p)
|
|
166
|
-
|| actor.circleHit(0.0, 0.0, r, p)
|
|
167
|
-
|| actor.
|
|
167
|
+
|| actor.lineHit(0.0, r, 6.0 * math.sin(self.clock * 3.0 + self.phase), r * 2.6, 2.0, p)}
|
|
168
168
|
|
|
169
169
|
fun popped(self) -> Bool =
|
|
170
170
|
self.poppedAt >= 0.0
|
|
@@ -411,6 +411,15 @@ test "pressing a balloon pops it and counts"
|
|
|
411
411
|
again := tick(io, t, pressAt(400.0, 300.0), 0.01)
|
|
412
412
|
assert again.state().pops == 1
|
|
413
413
|
|
|
414
|
+
test "a balloon is hit on itself, its shine, its knot and its string, and only there"
|
|
415
|
+
b := Balloon(id: 1, x: 0.0, y: 0.0, speed: 0.0, hue: 0, phase: 0.0, poppedAt: -1.0, clock: 0.0)
|
|
416
|
+
r := Radius
|
|
417
|
+
assert b.hit(Vec2(x: 0.0, y: 0.0)) && b.hit(Vec2(x: 0.0 - r * 0.35, y: 0.0 - r * 0.4))
|
|
418
|
+
assert b.hit(Vec2(x: 0.0 - r * 0.35 - r * 0.21, y: 0.0 - r * 0.4))
|
|
419
|
+
assert b.hit(Vec2(x: 0.0, y: r + 6.0)) && b.hit(Vec2(x: 0.0, y: r * 2.5))
|
|
420
|
+
assert !b.hit(Vec2(x: r * 0.8, y: r * 0.8)) && !b.hit(Vec2(x: 0.0, y: r * 2.7))
|
|
421
|
+
assert !Balloon(..b, poppedAt: 0.0).hit(Vec2(x: 0.0, y: 0.0))
|
|
422
|
+
|
|
414
423
|
test "pressing the sky does nothing"
|
|
415
424
|
g := withCentred(newGame())
|
|
416
425
|
t := tick(io, g, pressAt(50.0, 560.0), 0.01)
|