mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-06-27 16:10:34 +08:00
Progress: #3
This commit is contained in:
+7
-1
@@ -26,10 +26,16 @@ Game.prototype.start = function () {
|
||||
|
||||
Game.prototype.update = function () {
|
||||
var players = this.server.getPlayers(),
|
||||
entities = this.server.getEntities(),
|
||||
self = this;
|
||||
|
||||
players.forEach(function (player) {
|
||||
var newState = gamePhysics.getNewState(player.state, player.inputs, self.physicsDelta);
|
||||
var newState = gamePhysics.getNewPlayerState(player.state, player.inputs, self.physicsDelta);
|
||||
self.server.updatePlayerState(player.id, newState);
|
||||
});
|
||||
|
||||
entities.forEach(function (entity) {
|
||||
var newState = gamePhysics.getNewPlayerState(entity.state, self.physicsDelta);
|
||||
self.server.updateEntityState(entity.id, newState);
|
||||
});
|
||||
};
|
||||
@@ -6,7 +6,7 @@ $(function () {
|
||||
|
||||
GarageServerIO.initializeGarageServer('http://garageserver_io.jbillmann.c9.io', {
|
||||
logging: true,
|
||||
onUpdatePlayerPhysics: GamePhysics.getNewState,
|
||||
onUpdatePlayerPhysics: GamePhysics.getNewPlayerState,
|
||||
onInterpolation: GamePhysics.getInterpolatedState,
|
||||
onWorldState: function (state) {
|
||||
document.getElementById('gameCanvas').style.width = state.width;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(function(exports){
|
||||
|
||||
exports.getNewState = function (state, inputs, deltaTime) {
|
||||
exports.getNewPlayerState = function (state, inputs, deltaTime) {
|
||||
var i = 0;
|
||||
|
||||
if (!state.x && !state.y) {
|
||||
@@ -16,11 +16,17 @@
|
||||
state.y += (50 * deltaTime);
|
||||
} else if (inputs[i].input === 'up') {
|
||||
state.y -= (50 * deltaTime);
|
||||
} else if (inputs[i].input === 'space') {
|
||||
|
||||
}
|
||||
}
|
||||
return state;
|
||||
};
|
||||
|
||||
exports.getNewEntityState = function (state, deltaTime) {
|
||||
|
||||
};
|
||||
|
||||
exports.getInterpolatedState = function (previousState, targetState, amount) {
|
||||
var interpolationState = {};
|
||||
interpolationState.x = (previousState.x + amount * (targetState.x - previousState.x));
|
||||
|
||||
@@ -16,14 +16,14 @@ GarageServerGame.prototype.start = function () {
|
||||
var self = this;
|
||||
|
||||
this.startTime = new Date().getTime();
|
||||
this.stateIntervalId = setInterval(function () { self.updateState(); }, this.stateInterval);
|
||||
this.stateIntervalId = setInterval(function () { self.broadcastState(); }, this.stateInterval);
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.stop = function () {
|
||||
clearInterval(this.stateIntervalId);
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.updateState = function () {
|
||||
GarageServerGame.prototype.broadcastState = function () {
|
||||
var currentTime = new Date().getTime() - this.startTime,
|
||||
state = { time: currentTime, playerStates: [], entityStates: [] };
|
||||
|
||||
@@ -64,9 +64,17 @@ GarageServerGame.prototype.getPlayer = function (id) {
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.updatePlayerState = function (id, state) {
|
||||
var currentTime = new Date().getTime() - this.startTime;
|
||||
this.updateState(this.playerController, id, state);
|
||||
};
|
||||
|
||||
this.playerController.entities.some(function (player) {
|
||||
GarageServerGame.prototype.updateEntityState = function (id, state) {
|
||||
this.updateState(this.entityController, id, state);
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.updateState = function (controller, id, state) {
|
||||
var currentTime = new Date().getTime() - this.startTime;
|
||||
|
||||
controller.entities.some(function (player) {
|
||||
if (player.id === id) {
|
||||
player.addState(state, currentTime);
|
||||
player.sequence += player.inputs.length;
|
||||
|
||||
Reference in New Issue
Block a user