mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-06-30 16:40:46 +08:00
Progress #3
This commit is contained in:
+3
-2
@@ -49,5 +49,6 @@ garageServer.createGarageServer(sockets,{
|
||||
logging: true,
|
||||
interpolation: true,
|
||||
clientSidePrediction: true,
|
||||
onUpdatePlayerPhysics: gamePhysics.onUpdatePlayerPhysics
|
||||
});
|
||||
onUpdatePlayerPhysics: gamePhysics.onUpdatePlayerPhysics,
|
||||
onUpdateEntityPhysics: gamePhysics.onUpdateEntityPhysics
|
||||
});
|
||||
@@ -3,7 +3,7 @@
|
||||
exports.onUpdatePlayerPhysics = function (id, state, inputs, deltaTime) {
|
||||
var i = 0;
|
||||
|
||||
if (!state.x && !state.y) {
|
||||
if (!state.x && !state.y) {
|
||||
state.x = 0;
|
||||
state.y = 0;
|
||||
}
|
||||
@@ -20,5 +20,13 @@
|
||||
}
|
||||
return state;
|
||||
};
|
||||
|
||||
exports.onUpdateEntityPhysics = function (id, state, deltaTime) {
|
||||
if (!state.x && !state.y) {
|
||||
state.x = 0;
|
||||
state.y = 0;
|
||||
}
|
||||
state.x += (10 * deltaTime);
|
||||
};
|
||||
|
||||
})(typeof exports === 'undefined' ? window : exports);
|
||||
@@ -1,3 +1,5 @@
|
||||
var Entity = require('../entities/entity');
|
||||
|
||||
exports = module.exports = EntityController;
|
||||
|
||||
function EntityController () {
|
||||
@@ -6,7 +8,21 @@ function EntityController () {
|
||||
|
||||
EntityController.prototype = {
|
||||
add: function (id) {
|
||||
|
||||
var newEntity, entityFound = false;
|
||||
|
||||
this.entities.some(function (entity) {
|
||||
if (entity.id === id) {
|
||||
newEntity = entity;
|
||||
entityFound = true;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
if (!entityFound) {
|
||||
newEntity = new Entity(id);
|
||||
this.entities.push(newEntity);
|
||||
}
|
||||
return newEntity;
|
||||
},
|
||||
remove: function (id) {
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
exports = module.exports = Entity;
|
||||
|
||||
function Entity (id) {
|
||||
this.state;
|
||||
this.state = {};
|
||||
this.sequence = 1;
|
||||
this.id = id;
|
||||
this.stateHistory = [];
|
||||
@@ -10,6 +10,7 @@ function Entity (id) {
|
||||
Entity.prototype = {
|
||||
addState: function (state, executionTime) {
|
||||
var minTime, spliceTo = 0;
|
||||
this.state = state;
|
||||
this.stateHistory.push({ state: state, executionTime: executionTime });
|
||||
minTime = this.stateHistory[this.stateHistory.length - 1].executionTime - 1000;
|
||||
|
||||
|
||||
@@ -14,7 +14,8 @@ options = {
|
||||
onPlayerDisconnect: function (socket),
|
||||
onPing: function (socket, data),
|
||||
onPlayerState: function (socket, data),
|
||||
onUpdatePlayerPhysics: function (id, state, inputs, deltaTime)
|
||||
onUpdatePlayerPhysics: function (id, state, inputs, deltaTime),
|
||||
onUpdateEntityPhysics: function (id, state, deltaTime)
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
@@ -38,7 +38,9 @@ GarageServerGame.prototype.getState = function (controller) {
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.updatePhysics = function (options) {
|
||||
var self = this;
|
||||
var self = this,
|
||||
currentTime = new Date().getTime() - this.startTime;
|
||||
|
||||
this.playerController.entities.forEach(function (player) {
|
||||
if (player.inputs.length > 0) {
|
||||
if (options.onUpdatePlayerPhysics) {
|
||||
@@ -48,6 +50,12 @@ GarageServerGame.prototype.updatePhysics = function (options) {
|
||||
}
|
||||
}
|
||||
});
|
||||
this.entityController.entities.forEach(function (entity) {
|
||||
if (options.onUpdateEntityPhysics) {
|
||||
entity.addState(options.onUpdateEntityPhysics(entity.id, entity.state, self.physicsDelta), currentTime);
|
||||
entity.sequence += 1;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.addPlayer = function (client) {
|
||||
|
||||
Reference in New Issue
Block a user