package Game
{
	import flash.geom.Point;
	import flinjin.events.FlinjinEvent;
	import flinjin.events.FlinjinSpriteEvent;
	import flinjin.Flinjin;
	import flinjin.graphics.Layer;
	import flinjin.graphics.Sprite;
	import flinjin.input.Input;
	import flinjin.sound.FlinjinSound;
	import Game.Mobs.Mob;
	import Game.Objects.Factory;
	
	/**
	 * ...
	 * @author Michael Miriti
	 */
	public class Sun extends Layer
	{
		[Embed(source="../bmp/sun/sun-face.png")]
		private static var _sunfaceBitmap:Class;
		private var _sunface:Sprite = new Sprite(new _sunfaceBitmap(), new Point(100, 100));
		
		[Embed(source="../bmp/sun/sun-rays1.png")]
		private static var _sunrays1Bitmap:Class;
		private var _sunrays1:Sprite = new Sprite(new _sunrays1Bitmap(), new Point(100, 100));
		
		[Embed(source="../bmp/sun/sun-rays2.png")]
		private static var _sunrays2Bitmap:Class;
		private var _sunrays2:Sprite = new Sprite(new _sunrays2Bitmap(), new Point(100, 100));
		
		[Embed(source="../bmp/laser.png")]
		private static var _sunLaser:Class;
		
		[Embed(source="../snd/laser.mp3")]
		private static var _laserSnd:Class;		
		private var _laserSound:FlinjinSound = new FlinjinSound(new _laserSnd());
		
		[Embed(source="../snd/rocket.mp3")]
		private static var _rocketSnd:Class;
		private var _rocketSound:FlinjinSound = new FlinjinSound(new _rocketSnd());
		
		public static const MAX_SPEED:Number = 6;
		public static const ACC:Number = 0.1;
		
		private var _xSpeed:Number = 0;
		
		private var SunLaser1:Sprite = new Sprite(new _sunLaser(), new Point(0, 4));
		private var SunLaser2:Sprite = new Sprite(new _sunLaser(), new Point(0, 4));
		
		private var _indicator:PowerDisplay = new PowerDisplay();
		
		private var _target:Point;
		
		private var _currentWeapon:Number = WEAPON_LASER;
		private var _targetObj:Mob;
		private var _canFire:Boolean = true;
		private var _laserCounter:Number = 0;
		private var _rocketCounter:Number = 0;
		
		public static const WEAPON_LASER:Number = 1;
		public static const WEAPON_ROCKET:Number = 2;
		
		/**
		 * Angry and hot sun!
		 *
		 */
		public function Sun()
		{
			super(200, 200);
			y = height / 2;
			
			addSprite(_sunrays1, 100, 100);
			addSprite(_sunrays2, 100, 100);
			addSprite(_sunface, 100, 100);
			
			setCenterInBitmapCenter();
			
			addEventListener(FlinjinSpriteEvent.ADDED_TO_LAYER, onAdded);
		}
		
		private function onAdded(e:FlinjinSpriteEvent):void
		{
			SunLaser1.alpha = 0;
			SunLaser2.alpha = 0;
			(parent as GameMain).addSprite(SunLaser1, null, null, 6);
			(parent as GameMain).addSprite(SunLaser2, null, null, 6);
			(parent as GameMain).addSprite(_indicator, Flinjin.sceneWidth - _indicator.width - 10, 10, 1000);
		}
		
		private function setLaserPos():void
		{
			SunLaser1.x = x - 20;
			SunLaser1.y = y - 10;
			
			SunLaser2.x = x + 20;
			SunLaser2.y = y - 10;
			
			var _dx:Number = Input.MousePosition.x - x;
			var _dy:Number = Input.MousePosition.y - y;
			
			var _crossingPoint:Point;
			
			if (_target == null)
			{
				// finding point that crossing the groung:
				_crossingPoint = new Point(0, Flinjin.sceneHeight - 42);
				_crossingPoint.x = x + (_dx / _dy) * _crossingPoint.y;
			}
			else
			{
				_crossingPoint = _target;
			}
			
			_dx = _crossingPoint.x - (x - 20);
			_dy = _crossingPoint.y - (y - 10);
			SunLaser1.angle = Math.atan2(_dy, _dx);
			
			_dx = _crossingPoint.x - (x + 20);
			SunLaser2.angle = Math.atan2(_dy, _dx);
			
			if (SunLaser1.alpha > 0)
			{
				SunLaser1.alpha -= 0.1;
				SunLaser2.alpha -= 0.1;
			}
		}
		
		public function fireLaser():void
		{
			_laserCounter = Balance.laserDelay;
			_targetObj.Hit(Balance.laserPower);
			SunLaser1.alpha = 1;
			SunLaser2.alpha = 1;
			_laserSound.play();
		}
		
		public function fireRocket():void
		{
			_rocketCounter = Balance.rocketDelay;
			(parent as GameMain).addSprite(new SunRocket(_target, _targetObj), x, y);
			_rocketSound.play();
		}
		
		/**
		 * Kill this fucking hippie!
		 *
		 */
		public function Fire(target:Point = null, targetObj:Mob = null):void
		{
			if (targetObj is Factory)
				return;
			
			if (_canFire)
			{
				_target = target;
				_targetObj = targetObj;
				
				switch (_currentWeapon)
				{
					case WEAPON_LASER: 
						fireLaser();
						break;
					case WEAPON_ROCKET: 
						fireRocket();
						break;
				}
				
				_canFire = false;
			}
		}
		
		public function FireStop():void
		{
		}
		
		/**
		 *
		 */
		override public function Move():void
		{
			super.Move();
			
			x += _xSpeed;
			
			if (x < width / 2)
			{
				x = width / 2;
				_xSpeed = 0;
			}
			
			if (x > Flinjin.sceneWidth - width / 2)
			{
				x = Flinjin.sceneWidth - width / 2;
				_xSpeed = 0;
			}
			
			if (x > Input.MousePosition.x)
			{
				flipHorizontal = true;
				if (_xSpeed > -MAX_SPEED)
				{
					_xSpeed -= ACC;
				}
			}
			else if (x < Input.MousePosition.x)
			{
				_flipHorizontal = false;
				if (_xSpeed < MAX_SPEED)
				{
					_xSpeed += ACC;
				}
			}
			else
			{
				_xSpeed = 0;
			}
			
			setLaserPos();
			
			if (_currentWeapon == WEAPON_LASER)
			{
				_indicator.value = _laserCounter / Balance.laserDelay;
				
				if (_laserCounter > 0)
				{
					_laserCounter--;
				}
				else
				{
					_canFire = true;
				}
			}
			
			if (_currentWeapon == WEAPON_ROCKET)
			{
				_indicator.value = _rocketCounter / Balance.rocketDelay;
				
				if (_rocketCounter > 0)
				{
					_rocketCounter--;
				}
				else
				{
					_canFire = true;
				}
				
			}
		}
		
		public function get currentWeapon():Number
		{
			return _currentWeapon;
		}
		
		public function set currentWeapon(value:Number):void
		{
			_laserCounter = 0;
			_rocketCounter = 0;
			_currentWeapon = value;
		}
	}
}
import flash.geom.Point;
import flinjin.graphics.Sprite;
import flinjin.sound.FlinjinSound;
import Game.Balance;
import Game.Mobs.Mob;

