feat: added custiomized functions to ResourceManager

This commit is contained in:
Renge 2022-04-05 21:02:01 -04:00
parent a1bc6aa095
commit 3f7254ffb8

View File

@ -974,6 +974,100 @@ export default class ResourceManager {
/ this.loadonly_typesToLoad;
}
// Customized funtions below
// These funtions are NOT well tested!!!
// Only used for shatted sword specific purpose!!!
// Use them carefully!!!
public loadSingleTilemap(key: string, tiledMap: TiledTilemapData, callbackIfLast: Function): void {
// We can parse the object later - it's much faster than loading
this.tilemaps.add(key, tiledMap);
let resource = new ResourceReference(key, ResourceType.TILEMAP);
// Grab the tileset images we need to load and add them to the imageloading queue
for (let tileset of tiledMap.tilesets) {
if (tileset.image) {
let key = tileset.image;
let path = key;
this.loadSingleImage(key, path, true , callbackIfLast);
// Add this image as a dependency to the tilemap
resource.addDependency(new ResourceReference(key, ResourceType.IMAGE));
}
}
// Add the resource reference to the list of resource to unload
this.resourcesToUnload.push(resource);
}
private loadSingleSpritesheet(key: string, pathToSpritesheetJSON: string, callbackIfLast: Function): void {
this.loadTextFile(pathToSpritesheetJSON, (fileText: string) => {
let spritesheet = <Spritesheet>JSON.parse(fileText);
// We can parse the object later - it's much faster than loading
this.spritesheets.add(key, spritesheet);
let resource = new ResourceReference(key, ResourceType.SPRITESHEET);
// Grab the image we need to load and add it to the imageloading queue
let path = StringUtils.getPathFromFilePath(pathToSpritesheetJSON) + spritesheet.spriteSheetImage;
this.loadSingleImage(spritesheet.name, path, true, callbackIfLast);
resource.addDependency(new ResourceReference(spritesheet.name, ResourceType.IMAGE));
this.resourcesToUnload.push(resource);
});
}
public loadSingleImage(key: string, path: string, isDependency: boolean, callbackIfLast: Function): void {
var image = new Image();
image.onload = () => {
// Add to loaded images
this.images.add(key, image);
// If not a dependency, push it to the unload list. Otherwise it's managed by something else
if (!isDependency) {
this.resourcesToUnload.push(new ResourceReference(key, ResourceType.IMAGE));
}
// If WebGL is active, create a texture
if (this.gl_WebGLActive) {
this.createWebGLTexture(key, image);
}
// Finish image load
this.finishLoadingSingleObject(callbackIfLast);
}
image.src = path;
}
private loadSingleAudio(key: string, path: string, callbackIfLast: Function): void {
let audioCtx = AudioManager.getInstance().getAudioContext();
let request = new XMLHttpRequest();
request.open('GET', path, true);
request.responseType = 'arraybuffer';
request.onload = () => {
audioCtx.decodeAudioData(request.response, (buffer) => {
// Add to list of audio buffers
this.audioBuffers.add(key, buffer);
this.resourcesToUnload.push(new ResourceReference(key, ResourceType.AUDIO));
// Finish loading sound
this.finishLoadingSingleObject(callbackIfLast);
}, (error) => {
throw "Error loading sound";
});
}
request.send();
}
private finishLoadingSingleObject(callback: Function): void {
callback();
}
update(deltaT: number): void {
if (this.loading) {
if (this.onLoadProgress) {