mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-07-12 00:40:09 +08:00
Refactored getStates for client
This commit is contained in:
@@ -132,18 +132,21 @@ GarageServerIO.addInput(input)
|
||||
**input**
|
||||
Type: object literal
|
||||
|
||||
#### getStates
|
||||
#### getPlayerStates
|
||||
---
|
||||
```js
|
||||
GarageServerIO.getStates(callback([, playerState], [, entityState]))
|
||||
GarageServerIO.getPlayerStates() : [, playerState]
|
||||
```
|
||||
|
||||
**callback**
|
||||
Type: function(playerStates, entityStates)
|
||||
|
||||
**playerStates**
|
||||
Type: array of object literals
|
||||
|
||||
#### getEntityStates
|
||||
---
|
||||
```js
|
||||
GarageServerIO.getEntityStates() : [, entityState]
|
||||
```
|
||||
|
||||
**entityStates**
|
||||
Type: array of object literals
|
||||
|
||||
|
||||
@@ -18,7 +18,8 @@ options = {
|
||||
api methods
|
||||
initializeGarageServer(path, options)
|
||||
addInput(input)
|
||||
getStates(callback([, playerState], [, entityState]))
|
||||
getPlayerStates : [, playerState]
|
||||
getEntityStates : [, entityState]
|
||||
getId() : playerid
|
||||
sendServerEvent(data)
|
||||
*/
|
||||
@@ -300,26 +301,37 @@ var GarageServerIO = (function (socketio) {
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
getStates = function (stateCallback) {
|
||||
var playerStates = [], entityStates = [];
|
||||
|
||||
getPlayerStates = function () {
|
||||
var playerStates = [];
|
||||
|
||||
if (_stateController.interpolation && _options.onInterpolation) {
|
||||
processEntityStatesInterpolated(_entityController);
|
||||
processEntityStatesInterpolated(_playerController);
|
||||
} else {
|
||||
processEntityStatesCurrent(_entityController);
|
||||
processEntityStatesCurrent(_playerController);
|
||||
}
|
||||
|
||||
_playerController.entities.forEach(function(player) {
|
||||
playerStates.push({ state: player.state });
|
||||
});
|
||||
|
||||
return playerStates;
|
||||
},
|
||||
|
||||
getEntityStates = function () {
|
||||
var entityStates = [];
|
||||
|
||||
if (_stateController.interpolation && _options.onInterpolation) {
|
||||
processEntityStatesInterpolated(_entityController);
|
||||
} else {
|
||||
processEntityStatesCurrent(_entityController);
|
||||
}
|
||||
|
||||
_entityController.entities.forEach(function(entity) {
|
||||
entityStates.push({ state: entity.state });
|
||||
});
|
||||
|
||||
stateCallback(playerStates, entityStates);
|
||||
return entityStates;
|
||||
},
|
||||
|
||||
removePlayer = function (id) {
|
||||
@@ -405,7 +417,8 @@ var GarageServerIO = (function (socketio) {
|
||||
return {
|
||||
initializeGarageServer: initializeGarageServer,
|
||||
addInput: addInput,
|
||||
getStates: getStates,
|
||||
getPlayerStates: getPlayerStates,
|
||||
getEntityStates: getEntityStates,
|
||||
getId: getId,
|
||||
sendServerEvent: sendServerEvent
|
||||
};
|
||||
|
||||
@@ -23,14 +23,14 @@ $(function () {
|
||||
//Render Loop
|
||||
function () {
|
||||
ctxCanvas.clearRect(0, 0, canvas.width, canvas.height);
|
||||
GarageServerIO.getStates(function (playerStates, entityStates) {
|
||||
playerStates.forEach(function (player) {
|
||||
ctxCanvas.fillRect(player.state.x, player.state.y, playerSize, playerSize);
|
||||
});
|
||||
|
||||
entityStates.forEach(function (entity) {
|
||||
ctxCanvas.fillRect(entity.state.x, entity.state.y, entitySize, entitySize);
|
||||
});
|
||||
var playerStates = GarageServerIO.getPlayerStates(),
|
||||
entityStates = GarageServerIO.getEntityStates();
|
||||
playerStates.forEach(function (player) {
|
||||
ctxCanvas.fillRect(player.state.x, player.state.y, playerSize, playerSize);
|
||||
});
|
||||
entityStates.forEach(function (entity) {
|
||||
ctxCanvas.fillRect(entity.state.x, entity.state.y, entitySize, entitySize);
|
||||
});
|
||||
},
|
||||
//Update Loop
|
||||
|
||||
Reference in New Issue
Block a user