Advertisement
Guest User

Untitled

a guest
Oct 6th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package foo.bar;
  2.  
  3. typedef AssetDescriptor =
  4. {
  5.     var id: String;
  6.     var type: AssetType;
  7. }
  8.  
  9. enum AssetType
  10. {
  11.     sound;
  12.     music;
  13.     text;
  14.     font;
  15.     image;
  16.     spriteSheet;
  17. }
  18.  
  19. private typedef Asset =
  20. {
  21.     var asset: Dynamic;
  22.     var locked: Bool;
  23. }
  24.  
  25. /**
  26.  *
  27.  */
  28. class AssetManager
  29. {
  30.     static var instance: AssetManager;
  31.    
  32.     var loadedAssets: Hash<Asset>;
  33.    
  34.     public var pendingAssets(default, null): Hash<Asset>;
  35.        
  36.     function new()
  37.     {
  38.         loadedAssets = new Hash<Asset>();
  39.         pendingAssets = new Hash<Asset>();
  40.     }
  41.    
  42.     public static function createInstance()
  43.     {
  44.         if (instance != null)
  45.             throw "The current instance must first be released with releaseInstance().";
  46.     }
  47.    
  48.     public static function releaseInstance()
  49.     {
  50.         if (instance == null)
  51.             throw "No instance was previously created.";
  52.     }
  53.        
  54.     public static function getInstance()
  55.     {
  56.         if (instance == null)
  57.             throw "An instance must first be created with createInstance().";
  58.        
  59.         return instance;
  60.     }
  61.    
  62.     public function preloadAsset(descriptor)
  63.     {
  64.         if (loadedAssets.exists(descriptor.id) == true
  65.                 || pendingAssets.exists(descriptor.id) == true)
  66.         {
  67. #if debug
  68.             trace("preloadAsset: asset " + descriptor.id + " already loaded or pending.");
  69. #end
  70.             return;
  71.         }
  72.  
  73. #if debug
  74.         trace("preloadAsset: preloading asset " + descriptor.id + ".");
  75. #end
  76.    
  77.         // Load the asset based on its type
  78.         //
  79.        
  80.        
  81.     }
  82.    
  83.     public function preloadAssets(assets : Array<AssetDescriptor>)
  84.     {
  85.         for (descriptor in assets)
  86.             preloadAsset(descriptor);
  87.     }
  88.    
  89.     public function releaseAsset(descriptor)
  90.     {
  91.         if (loadedAssets.remove(descriptor.id) == false)
  92.         {
  93. #if debug
  94.             trace("releaseAsset: asset " + descriptor.id + " was not loaded.");
  95. #end            
  96.         }
  97.        
  98.         if (pendingAssets.remove(descriptor.id) == true)
  99.         {
  100. #if debug
  101.             trace("releaseAsset: asset " + descriptor.id + " was pending.");
  102. #end            
  103.         }
  104.     }
  105.    
  106.     public function releaseAssets(assets: Array<AssetDescriptor>)
  107.     {
  108.         for (descriptor in assets)
  109.             releaseAsset(descriptor);
  110.     }
  111.    
  112.     public function getAsset(id)
  113.     {
  114.         return loadedAssets.get(id);
  115.     }
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement