Refactored self into player controller

This commit is contained in:
Jeremiah Billmann
2013-07-12 18:38:08 -04:00
parent 906bf3719a
commit 0f00f1216e
3 changed files with 26 additions and 34 deletions
+19 -29
View File
@@ -18,7 +18,6 @@ var GarageServerIO = (function (socketio) {
"use strict";
function StateController() {
this.state = {};
this.clientTime = 0;
this.renderTime = 0;
this.physicsDelta = 0.0;
@@ -29,6 +28,7 @@ var GarageServerIO = (function (socketio) {
this.pingInterval = 2000;
this.clientSidePrediction = false;
this.smoothingFactor = 1;
this.worldState = {};
}
StateController.prototype = {
setTime: function (serverTime) {
@@ -74,6 +74,7 @@ var GarageServerIO = (function (socketio) {
this.updates = [];
this.id = id;
this.state = {};
this.inputController = new InputController();
}
Entity.prototype = {
addUpdate: function (state, seq, time) {
@@ -153,7 +154,6 @@ var GarageServerIO = (function (socketio) {
_socket = null,
_options = null,
_stateController = new StateController(),
_inputController = new InputController(),
_playerController = new PlayerController(),
_entityController = new EntityController(),
@@ -165,7 +165,8 @@ var GarageServerIO = (function (socketio) {
registerSocketEvents = function () {
_socket.on('connect', function () {
_stateController.id = _socket.id;
_stateController.id = _socket.socket.sessionid;
_playerController.add(_stateController.id);
if (_options.onPlayerConnect) {
_options.onPlayerConnect();
}
@@ -183,6 +184,8 @@ var GarageServerIO = (function (socketio) {
_stateController.pingInterval = data.pingInterval;
_stateController.clientSidePrediction = data.clientSidePrediction;
_stateController.smoothingFactor = data.smoothingFactor;
_stateController.worldState = data.worldState;
setInterval(function (){
_socket.emit('ping', new Date().getTime());
}, _stateController.pingInterval);
@@ -235,11 +238,15 @@ var GarageServerIO = (function (socketio) {
},
addInput = function (clientInput) {
_inputController.add(clientInput);
if (_stateController.clientSidePrediction && _options.onUpdatePlayerPhysics) {
_stateController.state = _options.onUpdatePlayerPhysics(_stateController.state, [{ input: clientInput }], _stateController.physicsDelta);
}
_socket.emit('input', [ clientInput, _inputController.sequenceNumber, _stateController.renderTime ]);
_playerController.entities.some(function (player) {
if (player.id === _stateController.id) {
if (_stateController.clientSidePrediction && _options.onUpdatePlayerPhysics) {
player.inputController.add(clientInput);
player.state = _options.onUpdatePlayerPhysics(player.state, [{ input: clientInput }], _stateController.physicsDelta);
}
_socket.emit('input', [ clientInput, player.inputController.sequenceNumber, _stateController.renderTime ]);
}
});
},
getStates = function (stateCallback) {
@@ -251,7 +258,7 @@ var GarageServerIO = (function (socketio) {
processEntityStatesCurrent(_entityController);
processEntityStatesCurrent(_playerController);
}
stateCallback(_stateController.state, _playerController.entities, _entityController.entities);
stateCallback(_playerController.entities, _entityController.entities);
},
removePlayer = function (id) {
@@ -267,11 +274,7 @@ var GarageServerIO = (function (socketio) {
updatePlayers = function (data) {
data.playerStates.forEach(function (playerState) {
if (_socket.socket.sessionid === playerState[0]) {
updateSelf(playerState);
} else {
updatePlayer(playerState, data.time);
}
updateEntity(_playerController, playerState, data.time);
if (_options.onPlayerUpdate) {
_options.onPlayerUpdate(playerState[1]);
@@ -279,19 +282,6 @@ var GarageServerIO = (function (socketio) {
});
},
updateSelf = function (playerState) {
_stateController.state = playerState[1];
_inputController.remove(playerState[2]);
if (_stateController.clientSidePrediction && _inputController.any()) {
_stateController.state = _options.onUpdatePlayerPhysics(_stateController.state, _inputController.inputs, _stateController.physicsDelta);
}
},
updatePlayer = function (playerState, time) {
updateEntity(_playerController, playerState, time);
},
updateEntities = function (data) {
data.entityStates.forEach(function (entityState) {
updateEntity(_entityController, entityState, data.time);
@@ -319,7 +309,7 @@ var GarageServerIO = (function (socketio) {
processEntityStatesCurrent = function (entityController) {
entityController.entities.forEach(function (entity) {
if (entity.anyUpdates()) {
if (entity.anyUpdates() && !entity.inputController.any()) {
entity.state = entity.latestUpdate().state;
}
});
@@ -328,7 +318,7 @@ var GarageServerIO = (function (socketio) {
processEntityStatesInterpolated = function (entityController) {
var positions, amount, newState;
entityController.entities.forEach(function (entity) {
if (entity.anyUpdates()) {
if (entity.anyUpdates() && !entity.inputController.any()) {
positions = entity.surroundingPositions(_stateController.renderTime);
if (positions.previous && positions.target) {
amount = getInterpolatedAmount(positions.previous.time, positions.target.time);
+4 -4
View File
@@ -15,8 +15,7 @@ $(function () {
//Render Loop
function () {
ctxCanvas.clearRect(0, 0, canvas.width, canvas.height);
GarageServerIO.getStates(function (selfState, playerStates, entityStates) {
GarageServerIO.getStates(function (playerStates, entityStates) {
playerStates.forEach(function (player) {
ctxCanvas.fillRect(player.state.x, player.state.y, 15, 15);
});
@@ -24,8 +23,6 @@ $(function () {
entityStates.forEach(function (entity) {
ctxCanvas.fillRect(entity.state.x, entity.state.y, 15, 15);
});
ctxCanvas.fillRect(selfState.x, selfState.y, 15, 15);
});
},
//Update Loop
@@ -42,6 +39,9 @@ $(function () {
if (keyboard.pressed('up')) {
GarageServerIO.addInput('up');
}
if (keyboard.pressed('space')) {
GarageServerIO.addInput('space');
}
}
);
});
+3 -1
View File
@@ -9,6 +9,7 @@ options = {
interpolationDelay: 100,
smoothingFactor: 0.3,
pingInterval: 2000,
worldState: {},
onPlayerConnect: function (socket),
onPlayerInput: function (socket, input),
onPlayerDisconnect: function (socket),
@@ -36,7 +37,8 @@ GarageServer.prototype.registerSocketEvents = function (options) {
interpolation: options.interpolation ? options.interpolation : false,
interpolationDelay: options.interpolationDelay ? options.interpolationDelay : 100,
pingInterval: options.pingInterval ? options.pingInterval : 2000,
clientSidePrediction: options.clientSidePrediction ? options.clientSidePrediction : false
clientSidePrediction: options.clientSidePrediction ? options.clientSidePrediction : false,
worldState: options.worldState ? options.worldState : {}
});
self.onPlayerConnect(socket, options);