54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
namespace TSE {
|
|
|
|
export const MESSAGE_ASSET_LOADER_ASSET_LOADED = "MESSAGE_ASSET_LOADER_ASSET_LOADED::";
|
|
|
|
export class AssetManager {
|
|
|
|
private static _loaders: IAssetLoader[] = [];
|
|
private static _loadedAssets: { [name: string]: IAsset } = {};
|
|
|
|
private constructor() {}
|
|
|
|
public static initialize(): void {
|
|
this.registerLoader(new ImageAssetLoader());
|
|
this.registerLoader(new JsonAssetLoader());
|
|
}
|
|
|
|
public static registerLoader(loader: IAssetLoader): void {
|
|
AssetManager._loaders.push(loader);
|
|
}
|
|
|
|
public static onAssetLoaded(asset: IAsset): void {
|
|
AssetManager._loadedAssets[asset.name] = asset;
|
|
Message.send(MESSAGE_ASSET_LOADER_ASSET_LOADED + asset.name, this, asset);
|
|
}
|
|
|
|
public static loadAsset(assetName: string): void {
|
|
let extention = assetName.split('.').pop().toLowerCase();
|
|
for (let loader of AssetManager._loaders) {
|
|
if (loader.supportedExtensions.indexOf(extention) !== -1) {
|
|
loader.loadAsset(assetName);
|
|
return;
|
|
}
|
|
}
|
|
|
|
console.warn("Unable to load asset with extention: '" + extention + "'. no loader was found.");
|
|
}
|
|
|
|
public static isAssetLoaded(assetName: string): boolean {
|
|
return AssetManager._loadedAssets[assetName] !== undefined;
|
|
}
|
|
|
|
public static getAsset(assetName: string): IAsset {
|
|
if (AssetManager.isAssetLoaded(assetName)) {
|
|
return AssetManager._loadedAssets[assetName];
|
|
} else {
|
|
AssetManager.loadAsset(assetName);
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
}
|
|
|
|
} |