Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Last active August 29, 2015 13:57
Show Gist options
  • Save agmcleod/9653767 to your computer and use it in GitHub Desktop.
Save agmcleod/9653767 to your computer and use it in GitHub Desktop.
game.PlayerEntity = me.ObjectEntity.extend({
init: function(x,y,settings){
this.parent(x,y,settings);
this.setVelocity(4,15);
this.updateColRect(8,48,-1,0);
me.game.viewport.follow(this.pos, me.game.viewport.AXIS.HORIZONTAL);
},
update: function(){
if(me.input.isKeyPressed("left")){
this.flipX(true);
this.vel.x-=this.accel.x * me.timer.tick;
}else if(me.input.isKeyPressed("right")){
this.flipX(false);
this.vel.x += this.accel.x *me.timer.tick;
} else{
this.vel.x = 0;
}
if(me.input.isKeyPressed("jump")){
if(!this.jumping && !this.falling){
this.vel.y = -this.maxVel.y * me.timer.tick;
this.jumping = true;
}
}
this.updateMovement();
var res = me.game.collide(this);
if(res){
if(res.obj.type === me.game.ENEMY_OBJECT){
if((res.y>0) && !this.jumping){
this.falling=false;
this.vel.y = -this.maxVel.y * me.timer.tick;
this.jumping = true;
}else{
this.renderable.flicker(45);
}
}
}
if (this.vel.x != 0 || this.vel.y != 0){
this.parent();
return true;
}
return false;
},
});
game.CoinEntity = me.CollectableEntity.extend({
init: function(x,y,settings){
settings.image ="spinning_coin_gold";
settings.spritewidth = 32;
this.parent(x,y,settings);
},
onCollision: function(){
this.collidable = false;
me.game.remove(this);
}
});
game.EnemyEntity = me.ObjectEntity.extend({
init: function(x,y, settings){
settings.image = "wheelie_right";
settings.spritewidth = 64;
this.parent(x,y,settings);
this.startX = x;
this.endX = x + settings.width - settings.spritewidth;
this.pos.x = x + settings.width -settings.spritewidth;
this.walkLeft = true;
this.setVelocity(4,6);
this.collidable = true;
this.type = me.game.ENEMY_OBJECT;
this.updateColRect(4,56,8,56);
},
onCollision: function(res,obj){
if(this.alive && (res.y>0) && obj.falling){
this.renderable.flicker(45);
}
},
update: function(){
if (!this.inViewport){
return false;
}
if(this.alive){
if(this.walkLeft && this.pos.x <= this.startX){
this.walkLeft = false;
}else if(!this.walkLeft && this.pos.x >= this.endX){
this.walkLeft = true;
}
this.flipX(this.walkLeft);
this.vel.x += (this.walkLeft) ? -this.accel.x * me.timer.tick : this.accel.x * me.timer.tick;
}else{
this.vel.x = 0;
}
this.updateMovement();
if(this.vel.x !==0 || this.vel.y !==0){
this.parent();
return true;
}
return false;
}
});
/* Game namespace */
var game = {
// an object where to store game information
data : {
// score
score : 0
},
// Run on page load.
"onload" : function () {
// Initialize the video.
if (!me.video.init("screen", 640, 480)) {
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 melonJS and display a loading screen.
me.state.change(me.state.LOADING);
},
// Run on game resources loaded.
"loaded" : function () {
me.state.set(me.state.MENU, new game.TitleScreen());
me.state.set(me.state.PLAY, new game.PlayScreen());
me.entityPool.add("mainPlayer", game.PlayerEntity);
me.entityPool.add("CoinEntity", game.CoinEntity);
me.entityPool.add("EnemyEntity",game.EnemyEntity);
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);
}
};
game.resources = [
/*{name: ,
type: ,
src:
}*/
{name:"area01_level_tiles",
type:"image",
src:"data/img/map/area01_level_tiles.png"
},
{name:"gripe_run_right",
type:"image",
src:"data/img/sprite/gripe_run_right.png"
},
{name:"area01",
type:"tmx",
src:"data/map/area01.tmx"
},
{name:"area01_bkg0",
type:"image",
src:"data/img/area01_bkg0.png"},
{name:"area01_bkg1",
type:"image",
src:"data/img/area01_bkg1.png"},
{name:"spinning_coin_gold" ,
type:"image" ,
src: "data/img/sprite/spinning_coin_gold.png"
},
{name: "wheelie_right" ,
type:"image" ,
src:"data/img/sprite/wheelie_right.png"
},
{ name: 'metatiles32x32', type: 'image', src: "data/img/map/metatiles32x32.png"}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment