125 lines
4.2 KiB
Python
125 lines
4.2 KiB
Python
"""Render-only effects: screen-shake, particles, and a vignette.
|
|
|
|
All state here is view state owned by the Renderer instance, never game state.
|
|
Lighting (visible bright / explored dimmed / unexplored black + radial falloff)
|
|
is applied per-tile inside ``renderer``; this module supplies the juice and the
|
|
edge-darkening vignette overlay.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
import random
|
|
from typing import Any
|
|
|
|
import pygame
|
|
|
|
_MAX_PARTICLES = 160
|
|
|
|
|
|
def torch_factor(dx: int, dy: int, radius: int) -> float:
|
|
"""Radial torch brightness at a tile offset from the bearer.
|
|
|
|
Bright warm core (1.0) graduating smoothly to a dim rim (~0.55) at the
|
|
sight edge — a soft gradient, not a hard disc. Quadratic falloff keeps the
|
|
core clearly bright. Chebyshev distance matches the FOV radius so the lit
|
|
disc stays round; walls occlude light via the visible set computed upstream.
|
|
"""
|
|
d = max(abs(dx), abs(dy))
|
|
if d >= radius:
|
|
return 0.55
|
|
t = d / radius
|
|
return 1.0 - 0.45 * (t * t)
|
|
|
|
|
|
class Shake:
|
|
"""Decaying screen-shake; returns a pixel offset each frame."""
|
|
|
|
def __init__(self) -> None:
|
|
self._mag = 0.0
|
|
|
|
def trigger(self, magnitude: float = 6.0) -> None:
|
|
self._mag = min(10.0, max(self._mag, magnitude))
|
|
|
|
def offset(self) -> tuple[int, int]:
|
|
if self._mag <= 0.05:
|
|
self._mag = 0.0
|
|
return 0, 0
|
|
dx = random.uniform(-1, 1) * self._mag
|
|
dy = random.uniform(-1, 1) * self._mag
|
|
self._mag *= 0.82
|
|
return int(dx), int(dy)
|
|
|
|
|
|
class Particles:
|
|
"""Pooled, capped particle bursts (hit sparks, blood, level-up stars, etc.)."""
|
|
|
|
def __init__(self) -> None:
|
|
self._parts: list[dict[str, Any]] = []
|
|
|
|
def burst(self, x: float, y: float, color: tuple[int, int, int],
|
|
count: int = 10, speed: float = 60.0, life: float = 0.4) -> None:
|
|
for _ in range(count):
|
|
if len(self._parts) >= _MAX_PARTICLES:
|
|
break
|
|
ang = random.uniform(0, math.tau)
|
|
spd = speed * random.uniform(0.4, 1.0)
|
|
self._parts.append({
|
|
"x": x, "y": y,
|
|
"vx": math.cos(ang) * spd, "vy": math.sin(ang) * spd,
|
|
"life": life * random.uniform(0.7, 1.2), "age": 0.0,
|
|
"color": color, "size": random.randint(2, 3),
|
|
})
|
|
|
|
def update(self, dt: float) -> None:
|
|
alive: list[dict[str, Any]] = []
|
|
for p in self._parts:
|
|
p["age"] += dt
|
|
if p["age"] >= p["life"]:
|
|
continue
|
|
p["x"] += p["vx"] * dt
|
|
p["y"] += p["vy"] * dt
|
|
p["vy"] += 90.0 * dt # gentle gravity
|
|
alive.append(p)
|
|
self._parts = alive
|
|
|
|
def draw(self, surface: pygame.Surface, cam_xy: tuple[int, int]) -> None:
|
|
ox, oy = cam_xy
|
|
for p in self._parts:
|
|
t = 1.0 - p["age"] / p["life"]
|
|
r = max(1, int(p["size"] * t))
|
|
c = p["color"]
|
|
col = (int(c[0] * t), int(c[1] * t), int(c[2] * t))
|
|
pygame.draw.circle(surface, col, (int(p["x"] - ox), int(p["y"] - oy)), r)
|
|
|
|
|
|
def vignette(size: tuple[int, int]) -> pygame.Surface:
|
|
"""Soft edge-darkening overlay (cached by size via module dict)."""
|
|
key = size
|
|
surf = _VIGNETTE_CACHE.get(key)
|
|
if surf is not None:
|
|
return surf
|
|
w, h = size
|
|
surf = pygame.Surface((w, h), pygame.SRCALPHA)
|
|
cx, cy = w / 2, h / 2
|
|
max_d = math.hypot(cx, cy)
|
|
# Subtle edge-only vignette: concentric non-overlapping annuli (width=step)
|
|
# so alpha never accumulates. Only the outer ~38% of the screen darkens; the
|
|
# center stays clear. Max alpha is gentle so edges are never crushed to black.
|
|
step = 8
|
|
inner_t = 0.62 # no darkening inside 62% of the corner distance
|
|
max_alpha = 78
|
|
r = int(max_d)
|
|
while r > 0:
|
|
t = r / max_d
|
|
if t > inner_t:
|
|
u = (t - inner_t) / (1.0 - inner_t)
|
|
alpha = int(max_alpha * u * u)
|
|
if alpha > 0:
|
|
pygame.draw.circle(surf, (0, 0, 0, alpha), (int(cx), int(cy)), r, step)
|
|
r -= step
|
|
_VIGNETTE_CACHE[key] = surf
|
|
return surf
|
|
|
|
|
|
_VIGNETTE_CACHE: dict[tuple[int, int], pygame.Surface] = {}
|