Files
agentic-labs-challenge-subm…/underdepths/render/renderer.py
T
2026-08-13 07:28:55 +02:00

236 lines
8.2 KiB
Python

"""Dungeon view renderer.
Draws the camera-followed 16px tile grid (walls/floor in visible vs explored
shades), entities by type color, the player, stairs, and the UI chrome: top bar
(floor / HP / level / XP), right log panel (last 8, color-coded), bottom bar
(equipment, sound state, active timers, and the contextual G/F prompt). Reads
state only; never mutates the session.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import pygame
from data.constants import (
TILE, COLORS,
VIEW_TILES_W, VIEW_TILES_H, PANEL_W, TOP_BAR_H, BOTTOM_BAR_H,
SCREEN_W, SCREEN_H, XP_THRESHOLDS, MAX_LEVEL, LOG_COLOR_GOOD,
)
from entities.item import Item
from render.camera import Camera
if TYPE_CHECKING:
from engine.game_state import GameSession
from engine.audio import Audio
MAP_X0_PX = 0
MAP_W_PX = VIEW_TILES_W * TILE
MAP_H_PX = VIEW_TILES_H * TILE
MAP_Y0 = TOP_BAR_H
PANEL_X0 = MAP_W_PX
BOTTOM_Y0 = TOP_BAR_H + MAP_H_PX
def draw(screen: pygame.Surface, sess: GameSession, fonts: dict,
camera: Camera, audio: Audio) -> None:
screen.fill(COLORS["background"])
cmap = sess.current_map
camera.follow(sess.player.x, sess.player.y, VIEW_TILES_W, VIEW_TILES_H,
cmap.width, cmap.height)
_draw_map(screen, sess, camera)
_draw_entities(screen, sess, camera)
_draw_top_bar(screen, sess, fonts)
_draw_log_panel(screen, sess, fonts)
_draw_bottom_bar(screen, sess, fonts, audio)
def _draw_map(screen: pygame.Surface, sess: GameSession, camera: Camera) -> None:
cmap = sess.current_map
wall_vis, wall_exp = COLORS["wall_visible"], COLORS["wall_explored"]
floor_vis, floor_exp = COLORS["floor_visible"], COLORS["floor_explored"]
tiles = cmap.tiles
visible = cmap.visible
explored = cmap.explored
for vy in range(VIEW_TILES_H):
my = camera.y + vy
if my < 0 or my >= cmap.height:
continue
vrow = visible[my]
erow = explored[my]
trow = tiles[my]
py = MAP_Y0 + vy * TILE
for vx in range(VIEW_TILES_W):
mx = camera.x + vx
if mx < 0 or mx >= cmap.width:
continue
if not (vrow[mx] or erow[mx]):
continue
if vrow[mx]:
color = wall_vis if trow[mx] == 0 else floor_vis
else:
color = wall_exp if trow[mx] == 0 else floor_exp
screen.fill(color, rect=pygame.Rect(MAP_X0_PX + vx * TILE, py, TILE, TILE))
def _screen_pos(camera: Camera, mx: int, my: int) -> tuple[int, int] | None:
vx = mx - camera.x
vy = my - camera.y
if 0 <= vx < VIEW_TILES_W and 0 <= vy < VIEW_TILES_H:
return (vx * TILE, MAP_Y0 + vy * TILE)
return None
def _draw_entities(screen: pygame.Surface, sess: GameSession, camera: Camera) -> None:
cmap = sess.current_map
if cmap.stairs_down is not None:
sx, sy = cmap.stairs_down
if 0 <= sy < cmap.height and 0 <= sx < cmap.width and cmap.explored[sy][sx]:
pos = _screen_pos(camera, sx, sy)
if pos is not None:
screen.fill(COLORS["stairs"], rect=pygame.Rect(pos[0], pos[1], TILE, TILE))
for e in cmap.entities:
if not (0 <= e.y < cmap.height and 0 <= e.x < cmap.width):
continue
if not cmap.visible[e.y][e.x]:
continue
pos = _screen_pos(camera, e.x, e.y)
if pos is None:
continue
screen.fill(e.color, rect=pygame.Rect(pos[0] + 2, pos[1] + 2, TILE - 4, TILE - 4))
p = sess.player
pos = _screen_pos(camera, p.x, p.y)
if pos is not None:
screen.fill(COLORS["player"], rect=pygame.Rect(pos[0], pos[1], TILE, TILE))
def _xp_fraction(p) -> float:
if p.level >= MAX_LEVEL:
return 1.0
upper = XP_THRESHOLDS[p.level - 1]
lower = XP_THRESHOLDS[p.level - 2] if p.level >= 2 else 0
if upper <= lower:
return 1.0
return max(0.0, min(1.0, (p.xp - lower) / (upper - lower)))
def _draw_bar(screen, x, y, w, h, frac, empty_col, fill_col) -> int:
pygame.draw.rect(screen, empty_col, pygame.Rect(x, y, w, h))
pygame.draw.rect(screen, fill_col, pygame.Rect(x, y, int(w * max(0.0, min(1.0, frac))), h))
return w
def _draw_top_bar(screen, sess, fonts) -> None:
screen.fill((15, 15, 18), pygame.Rect(0, 0, SCREEN_W, TOP_BAR_H))
if sess is None:
return
p = sess.player
body = fonts["body"]
x = 6
surf = body.render(f"Floor {sess.current_floor + 1}/10", True, COLORS["ui_fg"])
screen.blit(surf, (x, 5))
x += surf.get_width() + 18
surf = body.render(f"HP {p.hp}/{p.max_hp}", True, COLORS["ui_fg"])
screen.blit(surf, (x, 5))
x += surf.get_width() + 6
_draw_bar(screen, x, 7, 120, 12, p.hp / max(1, p.max_hp), (60, 20, 20), COLORS["ui_hp"])
x += 120 + 18
surf = body.render(f"Lv {p.level}", True, COLORS["ui_fg"])
screen.blit(surf, (x, 5))
x += surf.get_width() + 6
surf = body.render("XP", True, COLORS["ui_dim"])
screen.blit(surf, (x, 5))
x += surf.get_width() + 4
_draw_bar(screen, x, 7, 120, 12, _xp_fraction(p), (20, 30, 50), COLORS["ui_xp"])
def _log_color(msg: str):
low = msg.lower()
if any(k in low for k in LOG_COLOR_GOOD):
return COLORS["msg_good"]
if "miss" in low or ("you" in low and ("damage" in low or "breath" in low or "died" in low)):
return COLORS["msg_hurt"]
if any(k in low for k in ("pick up", "potion", "scroll", "equip", "weapon", "armor", "teleport")):
return COLORS["msg_item"]
return COLORS["msg_combat"]
def _wrap(text: str, font, max_width: int) -> list[str]:
words = text.split()
if not words:
return [""]
lines: list[str] = []
cur = words[0]
for w in words[1:]:
if font.size(cur + " " + w)[0] <= max_width:
cur += " " + w
else:
lines.append(cur)
cur = w
lines.append(cur)
return lines
def _draw_log_panel(screen, sess, fonts) -> None:
screen.fill((12, 12, 15), pygame.Rect(PANEL_X0, 0, PANEL_W, SCREEN_H))
pygame.draw.line(screen, (40, 40, 45), (PANEL_X0, 0), (PANEL_X0, SCREEN_H))
title = fonts["body"].render("Messages", True, COLORS["ui_dim"])
screen.blit(title, (PANEL_X0 + 8, 5))
msgs = sess.log if sess else []
recent = msgs[-8:]
small = fonts["small"]
max_w = PANEL_W - 16
y = 26
for m in recent:
for line in _wrap(m, small, max_w):
if y + small.get_height() > SCREEN_H:
return
screen.blit(small.render(line, True, _log_color(m)), (PANEL_X0 + 8, y))
y += small.get_height() + 1
def _timers_text(p) -> str:
parts = []
if p.timers.get("strength", 0) > 0:
parts.append(f"Strength {p.timers['strength']}t")
if p.timers.get("invisibility", 0) > 0:
parts.append(f"Invisible {p.timers['invisibility']}t")
return " ".join(parts)
def context_prompt(sess: GameSession) -> str:
p = sess.player
cmap = sess.current_map
for e in cmap.entities:
if isinstance(e, Item) and e.x == p.x and e.y == p.y:
return f"Press G to pick up {e.name}"
if cmap.stairs_down is not None and (p.x, p.y) == cmap.stairs_down:
return "Press F to descend"
return ""
def _draw_bottom_bar(screen, sess, fonts, audio) -> None:
screen.fill((15, 15, 18), pygame.Rect(0, BOTTOM_Y0, MAP_W_PX, BOTTOM_BAR_H))
pygame.draw.line(screen, (40, 40, 45), (0, BOTTOM_Y0), (MAP_W_PX, BOTTOM_Y0))
if sess is None:
return
p = sess.player
body = fonts["body"]
weapon = p.weapon.name if p.weapon is not None else "(empty)"
armor = p.armor_item.name if p.armor_item is not None else "(empty)"
screen.blit(body.render(f"Weapon: {weapon} Armor: {armor}", True, COLORS["ui_fg"]),
(6, BOTTOM_Y0 + 4))
if audio.enabled:
sound = "Sound: MUTED (m)" if audio.muted else "Sound: ON (m)"
else:
sound = "Sound: OFF"
screen.blit(body.render(sound, True, COLORS["ui_dim"]), (6, BOTTOM_Y0 + 22))
timers = _timers_text(p)
if timers:
screen.blit(body.render(timers, True, COLORS["msg_good"]), (6, BOTTOM_Y0 + 40))
prompt = context_prompt(sess)
if prompt:
screen.blit(body.render(prompt, True, COLORS["msg_item"]), (6, BOTTOM_Y0 + 60))