mirror of
https://github.com/4ian/GDevelop.git
synced 2025-10-15 10:19:04 +00:00

* Only the first scene and global objects resources (images, sounds, 3D models etc...) will be downloaded during launch of the game. This usually allows for a very fast loading time. * Other scenes resources will continue to load in the background. It has no impact on the game performance as this is done on other threads by the browser or the engine running the game. * Scenes are loaded in the order they are listed in the project manager. * You can also use actions and expressions to prioritize a scene (if it's known that a level will be needed soon for example) or read the current loading progress. This allows to create lightweight scenes that can act as custom loading screens. Otherwise, the launch loading screen will be shown if a scene is still loading when launched. * Read more about this on https://wiki.gdevelop.io/gdevelop5/all-features/resources-loading/.
55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
/*
|
|
* GDevelop JS Platform
|
|
* Copyright 2013-present Florian Rival (Florian.Rival@gmail.com). All rights reserved.
|
|
* This project is released under the MIT License.
|
|
*/
|
|
namespace gdjs {
|
|
/**
|
|
* A cache of resources that helps ensuring that files are only downloaded
|
|
* once.
|
|
*/
|
|
export class ResourceCache<C> {
|
|
private _nameToContent = new Map<string, C>();
|
|
private _fileToContent = new Map<string, C>();
|
|
|
|
constructor() {}
|
|
|
|
/**
|
|
* Gives a fast access to asset content when they were pre-loaded and
|
|
* on-the-fly loading is not allowed.
|
|
*/
|
|
getFromName(name: string): C | null {
|
|
return this._nameToContent.get(name) || null;
|
|
}
|
|
|
|
get(resource: ResourceData): C | null {
|
|
let existingContent = this._nameToContent.get(resource.name);
|
|
if (existingContent) {
|
|
return existingContent;
|
|
}
|
|
// When several assets use the same file, it avoids to download it again.
|
|
existingContent = this._fileToContent.get(resource.file);
|
|
if (existingContent) {
|
|
this._nameToContent.set(resource.name, existingContent);
|
|
return existingContent;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
set(resource: ResourceData, content: C) {
|
|
this._nameToContent.set(resource.name, content);
|
|
this._fileToContent.set(resource.file, content);
|
|
}
|
|
|
|
delete(resource: ResourceData) {
|
|
this._nameToContent.delete(resource.name);
|
|
this._fileToContent.delete(resource.file);
|
|
}
|
|
|
|
clear() {
|
|
this._nameToContent.clear();
|
|
this._fileToContent.clear();
|
|
}
|
|
}
|
|
}
|