Files
agentic-labs-challenge/underdepths/render/camera.py
T
2026-08-14 09:46:04 +02:00

47 lines
1.6 KiB
Python

"""Smooth easing camera that follows the player.
Stateless from the game's perspective: the camera keeps its own smoothed pixel
position (view state, not game state). ``main`` owns the instance and passes the
resulting offset to ``Renderer.render``; the renderer itself just applies it.
"""
from __future__ import annotations
from data.constants import MAP_W, MAP_H, TILE
class Camera:
def __init__(self, smoothing: float = 0.18) -> None:
self.x: float = 0.0
self.y: float = 0.0
self.smoothing = max(0.0, min(1.0, smoothing))
def reset(self, player_xy: tuple[int, int]) -> None:
self.x = float(player_xy[0] * TILE)
self.y = float(player_xy[1] * TILE)
def update(self, player_xy: tuple[int, int]) -> tuple[int, int]:
tx = player_xy[0] * TILE
ty = player_xy[1] * TILE
self.x += (tx - self.x) * self.smoothing
self.y += (ty - self.y) * self.smoothing
return int(self.x), int(self.y)
def offset_for(self, surface_wh: tuple[int, int], player_xy: tuple[int, int]) -> tuple[int, int]:
"""Center the player on screen, clamped to the map bounds (pixel offset)."""
sw, sh = surface_wh
px = int(self.x)
py = int(self.y)
ox = px - sw // 2
oy = py - sh // 2
max_ox = MAP_W * TILE - sw
max_oy = MAP_H * TILE - sh
if max_ox <= 0:
ox = (MAP_W * TILE - sw) // 2
else:
ox = max(0, min(max_ox, ox))
if max_oy <= 0:
oy = (MAP_H * TILE - sh) // 2
else:
oy = max(0, min(max_oy, oy))
return ox, oy