package game
{
   import interf.ISteper;
   import flash.display.Bitmap;
   import flash.geom.Rectangle;
   import flash.geom.Point;
   import vo.frameDelay;
   import vo.Mem;
   import flash.display.DisplayObjectContainer;
   
   public class Shoots extends Object implements ISteper
   {
      
      public function Shoots(max:int = 1)
      {
         var p:Shoots = null;
         super();
         this._max = max;
         if(max > 0)
         {
            p = this;
            while(--max > 0)
            {
               p._next = new Shoots();
               p = p._next;
            }
            p._next = this;
         }
         this.dpo.visible = false;
      }
      
      public const dpo:Bitmap = new Bitmap(Food.shootsBmpd);
      
      private var _max:int = 1;
      
      public function get max() : int
      {
         return this._max;
      }
      
      private var _next:Shoots;
      
      public function get next() : Shoots
      {
         return this._next;
      }
      
      private const rect:Rectangle = new Rectangle(0,0,52,52);
      
      private const speed_conf:Point = new Point(350,-600);
      
      private const speed:Point = new Point();
      
      private var panda:Panda;
      
      private var target:Enemy;
      
      private const acc:Number = 2000;
      
      public var running:Boolean;
      
      public var enemy:Enemy;
      
      public function start(p1:Panda, p2:Enemy = null) : void
      {
         var t:* = NaN;
         this.panda = p1;
         this.target = p2;
         this.running = this.dpo.visible = true;
         this.dpo.rotation = 0;
         this.speed.x = this.panda.speed.x + this.speed_conf.x;
         this.rect.x = this.dpo.x = p1.rect.x;
         this.rect.y = this.dpo.y = p1.rect.y;
         if(p2 != null)
         {
            t = (p2.rect.x - p1.rect.x) / this.speed.x;
            this.speed.y = (p2.rect.y - p1.rect.y - 0.5 * this.acc * t * t) / t;
         }
         else
         {
            this.speed.y = this.panda.speed.y + this.speed_conf.y;
         }
      }
      
      public function step() : void
      {
         var shoots:Shoots = this;
         var i:int = 0;
         while(i < this._max)
         {
            if(shoots.running)
            {
               shoots.physics();
            }
            shoots = shoots.next;
            i++;
         }
      }
      
      public function physics() : void
      {
         this.speed.y = this.speed.y + frameDelay * this.acc;
         this.dpo.y = this.rect.y = this.rect.y + this.speed.y * frameDelay;
         this.dpo.x = this.rect.x = this.rect.x + this.speed.x * frameDelay;
         this.dpo.rotation = this.dpo.rotation + 180 * frameDelay;
         var i:int = 0;
         while(i < Mem.numEnemys)
         {
            if(this.rect.y > 600)
            {
               this.running = false;
               return;
            }
            if((this.rect.intersects(this.enemy.rect)) && (!this.enemy.isBruised))
            {
               this.running = this.dpo.visible = false;
               this.enemy.clear();
               return;
            }
            this.enemy = this.enemy.next;
            i++;
         }
      }
      
      public function showAs(dpoc:DisplayObjectContainer) : void
      {
         dpoc.addChild(this.dpo);
      }
   }
}
