diff --git a/melonjs-tutorial/boilerplate-master/js/entities/HUD.js b/melonjs-tutorial/boilerplate-master/js/entities/HUD.js index 5a8252b..74a9063 100755 --- a/melonjs-tutorial/boilerplate-master/js/entities/HUD.js +++ b/melonjs-tutorial/boilerplate-master/js/entities/HUD.js @@ -24,7 +24,7 @@ game.HUD.Container = me.Container.extend({ this.name = "HUD"; // add our child score object at the top left corner - this.addChild(new game.HUD.ScoreItem(630, 440)); + this.addChild(new game.HUD.ScoreItem(430, 440)); } }); @@ -48,6 +48,9 @@ game.HUD.ScoreItem = me.Renderable.extend({ // local copy of the global score this.score = -1; + // local copy of the lives + this.lives = -1; + // make sure we use screen coordinates this.floating = true; }, @@ -58,8 +61,10 @@ game.HUD.ScoreItem = me.Renderable.extend({ update : function () { // we don't do anything fancy here, so just // return true if the score has been updated - if (this.score !== game.data.score) { + if (this.score !== game.data.score || + this.lives !== game.data.lives) { this.score = game.data.score; + this.lives = game.data.lives; return true; } return false; @@ -69,7 +74,8 @@ game.HUD.ScoreItem = me.Renderable.extend({ * draw the score */ draw : function (context) { - this.font.draw(context, game.data.score, this.pos.x, this.pos.y); + this.font.draw(context, game.data.score, this.pos.x + 200, this.pos.y); + this.font.draw(context, game.data.lives, this.pos.x, this.pos.y); } }); diff --git a/melonjs-tutorial/boilerplate-master/js/entities/entities.js b/melonjs-tutorial/boilerplate-master/js/entities/entities.js index e5b0bd6..e5b2a0d 100755 --- a/melonjs-tutorial/boilerplate-master/js/entities/entities.js +++ b/melonjs-tutorial/boilerplate-master/js/entities/entities.js @@ -21,11 +21,13 @@ game.PlayerEntity = me.Entity.extend({ this.body.vel.x = 0; } + var tried_to_jump = false; if (me.input.isKeyPressed("jump")) { if (!this.body.jumping && !this.body.falling) { this.body.vel.y = -this.body.maxVel.y * me.timer.tick; this.body.jumping = true; - me.audio.play("jump"); + // me.audio.play("jump"); + tried_to_jump = true; } } @@ -33,10 +35,7 @@ game.PlayerEntity = me.Entity.extend({ if (this.body.falling && this.pos.y > (me.game.viewport.pos.y + me.game.viewport.height)) { // We need to die - this.alive = false; - me.game.viewport.fadeIn("#dddddd", 1700, function() { - me.state.change(me.state.PLAY); - }); + this.die(); } this.body.update(dt); @@ -45,6 +44,9 @@ game.PlayerEntity = me.Entity.extend({ me.collision.check(this, true, this.collideHandler.bind(this), true); if (this.body.vel.x != 0 || this.body.vel.y != 0) { + if (tried_to_jump && this.body.vel.y != 0) { + me.audio.play("jump"); + } this._super(me.Entity, "update", [dt]); return true; } @@ -62,11 +64,29 @@ game.PlayerEntity = me.Entity.extend({ this.body.jumping = true; me.audio.play("stomp"); response.b.die(); + game.data.score += 30; } else { // Let's flicker in case we touched an enemy this.renderable.flicker(true); } + } else if (response.b.body.collisionType == me.collision.types.ACTION_OBJECT) { + // b is a LevelEntity. Indicate our new respawn point. + game.data.current_level = response.b.nextlevel; + } + }, + + die: function() { + if (this.alive) { + game.data.lives--; + this.alive = false; } + me.game.viewport.fadeIn("#dddddd", 1700, function() { + if (game.data.lives == 0) { + me.state.change(me.state.MENU); + } else { + me.state.change(me.state.PLAY); + } + }); } }); diff --git a/melonjs-tutorial/boilerplate-master/js/game.js b/melonjs-tutorial/boilerplate-master/js/game.js index 623d9b6..92326ad 100755 --- a/melonjs-tutorial/boilerplate-master/js/game.js +++ b/melonjs-tutorial/boilerplate-master/js/game.js @@ -5,7 +5,17 @@ var game = { // an object where to store game information data : { // score - score : 0 + score : 0, + // number of lives + lives : 3, + // current level + current_level: "area01", + // reset function + "reset" : function() { + this.score = 0; + this.lives = 3; + this.current_level = "area01"; + } }, diff --git a/melonjs-tutorial/boilerplate-master/js/screens/play.js b/melonjs-tutorial/boilerplate-master/js/screens/play.js index c461d25..6a59e33 100755 --- a/melonjs-tutorial/boilerplate-master/js/screens/play.js +++ b/melonjs-tutorial/boilerplate-master/js/screens/play.js @@ -4,10 +4,7 @@ game.PlayScreen = me.ScreenObject.extend({ */ onResetEvent: function() { // load a level - me.levelDirector.loadLevel("area01"); - - // reset the score - game.data.score = 0; + me.levelDirector.loadLevel(game.data.current_level); // add our HUD to the game world this.HUD = new game.HUD.Container(); diff --git a/melonjs-tutorial/boilerplate-master/js/screens/title.js b/melonjs-tutorial/boilerplate-master/js/screens/title.js index af0e3ca..57cb8d0 100755 --- a/melonjs-tutorial/boilerplate-master/js/screens/title.js +++ b/melonjs-tutorial/boilerplate-master/js/screens/title.js @@ -3,6 +3,9 @@ game.TitleScreen = me.ScreenObject.extend({ * action to perform on state change */ onResetEvent: function() { + // Reset score, lives, level. + game.data.reset(); + me.game.world.addChild( new me.Sprite(0, 0, me.loader.getImage("title_screen")), 1);