package game
{
   import interf.ISteper;
   import flash.display.BitmapData;
   import flash.geom.Rectangle;
   import flash.geom.Point;
   import flash.display.Bitmap;
   import flash.display.DisplayObjectContainer;
   import vo.gameTime;
   
   public class Smoke extends Object implements ISteper
   {
      
      public function Smoke(max:uint = 1, frist:Boolean = true)
      {
         super();
         var p:Smoke = null;
         this.dpo = new Bitmap(src);
         this._max = max;
         if(frist)
         {
            p = this;
            while(--max > 0)
            {
               p._next = new Smoke(max,false);
               p = p._next;
            }
            p._next = this;
         }
      }
      
      private static var BoardBmp:Class = Smoke_BoardBmp;
      
      public static const src:BitmapData = new BoardBmp().bitmapData;
      
      private static var frame:Vector.<BitmapData> = new Vector.<BitmapData>();
      
      public static function init() : void
      {
         var bmpd:BitmapData = null;
         var rect:Rectangle = new Rectangle(0,0,93,24);
         var dest:Point = new Point();
         var i:int = 0;
         while(i < 5)
         {
            bmpd = new BitmapData(rect.width,rect.height);
            rect.x = rect.width * i;
            bmpd.copyPixels(src,rect,dest);
            frame.push(bmpd);
            i++;
         }
      }
      
      private var _next:Smoke;
      
      public function get next() : Smoke
      {
         return this._next;
      }
      
      private var _max:int;
      
      public function get max() : int
      {
         return this._max;
      }
      
      private var dpo:Bitmap;
      
      public function showAs(dpoc:DisplayObjectContainer) : void
      {
         this.dpo.visible = false;
         dpoc.addChild(this.dpo);
      }
      
      private var startTime:int;
      
      private var running:Boolean;
      
      public function show(x:int, y:int) : void
      {
         this.startTime = gameTime;
         this.dpo.bitmapData = frame[0];
         this.dpo.x = x - 50;
         this.dpo.y = y - this.dpo.height;
         this.dpo.visible = true;
      }
      
      private var a:int = 0;
      
      public function step() : void
      {
         var f:* = 0;
         var smoke:Smoke = this;
         var i:int = 0;
         while(i < this._max)
         {
            if(smoke.dpo.visible)
            {
               f = int((gameTime - smoke.startTime) / 50);//add by LayaFlash, 需要整数时强制转换
               if(f > 3)
               {
                  smoke.dpo.visible = false;
               }
               else
               {
                  smoke.dpo.bitmapData = frame[f];
               }
            }
            smoke = smoke._next;
            i++;
         }
      }
   }
}
