package game
{
   import interf.ISteper;
   import flash.display.BitmapData;
   import flash.display.Sprite;
   import vo.gameTime;
   import tdh.utils.BitmapClip;
   
   public class Scene extends Object implements ISteper
   {
      
      public function Scene()
      {
         super();
      }
      
      private static const SpdUpBmp:Class = Scene_SpdUpBmp;
      
      public static const spdUpBmpd:BitmapData = new SpdUpBmp().bitmapData;
      
      public var panda:Panda;
      
      public var enemy:Enemy;
      
      public var board:Board;
      
      public var food:Food;
      
      public const dpoc:Sprite = new Sprite();
      
      public const width:int = 800;
      
      public const height:int = 480;
      
      private var updateBroad:Function;
      
      public function step() : void
      {
         if(this.panda.rect.y < 80)
         {
            this.dpoc.y = 80 - this.panda.rect.y;
         }
         else if(this.panda.rect.y > 325)
         {
            this.dpoc.y = 325 - this.panda.rect.y;
            if(this.dpoc.y < -185)
            {
               this.dpoc.y = -185;
            }
         }
         else
         {
            this.dpoc.y = 0;
         }
         
         if(this.panda.rect.x > 180)
         {
            this.dpoc.x = 180 - this.panda.rect.x;
         }
         this.updateBroad();
      }
      
      public function nextBoard() : Board
      {
         this.board.next.reset(this.board.right,10);
         return this.board = this.board.next;
      }
      
      public function nextBoardForHelp() : Board
      {
         this.board.next.resetForHelp(this.board.right,this.board.rect.x);
         return this.board = this.board.next;
      }
      
      private var _max:int = 1;
      
      public function get max() : int
      {
         return this._max;
      }
      
      public function get right() : int
      {
         return 800 - this.dpoc.x;
      }
      
      private function updateBroadForRandom() : void
      {
         var w:* = 0;
         var x2:* = 0;
         var x1:* = 0;
         var y:* = 0;
         if(this.board.rect.right < this.right)
         {
            w = int(Math.random() * (Board.limit - 1)) + 1;
            this.board.next.reset(this.board.right,w);
            this.board = this.board.next;
            if(Math.random() < 0.1 + gameTime / 300000)
            {
               x2 = this.board.rect.x + int(Math.random() * (this.board.rect.width - this.enemy.rect.width));
               this.enemy.next.reset(x2,this.board.rect.y);
               this.enemy = this.enemy.next;
               this.panda.leftEnemy = this.enemy.next;
               this.board.enemy = this.enemy;
            }
            else
            {
               this.board.enemy = null;
            }
         }
         if(this.food.rect.right < this.right)
         {
            x1 = this.food.rect.x + 100 + int(Math.random() * 8) * 50;
            y = int(Math.random() * 3) * 50;
            this.food.next.reset(x1,this.board.rect.y - 100 - y);
            this.food = this.food.next;
         }
      }
      
      private function updateBroadForEasy() : void
      {
      }
      
      public function normalUpdate() : void
      {
         this.updateBroad = this.updateBroadForRandom;
      }
      
      public function updateForHelp(level:int) : void
      {
         if((level < 1) || (level > 10))
         {
            return;
         }
         this.updateBroad = this["updateBroadForHelp_" + level];
      }
      
      private function updateBroadForHelp_1() : void
      {
         if(this.board.rect.right < this.right)
         {
            this.board.next.resetForHelp(this.board.rect.right,this.board.rect.y);
            this.board = this.board.next;
            this.board.enemy = null;
         }
      }
      
      private function updateBroadForHelp_2() : void
      {
         if(this.board.rect.right < this.right)
         {
            this.board.next.resetForHelp(this.board.rect.right + 150,this.board.rect.y);
            this.board = this.board.next;
            this.board.enemy = null;
         }
      }
      
      private function updateBroadForHelp_3() : void
      {
         if(this.board.rect.right < this.right)
         {
            this.board.next.resetForHelp(this.board.rect.right + 500,this.board.rect.y);
            this.board = this.board.next;
            this.board.enemy = null;
         }
      }
      
      private function updateBroadForHelp_4() : void
      {
         if(this.board.rect.right < this.right)
         {
            this.board.next.resetForHelp(this.board.rect.right + 1000,this.board.rect.y);
            this.board = this.board.next;
            this.board.enemy = null;
         }
      }
      
      private function updateBroadForHelp_5() : void
      {
         if(this.board.rect.right < this.right)
         {
            this.board.next.resetForHelp(this.board.rect.right + 150,this.board.rect.y);
            this.board = this.board.next;
            this.enemy.next.resetForHelp(this.board.rect.x,this.board.rect.y);
            this.enemy = this.enemy.next;
            this.panda.leftEnemy = this.enemy.next;
            this.board.enemy = this.enemy;
         }
      }
      
      private var bmpc:BitmapClip;
      
      public function buildSpdUpBmp() : void
      {
         this.bmpc = new BitmapClip(spdUpBmpd,4,7,32);
      }
      
      public function speedUp(x:int) : void
      {
         this.bmpc.y = 155;
         this.bmpc.x = x;
         this.bmpc.playOnce();
         this.dpoc.addChild(this.bmpc);
      }
   }
}
