diff --git a/client/garageserver.io.js b/client/garageserver.io.js index db80fae..f7c96c7 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -130,15 +130,11 @@ var GarageServerIO = (function (socketio) { function EntityController() { this.entities = []; } - - function PlayerController() { - EntityController.call(this); - } - PlayerController.prototype = { + EntityController.prototype = { add: function (id) { - var player = new Player(id); - this.entities.push(player); - return player; + var entity = new Entity(id); + this.entities.push(entity); + return entity; }, remove: function (id) { for (var i = 0; i < this.entities.length; i ++) { @@ -150,6 +146,15 @@ var GarageServerIO = (function (socketio) { } }; + function PlayerController() { + EntityController.call(this); + } + PlayerController.prototype.add = function (id) { + var player = new Player(id); + this.entities.push(player); + return player; + }; + var _io = socketio, _socket = null, _options = null, diff --git a/example/public/javascripts/game.js b/example/public/javascripts/game.js index 4ce1287..8f0d5c4 100644 --- a/example/public/javascripts/game.js +++ b/example/public/javascripts/game.js @@ -24,7 +24,7 @@ $(function () { }); entityStates.forEach(function (entity) { - ctxCanvas.fillRect(entity.state.x, entity.state.y, 15, 15); + ctxCanvas.fillRect(entity.state.x, entity.state.y, 5, 5); }); }); }, diff --git a/example/shared/core.js b/example/shared/core.js index 3cb5cfe..b0011be 100644 --- a/example/shared/core.js +++ b/example/shared/core.js @@ -6,19 +6,26 @@ if (!state.x && !state.y) { state.x = 0; state.y = 0; + state.direction = 'right'; } for (i = 0; i < inputs.length; i ++) { if (inputs[i].input === 'left') { state.x -= (50 * deltaTime); + state.direction = 'left'; } else if (inputs[i].input === 'right') { state.x += (50 * deltaTime); + state.direction = 'right'; } else if (inputs[i].input === 'down') { state.y += (50 * deltaTime); + state.direction = 'down'; } else if (inputs[i].input === 'up') { state.y -= (50 * deltaTime); + state.direction = 'up'; } else if (inputs[i].input === 'space') { if (garageServer) { - garageServer.addEntity(new guid(), { x: state.x, y: state.y, direction: '' }); + var newId = guid(); + garageServer.addEntity(newId); + garageServer.updateEntityState(newId, { x: state.x, y: state.y, direction: state.direction } ); } } } @@ -26,7 +33,17 @@ }; exports.getNewEntityState = function (state, deltaTime) { - + if (state.direction === 'left') { + state.x -= (75 * deltaTime); + } else if (state.direction === 'right') { + state.x += (75 * deltaTime); + } else if (state.direction === 'down') { + state.y += (75 * deltaTime); + } else if (state.direction === 'up') { + state.y -= (75 * deltaTime); + } + + return state; }; exports.getInterpolatedState = function (previousState, targetState, amount) { diff --git a/lib/server/garageserver.io.js b/lib/server/garageserver.io.js index e1bf673..06654e7 100644 --- a/lib/server/garageserver.io.js +++ b/lib/server/garageserver.io.js @@ -117,8 +117,8 @@ GarageServer.prototype.updateEntityState = function (id, state) { this.game.updateEntityState(id, state); }; -GarageServer.prototype.addEntity = function (id, state) { - this.game.addEntity(id, state); +GarageServer.prototype.addEntity = function (id) { + this.game.addEntity(id); }; GarageServer.prototype.removeEntity = function (id) { diff --git a/lib/server/garageservergame.js b/lib/server/garageservergame.js index 2f2b073..8a3f6ae 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -90,8 +90,8 @@ GarageServerGame.prototype.removePlayer = function (id) { this.playerController.remove(id); }; -GarageServerGame.prototype.addEntity = function (id, state) { - this.entityController.add(id, state); +GarageServerGame.prototype.addEntity = function (id) { + this.entityController.add(id); }; GarageServerGame.prototype.removeEntity = function (id) {