From 31a0c59f36d63cdb4eda2eec854e7218e243730e Mon Sep 17 00:00:00 2001 From: Amir Rajan Date: Sun, 8 Dec 2013 14:39:56 -0600 Subject: [PATCH] moved hit boxes to world.js (formerly called gravity.js), in preperation for getting death results undertest --- common/gravity.js | 43 ----------- common/world.js | 99 ++++++++++++++++++++++++++ lib/bot.js | 1 - lib/easyBot.js | 4 +- lib/engine.js | 81 ++++----------------- lib/game.js | 17 +++++ lib/player.js | 18 ++--- package.json | 2 +- server.js | 1 - specifications/describe_deaths_spec.js | 25 +++++++ specifications/describe_game_spec.js | 4 +- views/game.ejs | 2 +- 12 files changed, 169 insertions(+), 128 deletions(-) delete mode 100644 common/gravity.js create mode 100644 common/world.js create mode 100644 specifications/describe_deaths_spec.js diff --git a/common/gravity.js b/common/gravity.js deleted file mode 100644 index bf259a4..0000000 --- a/common/gravity.js +++ /dev/null @@ -1,43 +0,0 @@ -(function() { - var jumpPower = 25; - var backPedalY = 20; - var backPedalX = 10; - var kickDelta = 10; - var downwardForce = 1; - var stageBoundary = { left: 0, right: 1280 }; - 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.jumpPower = jumpPower; - exports.backPedalY = backPedalY; - exports.backPedalX = backPedalX; - exports.kickDelta = 10; - exports.tick = tick; - exports.downwardForce = downwardForce; - exports.stageBoundary = stageBoundary; -})(); diff --git a/common/world.js b/common/world.js new file mode 100644 index 0000000..bbdb9be --- /dev/null +++ b/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; +})(); diff --git a/lib/bot.js b/lib/bot.js index 4f3d236..f4f4374 100644 --- a/lib/bot.js +++ b/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) { diff --git a/lib/easyBot.js b/lib/easyBot.js index c0e4816..7815ab1 100644 --- a/lib/easyBot.js +++ b/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; diff --git a/lib/engine.js b/lib/engine.js index c75857d..d03c06a 100644 --- a/lib/engine.js +++ b/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 = [ ]; } @@ -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) { @@ -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); @@ -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) { @@ -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; } @@ -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; diff --git a/lib/game.js b/lib/game.js index 46b9e0e..332ff30 100644 --- a/lib/game.js +++ b/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) { @@ -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); diff --git a/lib/player.js b/lib/player.js index 2248d7c..db63738 100644 --- a/lib/player.js +++ b/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)); @@ -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() { @@ -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; @@ -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)); }; } diff --git a/package.json b/package.json index 5a2d64a..41c545a 100644 --- a/package.json +++ b/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": { diff --git a/server.js b/server.js index 3e2c326..889306e 100644 --- a/server.js +++ b/server.js @@ -45,7 +45,6 @@ function broadcast(game) { emit(game.id, 'gamestate', { frame: engine.frame(), players: engine.players(game), - boxes: engine.boxes(), }); game.shouldBroadcast = false; diff --git a/specifications/describe_deaths_spec.js b/specifications/describe_deaths_spec.js new file mode 100644 index 0000000..f1f0fe2 --- /dev/null +++ b/specifications/describe_deaths_spec.js @@ -0,0 +1,25 @@ +var engine = require('../lib/engine.js'); +var Game = require('../lib/game.js').Game; +var game = new Game(); +var _ = require("underscore"); + +describe('player kills another', function() { + beforeEach(function() { engine.reset(game); }); + + it('registers a death on tick', function() { + var standingPlayer = game.placePlayer({ + id: _.uniqueId(), + name: "standing player", + x: game.centerStage(), + y: game.ground(), + state: "standing" + }); + + var attackingPlayer = game.placePlayer({ + id: _.uniqueId(), + name: "standing player", + state: "kicking", + position: { footOverlaps: standingPlayer.head() } + }); + }); +}); diff --git a/specifications/describe_game_spec.js b/specifications/describe_game_spec.js index 47cdb25..bdc2c26 100644 --- a/specifications/describe_game_spec.js +++ b/specifications/describe_game_spec.js @@ -100,8 +100,8 @@ describe('hit boxes', function() { victim.direction = -1; victim.state = "standing"; - var foot = attacker.foot(engine.boxes()); - var bodyParts = victim.boxes(engine.boxes()); + var foot = attacker.foot(); + var bodyParts = victim.boxes(); engine.tick(game); expect(victim.state).toBe('dying'); expect(attacker.state).toBe('kicking'); diff --git a/views/game.ejs b/views/game.ejs index 8a8c1e3..2cf59f5 100644 --- a/views/game.ejs +++ b/views/game.ejs @@ -9,7 +9,7 @@ - +