/** * Game Loop Module for AI Interactive Fiction * Manages the main game logic and connects various modules */ import { BaseModule } from './base-module.js'; class GameLoopModule extends BaseModule { constructor() { super('game-loop', 'Game Loop'); // Dependencies this.dependencies = ['ui-controller', 'socket-client', 'tts-player', 'text-buffer']; // Game state this.gameState = { started: false, canLoad: false, currentRoom: null, inventory: [], commandHistory: [] }; this.isRunning = false; // Bind methods using parent's bindMethods utility this.bindMethods([ 'start', 'setupSocketEventListeners', 'updateGameState', 'updateUIState', 'requestStartGame', 'requestSaveGame', 'requestLoadGame', 'addText' ]); } async initialize() { this.reportProgress(100, "Game loop initialized"); return true; } start() { console.log("GameLoop: Starting game sequence..."); try { // The dependencies are now automatically available via getModule this.updateUIState(); console.log("GameLoop: Setting up socket listeners and connecting..."); // Set up socket event listeners and connect this.setupSocketEventListeners(); // Set the game loop as running this.isRunning = true; } catch (error) { console.error("Error starting game loop:", error); } } setupSocketEventListeners() { // Get the socket client module using parent's getModule method const socketClient = this.getModule('socket-client'); if (!socketClient) { console.error("Socket client module not found"); return; } // Connect UI controller to socket client for command handling const uiController = this.getModule('ui-controller'); if (uiController) { uiController.socketClient = socketClient; } else { console.warn("GameLoop: UI Controller not ready for Socket Client assignment."); } // Listen for socket connection event socketClient.on('connect', () => { console.log("GameLoop: Socket connected event received."); // Request a new game start when we connect // Only request start game if one isn't already in progress if (!this.gameState.started) { console.log("GameLoop: Requesting start game on connect."); this.requestStartGame(); } else { console.log("GameLoop: Game already started, skipping duplicate start request."); } }); // Listen for game state updates socketClient.on('gameStateUpdate', (data) => { console.log("GameLoop: Game state update received", data); this.updateGameState(data); }); // Listen for narrative responses socketClient.on('narrativeResponse', (data) => { console.log("GameLoop: Narrative response received", data); // Text processing is handled by socket-client -> text-buffer -> ui-controller pipeline }); // Listen for game introduction socketClient.on('gameIntroduction', (data) => { console.log("GameLoop: Received gameIntroduction"); this.gameState.started = true; this.updateUIState(); // Text processing is handled by socket-client -> text-buffer -> ui-controller pipeline }); // Connect to the socket server socketClient.connect().then(success => { if (success) { console.log("GameLoop: Socket connection established successfully."); } else { console.error("GameLoop: Failed to connect to socket server"); } }); } /** * Update the game state * @param {Object} data - New game state data */ updateGameState(data) { if (!data) return; // Update game state if (data.currentRoom) { this.gameState.currentRoom = data.currentRoom; } if (data.inventory) { this.gameState.inventory = data.inventory; } // Update UI with new game state this.updateUIState(); } /** * Update UI with current game state */ updateUIState() { const uiController = this.getModule('ui-controller'); if (!uiController) return; // Update UI components based on game state uiController.updateButtonStates(this.gameState); } /** * Request to start a new game */ requestStartGame() { const socketClient = this.getModule('socket-client'); if (!socketClient) return; socketClient.requestStartGame(); this.gameState.started = true; } /** * Request to save the current game */ requestSaveGame() { const socketClient = this.getModule('socket-client'); if (!socketClient) return; socketClient.requestSaveGame(); } /** * Request to load a saved game */ requestLoadGame() { const socketClient = this.getModule('socket-client'); if (!socketClient) return; socketClient.requestLoadGame(); } /** * Manually add text to the buffer * Useful for testing or adding local messages * @param {string} text - Text to add */ addText(text) { // Use parent's getModule method const textBuffer = this.getModule('text-buffer'); if (!textBuffer) { console.warn("Text buffer not available"); return; } textBuffer.addText(text); } } // Create the singleton instance const GameLoop = new GameLoopModule(); // Export the module export { GameLoop };