package Game.Mobs
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flinjin.events.FlinjinSpriteEvent;
	import flinjin.graphics.Sprite;
	import flinjin.sound.FlinjinSound;
	import Game.Balance;
	import Game.GameMain;
	
	/**
	 * ...
	 * @author Michael Miriti
	 */
	public class HippieTank extends Mob
	{
		[Embed(source="../../bmp/mobs/hippie-tank.png")] // 154x130
		private static var _tankAnimBitamp:Class;
		
		[Embed(source="../../snd/shot.mp3")]
		private static var _shotSnd:Class;
		
		private var _shotSound:FlinjinSound = new FlinjinSound(new _shotSnd());
		
		private var _tankAnim:Sprite = new Sprite(new _tankAnimBitamp(), null, true, 154, 130, 1 / 5);
		private var _minX:Number;
		private var _shotDelay:Number = 0;
		
		private var _centerPosition:Point = new Point();
		
		public function HippieTank()
		{
			super(_tankAnim.width, _tankAnim.height);
			addSprite(_tankAnim);
			
			addEventListener(FlinjinSpriteEvent.ADDED_TO_LAYER, onAdded);
			
			_healhPoints = Balance.hippieTankHealth;
		}
		
		private function onAdded(e:FlinjinSpriteEvent):void
		{
			/** @todo wtf?! hardcode! **/
			_minX = 350 - 100 * (y - 300) / 70;
		}
		
		override public function setState(newState:int):void
		{
			if (newState == Mob.STATE_ATTACK)
				_tankAnim.StopAnimation();
			super.setState(newState);
		}
		
		override public function get position():Point
		{
			return _centerPosition;
		}
		
		override public function Move():void
		{
			if (healhPoints <= 0)
			{
				setState(STATE_DIENG);
			}
			
			switch (_state)
			{
				case STATE_MOVING: 
					x -= Balance.hippieTankSpeed;
					if (x <= _minX)
						setState(Mob.STATE_ATTACK);
					break;
				
				case STATE_ATTACK: 
					if (_shotDelay == 0)
					{
						if ((parent as GameMain).factory != null)
						{
							(parent as GameMain).factory.Hit(Balance.hippieTankAttackPower);
							(parent as GameMain).addSprite(new TankShot(), x + 5, y + 15);
							_shotSound.play();
						}
						else
						{
							
							_minX = -200;
							setState(STATE_MOVING);
						}
						_shotDelay = 60;
					}
					else
					{
						_shotDelay--;
					}
					x = _minX + _shotDelay / 4;
					break;
				
				case STATE_DIENG: 
					Delete();
					return;
			}
			_centerPosition.x = x + width / 2;
			_centerPosition.y = y + height / 2;
			super.Move();
		}
	
	}

}
import flinjin.graphics.Sprite;

class TankShot extends Sprite
{
	[Embed(source="../../bmp/shot.png")]
	private static var _shotBitmap:Class;
	
	public function TankShot()
	{
		super(new _shotBitmap());
		setCenterInBitmapCenter();
	}
	
	override public function Move():void
	{
		alpha -= 0.5;
		if (alpha <= 0)
			Delete();
		super.Move();
	}
}