package Game
{
	import flash.display.Bitmap;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flinjin.Flinjin;
	import flinjin.graphics.Layer;
	import flinjin.graphics.Sprite;
	import Game.Mobs.Hippie;
	import Game.Mobs.HippieTank;
	import Game.Mobs.Mob;
	import Game.Objects.Factory;
	import Game.Objects.WeaponBonus;
	
	/**
	 * ...
	 * @author Michael Miriti
	 */
	public class GameMain extends Layer
	{
		[Embed(source="../bmp/sky.png")]
		public static var _skyBitmap:Class;
		
		[Embed(source="../bmp/grass_01.png")]
		private static var _grass1Bitmap:Class;
		
		[Embed(source="../bmp/grass_02.png")]
		private static var _grass2Bitmap:Class;
		
		[Embed(source="../bmp/mountains.png")]
		private static var _mountainBitmap:Class;
		
		[Embed(source="../bmp/info1.png")]
		private static var _info1Bitmap:Class;
		
		[Embed(source="../bmp/info2.png")]
		private static var _info2Bitmap:Class;
		
		[Embed(source="../bmp/gameover.png")]
		private static var _gameOverDieBitmap:Class;
		
		private var _gameplayTimer:Timer;
		private var _gameplayCounter:Number = 0;
		
		public var sun:Sun = new Sun();
		public var factory:Factory;
		
		public function GameMain()
		{
			super(Flinjin.sceneWidth, Flinjin.sceneHeight);
			addSprite(new Sprite(new _skyBitmap()), 0, 0, 1);
			
			var _mountBmp:Bitmap = new _mountainBitmap();
			addSprite(new Sprite(_mountBmp), 0, Flinjin.sceneHeight - _mountBmp.height, 2);
			
			var _grass1Bmp:Bitmap = new _grass1Bitmap();
			var _grass2Bmp:Bitmap = new _grass2Bitmap();
			
			addSprite(new Sprite(_grass2Bmp), 0, Flinjin.sceneHeight - _grass2Bmp.height, 7);
			addSprite(new Sprite(_grass1Bmp), 0, Flinjin.sceneHeight - _grass2Bmp.height - _grass1Bmp.height, 5);
			
			factory = new Factory();
			addSprite(factory, 0, Flinjin.sceneHeight - factory.height, 8);
			
			addSprite(sun, Flinjin.sceneWidth / 2, null, 5);
			
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			
			_gameplayTimer = new Timer(500);
			_gameplayTimer.addEventListener(TimerEvent.TIMER, onTimer);
			_gameplayTimer.start();
		}
		
		public function GameOver():void
		{
			var _die:InfoSprite = new InfoSprite(new _gameOverDieBitmap(), 3000);
			_die.setCenterInBitmapCenter();
			addSprite(_die, width / 2, height / 2, 2000);
			
			factory.Delete();
			factory.Dispose();
			factory = null;
		}
		
		public function addHippieTank(xShift:Number = 0):void
		{
			var _newTank:HippieTank = new HippieTank();
			_newTank.y = 300 + Math.random() * 70;
			_newTank.zIndex = _newTank.y;
			_newTank.x = Flinjin.sceneWidth + _newTank.width + xShift;
			addSprite(_newTank);
		}
		
		public function addHippie(xShift:Number = 0):void
		{
			var _newHippie:Hippie = new Hippie();
			_newHippie.y = 275 + Math.random() * 70;
			_newHippie.x = Flinjin.sceneWidth + _newHippie.width / 2 + xShift;
			addSprite(_newHippie);
		}
		
		private function onMouseUp(e:MouseEvent):void
		{
			sun.FireStop();
		}
		
		private function onTimer(e:TimerEvent):void
		{
			if (_gameplayCounter == 1)
			{
				addSprite(new InfoSprite(new _info1Bitmap(), 180), 150, 150, 1000);
			}
			
			if (_gameplayCounter == 2)
			{
				for (var i:int = 0; i < 3; i++)
				{
					addHippie(i * 150);
				}
			}
			
			if (_gameplayCounter == 13)
			{
				addSprite(new InfoSprite(new _info2Bitmap(), 120), 430, 180, 1000);
			}
			
			if (_gameplayCounter == 23)
			{
				for (var j:int = 0; j < 5; j++)
				{
					addHippie(j * 150);
				}
			}
			
			if (_gameplayCounter == 30)
			{
				for (var k:int = 0; k < 3; k++)
				{
					addHippieTank(k * 150);
				}
			}
			
			if (_gameplayCounter == 50)
			{
				for (k = 0; k < 6; k++)
				{
					addHippie(k * 150);
				}
			}
			
			if (_gameplayCounter == 80)
			{
				for (k = 0; k < 6; k++)
				{
					addHippieTank(k * 150);
				}
			}
			
			if (_gameplayCounter == 100)
			{
				for (var l:int = 0; l < 10; l++)
				{
					addHippie(l * 150);
				}
				for (var m:int = 0; m < 5; m++)
				{
					addHippieTank(m * 150);
				}
			}
			
			if (_gameplayCounter > 130)
			{
				if (_gameplayCounter % 4 == 0)
				{
					for (var n:int = 0; n < 1 + Math.floor(Math.random() * 3); n++)
					{
						if (Math.random() > 0.5)
						{
							addHippie();
						}
						else
						{
							addHippieTank();
						}
					}
				}
			}
			
			if ((_gameplayCounter > 0) && (_gameplayCounter % 50 == 0))
			{
				addSprite(new WeaponBonus(), 150 + Math.random() * (Flinjin.sceneWidth - 300), -100, 1000);
			}
			
			_gameplayCounter++;
		}
	}
}
import flash.display.Bitmap;
import flinjin.graphics.Sprite;

class InfoSprite extends Sprite
{
	private var _state:Number = 0;
	private var _counter:Number;
	
	public function InfoSprite(spriteBitmap:Bitmap, time:Number = 240)
	{
		super(spriteBitmap);
		alpha = 0;
		_counter = time;
		Interactive = false;
	}
	
	override public function Move():void
	{
		if (_state == 0)
		{
			if (alpha < 1)
				alpha += 0.1;
			else
				_state = 1;
		}
		
		if (_state == 1)
		{
			if (_counter > 0)
				_counter--;
			else
				_state = 2;
		}
		
		if (_state == 2)
		{
			if (alpha > 0)
				alpha -= 0.1;
			else
				Delete();
		}
		
		super.Move();
	}
}