package game.GameMain.LevelTiles
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flinjin.Flinjin;
	import flinjin.graphics.Sprite;
	import flinjin.sound.FlinjinSound;
	import flinjin.sound.FlinjinSoundCollection;
	import game.GameMain.LevelScroll;
	import game.GameMain.LevelTiles.LevelParts.Leaf;
	
	/**
	 * ...
	 * @author Michael Miriti
	 */
	public class Tile4 extends LevelTile
	{
		[Embed(source="../../../assets/level-tiles/4_lvl.png")]
		private static var _backBmp:Class;
		
		[Embed(source="../../../assets/level-tiles/4_mask.png")]
		private static var _mask:Class;
		
		[Embed(source="../../../assets/level-tiles/4_mask-2.png")]
		private static var _mask2:Class;
		
		private var _maskBmp:Bitmap;
		
		private var _mask2Bmp:Bitmap;
		
		[Embed(source="../../../assets/level-tiles/head.png")]
		private static var _woodHead:Class;
		
		private var _woodPeckerHead:Sprite;
		
		private var _knockTimer:int = 0;
		private var _knockCount:int = 0;
		private var _knockState:Boolean;
		
		private var _leaf:Leaf = new Leaf();
		
		private var _snd:FlinjinSound;
		
		override public function Dispose():void
		{
			(_parentSprite as LevelScroll).addRandomTile();
			super.Dispose();
		}
		
		override public function getPixelAt(nx:int, ny:int):uint
		{
			if (_knockTimer != 0)
			{
				Mask = _mask2Bmp.bitmapData;
			}
			else
			{
				Mask = _maskBmp.bitmapData;
			}
			return super.getPixelAt(nx, ny);
		}
		
		override public function InitMobs():void
		{
			// 420 275
			_leaf.y = y + 275;
			
			if (_flipHorizontal)
			{
				
				_leaf.x = width - 420;
				_leaf.flipHorizontal = false;
			}
			else
			{
				_leaf.x = 420 - _leaf.width;
				_leaf.flipHorizontal = true;
			}
			
			(_parentSprite as LevelScroll).addTile(_leaf, true);
			super.InitMobs();
		}
		
		override public function Move():void
		{
			if (_knockCount < 3)
			{
				if (_knockState)
				{
					if (_woodPeckerHead.angle > -Math.PI / 30)
					{
						_woodPeckerHead.angle -= 0.5;
					}
					else
					{
						_snd.play();
						_knockState = false;
					}
				}
				else
				{
					if (_woodPeckerHead.angle < Math.PI / 12)
					{
						_woodPeckerHead.angle += 0.5;
					}
					else
					{
						_knockCount++;
						_knockState = true;
					}
				}
			}
			else
			{
				_knockTimer++;
				
				if (_knockTimer == 66)
				{
					_knockTimer = 0;
					_knockCount = 0;
				}
			}
			super.Move();
		}
		
		public function Tile4()
		{
			super(Flinjin.sceneWidth, Flinjin.sceneHeight);
			
			_woodPeckerHead = new Sprite(new _woodHead() as Bitmap, new Point(90, 125));
			_woodPeckerHead.angle = Math.PI / 6;
			addSprite(_woodPeckerHead, 150, 250);
			
			Bmp = new Sprite(new _backBmp() as Bitmap);
			addSprite(Bmp);
			
			_maskBmp = new _mask() as Bitmap;
			Mask = _maskBmp.bitmapData;
			
			_mask2Bmp = new _mask2() as Bitmap;
			
			_snd = FlinjinSoundCollection.getSound("wood");
			_snd.volume = 0.5;
		}
	
	}

}