Mi2dRPGamEng/resources/logic/spawning.py

31 lines
1.4 KiB
Python

from __future__ import annotations
from engine.ai import AIController
from engine.character import Character
from engine.defs import DefRegistry
from engine.entity import EntityPosition
from engine.inventory import Inventory
def spawn_npc(registry: DefRegistry, entity_def_id: str, position: EntityPosition) -> Character:
"""Builds an NPC Character from an entities.json template - the non-player counterpart to
main.py's equip_starting_loadout/spawn_new_character_into_world (which only ever build the
player). Fills the AI slot from the template's own ai_behavior_id if it names one (see
engine/ai.py::AIController, resources/logic/ai.py::run_ai_tick); a template with none leaves
the NPC inert (ai=None), same as the player. Does not add the result to a World - callers
still need world.add_entity(...) themselves, same as the player spawn path.
"""
entity_def = registry.get_entity_def(entity_def_id)
npc = Character(
name=entity_def.name,
def_id=entity_def.id,
position=position,
species_id=entity_def.species_id,
default_body_parts=registry.new_default_body_parts(entity_def.species_id),
default_clothing=registry.new_default_clothing(entity_def.id),
ai=AIController(entity_def.ai_behavior_id) if entity_def.ai_behavior_id is not None else None,
)
if entity_def.inventory_size is not None:
npc.inventory = Inventory(*entity_def.inventory_size)
return npc