class SunRocket extends Sprite
{
	private var _target:Point;
	
	private var _targetObj:Mob;
	
	[Embed(source="../bmp/rocket.png")]
	private static var _rocketBitmap:Class;
	
	[Embed(source="../snd/rocket-hit.mp3")]
	private static var _hitSnd:Class;
	
	private var _hitSound:FlinjinSound = new FlinjinSound(new _hitSnd());
	
	function SunRocket(target:Point, targetObj:Mob):void
	{
		_target = target;
		_targetObj = targetObj;
		super(new _rocketBitmap(), new Point(10, 1), true, 47, 5);
	}
	
	override public function Move():void
	{
		angle = Math.atan2(_target.y - y, _target.x - x);
		
		x += Math.cos(_angle) * Balance.rocketSpeed;
		y += Math.sin(_angle) * Balance.rocketSpeed;
		
		var _distantion:Point = new Point(_target.x - x, _target.y - y);
		if (_distantion.length <= Balance.rocketSpeed)
		{
			Delete();
			_targetObj.Hit(Balance.rocketAttackPower);
			_hitSound.play();
		}
		_distantion = null;
		
		super.Move();
	}
}

class PowerDisplay extends Sprite
{
	[Embed(source="../bmp/fire-indicator.png")]
	private static var _indicatorBitmap:Class;
	
	function PowerDisplay()
	{
		super(new _indicatorBitmap(), null, true, 12, 58);
		StopAnimation();
	}
	
	public function set value(val:Number):void
	{
		if (val > 1)
			val = 1;
		if (val < 0)
			val = 0;
		
		currentFrame = Math.floor(val * 10);
	}
}