diff --git a/example/app.js b/example/app.js index 0b97c98..3fa2971 100644 --- a/example/app.js +++ b/example/app.js @@ -49,5 +49,6 @@ garageServer.createGarageServer(sockets,{ logging: true, interpolation: true, clientSidePrediction: true, - onUpdatePlayerPhysics: gamePhysics.onUpdatePlayerPhysics -}); + onUpdatePlayerPhysics: gamePhysics.onUpdatePlayerPhysics, + onUpdateEntityPhysics: gamePhysics.onUpdateEntityPhysics +}); \ No newline at end of file diff --git a/example/shared/core.js b/example/shared/core.js index 717535c..1c082e0 100644 --- a/example/shared/core.js +++ b/example/shared/core.js @@ -3,7 +3,7 @@ exports.onUpdatePlayerPhysics = function (id, state, inputs, deltaTime) { var i = 0; - if (!state.x && !state.y) { + if (!state.x && !state.y) { state.x = 0; state.y = 0; } @@ -20,5 +20,13 @@ } return state; }; + + exports.onUpdateEntityPhysics = function (id, state, deltaTime) { + if (!state.x && !state.y) { + state.x = 0; + state.y = 0; + } + state.x += (10 * deltaTime); + }; })(typeof exports === 'undefined' ? window : exports); \ No newline at end of file diff --git a/lib/server/controllers/entitycontroller.js b/lib/server/controllers/entitycontroller.js index 6670af4..6ac0eb1 100644 --- a/lib/server/controllers/entitycontroller.js +++ b/lib/server/controllers/entitycontroller.js @@ -1,3 +1,5 @@ +var Entity = require('../entities/entity'); + exports = module.exports = EntityController; function EntityController () { @@ -6,7 +8,21 @@ function EntityController () { EntityController.prototype = { add: function (id) { - + var newEntity, entityFound = false; + + this.entities.some(function (entity) { + if (entity.id === id) { + newEntity = entity; + entityFound = true; + return true; + } + }); + + if (!entityFound) { + newEntity = new Entity(id); + this.entities.push(newEntity); + } + return newEntity; }, remove: function (id) { diff --git a/lib/server/entities/entity.js b/lib/server/entities/entity.js index f6c3c14..dd3045f 100644 --- a/lib/server/entities/entity.js +++ b/lib/server/entities/entity.js @@ -1,7 +1,7 @@ exports = module.exports = Entity; function Entity (id) { - this.state; + this.state = {}; this.sequence = 1; this.id = id; this.stateHistory = []; @@ -10,6 +10,7 @@ function Entity (id) { Entity.prototype = { addState: function (state, executionTime) { var minTime, spliceTo = 0; + this.state = state; this.stateHistory.push({ state: state, executionTime: executionTime }); minTime = this.stateHistory[this.stateHistory.length - 1].executionTime - 1000; diff --git a/lib/server/garageserver.io.js b/lib/server/garageserver.io.js index f1d0d87..e0589c6 100644 --- a/lib/server/garageserver.io.js +++ b/lib/server/garageserver.io.js @@ -14,7 +14,8 @@ options = { onPlayerDisconnect: function (socket), onPing: function (socket, data), onPlayerState: function (socket, data), - onUpdatePlayerPhysics: function (id, state, inputs, deltaTime) + onUpdatePlayerPhysics: function (id, state, inputs, deltaTime), + onUpdateEntityPhysics: function (id, state, deltaTime) } */ diff --git a/lib/server/garageservergame.js b/lib/server/garageservergame.js index 0b56dbf..c8bcb9d 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -38,7 +38,9 @@ GarageServerGame.prototype.getState = function (controller) { }; GarageServerGame.prototype.updatePhysics = function (options) { - var self = this; + var self = this, + currentTime = new Date().getTime() - this.startTime; + this.playerController.entities.forEach(function (player) { if (player.inputs.length > 0) { if (options.onUpdatePlayerPhysics) { @@ -48,6 +50,12 @@ GarageServerGame.prototype.updatePhysics = function (options) { } } }); + this.entityController.entities.forEach(function (entity) { + if (options.onUpdateEntityPhysics) { + entity.addState(options.onUpdateEntityPhysics(entity.id, entity.state, self.physicsDelta), currentTime); + entity.sequence += 1; + } + }); }; GarageServerGame.prototype.addPlayer = function (client) {