mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-09-12 12:06:39 +08:00
Fixed client prediction and prepped for interpolation
This commit is contained in:
@@ -2,14 +2,14 @@ var garageServerGame = require('./garageservergame');
|
||||
|
||||
/*
|
||||
options = {
|
||||
onUpdatePhysics: function (state, inputs),
|
||||
physicsInterval: 15,
|
||||
playersInterval: 45,
|
||||
stateInterval: 45,
|
||||
logging: true,
|
||||
onPlayerConnect: function (socket),
|
||||
onPlayerInput: function (socket, input),
|
||||
onPlayerDisconnect: function (socket),
|
||||
onPing: function (socket, data),
|
||||
onUpdatePlayerPhysics: function (state, inputs),
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -74,7 +74,7 @@ GarageServer.prototype.onPlayerInput = function (socket, input, options) {
|
||||
};
|
||||
|
||||
GarageServer.prototype.onPing = function (socket, data, options) {
|
||||
socket.emit('ping', { 'pingTime': data, 'serverTime': new Date().getTime() });
|
||||
socket.emit('ping', data);
|
||||
if (options.onPing) {
|
||||
options.onPing(socket, data);
|
||||
}
|
||||
|
||||
@@ -2,27 +2,42 @@ function GarageServerGame(options) {
|
||||
var self = this;
|
||||
|
||||
this.players = [];
|
||||
this.entities = [];
|
||||
this.options = options;
|
||||
this.startTime = new Date().getTime();
|
||||
|
||||
this.physicsIntervalId = setInterval(function () { self.updatePhysics(options); }, options.physicsInterval ? options.physicsInterval : 15);
|
||||
this.playersIntervalId = setInterval(function () { self.updatePlayers(options); }, options.playersInterval ? options.playersInterval : 45);
|
||||
this.stateIntervalId = setInterval(function () { self.updateState(options); }, options.stateInterval ? options.stateInterval : 45);
|
||||
}
|
||||
|
||||
GarageServerGame.prototype.updateState = function (options) {
|
||||
this.updatePlayers(options);
|
||||
this.updateEntities(options);
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.updatePlayers = function (options) {
|
||||
var currentTime = new Date().getTime();
|
||||
for (var i = 0; i < this.players.length; i ++) {
|
||||
if(this.players[i].inputs.length > 0) {
|
||||
this.players[i].client.emit('update', { id: this.players[i].client.id, state: this.players[i].state, seq: this.players[i].sequence, timestamp: currentTime });
|
||||
this.players[i].client.broadcast.emit('update', { id: this.players[i].client.id, state: this.players[i].state, seq: this.players[i].sequence, timestamp: currentTime });
|
||||
}
|
||||
var currentTime = new Date().getTime() - this.startTime,
|
||||
state = { time: currentTime, playerStates: [] },
|
||||
i = 0;
|
||||
|
||||
for (i = 0; i < this.players.length; i ++) {
|
||||
state.playerStates.push({ id: this.players[i].client.id, state: this.players[i].state, seq: this.players[i].sequence });
|
||||
}
|
||||
|
||||
for (i = 0; i < this.players.length; i ++) {
|
||||
this.players[i].client.emit('update', state);
|
||||
}
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.updateEntities = function (options) {
|
||||
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.updatePhysics = function (options) {
|
||||
for (var i = 0; i < this.players.length; i ++) {
|
||||
if (this.players[i].inputs.length > 0) {
|
||||
if (this.options.onUpdatePhysics) {
|
||||
this.players[i].state = this.options.onUpdatePhysics(this.players[i].state, this.players[i].inputs);
|
||||
if (this.options.onUpdatePlayerPhysics) {
|
||||
this.players[i].state = this.options.onUpdatePlayerPhysics(this.players[i].state, this.players[i].inputs);
|
||||
}
|
||||
this.players[i].sequence += this.players[i].inputs.length;
|
||||
this.players[i].inputs = [];
|
||||
@@ -37,9 +52,10 @@ GarageServerGame.prototype.addPlayer = function (client) {
|
||||
}
|
||||
}
|
||||
|
||||
// Set player state callback
|
||||
var player = {
|
||||
client: client,
|
||||
state: {},
|
||||
state: {x: 0, y: 0},
|
||||
inputs: [],
|
||||
sequence: 1
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user