input = $input; $this->output = $output; $this->questionHelper = $helperSet->get('question'); } /** * 初始化核心基础设施 */ private function initializeInfrastructure(): void { // 数据库 $this->dbManager = new DatabaseManager(); $this->dbManager->loadInitialData(); // 事件分发器 (核心) $this->eventDispatcher = new EventDispatcher(); // ⭐ 实例化 Repository 层 $jsonLoader = new JsonFileLoader(); $this->itemRepository = new ItemRepository($jsonLoader); $this->enemyRepository = new EnemyRepository($jsonLoader); $this->abilityRepository = new AbilityRepository($jsonLoader); $this->questRepository = new QuestRepository($jsonLoader); // ⭐ 修正变量名 $this->npcRepository = new NPCRepository($jsonLoader); $this->mapRepository = new MapRepository($jsonLoader); } /** * 实例化并注册所有系统服务 */ public function registerServices(): EventDispatcher { $this->initializeInfrastructure(); // 状态管理器 $this->stateManager = new StateManager($this->dbManager->getConnection(),$this->mapRepository); $this->saveLoadService = new SaveLoadService($this->eventDispatcher, $this->stateManager); // 1. UI 服务 (只需要 Dispatcher 和 StateManager) $this->register(UIService::class, new UIService($this->output, $this->stateManager, $this->questRepository)); // 2. 核心逻辑服务 (依赖 Dispatcher, StateManager) $this->register(MapSystem::class, new MapSystem($this->eventDispatcher, $this->stateManager,$this->npcRepository,$this->mapRepository)); $this->register(CharacterService::class, new CharacterService($this->eventDispatcher, $this->stateManager)); $this->register(LootService::class, new LootService($this->eventDispatcher, $this->stateManager, $this->itemRepository, $this->enemyRepository)); $this->register(ItemService::class, new ItemService($this->eventDispatcher, $this->stateManager)); $this->register(EquipmentService::class, new EquipmentService($this->eventDispatcher, $this->stateManager)); // ⭐ 注册装备服务 $this->register(QuestService::class, new QuestService($this->eventDispatcher, $this->stateManager, $this->questRepository)); // ⭐ 实例化 AbilityService $abilityService = new AbilityService($this->eventDispatcher, $this->stateManager, $this->abilityRepository); $this->register(AbilityService::class, $abilityService); // ⭐ 实例化 DialogueService $dialogueService = new DialogueService($this->eventDispatcher, $this->stateManager, $this->input, $this->output, $this->questionHelper); $this->register(DialogueService::class, $dialogueService); // 3. I/O 交互服务 // ⭐ 使用 get 方法获取已注册的服务实例 // $dialogueService = $this->services[DialogueService::class]; // No need to fetch from array if we have var $this->register(InteractionSystem::class, new InteractionSystem( $this->eventDispatcher, $this->stateManager, $this->npcRepository, $this->questRepository, // ⭐ 修正变量名 $dialogueService, // ⭐ 新增注入 $this->input, $this->output, $this->questionHelper ) ); $this->register(BattleService::class, new BattleService($this->eventDispatcher, $this->stateManager, $this->input, $this->output, $this->questionHelper) ); $this->register(ShopService::class, new ShopService($this->eventDispatcher, $this->stateManager, $this->input, $this->output, $this->questionHelper, $this->itemRepository) ); // ⭐ 注册SaveLoadService为事件监听器,实现自动保存 $this->register(SaveLoadService::class, $this->saveLoadService); // 4. 输入处理服务 (驱动主循环,必须最后注册,因为它依赖于所有 I/O 组件) $this->register(InputHandler::class, new InputHandler($this->eventDispatcher, $this->input, $this->output, $this->questionHelper,$this->stateManager) ); // 返回分发器和状态管理器,供 GameCommand 使用 return $this->eventDispatcher; } public function getSaveLoadService(): SaveLoadService { return $this->saveLoadService; } /** * 注册服务并将其作为监听器添加到 Event Dispatcher */ private function register(string $key, object $service): void { $this->services[$key] = $service; // 仅注册实现了 EventListenerInterface 的服务 if ($service instanceof EventListenerInterface) { $this->eventDispatcher->registerListener($service); } } // 可选:添加 Getter 方法,方便在 GameCommand 中获取 Player 或 InputHandler public function getInputHandler(): InputHandler { return $this->services[InputHandler::class]; } public function getStateManager(): StateManager { return $this->stateManager; } // ⭐ 新增 Repository Getter (供需要配置数据的服务使用) public function getItemRepository(): ItemRepository { return $this->itemRepository; } public function getEnemyRepository(): EnemyRepository { return $this->enemyRepository; } public function getAbilityRepository(): AbilityRepository { return $this->abilityRepository; } public function getNpcRepository(): NPCRepository { return $this->npcRepository; } // ⭐ 新增 }