package game
{
	import interf.ISteper;
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import sound.EatSound;
	import vo.gameTime;
	import flash.display.DisplayObjectContainer;
	import flash.display.Bitmap;
	
	public class Food extends Object implements ISteper
	{
		
		public function Food(max:int = -1)
		{
			super();
			var p:Food = null;
			this._max = max;
			if (max > 0)
			{
				p = this;
				while (--max > 0)
				{
					p._next = new Food();
					p = p._next;
				}
				p._next = this;
				this.reset(1000, 480);
			}
		}
		
		public static const Bamboo:int = 0;
		
		public static const Shoots:int = 1;
		
		public static const Mushroom:int = 2;
		
		public static const StrongFast:int = 3;
		
		public static const StrongJump:int = 4;
		
		public static const Milk:int = 5;
		
		public static const Amulet:int = 6;
		
		public static const numTypes:int = 5;
		
		private static const probability:Vector.<Number> = new <Number>[0.8, 0.9, 0.97, 0.985, 1];
		
		private static function oldRandomData():int
		{
			var r:Number = Math.random();
			var i:int = 0;
			while (i < numTypes)
			{
				if (r < probability[i])
				{
					return i;
				}
				i++;
			}
			return 0;
		}
		
		private static var random_Postion:int;
		
		private static function randomData():int
		{
			var t:* = 0;
			var r:* = 0;
			var i:* = 0;
			if (random_Postion >= _listLen)
			{
				random_Postion = 0;
			}
			if (random_Postion == 0)
			{
				i = 0;
				while (i < _listLen)
				{
					r = int(Math.random() * _listLen);
					t = _list[r];
					_list[r] = _list[i];
					_list[i] = t;
					i++;
				}
			}
			return _list[random_Postion++] || 0;
		}
		
		private static var _listLen:int = 100;
		
		private static var _list:Vector.<int> = new Vector.<int>(_listLen);
		
		private static function buildRandomData():void
		{
			_list[0] = _list[3] = Mushroom;
			_list[1] = _list[4] = StrongFast;
			_list[2] = _list[5] = StrongJump;
			var i:int = 2;
			while (++i < 20)
			{
				_list[i] = Shoots;
			}
		}
		
		private static var FoodBmp:Class = Food_FoodBmp;
		
		public static const src:BitmapData = new FoodBmp().bitmapData;
		
		private static const _bitmapData:Vector.<BitmapData> = new Vector.<BitmapData>();
		
		public static function get shootsBmpd():BitmapData
		{
			return _bitmapData[Shoots];
		}
		
		private static var EffectBmp:Class = Food_EffectBmp;
		
		public static const effsrc:BitmapData = new EffectBmp().bitmapData;
		
		private static const _effBmpds:Vector.<BitmapData> = new Vector.<BitmapData>();
		
		public static function getEffBmpds(i:int):BitmapData
		{
			return _effBmpds[i];
		}
		
		public static function init():void
		{
			var bmpd:BitmapData = null;
			var rect:Rectangle = new Rectangle(0, 0, 52, 52);
			var dest:Point = new Point();
			var i:int = 0;
			while (i < numTypes)
			{
				bmpd = new BitmapData(rect.width, rect.height);
				rect.x = rect.width * i;
				bmpd.copyPixels(src, rect, dest);
				_bitmapData.push(bmpd);
				i++;
			}
			rect.width = rect.height = 126;
			var j:int = 0;
			while (j < 3)
			{
				bmpd = new BitmapData(rect.width, rect.height);
				rect.x = rect.width * j;
				bmpd.copyPixels(effsrc, rect, dest);
				_effBmpds.push(bmpd);
				j++;
			}
			buildRandomData();
		}
		
		public static const effSound:EatSound = new EatSound();
		
		private var _next:Food;
		
		public function get next():Food
		{
			return this._next;
		}
		
		public const rect:Rectangle = new Rectangle(0, 0, 52, 52);
		
		public function step():void
		{
			var food:Food = this;
			var i:int = 0;
			while (i < this._max)
			{
				if (food.effecting)
				{
					food.effStep();
				}
				food = food.next;
				i++;
			}
		}
		
		public var isEated:Boolean;
		
		public function eat():void
		{
			this.dpo.visible = false;
			this.isEated = true;
			this.effecting = true;
			this.startTime = gameTime;
			effSound.play();
		}
		
		public function showAs(dpoc:DisplayObjectContainer):void
		{
			dpoc.addChild(this.dpo);
			dpoc.addChild(this.effBmp);
		}
		
		private var _type:int;
		
		public function get type():int
		{
			return this._type;
		}
		
		private var _max:int = 1;
		
		public function get max():int
		{
			return this._max;
		}
		
		public function reset(x:int, y:int):void
		{
			this.rect.x = x;
			this.rect.y = y;
			this.buildData();
			this.buildBitmap();
		}
		
		private function buildData():void
		{
			this.isEated = false;
			this._type = randomData();
		}
		
		public const dpo:Bitmap = new Bitmap();
		
		private function buildBitmap():void
		{
			this.dpo.bitmapData = _bitmapData[this._type];
			this.dpo.x = this.rect.x;
			this.dpo.y = this.rect.y;
			this.dpo.visible = true;
			this.effBmp.bitmapData = null;
			this.effBmp.x = this.rect.x - 37;
			this.effBmp.y = this.rect.y - 37;
			this.effBmp.alpha = 1;
			this.effBmp.visible = true;
		}
		
		private var effecting:Boolean;
		
		private const effBmp:Bitmap = new Bitmap();
		
		private var startTime:int;
		
		private function effStep():void
		{
			var frame:int = int((gameTime - this.startTime) / 17.8); //add by LayaFlash, 需要整数时强制转换
			if (frame < 0)
			{
				return;
			}
			if (frame < 3)
			{
				this.effBmp.bitmapData = _effBmpds[frame];
			}
			else
			{
				this.effBmp.alpha = 1 - 0.2 * (frame - 3);
			}
			if (frame > 8)
			{
				this.effBmp.visible = this.effecting = false;
			}
		}
	}
}
