Coverage for src/retrospeedlab/games/saltskid/camera.py: 100%
28 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-03 23:09 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-03 23:09 +0000
1from retrospeedlab.games.saltskid.course import Course
2from retrospeedlab.games.saltskid.layout import CAMERA_X, CAMERA_Y
3from retrospeedlab.games.saltskid.skiff import Skiff
4from retrospeedlab.games.saltskid.trig import cosine, scaled, sine
5from retrospeedlab.games.saltskid.tuning import SCREEN_WIDTH, SUBPIXELS, VIEW_HEIGHT
6from retrospeedlab.games.shared.ram import Memory, set_word, word
8LEAD_FRAMES = 20
9LOOK = 44
10EASE = 6
13def eased(current: int, target: int) -> int:
14 step = (target - current) // EASE if target >= current else -((current - target) // EASE)
15 if step == 0 and target != current:
16 step = 1 if target > current else -1
17 return current + step
20def camera_target(course: Course, skiff: Skiff) -> tuple[int, int]:
21 lead_x = max(-LOOK, min(LOOK, scaled(cosine(skiff.travel), skiff.speed) * LEAD_FRAMES // SUBPIXELS))
22 lead_y = max(-LOOK, min(LOOK, scaled(sine(skiff.travel), skiff.speed) * LEAD_FRAMES // SUBPIXELS))
23 x = max(0, min(course.width - SCREEN_WIDTH, skiff.px + lead_x - SCREEN_WIDTH // 2))
24 y = max(0, min(course.height - VIEW_HEIGHT, skiff.py + lead_y - VIEW_HEIGHT // 2))
25 return x, y
28def follow_camera(ram: bytearray, course: Course, skiff: Skiff, snap: bool) -> None:
29 x, y = camera_target(course, skiff)
30 if not snap:
31 x, y = eased(word(ram, CAMERA_X), x), eased(word(ram, CAMERA_Y), y)
32 set_word(ram, CAMERA_X, x)
33 set_word(ram, CAMERA_Y, y)
36def camera(ram: Memory) -> tuple[int, int]:
37 return word(ram, CAMERA_X), word(ram, CAMERA_Y)