Skip to content

Commit

Permalink
moved hit boxes to world.js (formerly called gravity.js), in preperat…
Browse files Browse the repository at this point in the history
…ion for getting death results undertest
  • Loading branch information
amirrajan committed Dec 8, 2013
1 parent b690077 commit 31a0c59
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 128 deletions.
43 changes: 0 additions & 43 deletions common/gravity.js

This file was deleted.

99 changes: 99 additions & 0 deletions common/world.js
@@ -0,0 +1,99 @@
(function() {
var jumpPower = 25;
var backPedalY = 20;
var backPedalX = 10;
var kickDelta = 10;
var downwardForce = 1;
var stageBoundary = { left: 0, right: 1280 };
var boxes = {
playerHeight: 150,
playerCenter: 75.0/2.0,
"1": {
standing: [
{ x: -10, y: -140, x2: 8, y2: -120 },
{ x: -30, y: -115, x2: 15, y2: -80 },
{ x: -20, y: -80, x2: 15, y2: -40 },
{ x: -25, y: -40, x2: 25, y2: -10 }
],
jumping: [
{ x: -5, y: -135, x2: 15, y2: -115 },
{ x: -12, y: -115, x2: 12, y2: -65 },
{ x: -5, y: -70, x2: 7.5, y2: -12 },
{ x: 7.5, y: -70, x2: 25, y2: -50 },
{ x: -12, y: -50, x2: 12.5, y2: -30 }
],
kicking: [
{ x: -8, y: -120, x2: 8, y2: -100 },
{ x: -20, y: -105, x2: 5, y2: -95 },
{ x: -35, y: -95, x2: 0, y2: -77 },
{ x: -25, y: -77, x2: 20, y2: -58 },
{ x: -10, y: -60, x2: 10, y2: -40 },
{ x: 0, y: -40, x2: 15, y2: -25 },
{ x: 7, y: -35, x2: 25, y2: -20 },
{ x: 20, y: -28, x2: 35, y2: -10 } //foot
]
},
"-1": {
standing: [
{ x: -8, y: -140, x2: 10, y2: -120 },
{ x: -15, y: -115, x2: 30, y2: -80 },
{ x: -15, y: -80, x2: 20, y2: -40 },
{ x: -25, y: -40, x2: 25, y2: -10 }
],
jumping: [
{ x: -15, y: -135, x2: 5, y2: -115 },
{ x: -12, y: -115, x2: 12, y2: -65 },
{ x: -7.5, y: -70, x2: 5, y2: -12 },
{ x: -25, y: -70, x2: -7.5, y2: -50 },
{ x: -12.5, y: -50, x2: 12, y2: -30 }
],
kicking: [
{ x: -8, y: -120, x2: 8, y2: -100 },
{ x: -5, y: -105, x2: 20, y2: -95 },
{ x: 0, y: -95, x2: 35, y2: -77 },
{ x: -20, y: -77, x2: -25, y2: -58 },
{ x: -10, y: -60, x2: 10, y2: -40 },
{ x: -15, y: -40, x2: 0, y2: -25 },
{ x: -25, y: -35, x2: -7, y2: -20 },
{ x: -35, y: -28, x2: -20, y2: -10 } //foot
]
}
};

stageBoundary.center = (stageBoundary.right + stageBoundary.left) / 2;

function tick(player) {
if(player.state == "dying") return;

if(player.state == "jumping") {
player.y -= player.velocityY;
player.x -= player.velocityX;
player.velocityY -= downwardForce;
}

if(player.state == "kicking") {
player.y += kickDelta;
player.x += (kickDelta) * player.direction;
}

if(player.y > 0) {
player.y = 0;
player.state = "standing";
player.falling = false;
}
}

if(typeof exports == "undefined") {
app.gravity = { };
exports = app.gravity;
}

exports.boxes = boxes;
exports.jumpPower = jumpPower;
exports.backPedalY = backPedalY;
exports.backPedalX = backPedalX;
exports.kickDelta = 10;
exports.tick = tick;
exports.downwardForce = downwardForce;
exports.stageBoundary = stageBoundary;
})();
1 change: 0 additions & 1 deletion lib/bot.js
@@ -1,6 +1,5 @@
var _ = require('underscore');
var Player = require('./player.js').Player;
var gravity = require("../common/gravity.js");
var EasyBot = require("./easyBot.js").EasyBot;

