feat(core): add Camera abstraction for viewport scrolling

- Add Camera class with modes: vertical, horizontal, omni, floating
- Refactor scroll.py and demo to use Camera abstraction
- Add vis_offset for horizontal scrolling support
- Add camera_x to EffectContext for effects
- Add pygame window resize handling
- Add HUD effect plugin for demo mode
- Add --demo flag to run demo mode
- Add tests for Camera and vis_offset
This commit is contained in:
2026-03-16 01:46:21 -07:00
parent e1408dcf16
commit 9b139a40f7
10 changed files with 343 additions and 37 deletions

69
tests/test_camera.py Normal file
View File

@@ -0,0 +1,69 @@
from engine.camera import Camera, CameraMode
def test_camera_vertical_default():
"""Test default vertical camera."""
cam = Camera()
assert cam.mode == CameraMode.VERTICAL
assert cam.x == 0
assert cam.y == 0
def test_camera_vertical_factory():
"""Test vertical factory method."""
cam = Camera.vertical(speed=2.0)
assert cam.mode == CameraMode.VERTICAL
assert cam.speed == 2.0
def test_camera_horizontal():
"""Test horizontal camera."""
cam = Camera.horizontal(speed=1.5)
assert cam.mode == CameraMode.HORIZONTAL
cam.update(1.0)
assert cam.x > 0
def test_camera_omni():
"""Test omnidirectional camera."""
cam = Camera.omni(speed=1.0)
assert cam.mode == CameraMode.OMNI
cam.update(1.0)
assert cam.x > 0
assert cam.y > 0
def test_camera_floating():
"""Test floating camera with sinusoidal motion."""
cam = Camera.floating(speed=1.0)
assert cam.mode == CameraMode.FLOATING
y_before = cam.y
cam.update(0.5)
y_after = cam.y
assert y_before != y_after
def test_camera_reset():
"""Test camera reset."""
cam = Camera.vertical()
cam.update(1.0)
assert cam.y > 0
cam.reset()
assert cam.x == 0
assert cam.y == 0
def test_camera_custom_update():
"""Test custom update function."""
call_count = 0
def custom_update(camera, dt):
nonlocal call_count
call_count += 1
camera.x += int(10 * dt)
cam = Camera.custom(custom_update)
cam.update(1.0)
assert call_count == 1
assert cam.x == 10

View File

@@ -87,10 +87,26 @@ class TestRenderTickerZone:
def test_returns_list(self):
"""Returns a list of strings."""
result, cache = layers.render_ticker_zone([], 0, 10, 80, {}, 0.0)
result, cache = layers.render_ticker_zone(
[],
scroll_cam=0,
camera_x=0,
ticker_h=10,
w=80,
noise_cache={},
grad_offset=0.0,
)
assert isinstance(result, list)
def test_returns_dict_for_cache(self):
"""Returns a dict for the noise cache."""
result, cache = layers.render_ticker_zone([], 0, 10, 80, {}, 0.0)
result, cache = layers.render_ticker_zone(
[],
scroll_cam=0,
camera_x=0,
ticker_h=10,
w=80,
noise_cache={},
grad_offset=0.0,
)
assert isinstance(cache, dict)

32
tests/test_vis_offset.py Normal file
View File

@@ -0,0 +1,32 @@
from engine.effects.legacy import vis_offset, vis_trunc
def test_vis_offset_no_change():
"""vis_offset with offset 0 returns original."""
result = vis_offset("hello", 0)
assert result == "hello"
def test_vis_offset_trims_start():
"""vis_offset skips first N characters."""
result = vis_offset("hello world", 6)
assert result == "world"
def test_vis_offset_handles_ansi():
"""vis_offset handles ANSI codes correctly."""
result = vis_offset("\033[31mhello\033[0m", 3)
assert result == "lo\x1b[0m" or "lo" in result
def test_vis_offset_greater_than_length():
"""vis_offset with offset > length returns empty-ish."""
result = vis_offset("hi", 10)
assert result == ""
def test_vis_trunc_still_works():
"""Ensure vis_trunc still works after changes."""
result = vis_trunc("hello world", 5)
assert result == "hello"