refactor: engine.py
This commit is contained in:
77
game_loop.py
Normal file
77
game_loop.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import logging
|
||||
|
||||
from entities import Player
|
||||
from interaction import ask_entity
|
||||
from time_utils import WorldClock
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _build_name_lookup(entities):
|
||||
name_lookup = {}
|
||||
for entity_key, entity in entities.items():
|
||||
name_lookup[entity_key.lower()] = entity_key
|
||||
name_lookup[entity.name.lower()] = entity_key
|
||||
return name_lookup
|
||||
|
||||
|
||||
def start_game(entities, player_id=None, world_time=None, location="Unknown"):
|
||||
player = None
|
||||
if player_id:
|
||||
player = entities.get(player_id)
|
||||
if player is None:
|
||||
raise ValueError(f"Player entity '{player_id}' not found in scenario.")
|
||||
else:
|
||||
player = Player(
|
||||
name="Player",
|
||||
traits=["Curious"],
|
||||
stats={},
|
||||
voice_sample="Voice: 'Direct and concise.'",
|
||||
entity_id="player",
|
||||
)
|
||||
|
||||
available_entities = {
|
||||
entity_id: entity
|
||||
for entity_id, entity in entities.items()
|
||||
if entity_id != player_id
|
||||
}
|
||||
|
||||
world_clock = WorldClock.from_time_str(world_time)
|
||||
current_entity = None
|
||||
name_lookup = _build_name_lookup(available_entities)
|
||||
entity_names = "/".join(
|
||||
[entity.name for entity in available_entities.values()] + ["Exit"]
|
||||
)
|
||||
logger.info("--- WORLD INITIALIZED ---")
|
||||
logger.info("World initialized with %s active entities.", len(available_entities))
|
||||
logger.info("Current location: %s", location)
|
||||
logger.info(
|
||||
"World time: %s (%s)", world_clock.get_time_str(), world_clock.get_vibe()
|
||||
)
|
||||
|
||||
while True:
|
||||
target_name = (
|
||||
input(f"\nWho do you want to talk to? ({entity_names}): ").lower().strip()
|
||||
)
|
||||
|
||||
if target_name in ["exit", "quit"]:
|
||||
if current_entity:
|
||||
current_entity.reflect_and_summarize(world_clock, location)
|
||||
break
|
||||
|
||||
target_key = name_lookup.get(target_name)
|
||||
if target_key is None:
|
||||
logger.warning("Target not found.")
|
||||
continue
|
||||
|
||||
new_entity = available_entities[target_key]
|
||||
if current_entity and current_entity != new_entity:
|
||||
logger.info(
|
||||
"You leave %s and approach %s.", current_entity.name, new_entity.name
|
||||
)
|
||||
current_entity.reflect_and_summarize(world_clock, location)
|
||||
|
||||
current_entity = new_entity
|
||||
|
||||
user_msg = input(f"You to {current_entity.name}: ")
|
||||
ask_entity(current_entity, player, user_msg, world_clock, location)
|
||||
Reference in New Issue
Block a user