function add(game) {
Expand Down
4 changes: 2 additions & 2 deletions lib/easyBot.js
@@ -1,6 +1,6 @@
var _ = require('underscore');
var gravity = require("../common/gravity.js");
var stageBoundary = gravity.stageBoundary;
var world = require("../common/world.js");
var stageBoundary = world.stageBoundary;

function standingRoutine(player, bot) {
if(player.state != "standing") return false;
Expand Down
81 changes: 13 additions & 68 deletions lib/engine.js
@@ -1,62 +1,8 @@
var _ = require("underscore");
var gravity = require("../common/gravity.js");
var world = require("../common/world.js");
var Player = require("./player.js").Player;
var frame = 0;
var fps = 60.0;
var boxes = {
playerHeight: 150,
playerCenter: 75.0/2.0,
"1": {
standing: [
{ x: -10, y: -140, x2: 8, y2: -120 },
{ x: -30, y: -115, x2: 15, y2: -80 },
{ x: -20, y: -80, x2: 15, y2: -40 },
{ x: -25, y: -40, x2: 25, y2: -10 }
],
jumping: [
{ x: -5, y: -135, x2: 15, y2: -115 },
{ x: -12, y: -115, x2: 12, y2: -65 },
{ x: -5, y: -70, x2: 7.5, y2: -12 },
{ x: 7.5, y: -70, x2: 25, y2: -50 },
{ x: -12, y: -50, x2: 12.5, y2: -30 }
],
kicking: [
{ x: -8, y: -120, x2: 8, y2: -100 },
{ x: -20, y: -105, x2: 5, y2: -95 },
{ x: -35, y: -95, x2: 0, y2: -77 },
{ x: -25, y: -77, x2: 20, y2: -58 },
{ x: -10, y: -60, x2: 10, y2: -40 },
{ x: 0, y: -40, x2: 15, y2: -25 },
{ x: 7, y: -35, x2: 25, y2: -20 },
{ x: 20, y: -28, x2: 35, y2: -10 } //foot
]
},
"-1": {
standing: [
{ x: -8, y: -140, x2: 10, y2: -120 },
{ x: -15, y: -115, x2: 30, y2: -80 },
{ x: -15, y: -80, x2: 20, y2: -40 },
{ x: -25, y: -40, x2: 25, y2: -10 }
],
jumping: [
{ x: -15, y: -135, x2: 5, y2: -115 },
{ x: -12, y: -115, x2: 12, y2: -65 },
{ x: -7.5, y: -70, x2: 5, y2: -12 },
{ x: -25, y: -70, x2: -7.5, y2: -50 },
{ x: -12.5, y: -50, x2: 12, y2: -30 }
],
kicking: [
{ x: -8, y: -120, x2: 8, y2: -100 },
{ x: -5, y: -105, x2: 20, y2: -95 },
{ x: 0, y: -95, x2: 35, y2: -77 },
{ x: -20, y: -77, x2: -25, y2: -58 },
{ x: -10, y: -60, x2: 10, y2: -40 },
{ x: -15, y: -40, x2: 0, y2: -25 },
{ x: -25, y: -35, x2: -7, y2: -20 },
{ x: -35, y: -28, x2: -20, y2: -10 } //foot
]
}
};

function reset(game) { game.players = [ ]; }

Expand Down Expand Up @@ -86,12 +32,12 @@ function gravityTick(player) {
if(player.isJumping()) {
player.y -= player.velocityY;
player.x -= player.velocityX;
player.velocityY -= gravity.downwardForce;
player.velocityY -= world.downwardForce;
}

if(player.isKicking()) {
player.y += gravity.kickDelta;
player.x += (gravity.kickDelta) * player.direction;
player.y += world.kickDelta;
player.x += (world.kickDelta) * player.direction;
}

if(player.y > 0) {
Expand All @@ -108,7 +54,7 @@ function tick(game) {
var killResultsResults = [];

_.each(game.players, function(player) {
gravity.tick(player);
world.tick(player);

var killResults = kills(player, livePlayers);

Expand Down Expand Up @@ -162,9 +108,9 @@ function kills(player, livePlayers) {
if(target == player) return;
if(target.state == "dying") return;

var foot = player.foot(boxes);
var head = target.head(boxes);
var bodyParts = target.boxes(boxes);
var foot = player.foot();
var head = target.head();
var bodyParts = target.boxes();
var hitRegistered = false;

_.each(bodyParts, function(bodyPart) {
Expand All @@ -184,12 +130,12 @@ function kills(player, livePlayers) {
function applyBoundaryDeath(player) {
if(player.state == "dying") return false;

if(player.x <= gravity.stageBoundary.left) {
if(player.x <= world.stageBoundary.left) {
killPlayer(player);
return true;
}

if(player.x >= gravity.stageBoundary.right) {
if(player.x >= world.stageBoundary.right) {
killPlayer(player);
return true;
}
Expand All @@ -213,16 +159,15 @@ function hasCollision(points1, points2) {
}

exports.fps = fps;
exports.stageBoundary = gravity.stageBoundary;
exports.stageBoundary = world.stageBoundary;
exports.up = up;
exports.down = down;
exports.left = left;
exports.right = right;
exports.tick = tick;
exports.reset = reset;
exports.jumpPower = gravity.jumpPower;
exports.kickDelta = gravity.kickDelta;
exports.boxes = function() { return boxes; };
exports.jumpPower = world.jumpPower;
exports.kickDelta = world.kickDelta;
exports.players = function(game) { return game.players; };
exports.frame = function() { return frame; };
exports.hasCollision = hasCollision;
17 changes: 17 additions & 0 deletions lib/game.js
@@ -1,4 +1,5 @@
var _ = require("underscore");
var world = require("../common/world.js");
var Player = require("./player.js").Player;

function Game(gameId) {
Expand All @@ -10,6 +11,22 @@ function Game(gameId) {
return _.findWhere(this.players, { id: playerId });
};

this.centerStage = function() {
return world.stageBoundary.center;
};

this.ground = function() {
return 0;
};

this.placePlayer = function(args) {
var player = this.addPlayer(args.id, args.name);
player.state = args.state;
player.x = args.x;
player.y = args.y;
return player;
};

this.addPlayer = function(playerId, playerName) {
if(this.getPlayer(playerId)) return;
var player = new Player(playerId);
Expand Down
18 changes: 9 additions & 9 deletions lib/player.js
@@ -1,5 +1,5 @@
var _ = require("underscore");
var gravity = require("../common/gravity.js");
var world = require("../common/world.js");

function clone(obj) {
return JSON.parse(JSON.stringify(obj));
Expand Down Expand Up @@ -33,7 +33,7 @@ function Player(playerId) {
this.up = function() {
if(this.isDying()) return;
if(!this.isStanding()) return;
this.jump(0, gravity.jumpPower);
this.jump(0, world.jumpPower);
this.velocityX = 0;
};
this.left = function() {
Expand All @@ -53,11 +53,11 @@ function Player(playerId) {
this.down = function() {
if(this.isDying()) return;
if(!this.isStanding()) return;
this.jump(gravity.backPedalX * this.direction, gravity.backPedalY);
this.jump(world.backPedalX * this.direction, world.backPedalY);
};
this.boxes = function(boxes) {
this.boxes = function() {
if(this.isDying()) return null;
var boxesForUser = clone(boxes[this.direction][this.state]);
var boxesForUser = clone(world.boxes[this.direction][this.state]);

_.each(boxesForUser, function(box) {
box.x += this.x;
Expand All @@ -68,13 +68,13 @@ function Player(playerId) {

return boxesForUser;
};
this.foot = function(boxes) {
this.foot = function() {
if(!this.isKicking()) return null;

return _.last(this.boxes(boxes));
return _.last(this.boxes(world.boxes));
};
this.head = function(boxes) {
return _.first(this.boxes(boxes));
this.head = function() {
return _.first(this.boxes(world.boxes));
};
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "nodekick",
"version": "0.0.0-33",
"version": "0.0.0-34",
"decription": "multiplayer fighting game",
"main": "server.js",
"scripts": {
Expand Down
1 change: 0 additions & 1 deletion server.js
Expand Up @@ -45,7 +45,6 @@ function broadcast(game) {
emit(game.id, 'gamestate', {
frame: engine.frame(),
players: engine.players(game),
boxes: engine.boxes(),
});

game.shouldBroadcast = false;
Expand Down

0 comments on commit 31a0c59

Please sign in to comment.