Skip to content

Instantly share code, notes, and snippets.

@caubry
Last active January 3, 2016 17:39
Show Gist options
  • Save caubry/8497474 to your computer and use it in GitHub Desktop.
Save caubry/8497474 to your computer and use it in GitHub Desktop.
GameEngineClass = Class.extend({
move_dir: new Vec2(0,0),
dirVec: new Vec2(0,0),
gPlayer0: {
pos: {
x: 100,
y: 100
},
walkSpeed: 1,
mpPhysBody: new BodyDef()
},
//-----------------------------
setup: function () {
gInputEngine.setup();
},
update: function () {
if (gInputEngine.actions['move-up']) {
gGameEngine.move_dir.y -= 1;
}
if (gInputEngine.actions['move-down']) {
gGameEngine.move_dir.y += 1;
}
if (gInputEngine.actions['move-left']) {
gGameEngine.move_dir.x -= 1;
}
if (gInputEngine.actions['move-right']) {
gGameEngine.move_dir.x += 1;
}
if (gGameEngine.move_dir.LengthSquared()) {
gGameEngine.move_dir.Normalize();
gGameEngine.move_dir.Multiply(gGameEngine.gPlayer0.walkSpeed);
}
gGameEngine.gPlayer0.mpPhysBody.setLinearVelocity(gGameEngine.move_dir.x, gGameEngine.move_dir.y);
if (gInputEngine.actions.fire0 || gInputEngine.actions.fire1) {
var playerInScreenSpace = {
x: gRenderEngine.getScreenPosition(this.gPlayer0.pos).x,
y: gRenderEngine.getScreenPosition(this.gPlayer0.pos).y
};
dirVec.x = gInputEngine.mouse.x - playerInScreenSpace.x;
dirVec.y = gInputEngine.mouse.y - playerInScreenSpace.y;
dirVec.normalize();
}
if (gInputEngine.state('fire-up')) {
gGameEngine.dirVec.y--;
}
else if (gInputEngine.state('fire-down')) {
gGameEngine.dirVec.y++;
}
if (gInputEngine.state('fire-left')) {
gGameEngine.dirVec.x--;
}
else if (gInputEngine.state('fire-right')) {
gGameEngine.dirVec.x++;
}
}
});
gGameEngine = new GameEngineClass();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment