| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- """Render game NPC standing frames without constructing gameplay NPC objects."""
- import pygame
- from const.window import GAME_SQUARE
- class NpcPreview:
- def __init__(self, app):
- self.app = app
- self.cache = {}
- self.errors = {}
- def load(self, name):
- if name in self.cache:
- return self.cache[name]
- if name in self.errors:
- return None
- try:
- from client_local.file_loader.fileLoader import load_json
- from client_local.frame.character import get_character, CHARACTER_COMPONENTS
- config = load_json("game/npc/%s.json" % name) or load_json("game/npcs/%s.json" % name)
- if not config:
- raise ValueError("找不到 game/npc/%s.json" % name)
- character_name = config["character"]
- character = load_json("game/frame/character/%s.json" % character_name) or load_json("game/character/%s.json" % character_name)
- if not character:
- raise ValueError("找不到角色 " + character_name)
- decorations = {}
- for part, value in (config.get("decorations") or {}).items():
- if value is not None:
- decorations[part.capitalize()] = load_json("game/frame/%s/%s.json" % (part.lower(), value))
- frames = get_character(character, (255, 0, 0), decorations)
- orient = next((o for o in ("STAND_D", "STAND_R", "STAND_L", "STAND_U", "D") if o in frames), None)
- if orient is None:
- raise ValueError("角色缺少可用站立帧")
- value = (frames[orient], CHARACTER_COMPONENTS[orient[-1]], max(0, float(config.get("resent_dist", 0))))
- self.cache[name] = value
- return value
- except Exception as error:
- self.errors[name] = str(error)
- self.app.menu.notify("模型加载失败 %s:%s" % (name, error))
- return None
- def draw(self, surface, npc, scroll, color, frame_index=None, show_vision=True):
- model = self.load(npc.get("name", ""))
- if model is None:
- return False
- frames, components, resent = model
- x = (npc["x"] - scroll[0] + 0.5) * GAME_SQUARE
- y = (npc["y"] - scroll[1] + 0.5) * GAME_SQUARE
- radius = resent * GAME_SQUARE
- if show_vision and radius > 0:
- rect = pygame.Rect(round(x - radius), round(y - radius), round(radius * 2), round(radius * 2))
- pygame.draw.rect(surface, (12, 16, 22, 240), rect.inflate(2, 2), 5)
- pygame.draw.rect(surface, color + (255,), rect, 3)
- tick = pygame.time.get_ticks() // 200 if frame_index is None else frame_index
- for component in components:
- sequence = frames.get(component)
- if sequence:
- frame = sequence[tick % len(sequence)]
- surface.blit(frame.image, (round(x + frames["Cx"] + frame.cx), round(y + frames["Cy"] + frame.cy)))
- return True
|