diff --git a/src/client/Client.ts b/src/client/Client.ts index 01d5d0b4e..b1b223ed4 100644 --- a/src/client/Client.ts +++ b/src/client/Client.ts @@ -18,7 +18,7 @@ class Client { private hasJoined = false private socket: WebSocket | null = null; - private terrainMap: TerrainMap + private terrainMap: Promise private game: ClientGame private lobbiesContainer: HTMLElement | null; @@ -115,13 +115,15 @@ class Client { if (this.game != null) { return; } - this.game = createClientGame(getUsername(), new PseudoRandom(Date.now()).nextID(), lobby.id, getConfig(), this.terrainMap); - this.game.join(); - const g = this.game; - window.addEventListener('beforeunload', function (event) { - console.log('Browser is closing'); - g.stop(); - }); + this.terrainMap.then(tm => { + this.game = createClientGame(getUsername(), new PseudoRandom(Date.now()).nextID(), lobby.id, getConfig(), tm); + this.game.join(); + const g = this.game; + window.addEventListener('beforeunload', function (event) { + console.log('Browser is closing'); + g.stop(); + }); + }) } diff --git a/src/core/TerrainMapLoader.ts b/src/core/TerrainMapLoader.ts index ba1fd5fc7..fae5e5953 100644 --- a/src/core/TerrainMapLoader.ts +++ b/src/core/TerrainMapLoader.ts @@ -29,8 +29,11 @@ export class Terrain { constructor(public type: TerrainType) { } } -export function loadTerrainMap(): TerrainMap { - const fileData = binAsString; +export async function loadTerrainMap(): Promise { + // Simulate an asynchronous file load + const fileData = await new Promise((resolve) => { + setTimeout(() => resolve(binAsString), 100); + }); console.log(`Loaded data length: ${fileData.length} bytes`); @@ -40,9 +43,6 @@ export function loadTerrainMap(): TerrainMap { console.log(`Decoded dimensions: ${width}x${height}`); - // Log the first 100 bytes of data (including the width and height bytes) - // logBinaryAsAscii(fileData, 100); - // Check if the data length matches the expected size if (fileData.length != width * height + 4) { // +4 for the width and height bytes throw new Error(`Invalid data: buffer size ${fileData.length} incorrect for ${width}x${height} terrain plus 4 bytes for dimensions.`);