119 lines
4.1 KiB
Python
119 lines
4.1 KiB
Python
"""Rasterize pixel-art grids to cached pygame Surfaces."""
|
|
from __future__ import annotations
|
|
|
|
import pygame
|
|
|
|
from data import sprites
|
|
from data.constants import TILE_SIZE
|
|
|
|
_cache: dict[tuple[str, int], pygame.Surface] = {}
|
|
_flash_cache: dict[tuple[str, int], pygame.Surface] = {}
|
|
_item_icon_cache: dict[str, str] = {
|
|
"rusty_dagger": "weapon_dagger",
|
|
"short_sword": "weapon_sword",
|
|
"battle_axe": "weapon_axe",
|
|
"war_hammer": "weapon_hammer",
|
|
"leather_armor": "armor_leather",
|
|
"chain_mail": "armor_chain",
|
|
"plate_armor": "armor_plate",
|
|
"healing_potion": "potion_heal",
|
|
"strength_potion": "potion_strength",
|
|
"invisibility_potion": "potion_invis",
|
|
"scroll_fireball": "scroll",
|
|
"scroll_teleport": "scroll",
|
|
"scroll_mapping": "scroll",
|
|
}
|
|
|
|
|
|
def item_sprite_key(item_key: str, fallback: str) -> str:
|
|
return _item_icon_cache.get(item_key, fallback if fallback in sprites.GRIDS else "scroll")
|
|
|
|
|
|
def _rasterize(grid: list[str], palette: dict[str, tuple[int, int, int]]) -> pygame.Surface:
|
|
height = len(grid)
|
|
width = len(grid[0])
|
|
surface = pygame.Surface((width, height), pygame.SRCALPHA)
|
|
for y, row in enumerate(grid):
|
|
for x, char in enumerate(row):
|
|
color = palette.get(char)
|
|
if color is not None:
|
|
surface.set_at((x, y), (*color, 255))
|
|
return surface
|
|
|
|
|
|
def sprite_size(key: str) -> int:
|
|
"""Blit edge length in px for a sprite key at the current TILE_SIZE."""
|
|
return max(1, round(TILE_SIZE * sprites.SCALE_FACTORS.get(key, 1.0)))
|
|
|
|
|
|
def get_sprite(key: str) -> pygame.Surface:
|
|
"""Sprite scaled to its tile-relative size (cached)."""
|
|
size = sprite_size(key)
|
|
cached = _cache.get((key, size))
|
|
if cached is None:
|
|
grid = sprites.GRIDS[key]
|
|
native = _rasterize(grid, sprites.PALETTES[key])
|
|
surface = (
|
|
native
|
|
if native.get_size() == (size, size)
|
|
else pygame.transform.scale(native, (size, size))
|
|
)
|
|
_cache[(key, size)] = surface
|
|
cached = surface
|
|
return cached
|
|
|
|
|
|
def get_player_sprite(weapon_key: str, armor_key: str, facing: str) -> pygame.Surface:
|
|
"""Player sprite composed from base grid + weapon overlay, armor-tinted.
|
|
|
|
'left' mirrors the 'side' facing. Cached by composite key.
|
|
"""
|
|
mirrored = facing == "left"
|
|
base_facing = "side" if facing in ("left", "right") else facing
|
|
key = f"player:{weapon_key}:{armor_key}:{base_facing}"
|
|
size = TILE_SIZE
|
|
cached = _cache.get((key, size))
|
|
if cached is None:
|
|
grid = sprites.player_grid(weapon_key, armor_key, base_facing)
|
|
native = _rasterize(grid, sprites.player_palette(armor_key))
|
|
surface = pygame.transform.scale(native, (size, size))
|
|
_cache[(key, size)] = surface
|
|
cached = surface
|
|
return pygame.transform.flip(cached, mirrored, False) if mirrored else cached
|
|
|
|
|
|
def get_flash(surface_key: str, surface: pygame.Surface) -> pygame.Surface:
|
|
"""White hit-flash variant of a cached sprite (keeps silhouette alpha)."""
|
|
key = (surface_key, surface.get_width())
|
|
cached = _flash_cache.get(key)
|
|
if cached is None:
|
|
cached = surface.copy()
|
|
width, height = cached.get_size()
|
|
for x in range(width):
|
|
for y in range(height):
|
|
if cached.get_at((x, y)).a > 0:
|
|
cached.set_at((x, y), (255, 255, 255, 255))
|
|
_flash_cache[key] = cached
|
|
return cached
|
|
|
|
|
|
def get_tile(kind: str, variant: int) -> pygame.Surface:
|
|
"""Wall/floor tile variant rasterized at 2 px per pattern cell (cached)."""
|
|
key = f"tile:{kind}:{variant}"
|
|
size = TILE_SIZE
|
|
cached = _cache.get((key, size))
|
|
if cached is not None:
|
|
return cached
|
|
variants = sprites.WALL_VARIANTS if kind == "wall" else sprites.FLOOR_VARIANTS
|
|
grid = variants[variant % len(variants)]
|
|
palette = sprites.PALETTES.get(kind, sprites._BASE_PALETTE)
|
|
native = _rasterize(grid, palette)
|
|
surface = pygame.transform.scale(native, (size, size))
|
|
_cache[(key, size)] = surface
|
|
return surface
|
|
|
|
|
|
def clear_cache() -> None:
|
|
_cache.clear()
|
|
_flash_cache.clear()
|