(function(window){	function GameController(stage, spaceship, aliens, scoreBoard, keyController)	{		this.stage			= stage;		this.spaceship		= spaceship;		this.aliens			= aliens;		this.scoreBoard		= scoreBoard;		this.keyController	= keyController;		this.initialize();			}	GameController.prototype.bullits		= [];	// Array to hold all bullits fired	GameController.prototype.score			= 0;	// The actual game score	GameController.prototype.scoreModifiers	= {spaceshipShooting: 0, spaceshipShot: 0, alienKilled: 0, aliensReachedBottom: 0, levelCompleted: 0, asTimeGoesBy: 0 };	// Views/Controller	GameController.prototype.stage			= null;	GameController.prototype.spaceship		= null;	GameController.prototype.aliens			= null;	GameController.prototype.scoreBoard		= null;	GameController.prototype.keyController	= null;	// Signals	GameController.prototype.completed		= new signals.Signal();	/**	* GAME CONTROL METHODS	*/	GameController.prototype.initialize = function()	{		// Set FPS monitor		fpsMonitor		= new Text('0 FPS','bold 10px OCRAStd','#000');		fpsMonitor.x	= 654;		fpsMonitor.y	= 55;		this.stage.addChild(fpsMonitor);		// The ideal solution would be to have the Ticker start in the startGame() method. But somehow, all texts aren't drawn to stage unless we make alot of ticks?!		// Therefore, we start the Ticker and have a "enabled property" in Aliens that keeps them from moving until true... :/		Ticker.addListener(this);		Ticker.setFPS(30);	}	GameController.prototype.enable	= function()	{		// Listen for Signals		var _this	= this;		this.spaceship.shotFired.add(function(){_this.onSpaceshipShooting();});		this.spaceship.hit.addOnce(function(bullit){_this.onSpaceshipHit(bullit);});		this.aliens.shotFired.add(function(point){_this.onAlienShooting(point);});		this.aliens.alienKilled.add(function(point,color,bullit){_this.onAlienKilled(point,color,bullit);});		this.aliens.allKilled.addOnce(function(){_this.onAllAliensKilled();});		this.aliens.reachedBottom.addOnce(function(){_this.onAliensReachedBottom();});	}	GameController.prototype.startGame = function()	{		this.keyController.enable();		this.scoreModifiers.asTimeGoesBy	= -1;		this.aliens.enable();	}	GameController.prototype.stopGame = function()	{		this.completed.dispatch(this.score);		//Ticker.removeAllListeners();	}	GameController.prototype.restart = function()	{		this.spaceship.reset();		this.aliens.reset();		this.resetScore();		this.enable();		this.startGame();	}	GameController.prototype.resetScore = function()	{		this.score	= 0;		this.scoreModifiers.spaceshipShooting	= -10;		this.scoreModifiers.spaceshipShot		= -2000;		this.scoreModifiers.alienKilled			= 1500;		this.scoreModifiers.aliensReachedBottom	= -1000;		this.scoreModifiers.levelCompleted		= 10000;		this.scoreModifiers.asTimeGoesBy		= 0;	}	/**	* RENDERING METHODS	*/	GameController.prototype.tick = function()	{		this.updateSpaceship();		this.updateAliens();		this.updateBullits();		this.score	+= this.scoreModifiers.asTimeGoesBy;		this.updateScore();		fpsMonitor.text = Math.round(Ticker.getMeasuredFPS()) + ' FPS';		this.stage.update();	}	GameController.prototype.onKeyControllerStarted = function()	{		Ticker.addListener(stage);		Ticker.setFPS(30);	}	GameController.prototype.updateScore = function()	{		this.scoreBoard.update(this.score);	}	GameController.prototype.gameOver = function()	{		this.scoreModifiers.asTimeGoesBy	= 0;		this.keyController.disable();		this.spaceship.disable();		this.aliens.disable();		var _this	= this;		var timer	= new Timer(2000,1); // Give some time for the final animations		timer.TIMER.addOnce(function(){_this.stopGame();});		timer.start();	}	/**	* SPACESHIP METHODS	*/	GameController.prototype.updateSpaceship = function()	{		// Shooting		if(KeyController.shootKeyDown === true) this.spaceship.shoot();		// Movement		if(KeyController.leftKeyDown === true) this.spaceship.moveLeft();		else if(KeyController.rightKeyDown === true) this.spaceship.moveRight();		else this.spaceship.slowDownAndBreak();	}	GameController.prototype.onSpaceshipShooting = function()	{		this.score	+= this.scoreModifiers.spaceshipShooting;		this.stage.addChildAt(this.getBullit(new Point(this.spaceship.x+this.spaceship.bullit.x, this.spaceship.y+this.spaceship.bullit.y+6), -1, 10, this.spaceship.bullitColor), 0);	}	GameController.prototype.onSpaceshipHit = function(bullit)	{		this.score	+= this.scoreModifiers.spaceshipShot;		this.destroyBullit(bullit);		this.destroySpaceship();		this.gameOver();	}	GameController.prototype.destroySpaceship = function()	{		this.createExplosion(new Point(this.spaceship.x + this.spaceship.width/2, this.spaceship.y), this.spaceship.shipColor);		this.spaceship.visible	= false;	}	/**	* ALIENS METHODS	*/	GameController.prototype.updateAliens = function()	{		this.aliens.update();	}	GameController.prototype.onAlienShooting = function(point)	{		this.stage.addChildAt(this.getBullit(point, 1, 5), 0);	}	GameController.prototype.onAlienKilled = function(point, color, bullit)	{		this.score	+= this.scoreModifiers.alienKilled;		this.destroyBullit(bullit);		this.createExplosion(point, color);	}	GameController.prototype.onAllAliensKilled = function()	{		this.score	+= this.scoreModifiers.levelCompleted;		this.gameOver();	}	GameController.prototype.onAliensReachedBottom = function()	{		this.score	+= this.scoreModifiers.aliensReachedBottom;		this.destroySpaceship();		this.gameOver();	}	/**	* BULLIT METHODS	*/	GameController.prototype.getBullit = function(point, vY, speed, color)	{		var bullit		= new Bullit(color);		bullit.x		= point.x;		bullit.y		= point.y;		bullit.vY		= vY;		bullit.speed	= speed;		this.bullits.push(bullit);		return bullit;	}	GameController.prototype.updateBullits = function()	{		var _this	= this;		var _total	= this.bullits.length;		if(_total > 0)		{			for(var i=0; i<_total; ++i)			{				var bullit	= this.bullits[i];				if(bullit)				{					bullit.y += (bullit.vY * bullit.speed);					if(bullit.y < -bullit.radius + 50 || bullit.y > Registry.get('STAGE_HEIGHT'))					{						this.destroyBullit(bullit);						this.createExplosion(new Point(bullit.x, bullit.y), bullit.color);					}					else					{						if(bullit.vY < 0) this.aliens.checkForHit(bullit);						if(bullit.vY > 0) this.spaceship.checkForHit(bullit);						}				}			}		}	}	GameController.prototype.destroyBullit = function(bullit)	{		if(this.bullits.indexOf(bullit) >= 0) this.bullits.splice(this.bullits.indexOf(bullit), 1);		this.stage.removeChild(bullit);	}	/**	* EXPLOSION METHODS	*/	GameController.prototype.createExplosion = function(point, color)	{		var explosion	= new ParticleExplosion(color);		explosion.x		= point.x;		explosion.y		= point.y;		explosion.completed.addOnce(this.onExplosionCompleted);		this.stage.addChildAt(explosion, 0);	}	GameController.prototype.onExplosionCompleted = function(explosion)	{		this.stage.removeChild(explosion);		explosion	= null;	}	window.GameController = GameController;}(window));
