mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-08-17 11:12:55 +08:00
Finished initial server refactor for #17
This commit is contained in:
+9
-2
@@ -7,12 +7,13 @@ function Game (sockets) {
|
||||
this.physicsInterval = 15;
|
||||
this.physicsDelta = this.physicsInterval / 1000;
|
||||
this.physicsIntervalId = 0;
|
||||
|
||||
|
||||
this.server = garageServer.createGarageServer(sockets,
|
||||
{
|
||||
logging: true,
|
||||
interpolation: true,
|
||||
clientSidePrediction: true,
|
||||
smoothingFactor: this.physicsDelta * 20,
|
||||
onUpdatePlayerPhysics: gamePhysics.onUpdatePlayerPhysics,
|
||||
onUpdateEntityPhysics: gamePhysics.onUpdateEntityPhysics
|
||||
});
|
||||
@@ -20,10 +21,16 @@ function Game (sockets) {
|
||||
|
||||
Game.prototype.start = function () {
|
||||
var self = this;
|
||||
this.server.start();
|
||||
this.physicsIntervalId = setInterval(function () { self.update(); }, this.physicsInterval);
|
||||
this.server.start();
|
||||
};
|
||||
|
||||
Game.prototype.update = function () {
|
||||
var players = this.server.getPlayers(),
|
||||
self = this;
|
||||
|
||||
players.forEach(function (player) {
|
||||
var newState = gamePhysics.getNewPlayerState(player.state, player.inputs, self.physicsDelta);
|
||||
self.server.updatePlayerState(player.id, newState);
|
||||
});
|
||||
};
|
||||
@@ -21,6 +21,27 @@
|
||||
return state;
|
||||
};
|
||||
|
||||
exports.getNewPlayerState = function (state, inputs, deltaTime) {
|
||||
var i = 0;
|
||||
|
||||
if (!state.x && !state.y) {
|
||||
state.x = 0;
|
||||
state.y = 0;
|
||||
}
|
||||
for (i = 0; i < inputs.length; i ++) {
|
||||
if (inputs[i].input === 'left') {
|
||||
state.x -= (50 * deltaTime);
|
||||
} else if (inputs[i].input === 'right') {
|
||||
state.x += (50 * deltaTime);
|
||||
} else if (inputs[i].input === 'down') {
|
||||
state.y += (50 * deltaTime);
|
||||
} else if (inputs[i].input === 'up') {
|
||||
state.y -= (50 * deltaTime);
|
||||
}
|
||||
}
|
||||
return state;
|
||||
};
|
||||
|
||||
exports.onUpdateEntityPhysics = function (id, state, deltaTime) {
|
||||
if (!state.x && !state.y) {
|
||||
state.x = 0;
|
||||
|
||||
Reference in New Issue
Block a user