package Game.Objects
{
	import flash.events.TimerEvent;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	import flinjin.events.FlinjinSpriteEvent;
	import flinjin.Flinjin;
	import flinjin.graphics.Sprite;
	import flinjin.graphics.SpriteText;
	import Game.Balance;
	import Game.GameMain;
	
	/**
	 * ...
	 * @author Michael Miriti
	 */
	public class Factory extends GameObject
	{
		[Embed(source="../../bmp/factory.png")]
		private static var _factoryBitmap:Class;
		private var _timer:Timer;
		private var _HudDisplay:SpriteText = new SpriteText('100%', new TextFormat("Tahoma", 14, 0xffffff));
		
		public function Factory()
		{
			super(221, 241);
			addSprite(new Sprite(new _factoryBitmap()));
			_timer = new Timer(1300);
			_timer.addEventListener(TimerEvent.TIMER, onTimer);
			_timer.start();
			_healhPoints = Balance.factoryHealth;
			Interactive = false;
			
			addEventListener(FlinjinSpriteEvent.ADDED_TO_LAYER, onAddedToLayer);
		}
		
		private function onAddedToLayer(e:FlinjinSpriteEvent):void
		{
			(parent as GameMain).addSprite(_HudDisplay, x + width / 2.5, y, zIndex + 1);
		}
		
		override public function Move():void
		{
			if (_healhPoints <= 0)
			{
				(parent as GameMain).GameOver();
			}
			else
			{
				if (_healhPoints < Balance.factoryHealth)
					_healhPoints += Balance.factoryHealthRecover;
				
				_HudDisplay.text = Math.floor((_healhPoints / Balance.factoryHealth) * 100) + '%';
			}
			super.Move();
		}
		
		private function onTimer(e:TimerEvent):void
		{
			if (_healhPoints > 0)
			{
				var _initAlpha:Number = _healhPoints / Balance.factoryHealth;
				(parent as GameMain).addSprite(new FactorySmoke(_initAlpha), 62, Flinjin.sceneHeight - (height), zIndex - 1);
				(parent as GameMain).addSprite(new FactorySmoke(_initAlpha), 5, Flinjin.sceneHeight - (height), zIndex - 1);
			}
		}
	
	}

}
import flash.geom.Point;
import flinjin.events.FlinjinSpriteEvent;
import flinjin.graphics.Sprite;

class FactorySmoke extends Sprite
{
	private var _startY:Number;
	
	[Embed(source="../../bmp/smoke.png")]
	public static var _smokeBitmap:Class;
	
	function FactorySmoke(_initAlpha:Number = 1)
	{
		super(new _smokeBitmap(), new Point(26, 26));
		addEventListener(FlinjinSpriteEvent.ADDED_TO_LAYER, onAdded);
		alpha = _initAlpha;
	}
	
	private function onAdded(e:FlinjinSpriteEvent):void
	{
		_startY = y;
		y += -3 + Math.random() * 6;
	}
	
	override public function Move():void
	{
		y -= 0.5;
		x += 1 * -((y - _startY) / _startY);
		scale += 0.008;
		alpha -= 0.002;
		if ((y < -50) || (alpha <= 0))
			Delete();
		super.Move();
	}
}