19 lines
543 B
Python
19 lines
543 B
Python
"""Camera: viewport offset that follows the player, clamped to the map."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class Camera:
|
|
x: int = 0
|
|
y: int = 0
|
|
|
|
def follow(self, target_x: int, target_y: int, view_w: int, view_h: int,
|
|
map_w: int, map_h: int) -> None:
|
|
self.x = target_x - view_w // 2
|
|
self.y = target_y - view_h // 2
|
|
self.x = max(0, min(self.x, max(0, map_w - view_w)))
|
|
self.y = max(0, min(self.y, max(0, map_h - view_h)))
|