feat: added loadTilemapFromObject

This commit is contained in:
Renge 2022-04-05 22:16:22 -04:00
parent 3f7254ffb8
commit 11e3362bf2
3 changed files with 74 additions and 44 deletions

View File

@ -42,7 +42,7 @@
{ {
"columns":8, "columns":8,
"firstgid":1, "firstgid":1,
"image":"forest_tileset.png", "image":"shattered_sword_assets/tilemaps/forest_tileset.png",
"imageheight":128, "imageheight":128,
"imagewidth":128, "imagewidth":128,
"margin":0, "margin":0,

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 ########################################*/
@ -311,26 +319,30 @@ 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.loadTilemapsFromQueue(() => {
console.log("Loaded Tilemaps");
this.loadSpritesheetsFromQueue(() => {
console.log("Loaded Spritesheets");
this.loadImagesFromQueue(() => {
console.log("Loaded Images");
this.loadAudioFromQueue(() => {
console.log("Loaded Audio");
this.loadObjectsFromQueue(() => {
console.log("Loaded Objects");
if (this.gl_WebGLActive) { this.loadTilemapObjectFromQueue(() => {
this.gl_LoadShadersFromQueue(() => { console.log("Loaded TilemapObjects");
console.log("Loaded Shaders"); this.loadTilemapsFromQueue(() => {
console.log("Loaded Tilemaps");
this.loadSpritesheetsFromQueue(() => {
console.log("Loaded Spritesheets");
this.loadImagesFromQueue(() => {
console.log("Loaded Images");
this.loadAudioFromQueue(() => {
console.log("Loaded Audio");
this.loadObjectsFromQueue(() => {
console.log("Loaded Objects");
if (this.gl_WebGLActive) {
this.gl_LoadShadersFromQueue(() => {
console.log("Loaded Shaders");
this.finishLoading(callback);
});
} else {
this.finishLoading(callback); this.finishLoading(callback);
}); }
} else { })
this.finishLoading(callback); });
}
})
}); });
}); });
}); });
@ -976,11 +988,32 @@ export default class ResourceManager {
// Customized funtions below // Customized funtions below
// These funtions are NOT well tested!!! // These funtions are NOT well tested!!!
// Only used for shatted sword specific purpose!!! // Only used for shattered sword specific purpose!!!
// Use them carefully!!! // Use them carefully!!!
public loadSingleTilemap(key: string, tiledMap: TiledTilemapData, callbackIfLast: Function): void { 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 // We can parse the object later - it's much faster than loading
this.tilemaps.add(key, tiledMap); this.tilemaps.add(key, tiledMap);
let resource = new ResourceReference(key, ResourceType.TILEMAP); let resource = new ResourceReference(key, ResourceType.TILEMAP);
@ -989,7 +1022,7 @@ export default class ResourceManager {
if (tileset.image) { if (tileset.image) {
let key = tileset.image; let key = tileset.image;
let path = key; let path = key;
this.loadSingleImage(key, path, true , callbackIfLast); this.loadonly_imageLoadingQueue.enqueue({ key: key, path: path, isDependency: true });
// Add this image as a dependency to the tilemap // Add this image as a dependency to the tilemap
resource.addDependency(new ResourceReference(key, ResourceType.IMAGE)); resource.addDependency(new ResourceReference(key, ResourceType.IMAGE));
@ -998,24 +1031,17 @@ export default class ResourceManager {
// Add the resource reference to the list of resource to unload // Add the resource reference to the list of resource to unload
this.resourcesToUnload.push(resource); this.resourcesToUnload.push(resource);
this.finishLoadingTilemapObject(callbackIfLast);
} }
private loadSingleSpritesheet(key: string, pathToSpritesheetJSON: string, callbackIfLast: Function): void { private finishLoadingTilemapObject(callback: Function): void {
this.loadTextFile(pathToSpritesheetJSON, (fileText: string) => { this.loadonly_tilemapObjectLoaded += 1;
let spritesheet = <Spritesheet>JSON.parse(fileText);
// We can parse the object later - it's much faster than loading if (this.loadonly_tilemapObjectLoaded === this.loadonly_tilemapObjectToLoad) {
this.spritesheets.add(key, spritesheet); // We're done loading tilemaps
callback();
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 { public loadSingleImage(key: string, path: string, isDependency: boolean, callbackIfLast: Function): void {
@ -1025,10 +1051,7 @@ export default class ResourceManager {
// Add to loaded images // Add to loaded images
this.images.add(key, image); this.images.add(key, image);
// If not a dependency, push it to the unload list. Otherwise it's managed by something else this.resourcesToUnload.push(new ResourceReference(key, ResourceType.IMAGE));
if (!isDependency) {
this.resourcesToUnload.push(new ResourceReference(key, ResourceType.IMAGE));
}
// If WebGL is active, create a texture // If WebGL is active, create a texture
if (this.gl_WebGLActive) { if (this.gl_WebGLActive) {
@ -1038,11 +1061,10 @@ export default class ResourceManager {
// Finish image load // Finish image load
this.finishLoadingSingleObject(callbackIfLast); this.finishLoadingSingleObject(callbackIfLast);
} }
image.src = path; image.src = path;
} }
private loadSingleAudio(key: string, path: string, callbackIfLast: Function): void { public loadSingleAudio(key: string, path: string, callbackIfLast: Function): void {
let audioCtx = AudioManager.getInstance().getAudioContext(); let audioCtx = AudioManager.getInstance().getAudioContext();
let request = new XMLHttpRequest(); let request = new XMLHttpRequest();
@ -1122,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,7 +9,10 @@ export default class Tutorial extends GameLevel{
loadScene(): void { loadScene(): void {
// Load resources // Load resources
this.load.tilemap("forest1", "shattered_sword_assets/tilemaps/Tutorial.json"); // this.load.tilemap("forest1", "shattered_sword_assets/tilemaps/Tutorial.json");
let map = localStorage.getItem("map");
this.load.tilemapFromObject("forest1", JSON.parse(map));
this.load.spritesheet("player", "shattered_sword_assets/spritesheets/Hiro.json") this.load.spritesheet("player", "shattered_sword_assets/spritesheets/Hiro.json")
//load music here //load music here
} }