perf: stream story assets by page
This commit is contained in:
@@ -63,6 +63,57 @@ export function hasUnitSheetAsset(key: string) {
|
||||
return unitSheetAssetsByKey.has(key);
|
||||
}
|
||||
|
||||
export function unitBaseSheetsReady(scene: Phaser.Scene, keys: Iterable<string>) {
|
||||
return Array.from(keys).every((key) => scene.textures.exists(key));
|
||||
}
|
||||
|
||||
export function waitForUnitBaseSheets(scene: Phaser.Scene, keys: Iterable<string>, onReady: () => void) {
|
||||
waitForUnitBaseSheetsAttempt(scene, Array.from(new Set(keys)), onReady);
|
||||
}
|
||||
|
||||
export function loadUnitBaseSheets(scene: Phaser.Scene, keys: Iterable<string>, onReady: () => void) {
|
||||
const uniqueKeys = Array.from(new Set(keys));
|
||||
if (unitBaseSheetsReady(scene, uniqueKeys)) {
|
||||
onReady();
|
||||
return;
|
||||
}
|
||||
|
||||
const pendingLoads: Array<{ key: string; url: string }> = [];
|
||||
uniqueKeys.forEach((key) => {
|
||||
const sheet = unitSheetAssetsByKey.get(key) ?? missingUnitSheet(key);
|
||||
if (!scene.textures.exists(sheet.key) && !pendingUnitTextureKeys.has(sheet.key)) {
|
||||
pendingUnitTextureKeys.add(sheet.key);
|
||||
pendingLoads.push({ key: sheet.key, url: sheet.url });
|
||||
}
|
||||
});
|
||||
|
||||
if (pendingLoads.length <= 0) {
|
||||
waitForUnitBaseSheets(scene, uniqueKeys, onReady);
|
||||
return;
|
||||
}
|
||||
|
||||
const clearPendingLoads = () => {
|
||||
pendingLoads.forEach(({ key }) => pendingUnitTextureKeys.delete(key));
|
||||
};
|
||||
|
||||
scene.load.once('complete', () => {
|
||||
clearPendingLoads();
|
||||
const missing = uniqueKeys.filter((key) => !scene.textures.exists(key));
|
||||
if (missing.length > 0) {
|
||||
console.warn(`Failed to load unit base sheet textures: ${missing.join(', ')}`);
|
||||
}
|
||||
onReady();
|
||||
});
|
||||
scene.load.once('loaderror', clearPendingLoads);
|
||||
pendingLoads.forEach(({ key, url }) => {
|
||||
scene.load.spritesheet(key, url, {
|
||||
frameWidth: unitSheetFrameSize,
|
||||
frameHeight: unitSheetFrameSize
|
||||
});
|
||||
});
|
||||
scene.load.start();
|
||||
}
|
||||
|
||||
export function loadUnitSheets(scene: Phaser.Scene, keys: Iterable<string>, onReady: () => void) {
|
||||
const uniqueKeys = Array.from(new Set(keys));
|
||||
if (unitSheetsReady(scene, uniqueKeys)) {
|
||||
@@ -110,6 +161,22 @@ function unitSheetsReady(scene: Phaser.Scene, keys: Iterable<string>) {
|
||||
return Array.from(keys).every((key) => scene.textures.exists(key) && scene.textures.exists(`${key}-actions`));
|
||||
}
|
||||
|
||||
function waitForUnitBaseSheetsAttempt(scene: Phaser.Scene, keys: string[], onReady: () => void, attempts = 0) {
|
||||
if (unitBaseSheetsReady(scene, keys)) {
|
||||
onReady();
|
||||
return;
|
||||
}
|
||||
|
||||
if (attempts > 200) {
|
||||
const missing = keys.filter((key) => !scene.textures.exists(key));
|
||||
console.warn(`Timed out waiting for unit base sheet textures: ${missing.join(', ')}`);
|
||||
onReady();
|
||||
return;
|
||||
}
|
||||
|
||||
scene.time.delayedCall(50, () => waitForUnitBaseSheetsAttempt(scene, keys, onReady, attempts + 1));
|
||||
}
|
||||
|
||||
function waitForUnitSheets(scene: Phaser.Scene, keys: string[], onReady: () => void, attempts = 0) {
|
||||
if (unitSheetsReady(scene, keys)) {
|
||||
onReady();
|
||||
|
||||
Reference in New Issue
Block a user