reverted resourceManager

This commit is contained in:
OfficialCHenry 2022-04-13 23:17:00 -04:00
parent 1f0764087a
commit 6e34e8b2d4
2 changed files with 237 additions and 115 deletions

View File

@ -81,6 +81,10 @@ export default class ResourceManager {
private loadonly_gl_ShaderProgramsToLoad: number; private loadonly_gl_ShaderProgramsToLoad: number;
private loadonly_gl_ShaderLoadingQueue: Queue<KeyPath_Shader>; private loadonly_gl_ShaderLoadingQueue: Queue<KeyPath_Shader>;
private loadonly_tilemapObjectToLoad: number;
private loadonly_tilemapObjectLoaded: number;
private loadonly_tilemapObjectLoadingQueue: Queue<KeyMapPair>;
private gl_ShaderPrograms: Map<WebGLProgramType>; private gl_ShaderPrograms: Map<WebGLProgramType>;
private gl_Textures: Map<number>; private gl_Textures: Map<number>;
@ -137,6 +141,10 @@ export default class ResourceManager {
this.resourcesToUnload = new Array(); this.resourcesToUnload = new Array();
this.resourcesToKeep = new Array(); this.resourcesToKeep = new Array();
this.loadonly_tilemapObjectToLoad = 0;
this.loadonly_tilemapObjectToLoad = 0;
this.loadonly_tilemapObjectLoadingQueue = new Queue();
}; };
/* ######################################## SINGLETON ########################################*/ /* ######################################## SINGLETON ########################################*/
@ -190,9 +198,9 @@ export default class ResourceManager {
*/ */
public getImage(key: string): HTMLImageElement { public getImage(key: string): HTMLImageElement {
let image = this.images.get(key); let image = this.images.get(key);
if(image === undefined){ // if (image === undefined) {
throw `There is no image associated with key "${key}"` // throw `There is no image associated with key "${key}"`
} // }
return image; return image;
} }
@ -311,6 +319,9 @@ export default class ResourceManager {
this.loading = true; this.loading = true;
// Load everything in the queues. Tilemaps have to come before images because they will add new images to the queue // Load everything in the queues. Tilemaps have to come before images because they will add new images to the queue
this.loadTilemapObjectFromQueue(() => {
console.log("Loaded TilemapObjects");
this.loadTilemapsFromQueue(() => { this.loadTilemapsFromQueue(() => {
console.log("Loaded Tilemaps"); console.log("Loaded Tilemaps");
this.loadSpritesheetsFromQueue(() => { this.loadSpritesheetsFromQueue(() => {
@ -335,6 +346,7 @@ export default class ResourceManager {
}); });
}); });
}); });
});
} }
private finishLoading(callback: Function): void { private finishLoading(callback: Function): void {
@ -974,6 +986,110 @@ export default class ResourceManager {
/ this.loadonly_typesToLoad; / this.loadonly_typesToLoad;
} }
// Customized funtions below
// These funtions are NOT well tested!!!
// Only used for shattered sword specific purpose!!!
// Use them carefully!!!
public tilemapFromObject(key: string, tilemap: TiledTilemapData): void {
this.loadonly_tilemapObjectLoadingQueue.enqueue({ key: key, map: tilemap });
}
private loadTilemapObjectFromQueue(onFinishLoading: Function) {
this.loadonly_tilemapObjectToLoad = this.loadonly_tilemapObjectLoadingQueue.getSize();
this.loadonly_tilemapObjectLoaded = 0;
// If no items to load, we're finished
if (this.loadonly_tilemapObjectToLoad === 0) {
onFinishLoading();
return;
}
while (this.loadonly_tilemapObjectLoadingQueue.hasItems()) {
let map = this.loadonly_tilemapObjectLoadingQueue.dequeue();
this.loadTilemapFromObject(map.key, map.map, onFinishLoading);
}
}
private loadTilemapFromObject(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.loadonly_imageLoadingQueue.enqueue({ key: key, path: path, isDependency: true });
// 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);
this.finishLoadingTilemapObject(callbackIfLast);
}
private finishLoadingTilemapObject(callback: Function): void {
this.loadonly_tilemapObjectLoaded += 1;
if (this.loadonly_tilemapObjectLoaded === this.loadonly_tilemapObjectToLoad) {
// We're done loading tilemaps
callback();
}
}
public singleImage(key: string, path: string, callbackIfLast: Function): void {
var image = new Image();
image.onload = () => {
// Add to loaded images
this.images.add(key, image);
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;
}
public singleAudio(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 { update(deltaT: number): void {
if (this.loading) { if (this.loading) {
if (this.onLoadProgress) { if (this.onLoadProgress) {
@ -1028,6 +1144,11 @@ class KeyPathPair {
isDependency?: boolean = false; isDependency?: boolean = false;
} }
class KeyMapPair {
key: string;
map: TiledTilemapData;
}
class KeyPath_Shader { class KeyPath_Shader {
key: string; key: string;
vpath: string; vpath: string;

View File

@ -9,6 +9,7 @@ import Sprite from "../../Wolfie2D/Nodes/Sprites/Sprite";
import { GameEventType } from "../../Wolfie2D/Events/GameEventType"; import { GameEventType } from "../../Wolfie2D/Events/GameEventType";
import Input from "../../Wolfie2D/Input/Input"; import Input from "../../Wolfie2D/Input/Input";
enum Mode { enum Mode {
GAME_MODE = "GameMode", GAME_MODE = "GameMode",
STORY_MODE = "StoryMode", STORY_MODE = "StoryMode",