40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
"""Camera: which slice of the map the viewport currently shows.
|
|
|
|
The camera centers on the player and clamps to the map bounds so the viewport
|
|
never drifts past the dungeon edge. It is pure data + a small helper — the
|
|
renderer asks it for the top-left tile offset each frame.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
__all__ = ["Camera", "compute_camera"]
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class Camera:
|
|
x: int # top-left tile column currently in view
|
|
y: int # top-left tile row currently in view
|
|
|
|
|
|
def compute_camera(player_x: int, player_y: int,
|
|
map_w: int, map_h: int,
|
|
view_w: int, view_h: int) -> Camera:
|
|
"""Center the viewport on the player, clamped to the map.
|
|
|
|
When the map is smaller than the viewport the camera pins to (0, 0) and the
|
|
renderer simply shows the whole map with empty border around it.
|
|
"""
|
|
if map_w <= view_w:
|
|
cx = 0
|
|
else:
|
|
cx = player_x - view_w // 2
|
|
cx = max(0, min(cx, map_w - view_w))
|
|
if map_h <= view_h:
|
|
cy = 0
|
|
else:
|
|
cy = player_y - view_h // 2
|
|
cy = max(0, min(cy, map_h - view_h))
|
|
return Camera(x=cx, y=cy)
|