/*!
 *   RobertFrench.ca Banner Game - Main Game File 
 *   Uses melonJS (http://www.melonjs.org) an HTML5 JS Game Library
 *	 Coded by Robert French (rfrench@robertfrench.ca)
 *   Last Updated: Oct 13 2011 
 **/

//game resources
var g_resources = [{
    name: "area01_level_tiles",
    type: "image",
    src: "data/area01_tileset/area01_level_tiles.png"
}, {
    name: "area01",
    type: "tmx",
    src: "data/area01.tmx"
}, { 
		name: "area02",
		type: "tmx",
		src: "data/area02.tmx"
}, { 
			name: "area03",
			type: "tmx",
			src: "data/area03.tmx"
}, { 
		name: "areagpc",
		type: "tmx",
		src: "data/areagpc.tmx"
}, {
    name: "rob_sprite",
    type: "image",
    src: "data/sprite/rob_sprite.png"
},
// the parallax background
{
    name: "background0",
    type: "image",
    src: "data/area01_parallax/background0.png"
}, {
    name: "background1",
    type: "image",
    src: "data/area01_parallax/background1.png"
}, {
    name: "background2",
    type: "image",
    src: "data/area01_parallax/background2.png"
}, {
		name: "background00",
	  type: "image",
	  src: "data/area01_parallax/background00.png"
},
 
// the spinning coin spritesheet
{
    name: "spinning_coin_gold",
    type: "image",
    src: "data/sprite/spinning_coin_gold.png"
}, 
// a #GPC spritesheet
{
    name: "spinning_gpc",
    type: "image",
    src: "data/sprite/spinning_gpc.png"
},
// our cloud entity
{
    name: "cloud_animation",
    type: "image",
    src: "data/sprite/cloud_animation.png"
},
// a #zap spritesheet
{
    name: "spinning_gpc",
    type: "image",
    src: "data/sprite/spinning_gpc.png"
}, 
// game font
{
    name: "32x32_font",
    type: "image",
    src: "data/sprite/32x32_font.png"
},
// audio resources
{
    name: "cling",
    type: "audio",
    src: "data/audio/",
    channel: 2
}];


var jsApp	= 
{	
	/* ---
	
		Initialize the jsApp
		
		---			*/
	onload: function()
	{
		
		me.debug.renderHitBox = false;
		
      // init the video
		if (!me.video.init('jsapp', 960, 320, false, 1.0))
		{
				alert("Sorry but your browser does not support html 5 canvas.");
				return;
		}
				
		// initialize the "audio"
		me.audio.init("mp3,ogg");
		
		// set all resources to be loaded
		me.loader.onload = this.loaded.bind(this);
		
		// set all resources to be loaded
		me.loader.preload(g_resources);

		// load everything & display a loading screen
		//me.state.change(me.state.LOADING);
	},
	
	
	/* ---
	
		callback when everything is loaded
		
		---										*/
	loaded: function ()
	{
		// set the "Play/Ingame" Screen Object
		me.state.set(me.state.PLAY, new PlayScreen());
		
		// add our player entity in the entity pool
		me.entityPool.add("mainPlayer", PlayerEntity);
		me.entityPool.add("CoinEntity", CoinEntity);
		me.entityPool.add("GPCEntity", GPCEntity);
		me.entityPool.add("CloudEntity", CloudEntity);
		me.entityPool.add("ZapEntity", ZapEntity);
		
		// enable the keyboard
		me.input.bindKey(me.input.KEY.LEFT, "left");
		me.input.bindKey(me.input.KEY.RIGHT, "right");
		me.input.bindKey(me.input.KEY.X, "jump", true); 
		me.input.bindKey(me.input.KEY.Z, "blink", true);
		me.input.bindKey(me.input.KEY.SPACE, "space", true);
	  
		me.sys.dirtyRegion = true;
		me.debug.renderDirty = true;
	
	
      // start the game 
		me.state.change(me.state.PLAY);
	}

}; // jsApp

/* the in game stuff*/
var PlayScreen = me.ScreenObject.extend(
{
		onResetEvent: function() {	
      	// stuff to reset on state change
	  		// load a level
        me.levelDirector.loadLevel("areagpc");
				
				// add a default HUD to the game mngr
				me.game.addHUD(0, 0, 960, 320); // full area (may cause issues w touch)

				// add a new HUD item
				me.game.HUD.addItem("score", new ScoreObject(960, 289));

			//	me.game.HUD.addItem("text", new );

				// make sure everyhting is in the right order
				me.game.sort();

	},
	
	
	/* ---
	
		 action to perform when game is finished (state change)
		
		---	*/
	onDestroyEvent: function() {
			// remove the HUD
	  	me.game.disableHUD();
	
  }

});


//bootstrap :)
window.onReady(function() 
{
	jsApp.onload();
});




