Polish early battle onboarding
This commit is contained in:
@@ -47,6 +47,7 @@ const unitSheets = Object.entries(unitSheetUrls)
|
||||
.sort((left, right) => left.key.localeCompare(right.key));
|
||||
|
||||
const unitSheetAssetsByKey = new Map(unitSheets.map((sheet) => [sheet.key, sheet]));
|
||||
const pendingUnitTextureKeys = new Set<string>();
|
||||
|
||||
export function unitTextureVariantKeys(baseKey: string) {
|
||||
const prefix = `${baseKey}-`;
|
||||
@@ -64,33 +65,65 @@ export function hasUnitSheetAsset(key: string) {
|
||||
|
||||
export function loadUnitSheets(scene: Phaser.Scene, keys: Iterable<string>, onReady: () => void) {
|
||||
const uniqueKeys = Array.from(new Set(keys));
|
||||
const missingSheets = uniqueKeys
|
||||
.filter((key) => !scene.textures.exists(key) || !scene.textures.exists(`${key}-actions`))
|
||||
.map((key) => unitSheetAssetsByKey.get(key) ?? missingUnitSheet(key));
|
||||
|
||||
if (missingSheets.length <= 0) {
|
||||
if (unitSheetsReady(scene, uniqueKeys)) {
|
||||
onReady();
|
||||
return;
|
||||
}
|
||||
|
||||
scene.load.once('complete', onReady);
|
||||
missingSheets.forEach(({ key, url, actionKey, actionUrl }) => {
|
||||
if (!scene.textures.exists(key)) {
|
||||
scene.load.spritesheet(key, url, {
|
||||
frameWidth: unitSheetFrameSize,
|
||||
frameHeight: unitSheetFrameSize
|
||||
});
|
||||
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 (!scene.textures.exists(actionKey)) {
|
||||
scene.load.spritesheet(actionKey, actionUrl, {
|
||||
frameWidth: unitSheetFrameSize,
|
||||
frameHeight: unitSheetFrameSize
|
||||
});
|
||||
if (!scene.textures.exists(sheet.actionKey) && !pendingUnitTextureKeys.has(sheet.actionKey)) {
|
||||
pendingUnitTextureKeys.add(sheet.actionKey);
|
||||
pendingLoads.push({ key: sheet.actionKey, url: sheet.actionUrl });
|
||||
}
|
||||
});
|
||||
|
||||
if (pendingLoads.length <= 0) {
|
||||
waitForUnitSheets(scene, uniqueKeys, onReady);
|
||||
return;
|
||||
}
|
||||
|
||||
const clearPendingLoads = () => {
|
||||
pendingLoads.forEach(({ key }) => pendingUnitTextureKeys.delete(key));
|
||||
};
|
||||
|
||||
scene.load.once('complete', () => {
|
||||
clearPendingLoads();
|
||||
waitForUnitSheets(scene, uniqueKeys, onReady);
|
||||
});
|
||||
scene.load.once('loaderror', clearPendingLoads);
|
||||
pendingLoads.forEach(({ key, url }) => {
|
||||
scene.load.spritesheet(key, url, {
|
||||
frameWidth: unitSheetFrameSize,
|
||||
frameHeight: unitSheetFrameSize
|
||||
});
|
||||
});
|
||||
scene.load.start();
|
||||
}
|
||||
|
||||
function unitSheetsReady(scene: Phaser.Scene, keys: Iterable<string>) {
|
||||
return Array.from(keys).every((key) => scene.textures.exists(key) && scene.textures.exists(`${key}-actions`));
|
||||
}
|
||||
|
||||
function waitForUnitSheets(scene: Phaser.Scene, keys: string[], onReady: () => void, attempts = 0) {
|
||||
if (unitSheetsReady(scene, keys)) {
|
||||
onReady();
|
||||
return;
|
||||
}
|
||||
|
||||
if (attempts > 200) {
|
||||
const missing = keys.filter((key) => !scene.textures.exists(key) || !scene.textures.exists(`${key}-actions`));
|
||||
throw new Error(`Timed out waiting for unit sheet textures: ${missing.join(', ')}`);
|
||||
}
|
||||
|
||||
scene.time.delayedCall(50, () => waitForUnitSheets(scene, keys, onReady, attempts + 1));
|
||||
}
|
||||
|
||||
export function ensureUnitAnimations(scene: Phaser.Scene, keys: Iterable<string>) {
|
||||
Array.from(new Set(keys)).forEach((key) => {
|
||||
if (!scene.textures.exists(key)) {
|
||||
|
||||
Reference in New Issue
Block a user