Coverage for src/retrospeedlab/games/duskstep/effects.py: 100%
56 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-04 00:01 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-04 00:01 +0000
1from retrospeedlab.games.duskstep.codes import SPARK, STAR
2from retrospeedlab.games.duskstep.layout import (
3 BANNER,
4 BANNER_TIMER,
5 BLOCKED_TIMER,
6 FX_RNG,
7 LANDING_SQUASH,
8 PARTICLE_KIND,
9 PARTICLE_LIFE,
10 PARTICLE_NEXT,
11 PARTICLE_SIZE,
12 PARTICLE_SLOTS,
13 PARTICLE_VX,
14 PARTICLE_VY,
15 PARTICLE_X,
16 PARTICLE_Y,
17 PARTICLES,
18 SHAKE,
19 SOUND_EVENTS,
20)
21from retrospeedlab.games.duskstep.tuning import BANNER_FRAMES, BYTE_LIMIT, SCREEN_HEIGHT
22from retrospeedlab.games.shared.lcg import next_random
23from retrospeedlab.games.shared.ram import set_byte, set_word, signed_byte, word
25PARTICLE_GRAVITY = 1
26PARTICLE_TERMINAL = 3
27PARTICLES_END = PARTICLES + PARTICLE_SLOTS * PARTICLE_SIZE
28SPREAD_RANGE = 256
29GRAVITY_EVERY = 3
30BASE_LIFE = 12
31LIFE_SPREAD = 4
32FLOATING = frozenset({SPARK, STAR})
33TIMERS = (SHAKE, BANNER_TIMER, LANDING_SQUASH, BLOCKED_TIMER)
36def signal(ram: bytearray, event: int) -> None:
37 set_word(ram, SOUND_EVENTS, word(ram, SOUND_EVENTS) | event)
40def shake(ram: bytearray, frames: int) -> None:
41 ram[SHAKE] = max(ram[SHAKE], frames)
44def banner(ram: bytearray, kind: int) -> None:
45 ram[BANNER], ram[BANNER_TIMER] = kind, BANNER_FRAMES
48def spread(ram: bytearray, speed: int) -> int:
49 return next_random(ram, FX_RNG) * (2 * speed + 1) // SPREAD_RANGE - speed
52def emit(ram: bytearray, spot: tuple[int, int], velocity: tuple[int, int], kind: int, life: int) -> None:
53 slot = ram[PARTICLE_NEXT] % PARTICLE_SLOTS
54 ram[PARTICLE_NEXT] = (slot + 1) % PARTICLE_SLOTS
55 base = PARTICLES + slot * PARTICLE_SIZE
56 ram[base + PARTICLE_LIFE], ram[base + PARTICLE_KIND] = max(1, min(BYTE_LIMIT, life)), kind
57 ram[base + PARTICLE_X] = max(0, min(BYTE_LIMIT, spot[0]))
58 ram[base + PARTICLE_Y] = max(0, min(BYTE_LIMIT, spot[1]))
59 set_byte(ram, base + PARTICLE_VX, velocity[0])
60 set_byte(ram, base + PARTICLE_VY, velocity[1])
63def burst(ram: bytearray, spot: tuple[int, int], kind: int, count: int, speed: int) -> None:
64 for _ in range(count):
65 velocity = (spread(ram, speed), spread(ram, speed) - speed // 2)
66 emit(ram, spot, velocity, kind, BASE_LIFE + spread(ram, LIFE_SPREAD))
69def move_particle(ram: bytearray, base: int, scroll: int) -> None:
70 ram[base + PARTICLE_LIFE] -= 1
71 vy = signed_byte(ram, base + PARTICLE_VY)
72 if ram[base + PARTICLE_KIND] not in FLOATING and ram[base + PARTICLE_LIFE] % GRAVITY_EVERY == 0:
73 vy = min(PARTICLE_TERMINAL, vy + PARTICLE_GRAVITY)
74 set_byte(ram, base + PARTICLE_VY, vy)
75 x = ram[base + PARTICLE_X] + signed_byte(ram, base + PARTICLE_VX) - scroll
76 y = ram[base + PARTICLE_Y] + vy
77 if not 0 <= x <= BYTE_LIMIT or not 0 <= y < SCREEN_HEIGHT:
78 ram[base + PARTICLE_LIFE] = 0
79 return
80 ram[base + PARTICLE_X], ram[base + PARTICLE_Y] = x, y
83def tick_effects(ram: bytearray, scroll: int) -> None:
84 for address in TIMERS:
85 ram[address] = max(0, ram[address] - 1)
86 lives = ram[PARTICLES + PARTICLE_LIFE : PARTICLES_END : PARTICLE_SIZE]
87 if not any(lives):
88 return
89 for slot, life in enumerate(lives):
90 if life:
91 move_particle(ram, PARTICLES + slot * PARTICLE_SIZE, scroll)