Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package otherlib.generic_girl {
  2.     import flash.display.Sprite;
  3.     import otherlib.core.DisplayFrame;
  4. /*
  5.  *  we need a variation on the singleton pattern.
  6.  *  Instead of a single dataset shared between all class instance
  7.  *  we need for each archetype to share a set of data
  8.  *
  9.  *  so we implemnt this by taking the dataset (which is just an array)
  10.  *  and storing it in a static Object instance, indexed by arch name
  11.  */
  12.     public class MinipicManager {
  13.         static var archetypes:Object = {};
  14.  
  15.         public function MinipicManager() {
  16.         }
  17.  
  18. /*
  19.  *      get the array for this archetype
  20.  *      if one doesn't exist yet, create it,
  21.  *      and add it to the object, indexed by arch
  22.  */
  23.         function get_list(arch:String):Array
  24.         {
  25.             var list:Array = archetypes[arch];
  26.             if(list == null) {
  27.                 list = [];
  28.                 archetypes[arch] = list;
  29.             }
  30.             return list;
  31.         }
  32.  
  33.         public function add(base:String, arch:String, path:String)
  34.         {
  35.             var list:Array = get_list(arch);
  36.  
  37.             var mc:Sprite = new DisplayFrame(50,50);
  38.             list.push({
  39.                 sprite  : mc,
  40.                 used    : false
  41.             });
  42.             var pi = new PictureImage({ path : base + "/" + path });
  43.             pi.make_mc(0, mc);
  44.         }
  45.        
  46.         public function release(arch:String, incoming:Sprite)
  47.         {
  48.             var list:Array = get_list(arch);
  49.  
  50.             for each (var o in list) {
  51. /*
  52.  *              "===" is strict equivalence.
  53.  *              not only must the objects reduce to the
  54.  *              same value, they must be the same instance.
  55.  *
  56.  *              is there a !== form? I'm about to find out
  57.  */
  58.                 if(incoming !== o.sprite)  {
  59.                     continue;
  60.                 }
  61.                 o.used = false;
  62.             }
  63.         }
  64.         public function next_pic(arch):Sprite
  65.         {
  66.             var count = 0;
  67.             var list:Array = get_list(arch);
  68.  
  69.             for each (var o in list) {
  70.                 if(o.used) {
  71.                     count ++;
  72.                     continue;
  73.                 }
  74.                 o.used = true;
  75.                 // "sprite" is actually a dispaly frame at the moment
  76.                 o.sprite.id = arch + ":" + count;
  77.                 return o.sprite;
  78.             }
  79.             throw new Error("No free pics for archetype '" + arch + "'");
  80.             return null;
  81.         }
  82.     }
  83.    
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement