mirror of
https://github.com/wassname/GarageServer.IO.git
synced 2026-07-25 13:00:24 +08:00
Refactor ahead of changes for #3
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
exports = module.exports = EntityController;
|
||||
|
||||
function EntityController () {
|
||||
this.entities = [];
|
||||
}
|
||||
|
||||
EntityController.prototype = {
|
||||
add: function (id) {
|
||||
|
||||
},
|
||||
remove: function (id) {
|
||||
|
||||
}
|
||||
};
|
||||
@@ -7,4 +7,50 @@ function PlayerController () {
|
||||
EntityController.call(this);
|
||||
}
|
||||
|
||||
PlayerController.prototype = Object.create(EntityController.prototype);
|
||||
PlayerController.prototype = Object.create(EntityController.prototype);
|
||||
|
||||
PlayerController.prototype.add = function (client) {
|
||||
var newPlayer, playerFound = false;
|
||||
|
||||
this.entities.some(function (player) {
|
||||
if (player.client.id === client.id) {
|
||||
newPlayer = player;
|
||||
playerFound = true;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
if (!playerFound) {
|
||||
newPlayer = new Player(client);
|
||||
this.entities.push(newPlayer);
|
||||
}
|
||||
return newPlayer;
|
||||
};
|
||||
|
||||
PlayerController.prototype.remove = function (id) {
|
||||
for (var i = 0; i < this.entities.length; i ++) {
|
||||
if (this.entities[i].client.id === id) {
|
||||
this.entities.splice(i, 1)[0];
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
PlayerController.prototype.addInput = function (id, input, sequence, time) {
|
||||
this.entities.some(function (player) {
|
||||
if (player.client.id === id) {
|
||||
player.inputs.push({ input: input, seq: sequence, time: time });
|
||||
return true;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
PlayerController.prototype.updateState = function (id, state) {
|
||||
this.entities.some(function (player) {
|
||||
if (player.client.id === id) {
|
||||
player.state = state;
|
||||
player.inputs = [];
|
||||
return true;
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -1,6 +1,9 @@
|
||||
exports = module.exports = Entity;
|
||||
|
||||
function Entity () {
|
||||
this.state;
|
||||
this.sequence = 1;
|
||||
this.id;
|
||||
}
|
||||
|
||||
Entity.prototype = {
|
||||
|
||||
@@ -2,8 +2,11 @@ var Entity = require('./entity');
|
||||
|
||||
exports = module.exports = Player;
|
||||
|
||||
function Player () {
|
||||
function Player (client) {
|
||||
Entity.call(this);
|
||||
this.client = client;
|
||||
this.id = client.id;
|
||||
this.inputs = [];
|
||||
}
|
||||
|
||||
Player.prototype = Object.create(Entity.prototype);
|
||||
@@ -78,7 +78,7 @@ GarageServer.prototype.onPlayerConnect = function (socket, options) {
|
||||
};
|
||||
|
||||
GarageServer.prototype.onPlayerDisconnect = function (socket, options) {
|
||||
this.game.removePlayer(socket);
|
||||
this.game.removePlayer(socket.id);
|
||||
socket.broadcast.emit('removePlayer', socket.id);
|
||||
if (options.onPlayerDisconnect) {
|
||||
options.onPlayerDisconnect(socket);
|
||||
@@ -86,7 +86,7 @@ GarageServer.prototype.onPlayerDisconnect = function (socket, options) {
|
||||
};
|
||||
|
||||
GarageServer.prototype.onPlayerInput = function (socket, input, options) {
|
||||
this.game.addPlayerInput(socket, input);
|
||||
this.game.addPlayerInput(socket.id, input[0], input[1], input[2]);
|
||||
if (options.onPlayerInput) {
|
||||
options.onPlayerInput(socket, input);
|
||||
}
|
||||
@@ -100,7 +100,7 @@ GarageServer.prototype.onPing = function (socket, data, options) {
|
||||
};
|
||||
|
||||
GarageServer.prototype.onPlayerState = function (socket, data, options) {
|
||||
this.game.setPlayerState(socket, data);
|
||||
this.game.setPlayerState(socket.id, data);
|
||||
if (options.onPlayerState) {
|
||||
options.onPlayerState(socket, data);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
var PlayerController = require('./controllers/playercontroller');
|
||||
var PlayerController = require('./controllers/playercontroller'),
|
||||
EntityController = require('./controllers/entitycontroller');
|
||||
|
||||
exports = module.exports = GarageServerGame;
|
||||
|
||||
function GarageServerGame(options) {
|
||||
var self = this;
|
||||
|
||||
this.players = [];
|
||||
this.entities = [];
|
||||
this.playerController = new PlayerController();
|
||||
this.entityController = new EntityController();
|
||||
this.options = options;
|
||||
this.startTime = new Date().getTime();
|
||||
this.physicsInterval = options.physicsInterval ? options.physicsInterval : 15;
|
||||
@@ -20,37 +21,25 @@ GarageServerGame.prototype.updateState = function (options) {
|
||||
var currentTime = new Date().getTime() - this.startTime,
|
||||
state = { time: currentTime, playerStates: [], entityStates: [] };
|
||||
|
||||
state.playerStates = this.updatePlayers();
|
||||
state.entityStates = this.updateEntities();
|
||||
state.playerStates = this.getState(this.playerController);
|
||||
state.entityStates = this.getState(this.entityController);
|
||||
|
||||
this.players.forEach(function (player) {
|
||||
this.playerController.entities.forEach(function (player) {
|
||||
player.client.emit('update', state);
|
||||
});
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.updatePlayers = function () {
|
||||
GarageServerGame.prototype.getState = function (controller) {
|
||||
var states = [];
|
||||
|
||||
this.players.forEach(function (player) {
|
||||
states.push([ player.client.id, player.state, player.sequence ]);
|
||||
});
|
||||
|
||||
return states;
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.updateEntities = function () {
|
||||
var states = [];
|
||||
|
||||
this.entities.forEach(function (entity) {
|
||||
controller.entities.forEach(function (entity) {
|
||||
states.push([ entity.id, entity.state, entity.sequence ]);
|
||||
});
|
||||
|
||||
return states;
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.updatePhysics = function (options) {
|
||||
var self = this;
|
||||
this.players.forEach(function (player) {
|
||||
this.playerController.entities.forEach(function (player) {
|
||||
if (player.inputs.length > 0) {
|
||||
if (options.onUpdatePlayerPhysics) {
|
||||
player.state = options.onUpdatePlayerPhysics(player.client.id, player.state, player.inputs, self.physicsDelta);
|
||||
@@ -62,46 +51,17 @@ GarageServerGame.prototype.updatePhysics = function (options) {
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.addPlayer = function (client) {
|
||||
this.players.some(function (player) {
|
||||
if (player.client.id === client.id) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
var player = {
|
||||
client: client,
|
||||
state: {},
|
||||
inputs: [],
|
||||
sequence: 1
|
||||
};
|
||||
|
||||
this.players.push(player);
|
||||
this.playerController.add(client);
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.removePlayer = function (client) {
|
||||
for (var i = 0; i < this.players.length; i ++) {
|
||||
if (this.players[i].client.id === client.id) {
|
||||
this.players.splice(i, 1)[0];
|
||||
return;
|
||||
}
|
||||
}
|
||||
GarageServerGame.prototype.removePlayer = function (id) {
|
||||
this.playerController.remove(id);
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.setPlayerState = function (client, state) {
|
||||
this.players.some(function (player) {
|
||||
if (player.client.id === client.id) {
|
||||
player.state = state;
|
||||
player.inputs = [];
|
||||
return true;
|
||||
}
|
||||
});
|
||||
GarageServerGame.prototype.setPlayerState = function (id, state) {
|
||||
this.playerController.updateState(id, state);
|
||||
};
|
||||
|
||||
GarageServerGame.prototype.addPlayerInput = function (client, input) {
|
||||
this.players.some(function (player) {
|
||||
if (player.client.id === client.id) {
|
||||
player.inputs.push({ input: input[0], seq: input[1], time: input[2] });
|
||||
return true;
|
||||
}
|
||||
});
|
||||
GarageServerGame.prototype.addPlayerInput = function (id, input, sequence, time) {
|
||||
this.playerController.addInput(id, input, sequence, time);
|
||||
};
|
||||
Reference in New Issue
Block a user