Skip to content

Commit

Permalink
Added some features. 3 men, man count display, no jump sound if you c…
Browse files Browse the repository at this point in the history
…an't jump.
  • Loading branch information
ripsawridge committed Nov 17, 2014
1 parent c3a2f2b commit ff38972
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
12 changes: 9 additions & 3 deletions melonjs-tutorial/boilerplate-master/js/entities/HUD.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});

Expand All @@ -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;
},
Expand All @@ -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;
Expand All @@ -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);
}

});
30 changes: 25 additions & 5 deletions melonjs-tutorial/boilerplate-master/js/entities/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@ 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;
}
}

// Did we fall off the screen?
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);
Expand All @@ -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;
}
Expand All @@ -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);
}
});
}
});

Expand Down
12 changes: 11 additions & 1 deletion melonjs-tutorial/boilerplate-master/js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
},


Expand Down
5 changes: 1 addition & 4 deletions melonjs-tutorial/boilerplate-master/js/screens/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions melonjs-tutorial/boilerplate-master/js/screens/title.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ff38972

Please sign in to comment.