package  
{
	import net.flashpunk.FP;
	import  net.flashpunk.Entity;
	import net.flashpunk.graphics.Spritemap;
	
	/**
	 * ...
	 * @author AntoineGuerchais
	 */
	public class Bud extends Entity
	{
		[Embed(source = '../assets/graphics/buds.png')]
		private const BudAnim:Class;

		private var parent:Branch;
		private var haveChild:Boolean;
		private var anim:Spritemap;
		
		public function Bud(parent:Branch) 
		{
			this.parent = parent;
			
			graphic = anim = new Spritemap(BudAnim, 84, 68);
			
			anim.add("idle", [0], 30, false);
			var animArray:Array = new Array();
			for (var i:int = 1; i < 27;++i)
			{
				animArray[i - 1] = i;
			}
			anim.add("buds", animArray,0.3, true);
			anim.originX = 50;
			anim.originY = 34;
			haveChild = false;
			layer = -1;
		}
		
		override public function update():void
		{
			
			if (!(FP.world as TreeWorld).paused)
			{
				x = parent.endX;
				y = parent.endY;
				anim.angle = parent.angle;
				//anim.update();
				
				if (parent.childs.length != 0 && !haveChild)
				{
				 //play anim
				 anim.play("buds");
				 trace("plop");
				}
				
				haveChild = (parent.childs.length != 0);
				
				//Anim end
				if (anim.frame >= 26)
				{
					FP.world.remove(this);
				}
			}
			else
			{
				anim.rate = 0;
			}
		}
		
		public function kill():void
		{
			anim.play("buds");
		}
		
		
	}

}