package game
{
   import interf.ISteper;
   import flash.display.Bitmap;
   import flash.display.BitmapData;
   import flash.utils.getTimer;
   import vo.frameDelay;
   import vo.gameTime;
   import flash.display.DisplayObjectContainer;
   import flash.display.Shape;
   import flash.events.Event;
   import flash.geom.Rectangle;
   import flash.system.Capabilities;
   import flash.geom.Point;
   
   public class Ground extends Object implements ISteper
   {
      
      public function Ground()
      {
         var normalSrc:BitmapData = null;
         var blurSrc:BitmapData = null;
         var rect:Rectangle = null;
         this.ylist = new <int>[210,250,370,220,460,525,500];
         this._e = new Shape();
         super();
         var str:String = Capabilities.version.substr(0,3);
         this.type = str == "IOS"?ios:android;
         var dest:Point = new Point();
         normalSrc = new BG1Bmp().bitmapData;
         blurSrc = new BG1Bmp2().bitmapData;
         this.bg1_normal = new BitmapData(normalSrc.width + 800,normalSrc.height);
         this.bg1_blur = new BitmapData(normalSrc.width + 800,normalSrc.height);
         rect = normalSrc.rect;
         dest.x = 0;
         this.bg1_normal.copyPixels(normalSrc,rect,dest);
         this.bg1_blur.copyPixels(blurSrc,rect,dest);
         dest.x = normalSrc.width;
         this.bg1_normal.copyPixels(normalSrc,rect,dest);
         this.bg1_blur.copyPixels(blurSrc,rect,dest);
         this.bg1 = new Bitmap(this.bg1_normal);
         normalSrc.dispose();
         blurSrc.dispose();
         normalSrc = new BG2Bmp().bitmapData;
         blurSrc = new BG2Bmp2().bitmapData;
         this.bg2_normal = new BitmapData(normalSrc.width + 800,normalSrc.height);
         this.bg2_blur = new BitmapData(normalSrc.width + 800,normalSrc.height);
         rect = normalSrc.rect;
         dest.x = 0;
         this.bg2_normal.copyPixels(normalSrc,rect,dest);
         this.bg2_blur.copyPixels(blurSrc,rect,dest);
         dest.x = normalSrc.width;
         this.bg2_normal.copyPixels(normalSrc,rect,dest);
         this.bg2_blur.copyPixels(blurSrc,rect,dest);
         this.bg2 = new Bitmap(this.bg2_normal);
         normalSrc.dispose();
         blurSrc.dispose();
         normalSrc = new BG3Bmp().bitmapData;
         blurSrc = new BG3Bmp2().bitmapData;
         this.bg3_normal = new BitmapData(normalSrc.width + 800,normalSrc.height);
         this.bg3_blur = new BitmapData(normalSrc.width + 800,normalSrc.height);
         rect = normalSrc.rect;
         dest.x = 0;
         this.bg3_normal.copyPixels(normalSrc,rect,dest);
         this.bg3_blur.copyPixels(blurSrc,rect,dest);
         dest.x = normalSrc.width;
         this.bg3_normal.copyPixels(normalSrc,rect,dest);
         this.bg3_blur.copyPixels(blurSrc,rect,dest);
         this.bg3 = new Bitmap(this.bg3_normal);
         normalSrc.dispose();
         blurSrc.dispose();
         this.bg4_normal = new Tree1Bmp().bitmapData;
         this.bg4_blur = new Tree1Bmp2().bitmapData;
         this.bg4 = new Bitmap(this.bg4_normal);
         normalSrc = new Tree2Bmp().bitmapData;
         blurSrc = new Tree2Bmp2().bitmapData;
         this.bg5_normal = new BitmapData(1400,normalSrc.height,true,0);
         this.bg5_blur = new BitmapData(1400,normalSrc.height,true,0);
         rect = normalSrc.rect;
         var i:int = 0;
         while(i < 4)
         {
            dest.x = 350 * i;
            this.bg5_normal.copyPixels(normalSrc,rect,dest,null,null,true);
            this.bg5_blur.copyPixels(blurSrc,rect,dest,null,null,true);
            i++;
         }
         this.bg5 = new Bitmap(this.bg5_normal);
         normalSrc.dispose();
         blurSrc.dispose();
         normalSrc = new GrassBmp().bitmapData;
         blurSrc = new GrassBmp2().bitmapData;
         this.fg1_normal = new BitmapData(normalSrc.width * 3,normalSrc.height);
         this.fg1_blur = new BitmapData(normalSrc.width * 3,normalSrc.height);
         rect = normalSrc.rect;
         i = 0;
         while(i < 3)
         {
            dest.x = normalSrc.width * i;
            this.fg1_normal.copyPixels(normalSrc,rect,dest);
            this.fg1_blur.copyPixels(blurSrc,rect,dest);
            i++;
         }
         this.fg1 = new Bitmap(this.fg1_normal);
         normalSrc.dispose();
         blurSrc.dispose();
         normalSrc = new FlowerBmp().bitmapData;
         blurSrc = new FlowerBmp2().bitmapData;
         this.fg2_normal = new BitmapData(normalSrc.width * 3,normalSrc.height);
         this.fg2_blur = new BitmapData(normalSrc.width * 3,normalSrc.height);
         rect = normalSrc.rect;
         i = 0;
         while(i < 3)
         {
            dest.x = normalSrc.width * i;
            this.fg2_normal.copyPixels(normalSrc,rect,dest);
            this.fg2_blur.copyPixels(blurSrc,rect,dest);
            i++;
         }
         this.fg2 = new Bitmap(this.fg2_normal);
         normalSrc.dispose();
         blurSrc.dispose();
         this.initY();
      }
      
      private static var BG1Bmp2:Class = Ground_BG1Bmp2;
      
      private static var BG2Bmp2:Class = Ground_BG2Bmp2;
      
      private static var BG3Bmp2:Class = Ground_BG3Bmp2;
      
      private static var Tree1Bmp2:Class = Ground_Tree1Bmp2;
      
      private static var Tree2Bmp2:Class = Ground_Tree2Bmp2;
      
      private static var GrassBmp2:Class = Ground_GrassBmp2;
      
      private static var FlowerBmp2:Class = Ground_FlowerBmp2;
      
      private static var BG1Bmp:Class = Ground_BG1Bmp;
      
      private static var BG2Bmp:Class = Ground_BG2Bmp;
      
      private static var BG3Bmp:Class = Ground_BG3Bmp;
      
      private static var Tree1Bmp:Class = Ground_Tree1Bmp;
      
      private static var Tree2Bmp:Class = Ground_Tree2Bmp;
      
      private static var GrassBmp:Class = Ground_GrassBmp;
      
      private static var FlowerBmp:Class = Ground_FlowerBmp;
      
      public static const android:int = 1;
      
      public static const ios:int = 2;
      
      private var bg1:Bitmap;
      
      private var bg2:Bitmap;
      
      private var bg3:Bitmap;
      
      private var bg4:Bitmap;
      
      private var bg5:Bitmap;
      
      private var fg1:Bitmap;
      
      private var fg2:Bitmap;
      
      private var ylist:Vector.<int>;
      
      private var bg1_normal:BitmapData;
      
      private var bg2_normal:BitmapData;
      
      private var bg3_normal:BitmapData;
      
      private var bg4_normal:BitmapData;
      
      private var bg5_normal:BitmapData;
      
      private var fg1_normal:BitmapData;
      
      private var fg2_normal:BitmapData;
      
      private var bg1_blur:BitmapData;
      
      private var bg2_blur:BitmapData;
      
      private var bg3_blur:BitmapData;
      
      private var bg4_blur:BitmapData;
      
      private var bg5_blur:BitmapData;
      
      private var fg1_blur:BitmapData;
      
      private var fg2_blur:BitmapData;
      
      private function initY() : void
      {
         this.bg1.y = this.ylist[0];
         this.bg2.y = this.ylist[1];
         this.bg3.y = this.ylist[2];
         this.bg4.y = this.ylist[3];
         this.bg5.y = this.ylist[4];
         this.fg1.y = this.ylist[5];
         this.fg2.y = this.ylist[6];
      }
      
      public function bulr() : void
      {
         this.blur = true;
         this.bg1.bitmapData = this.bg1_blur;
         this.bg2.bitmapData = this.bg2_blur;
         this.bg3.bitmapData = this.bg3_blur;
         this.bg4.bitmapData = this.bg4_blur;
         this.bg5.bitmapData = this.bg5_blur;
         this.fg1.bitmapData = this.fg1_blur;
         this.fg2.bitmapData = this.fg2_blur;
      }
      
      private var blur:Boolean;
      
      public function normal() : void
      {
         if(this.blur)
         {
            this.blur = false;
            this.bg1.bitmapData = this.bg1_normal;
            this.bg2.bitmapData = this.bg2_normal;
            this.bg3.bitmapData = this.bg3_normal;
            this.bg4.bitmapData = this.bg4_normal;
            this.bg5.bitmapData = this.bg5_normal;
            this.fg1.bitmapData = this.fg1_normal;
            this.fg2.bitmapData = this.fg2_normal;
            return;
         }
      }
      
      public function step() : void
      {
         this.updateX();
         this.updateY();
      }
      
      public var scene:Scene;
      
      private function updateY() : void
      {
         if(this.scene.dpoc.y >= 0)
         {
            this.bg1.y = this.ylist[0] + this.scene.dpoc.y / 4;
            this.bg2.y = this.ylist[1] + this.scene.dpoc.y / 4;
            this.bg3.y = this.ylist[2] + this.scene.dpoc.y / 4;
            this.bg4.y = this.ylist[3] + this.scene.dpoc.y / 2;
            this.bg5.y = this.ylist[4] + this.scene.dpoc.y / 2;
            this.fg1.y = this.ylist[5] + this.scene.dpoc.y;
            this.fg2.y = this.ylist[6] + this.scene.dpoc.y;
         }
         else if(this.type == android)
         {
            this.bg1.y = this.ylist[0] + this.scene.dpoc.y;
            this.bg2.y = this.ylist[1] + this.scene.dpoc.y;
            this.bg3.y = this.ylist[2] + this.scene.dpoc.y;
            this.bg4.y = this.ylist[3] + this.scene.dpoc.y;
            this.bg5.y = this.ylist[4] + this.scene.dpoc.y;
            this.fg1.y = this.ylist[5] + this.scene.dpoc.y;
            this.fg2.y = this.ylist[6] + this.scene.dpoc.y;
         }
         
      }
      
      public var type:int = 1;
      
      private var delay:Number;
      
      private var prevTime:int;
      
      public var auto:Boolean = true;
      
      private function updateX() : void
      {
         var currTime:int = getTimer();
         this.delay = (currTime - this.prevTime) / 1000;
         this.prevTime = currTime;
         if(!this.auto)
         {
            this.delay = frameDelay;
         }
         this.bg1.x = this.bg1.x - this.delay * 30;
         if(this.bg1.x < 800 - this.bg1.width)
         {
            this.bg1.x = 0;
         }
         this.bg2.x = this.bg2.x - this.delay * 60;
         if(this.bg2.x < 800 - this.bg2.width)
         {
            this.bg2.x = 0;
         }
         this.bg3.x = this.bg3.x - this.delay * 120;
         if(this.bg3.x < 800 - this.bg3.width)
         {
            this.bg3.x = 0;
         }
         this.bg4.x = this.bg4.x - this.delay * 150;
         if(this.bg4.x < -500)
         {
            this.bg4.x = 800;
         }
         this.bg5.x = this.bg5.x - this.delay * 180;
         if(this.bg5.x < -400)
         {
            this.bg5.x = -50;
         }
         this.fg1.x = this.fg1.x - this.delay * 400;
         if(this.fg1.x < -522)
         {
            this.fg1.x = 0;
         }
         this.fg2.x = this.fg2.x - this.delay * 500;
         if(this.fg2.x < -464)
         {
            this.fg2.x = 0;
         }
      }
      
      private var sound1playing:Boolean;
      
      private var sound2playing:Boolean;
      
      private function sound() : void
      {
         if(gameTime - this.prevTime > 5000)
         {
            if(!this.sound1playing)
            {
               this.sound1playing = true;
            }
            this.prevTime = gameTime;
         }
      }
      
      public function showAs(dpoc:DisplayObjectContainer) : void
      {
         if(this.type == android)
         {
            dpoc.addChild(this.bg1);
            dpoc.addChild(this.bg2);
            dpoc.addChild(this.bg3);
            dpoc.addChild(this.bg4);
            dpoc.addChild(this.bg5);
            dpoc.addChild(this.fg1);
            dpoc.addChild(this.fg2);
         }
         else
         {
            dpoc.addChild(this.bg2);
         }
      }
      
      private var _max:int = 1;
      
      public function get max() : int
      {
         return this._max;
      }
      
      private var _e:Shape;
      
      public function startAlone() : void
      {
         this._e.addEventListener(Event.ENTER_FRAME,this.stepAlone);
         this.prevTime = getTimer();
         this.initY();
         this.normal();
      }
      
      private function stepAlone(p:* = null) : void
      {
         this.updateX();
      }
      
      public function stopAlone() : void
      {
         this._e.removeEventListener(Event.ENTER_FRAME,this.stepAlone);
      }
   }
}
