Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package foo.bar;
- typedef AssetDescriptor =
- {
- var id: String;
- var type: AssetType;
- }
- enum AssetType
- {
- sound;
- music;
- text;
- font;
- image;
- spriteSheet;
- }
- private typedef Asset =
- {
- var asset: Dynamic;
- var locked: Bool;
- }
- /**
- *
- */
- class AssetManager
- {
- static var instance: AssetManager;
- var loadedAssets: Hash<Asset>;
- public var pendingAssets(default, null): Hash<Asset>;
- function new()
- {
- loadedAssets = new Hash<Asset>();
- pendingAssets = new Hash<Asset>();
- }
- public static function createInstance()
- {
- if (instance != null)
- throw "The current instance must first be released with releaseInstance().";
- }
- public static function releaseInstance()
- {
- if (instance == null)
- throw "No instance was previously created.";
- }
- public static function getInstance()
- {
- if (instance == null)
- throw "An instance must first be created with createInstance().";
- return instance;
- }
- public function preloadAsset(descriptor)
- {
- if (loadedAssets.exists(descriptor.id) == true
- || pendingAssets.exists(descriptor.id) == true)
- {
- #if debug
- trace("preloadAsset: asset " + descriptor.id + " already loaded or pending.");
- #end
- return;
- }
- #if debug
- trace("preloadAsset: preloading asset " + descriptor.id + ".");
- #end
- // Load the asset based on its type
- //
- }
- public function preloadAssets(assets : Array<AssetDescriptor>)
- {
- for (descriptor in assets)
- preloadAsset(descriptor);
- }
- public function releaseAsset(descriptor)
- {
- if (loadedAssets.remove(descriptor.id) == false)
- {
- #if debug
- trace("releaseAsset: asset " + descriptor.id + " was not loaded.");
- #end
- }
- if (pendingAssets.remove(descriptor.id) == true)
- {
- #if debug
- trace("releaseAsset: asset " + descriptor.id + " was pending.");
- #end
- }
- }
- public function releaseAssets(assets: Array<AssetDescriptor>)
- {
- for (descriptor in assets)
- releaseAsset(descriptor);
- }
- public function getAsset(id)
- {
- return loadedAssets.get(id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement