Fix TTS module initialization and dependency issues. Update module IDs for consistency, improve circular dependency detection, and fix UI Controller event handling.
This commit is contained in:
+39
-79
@@ -8,11 +8,11 @@ import { moduleRegistry } from './module-registry.js';
|
||||
class GameLoopModule extends BaseModule {
|
||||
constructor() {
|
||||
super('game-loop', 'Game Loop');
|
||||
this.uiController = null;
|
||||
this.socketClient = null;
|
||||
this.ttsPlayer = null;
|
||||
this.textBuffer = null;
|
||||
this.isRunning = false;
|
||||
|
||||
// Dependencies
|
||||
this.dependencies = ['ui-controller', 'socket-client', 'tts-player', 'text-buffer'];
|
||||
|
||||
// Game state
|
||||
this.gameState = {
|
||||
started: false,
|
||||
canLoad: false,
|
||||
@@ -20,78 +20,33 @@ class GameLoopModule extends BaseModule {
|
||||
inventory: [],
|
||||
commandHistory: []
|
||||
};
|
||||
|
||||
this.isRunning = false;
|
||||
|
||||
// Bind methods using parent's bindMethods utility
|
||||
this.bindMethods([
|
||||
'start',
|
||||
'setupSocketEventListeners',
|
||||
'updateGameState',
|
||||
'updateUIState',
|
||||
'requestStartGame',
|
||||
'requestSaveGame',
|
||||
'requestLoadGame',
|
||||
'addText'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load module dependencies
|
||||
* @returns {Promise} - Resolves when dependencies are loaded
|
||||
*/
|
||||
async loadDependencies() {
|
||||
// Basic dependency declaration - details handled in waitForDependencies
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for dependencies to be ready
|
||||
*/
|
||||
async waitForDependencies() {
|
||||
try {
|
||||
// Wait for TTS module with a timeout
|
||||
const ttsReady = await moduleRegistry.waitForModule('tts', 15000);
|
||||
|
||||
if (ttsReady) {
|
||||
this.ttsPlayer = moduleRegistry.getModule('tts');
|
||||
this.reportProgress(30, "TTS module ready");
|
||||
} else {
|
||||
console.warn("TTS module not ready, game will have limited functionality");
|
||||
}
|
||||
|
||||
// Wait for UI Controller with a timeout
|
||||
const uiReady = await moduleRegistry.waitForModule('ui-controller', 15000);
|
||||
|
||||
if (uiReady) {
|
||||
this.uiController = moduleRegistry.getModule('ui-controller');
|
||||
this.reportProgress(50, "UI Controller ready");
|
||||
} else {
|
||||
console.warn("UI Controller not ready, game will have limited functionality");
|
||||
}
|
||||
|
||||
// Get text buffer reference
|
||||
this.textBuffer = moduleRegistry.getModule('text-buffer');
|
||||
if (this.textBuffer) {
|
||||
this.reportProgress(60, "Text buffer ready");
|
||||
}
|
||||
|
||||
// Continue even with limited functionality
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("Error waiting for dependencies:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the module
|
||||
* @returns {Promise<boolean>} - Resolves with success status
|
||||
*/
|
||||
|
||||
async initialize() {
|
||||
this.reportProgress(100, "Game loop initialized");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the game loop
|
||||
*/
|
||||
start() {
|
||||
console.log("GameLoop: Starting game sequence...");
|
||||
|
||||
try {
|
||||
// Update UI with initial game state
|
||||
if (this.uiController && this.ttsPlayer) {
|
||||
this.updateUIState();
|
||||
} else {
|
||||
console.warn("GameLoop: UI Controller or TTS Player not ready for status update.");
|
||||
}
|
||||
// The dependencies are now automatically available via getModule
|
||||
this.updateUIState();
|
||||
|
||||
console.log("GameLoop: Setting up socket listeners and connecting...");
|
||||
|
||||
@@ -105,12 +60,9 @@ class GameLoopModule extends BaseModule {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up socket event listeners and connect to server
|
||||
*/
|
||||
setupSocketEventListeners() {
|
||||
// Get the socket client module
|
||||
this.socketClient = moduleRegistry.getModule('socket-client');
|
||||
// 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");
|
||||
@@ -118,6 +70,8 @@ class GameLoopModule extends BaseModule {
|
||||
}
|
||||
|
||||
// Connect UI controller to socket client for command handling
|
||||
this.uiController = this.getModule('ui-controller');
|
||||
|
||||
if (this.uiController) {
|
||||
this.uiController.socketClient = this.socketClient;
|
||||
} else {
|
||||
@@ -128,9 +82,14 @@ class GameLoopModule extends BaseModule {
|
||||
this.socketClient.on('connect', () => {
|
||||
console.log("GameLoop: Socket connected event received.");
|
||||
|
||||
// Request a new game start when we (re)connect
|
||||
console.log("GameLoop: Requesting start game on (re)connect.");
|
||||
this.requestStartGame();
|
||||
// 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
|
||||
@@ -157,8 +116,6 @@ class GameLoopModule extends BaseModule {
|
||||
this.socketClient.connect().then(success => {
|
||||
if (success) {
|
||||
console.log("GameLoop: Socket connection established successfully.");
|
||||
console.log("GameLoop: Requesting to start a new game");
|
||||
this.requestStartGame();
|
||||
} else {
|
||||
console.error("GameLoop: Failed to connect to socket server");
|
||||
}
|
||||
@@ -229,12 +186,15 @@ class GameLoopModule extends BaseModule {
|
||||
* @param {string} text - Text to add
|
||||
*/
|
||||
addText(text) {
|
||||
if (!this.textBuffer) {
|
||||
// Use parent's getModule method
|
||||
const textBuffer = this.getModule('text-buffer');
|
||||
|
||||
if (!textBuffer) {
|
||||
console.warn("Text buffer not available");
|
||||
return;
|
||||
}
|
||||
|
||||
this.textBuffer.addText(text);
|
||||
textBuffer.addText(text);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user