From 0380e6389911f338fb4b5bcad553a9caa83ecd35 Mon Sep 17 00:00:00 2001 From: Jeremiah Billmann Date: Sun, 7 Jul 2013 15:46:35 -0400 Subject: [PATCH] Changed state fetch to return all states - allow client more control --- client/garageserver.io.js | 31 ++++++++++-------------------- example/public/javascripts/game.js | 13 +++++++++++-- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/client/garageserver.io.js b/client/garageserver.io.js index 7c69929..e9c9e1b 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -330,33 +330,27 @@ window.GarageServerIO = (function (window, socketio) { } }, - getPlayerStates = function (stateCallback) { - getStates(_playerController, stateCallback); - stateCallback(_stateController.state); - }, - - getEntityStates = function (stateCallback) { - getStates(_entityController, stateCallback); - }, - - getStates = function (controller, stateCallback) { + getStates = function (stateCallback) { if (_stateController.interpolation && _options.onInterpolation) { - getEntityStatesInterpolated(controller, stateCallback); + getEntityStatesInterpolated(_entityController); + getEntityStatesInterpolated(_playerController); } else { - getEntityStatesCurrent(controller, stateCallback); + getEntityStatesCurrent(_entityController); + getEntityStatesCurrent(_playerController); } + stateCallback(_stateController.state, _playerController.entities, _entityController.entities); }, - getEntityStatesCurrent = function (entityController, stateCallback) { + getEntityStatesCurrent = function (entityController) { entityController.entities.forEach(function (entity) { if (entity.anyUpdates()) { - stateCallback(entity.latestUpdate().state); + entity.currentState = entity.latestUpdate().state; } }); }, - getEntityStatesInterpolated = function (entityController, stateCallback) { + getEntityStatesInterpolated = function (entityController) { var positions, amount, newState; entityController.entities.forEach(function (entity) { if (entity.anyUpdates()) { @@ -365,10 +359,6 @@ window.GarageServerIO = (function (window, socketio) { amount = getInterpolatedAmount(positions.previous.time, positions.target.time); newState = _options.onInterpolation(entity.id, positions.previous.state, positions.target.state, amount); entity.currentState = newState = _options.onInterpolation(entity.id, entity.currentState, newState, _stateController.physicsDelta * 20); - stateCallback(entity.currentState); - } - else { - stateCallback(entity.currentState); } } }); @@ -387,8 +377,7 @@ window.GarageServerIO = (function (window, socketio) { start: start, update: update, addPlayerInput: addPlayerInput, - getPlayerStates: getPlayerStates, - getEntityStates: getEntityStates, + getStates: getStates, getPlayerId: getPlayerId, setPlayerState: setPlayerState }; diff --git a/example/public/javascripts/game.js b/example/public/javascripts/game.js index 1b82e7b..0851619 100644 --- a/example/public/javascripts/game.js +++ b/example/public/javascripts/game.js @@ -38,8 +38,17 @@ $(function () { ctxCanvas.clearRect(0, 0, canvas.width, canvas.height); - GarageServerIO.getPlayerStates(function (state) { - ctxCanvas.fillRect(state.x, state.y, 15, 15); + GarageServerIO.getStates(function (selfState, playerStates, entityStates) { + + playerStates.forEach(function (player) { + ctxCanvas.fillRect(player.currentState.x, player.currentState.y, 15, 15); + }); + + entityStates.forEach(function (entity) { + ctxCanvas.fillRect(entity.currentState.x, entity.currentState.y, 15, 15); + }); + + ctxCanvas.fillRect(selfState.x, selfState.y, 15, 15); }); } }); \ No newline at end of file