add sect regions

This commit is contained in:
bridge
2025-10-08 23:26:19 +08:00
parent 5489bc725c
commit ad163c2af1
9 changed files with 158 additions and 19 deletions

View File

@@ -7,7 +7,7 @@ from src.classes.avatar import Avatar, Gender
from .theme import COLORS
from .fonts import create_font, get_region_font as _get_region_font_cached
from .assets import load_tile_images, load_avatar_images
from .assets import load_tile_images, load_avatar_images, load_sect_images
from .rendering import (
draw_map,
draw_region_labels,
@@ -64,6 +64,7 @@ class Front:
self.colors = COLORS
self.tile_images = load_tile_images(self.pygame, self.tile_size)
self.sect_images = load_sect_images(self.pygame, self.tile_size)
self.male_avatars, self.female_avatars = load_avatar_images(self.pygame, self.tile_size)
self.avatar_images: Dict[str, object] = {}
self._assign_avatar_images()
@@ -131,6 +132,9 @@ class Front:
self.margin,
STATUS_BAR_HEIGHT,
)
# 底图后叠加宗门总部图层2x2
from .rendering import draw_sect_headquarters
draw_sect_headquarters(pygame, self.screen, self.world, self.sect_images, self.tile_size, self.margin, STATUS_BAR_HEIGHT)
hovered_region = draw_region_labels(
pygame,
self.screen,

View File

@@ -1,5 +1,6 @@
import os
from typing import Dict, List
from pathlib import Path
from src.classes.tile import TileType
@@ -50,6 +51,25 @@ def load_avatar_images(pygame_mod, tile_size: int):
return male_avatars, female_avatars
__all__ = ["load_tile_images", "load_avatar_images"]
def load_sect_images(pygame_mod, tile_size: int):
"""
加载宗门总部图片,缩放为 2x2 tile 大小,返回按文件名(不含后缀)为键的图像字典。
文件名建议与宗门名称一致。
"""
images: Dict[str, object] = {}
base_dir = Path("assets/sects")
if base_dir.exists():
for filename in base_dir.iterdir():
if filename.suffix.lower() == ".png" and filename.name != "original.png":
try:
image = pygame_mod.image.load(str(filename))
scaled = pygame_mod.transform.scale(image, (tile_size * 2, tile_size * 2))
images[filename.stem] = scaled
except pygame_mod.error:
continue
return images
__all__ = ["load_tile_images", "load_avatar_images", "load_sect_images"]

View File

@@ -38,6 +38,34 @@ def draw_map(pygame_mod, screen, colors, world, tile_images, ts: int, m: int, to
draw_grid(pygame_mod, screen, colors, map_obj, ts, m, top_offset)
def draw_sect_headquarters(pygame_mod, screen, world, sect_images: dict, ts: int, m: int, top_offset: int = 0):
"""
在底图绘制完成后叠加绘制宗门总部2x2 tile
以区域左上角north_west_cor为锚点绘制。
"""
for region in world.map.regions.values():
if getattr(region, "get_region_type", lambda: "")() != "sect":
continue
img_path: str | None = getattr(region, "image_path", None)
if not img_path:
# 可回退到按名称找图:期望 assets/sects/{region.name}.png
key = str(getattr(region, "name", ""))
image = sect_images.get(key)
else:
key = str(pygame_mod.Path(img_path).stem) if hasattr(pygame_mod, "Path") else img_path.split("/")[-1].split("\\")[-1].split(".")[0]
image = sect_images.get(key)
if not image:
# 未加载到图片则跳过
continue
try:
nw = tuple(map(int, str(getattr(region, "north_west_cor", "0,0")).split(",")))
except Exception:
continue
x_px = m + nw[0] * ts
y_px = m + top_offset + nw[1] * ts
screen.blit(image, (x_px, y_px))
def calculate_font_size_by_area(tile_size: int, area: int) -> int:
base = int(tile_size * 1.1)
growth = int(max(0, min(24, (area ** 0.5))))
@@ -48,11 +76,10 @@ def draw_region_labels(pygame_mod, screen, colors, world, get_region_font, tile_
ts = tile_size
m = margin
mouse_x, mouse_y = pygame_mod.mouse.get_pos()
from src.classes.region import regions_by_id
hovered_region = None
# 以区域面积降序放置,优先保证大区域标签可读性
regions = sorted(list(regions_by_id.values()), key=lambda r: getattr(r, "area", 0), reverse=True)
regions = sorted(list(world.map.regions.values()), key=lambda r: getattr(r, "area", 0), reverse=True)
placed_rects = [] # 已放置标签的矩形列表,用于碰撞检测