
/*------------------- 
a player entity
-------------------------------- */
var PlayerEntity = me.ObjectEntity.extend({

    /* -----

    constructor

    ------ */

    init: function(x, y, settings) {
        // call the constructor
        this.parent(x, y, settings);

        // set the walking & jumping speed
        this.setVelocity(10, 15);
		
				// adjust the bounding box
				this.updateColRect(14, 64, -1, 128);

        // set the display to follow our position on both axis
        me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);

				// blink animatin
				this.addAnimation ("blinking", [0,10,11,12,13,14,14,13,12,11,10,0,0,0,0,0,0,0,0,0,0,0,0,0]);
				// eating animatin
				//this.addAnimation ("eat", [6,6]);
				// rolling animatin
				//this.addAnimation ("roll", [7,8,9,10]);
    },

    /* -----

    update the player pos

    ------ */
    update: function() {

        if (me.input.isKeyPressed('left')) {
            this.doWalk(true);
        } else if (me.input.isKeyPressed('right')) {
            this.doWalk(false);
        } else {
            this.vel.x = 0;
        } 

				if (me.input.isKeyPressed('space')) {
					//showText("");
					setElementPos('message', this.pos.x + 45, this.pos.y - 75);
					toggleElement("message"); 
				}
        if (me.input.isKeyPressed('jump')) {
            this.doJump();
        }

				if (me.input.isKeyPressed('blink')) {
						//this.doBlink();
						//this.setCurrentAnimation("blinking"); 
						this.doJump();
				}

        // check & update player movement
        updated = this.updateMovement();


				// check for a messagebox and update if needed 
				if(visibleElement('message')) {
					setElementPos('message', this.pos.x + 45, this.pos.y - 100)
					
				}

        // check for collision
				    res = me.game.collide(this);

				    if (res) {
				        // if we collide with an enemy
				        if (res.type == me.game.ENEMY_OBJECT) {
				            // check if we jumped on it
				            if ((res.y > 0) && ! this.jumping) {
				                // bounce
				                this.forceJump();
				            } else {
				                // let's flicker in case we touched an enemy
				                this.flicker(45);
				            }
				        }
				
								// if we collide with a cloud
				        if (res.type == me.game.NO_OBJECT) {
				            // check if we jumped on it
				            if ((res.y > 0) && ! this.jumping) {
				                // bounce
				                this.forceJump();
				            }
								}
				
				    }
						
						// check for inside bounds of screen / visible area å
						if (!this.visible) {
							// we fell off the screen somehow, kill the user
							this.alive = false;
							
							
						}
						
				    // update animation
				    if (updated) {
				        // update objet animation
				        this.parent(this);
				    }
				    return updated;
    }


	


});



/*----------------
 a Coin entity
------------------------ */
var CoinEntity = me.CollectableEntity.extend({
    // extending the init function is not mandatory
    // unless you need to add some extra initialization
    init: function(x, y, settings) {
        // call the parent constructor
        this.parent(x, y, settings);
				
    },

    // this function is called by the engine, when
    // an object is destroyed (here collected)
    onDestroyEvent : function () {
			// do something when collide
			me.audio.play("cling");
			// increase score
			me.game.HUD.updateItemValue("score", 250);
			//console.log("score is " + score);
		}

});



/*----------------
 a #GPC entity
------------------------ */
var GPCEntity = me.CollectableEntity.extend({
    // extending the init function is not mandatory
    // unless you need to add some extra initialization
    init: function(x, y, settings) {
        // call the parent constructor
        this.parent(x, y, settings);
		
    },

    // this function is called by the engine, when
    // an object is destroyed (here collected)
    onDestroyEvent : function () {
			// do something when collide
			//setElementPos(player.x)
			toggleElement('message');	
			setElementText("<p>Awesome! You're here!!<br/>I'm currently working on some stuff for .... uh ... somethin<br/> Check back frequently!!!</p>");	
			me.audio.play("cling");
			
			// increase score
			//me.game.HUD.updateItemValue("score", 250);
			//console.log("score is " + score);
		}

});




/*----------------
 a Cloud entity
------------------------ */
var CloudEntity = me.ObjectEntity.extend({
    // extending the init function is not mandatory
    // unless you need to add some extra initialization
		init: function(x, y, settings) {
        // call the parent constructor
        this.parent(x, y, settings);
				this.startX = x;
				this.startY = y;
				this.endX = x + settings.width - settings.spritewidth;
        
				// size of sprite
				
       	// make it start from the right
				this.pos.x = x + settings.width - settings.spritewidth;
				this.walkLeft = true;
				
				// set the floating speed
        this.setVelocity(15, 0);
				
				// make it non-collidable
				this.collidable = true;
				
				// make it a enemy object
				this.type = me.game.NO_OBJECT;
				this.gravity = 0;
				
		},

    
		// manage the cloud movement
		update: function() {
				
				//console.log("update");
				// do nothing if not visible
				if (!this.visible && !this.flickering) {
						//console.log("not visible or flashing");
						return false; 
				}
				
				if (this.alive) {
						if (this.walkLeft && this.pos.x <= this.startX) {
							this.walkLeft = false;
							
							//console.log("cloud moving right");
		        } else if (!this.walkLeft && this.pos.x >= this.endX) {
		          this.walkLeft = true;
							//console.log("cloud moving left");
		        }
       	} else { 
					this.vel.x = 0; 
				}
				
				this.pos.y = this.startY; 
				this.doWalk(this.walkLeft);
				
				// check & update movement
				updated = this.updateMovement();

				if (updated) {
					// update the object animation
				  this.parent();
				}
	     	return updated;
				
   	}

});


/*----------------
 a #GPC entity
------------------------ */
var ZapEntity = me.CollectableEntity.extend({
    // extending the init function is not mandatory
    // unless you need to add some extra initialization
    init: function(x, y, settings) {
        // call the parent constructor
        this.parent(x, y, settings);
		
    },

    // this function is called by the engine, when
    // an object is destroyed (here collected)
    onDestroyEvent : function () {
			// do something when collide
			me.audio.play("cling");
			// increase score
			// debugging 
			var foo = document.getElementById('foo');
			var select = foo.getElementsByTagName('select');
			select[0].options[0].text = ' two ';
			 
			//document.getElementById("jsapp").innerHTML =  "";
			//window.open("http://www.robertfrench.ca");
			//location.reload(true);
			//me.game.HUD.updateItemValue("score", 250);
			//console.log("score is " + score);
		}

});




var SpeakObject = me.HUD_Item.extend({
    init: function(x, y) {
        // call the parent constructor
        this.parent(x, y);
        // create a font
        this.font = new me.BitmapFont("32x32_font", 32);
    },

    /* -----

    draw our score

    ------ */
    draw: function(context, x, y) {
        this.font.draw(context, this.value, this.pos.x + x, this.pos.y + y);
    }

});


/*-------------- 
a score HUD Item
--------------------- */

var ScoreObject = me.HUD_Item.extend({
    init: function(x, y) {
        // call the parent constructor
        this.parent(x, y);
        // create a font
        this.font = new me.BitmapFont("32x32_font", 32);
    },

    /* -----

    draw our score

    ------ */
    draw: function(context, x, y) {
        this.font.draw(context, this.value, this.pos.x + x, this.pos.y + y);
    }

});


