Files
agentic-labs-challenge/underdepths/render/effects.py
T
2026-08-13 15:23:35 +02:00

182 lines
6.4 KiB
Python

"""Procedural visual effects: particles, floating combat text, screen shake,
lighting helpers (torch falloff, vignette). All drawn from state; none mutate it.
"""
from __future__ import annotations
import math
import random
try:
import pygame
except Exception: # pragma: no cover
pygame = None # type: ignore
# --------------------------------------------------------------------------- #
# Particles — pooled and capped.
# --------------------------------------------------------------------------- #
class Particles:
def __init__(self, cap: int = 260) -> None:
self._pool: list[dict] = []
self._cap = cap
def clear(self) -> None:
self._pool.clear()
def burst(self, x: float, y: float, color, n: int = 10, speed: float = 70.0,
life: float = 0.45, gravity: float = 0.0, size: int = 2) -> None:
for _ in range(n):
if len(self._pool) >= self._cap:
self._pool.pop(0)
ang = random.random() * math.tau
spd = random.uniform(speed * 0.25, speed)
self._pool.append(dict(
x=x, y=y, vx=math.cos(ang) * spd, vy=math.sin(ang) * spd,
life=life, max=life, color=color, g=gravity, size=size))
def update(self, dt: float) -> None:
out = []
drag = max(0.0, 1.0 - 2.2 * dt)
for p in self._pool:
p["life"] -= dt
if p["life"] <= 0:
continue
p["x"] += p["vx"] * dt
p["y"] += p["vy"] * dt
p["vy"] += p["g"] * dt
p["vx"] *= drag
p["vy"] *= drag
out.append(p)
self._pool = out
def draw(self, surf, cam) -> None:
if pygame is None:
return
for p in self._pool:
frac = max(0.0, p["life"] / p["max"])
a = max(0, min(255, int(255 * frac)))
r = max(1, int(p["size"] * (0.6 + 0.4 * frac)))
sx = int(p["x"] - cam[0])
sy = int(p["y"] - cam[1])
if -8 <= sx <= surf.get_width() + 8 and -8 <= sy <= surf.get_height() + 8:
s = pygame.Surface((r * 2, r * 2), pygame.SRCALPHA)
pygame.draw.rect(s, (*p["color"], a), (0, 0, r * 2, r * 2))
surf.blit(s, (sx - r, sy - r))
# --------------------------------------------------------------------------- #
# Floating combat text.
# --------------------------------------------------------------------------- #
class FloatText:
def __init__(self) -> None:
self._items: list[dict] = []
def clear(self) -> None:
self._items.clear()
def add(self, x: float, y: float, text: str, color, big: bool = False) -> None:
self._items.append(dict(
x=x, y=y, text=text, color=color, life=0.95, max=0.95,
vy=-30.0, big=big))
def update(self, dt: float) -> None:
out = []
for it in self._items:
it["life"] -= dt
if it["life"] <= 0:
continue
it["y"] += it["vy"] * dt
it["vy"] *= max(0.0, 1.0 - 1.6 * dt)
out.append(it)
self._items = out
def draw(self, surf, font, font_big, cam) -> None:
if pygame is None:
return
for it in self._items:
frac = max(0.0, it["life"] / it["max"])
a = max(0, min(255, int(255 * (frac ** 0.6))))
f = font_big if it["big"] else font
txt = f.render(it["text"], True, it["color"])
txt.set_alpha(a)
surf.blit(txt, (int(it["x"] - cam[0] - txt.get_width() // 2),
int(it["y"] - cam[1])))
# --------------------------------------------------------------------------- #
# Screen shake — decaying random offset.
# --------------------------------------------------------------------------- #
class Shake:
def __init__(self) -> None:
self.mag: float = 0.0
def trigger(self, mag: float = 6.0) -> None:
self.mag = max(self.mag, mag)
def update(self, dt: float) -> None:
self.mag *= math.exp(-dt * 6.5)
if self.mag < 0.25:
self.mag = 0.0
def offset(self):
if self.mag <= 0.0:
return (0.0, 0.0)
return (random.uniform(-self.mag, self.mag),
random.uniform(-self.mag, self.mag))
# --------------------------------------------------------------------------- #
# Lighting helpers.
# --------------------------------------------------------------------------- #
def torch_factor(dist: float, radius: int) -> float:
"""Brightness 1.0 at the player fading to ~0.22 at the sight edge.
A lower edge floor than before so the step from the last visible tile to
black unexplored space is less jarring; the smooth radial torch glow
(see make_torch) layered on top supplies the soft circular falloff.
"""
if radius <= 0:
return 1.0
f = 1.0 - max(0.0, dist) / radius
return max(0.22, min(1.0, 0.22 + 0.78 * f))
def make_torch(radius_px: int):
"""Cached smooth radial torch glow (warm, additive) centred on the player.
Returns a Surface of side 2*radius_px whose RGB falls smoothly from a warm
yellow-white at the centre to black at the edge; the renderer blits it with
BLEND_RGB_ADD so it lays a soft circular light pool over the per-tile grid,
hiding the tile-aligned FOV boundary. Rendered small then smooth-scaled up.
"""
if pygame is None:
return None
d = max(8, radius_px * 2)
sw = max(28, d // 3)
s = pygame.Surface((sw, sw), pygame.SRCALPHA)
c = sw / 2
for y in range(sw):
for x in range(sw):
dist = math.hypot(x - c, y - c) / c
if dist >= 1.0:
continue
f = (1.0 - dist) ** 1.3
s.set_at((x, y), (int(150 * f), int(132 * f), int(96 * f), 255))
return pygame.transform.smoothscale(s, (d, d))
def make_vignette(w: int, h: int):
"""Cached radial darkening overlay (transparent centre, dark corners)."""
if pygame is None:
return None
sw, sh = max(2, w // 4), max(2, h // 4)
s = pygame.Surface((sw, sh), pygame.SRCALPHA)
cx, cy = sw / 2, sh / 2
maxd = math.hypot(cx, cy)
for y in range(sh):
for x in range(sw):
d = math.hypot(x - cx, y - cy) / maxd
a = int(min(165, max(0, (d - 0.52)) * 370))
s.set_at((x, y), (0, 0, 0, a))
return pygame.transform.smoothscale(s, (w, h))