Files
ai.interactive.fiction/public/js/game-loop.js

212 lines
6.2 KiB
JavaScript

/**
* Game Loop Module for AI Interactive Fiction
* Manages the main game logic and connects various modules
*/
import { BaseModule } from './base-module.js';
import { moduleRegistry } from './module-registry.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
this.socketClient = this.getModule('socket-client');
if (!this.socketClient) {
console.error("Socket client module not found");
return;
}
// Connect UI controller to socket client for command handling
this.uiController = this.getModule('ui-controller');
if (this.uiController) {
this.uiController.socketClient = this.socketClient;
} else {
console.warn("GameLoop: UI Controller not ready for Socket Client assignment.");
}
// Listen for socket connection event
this.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
this.socketClient.on('gameStateUpdate', (data) => {
console.log("GameLoop: Game state update received", data);
this.updateGameState(data);
});
// Listen for narrative responses
this.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
this.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
this.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() {
if (!this.uiController) return;
// Update UI components based on game state
this.uiController.updateButtonStates(this.gameState);
}
/**
* Request to start a new game
*/
requestStartGame() {
if (!this.socketClient) return;
this.socketClient.requestStartGame();
this.gameState.started = true;
}
/**
* Request to save the current game
*/
requestSaveGame() {
if (!this.socketClient) return;
this.socketClient.requestSaveGame();
}
/**
* Request to load a saved game
*/
requestLoadGame() {
if (!this.socketClient) return;
this.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();
// Register with the module registry
moduleRegistry.register(GameLoop);
// Export the module
export { GameLoop };
// Keep a reference in window for loader system
window.GameLoop = GameLoop;