Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

Commit

Permalink
Merge branch bugfix/asset-loading-race-condition into main (#103)
Browse files Browse the repository at this point in the history
Fix asset loading

Fix race condition while asset loading in Chrome browser
  • Loading branch information
Kitt3120 authored Jun 19, 2023
1 parent 7772a0c commit ccca5f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
18 changes: 18 additions & 0 deletions src/ts/engine/assets/assetloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class AssetLoader {

private _imagesLoadedEvent: EngineEventHandler<boolean, EngineEvent<boolean>>;
private _audiosLoadedEvent: EngineEventHandler<boolean, EngineEvent<boolean>>;
private _assetsLoadedPreEvent: EngineEventHandler<
boolean,
EngineEvent<boolean>
>;
private _assetsLoadedEvent: EngineEventHandler<boolean, EngineEvent<boolean>>;

private _imageLoadTrackerTask: number | undefined;
Expand All @@ -28,6 +32,13 @@ class AssetLoader {
return this._audiosLoadedEvent;
}

public get assetsLoadedPreEvent(): EngineEventHandler<
boolean,
EngineEvent<boolean>
> {
return this._assetsLoadedPreEvent;
}

public get assetsLoadedEvent(): EngineEventHandler<
boolean,
EngineEvent<boolean>
Expand All @@ -47,6 +58,10 @@ class AssetLoader {
boolean,
EngineEvent<boolean>
>();
this._assetsLoadedPreEvent = new EngineEventHandler<
boolean,
EngineEvent<boolean>
>();
this._assetsLoadedEvent = new EngineEventHandler<
boolean,
EngineEvent<boolean>
Expand Down Expand Up @@ -177,6 +192,9 @@ class AssetLoader {

this._assetLoadTrackerTask = setInterval(() => {
let assetsReady = this.areAssetsReady();
this._assetsLoadedPreEvent.dispatch(
new EngineEvent<boolean>(assetsReady)
);
this._assetsLoadedEvent.dispatch(new EngineEvent<boolean>(assetsReady));

if (assetsReady) {
Expand Down
8 changes: 6 additions & 2 deletions src/ts/engine/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ class Game {
this._levelManager
);

this._assetLoader.assetsLoadedEvent.subscribe(
this._assetLoader.assetsLoadedPreEvent.subscribe(
(engineEvent: EngineEvent<boolean>) => {
if (!engineEvent.eventData) {
console.log("Loading assets...");
console.log(
`Loading assets: ${this._assetLoader.getAssetsReadyCount()}/${this._assetLoader.getAssetsCount()}`
);
} else {
console.log(`${this._assetLoader.getAssetsCount()} assets loaded`);

engineSetup.registerTextures(
this._assetLoader,
this._assetManager,
Expand Down
16 changes: 6 additions & 10 deletions src/ts/game/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,13 @@ function init(): void {

let assetLoader = Services.resolve<AssetLoader>("AssetLoader");

if (!assetLoader.areAssetsReady()) {
assetLoader.assetsLoadedEvent.subscribe(
(engineEvent: EngineEvent<boolean>) => {
if (engineEvent.eventData) {
start(game);
}
assetLoader.assetsLoadedEvent.subscribe(
(engineEvent: EngineEvent<boolean>) => {
if (engineEvent.eventData) {
start(game);
}
);
} else {
start(game);
}
}
);
}

function start(game: Game): void {
Expand Down

0 comments on commit ccca5f8

Please sign in to comment.