make terrain map load async

This commit is contained in:
evanpelle
2024-08-30 11:56:29 -07:00
parent 64265b1a75
commit 9cfe983824
2 changed files with 15 additions and 13 deletions
+10 -8
View File
@@ -18,7 +18,7 @@ class Client {
private hasJoined = false
private socket: WebSocket | null = null;
private terrainMap: TerrainMap
private terrainMap: Promise<TerrainMap>
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();
});
})
}
+5 -5
View File
@@ -29,8 +29,11 @@ export class Terrain {
constructor(public type: TerrainType) { }
}
export function loadTerrainMap(): TerrainMap {
const fileData = binAsString;
export async function loadTerrainMap(): Promise<TerrainMap> {
// Simulate an asynchronous file load
const fileData = await new Promise<string>((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.`);