Skip to content

Instantly share code, notes, and snippets.

@cbmeeks
Last active December 26, 2015 04:29
Show Gist options
  • Save cbmeeks/7093662 to your computer and use it in GitHub Desktop.
Save cbmeeks/7093662 to your computer and use it in GitHub Desktop.
Samus Aran melonjs
// The Game
var game = {
'onload': function () {
// Initialize video
if (!me.video.init("screen", 640, 480, true, 'auto')) {
alert("Your browser does not support HTML5 canvas.");
return;
}
// Add "#debug" to the URL to enable the debug panel
if (document.location.hash === "#debug") {
window.onReady(function () {
me.plugin.register.defer(debugPanel, "debug");
});
}
// Initialize the audio
me.audio.init('mp3,ogg');
// Set a callback to run when loading is complete
me.loader.onload = this.loaded.bind(this);
// Load the resources
me.loader.preload(game.resources);
// Initialize the melonJS and display the loading screen
me.state.change(me.state.LOADING);
},
// Run once game resources loaded
'loaded': function () {
me.state.set(me.state.MENU, new game.TitleScreen());
me.state.set(me.state.PLAY, new game.PlayScreen());
// Add Samus to the entity pool
me.entityPool.add('samusAran', game.SamusEntity);
// Enable keyboard (joypad to come later.. ;-) )
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);
// Start the game.
me.state.change(me.state.PLAY);
}
};
window.onReady(function onReady() {
game.onload();
})
game.resources = [
/* Graphics */
{name: 'metroid_tiles', type: 'image', src: '/javascripts/metroid_classic/images/metroid_tiles.png'},
{name: 'samus_aran', type: 'image', src: '/javascripts/metroid_classic/images/Samus_Aran_Trans.png'},
/* Atlases
* @example
* {name: "example_tps", type: "tps", src: "data/img/example_tps.json"},
*/
/* Maps */
{name: 'test-001', type: 'tmx', src: '/javascripts/metroid_classic/maps/test/test-001.tmx'}
/* Background music.
* @example
* {name: "example_bgm", type: "audio", src: "data/bgm/", channel : 1},
*/
/* Sound effects.
* @example
* {name: "example_sfx", type: "audio", src: "data/sfx/", channel : 2}
*/
];
/**
* Samus Aran
*
* @type {*}
*/
game.SamusEntity = me.ObjectEntity.extend({
init: function (x, y, settings) {
// Call the constructor
this.parent(x, y, settings);
// Set the default horizontal / vertical acceleration
this.setVelocity(4, 15);
this.maxVel.y = 18;
// Adjust the bounding box
this.updateColRect(20, 24, 12, 64);
// Add animation sets
this.renderable.addAnimation('still', [0]);
this.renderable.addAnimation('run_left', [9, 10, 11, 12, 13, 14]);
this.renderable.addAnimation('run_right', [3, 4, 5, 6, 7, 8]);
// Set the display to follow Samus on both axis
me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
me.debug.renderHitBox = true;
},
update: function () {
if (me.input.isKeyPressed('left')) {
this.renderable.setCurrentAnimation('run_left');
this.vel.x -= this.accel.x * me.timer.tick;
} else if (me.input.isKeyPressed('right')) {
this.renderable.setCurrentAnimation('run_right');
this.vel.x += this.accel.x * me.timer.tick;
} else {
this.renderable.setCurrentAnimation('still');
this.vel.x = 0;
}
if (me.input.isKeyPressed('jump')) {
// make sure we are not already jumping or falling
if (!this.jumping && !this.falling) {
// set current vel to the maximum defined value
// gravity will then do the rest
this.vel.y = -this.maxVel.y * me.timer.tick;
// set the jumping flag
this.jumping = true;
}
}
// Update Samus' movement
this.updateMovement();
this.parent();
return true;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment