mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
update of client data by server
This commit is contained in:
parent
2cb401bbd3
commit
c8d0a48489
11 changed files with 239 additions and 120 deletions
|
|
@ -3,13 +3,14 @@ var requires = [
|
|||
"Chuck/Player",
|
||||
"Vendor/Box2D",
|
||||
"Chuck/Loader/Level",
|
||||
"Chuck/Control/InputController",
|
||||
"RequestAnimationFrame"
|
||||
];
|
||||
|
||||
define(requires, function(PhysicsEngine, Player, Box2D, Level, requestAnimFrame){
|
||||
define(requires, function(PhysicsEngine, Player, Box2D, Level, InputController, requestAnimFrame){
|
||||
|
||||
function ServerProcessor (ServerProcessor) {
|
||||
this.ServerProcessor = ServerProcessor;
|
||||
function ServerProcessor (serverGame) {
|
||||
this.serverGame = serverGame;
|
||||
this.players = {};
|
||||
this.init();
|
||||
};
|
||||
|
|
@ -18,6 +19,7 @@ define(requires, function(PhysicsEngine, Player, Box2D, Level, requestAnimFrame)
|
|||
this.physicsEngine = new PhysicsEngine();
|
||||
|
||||
this.update();
|
||||
this.updateWorld();
|
||||
}
|
||||
|
||||
ServerProcessor.prototype.loadLevel = function(path) {
|
||||
|
|
@ -39,7 +41,7 @@ define(requires, function(PhysicsEngine, Player, Box2D, Level, requestAnimFrame)
|
|||
|
||||
this.physicsEngine.update();
|
||||
for(var id in this.players) {
|
||||
this.players[id].update();
|
||||
this.players[id].player.update();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -49,23 +51,33 @@ define(requires, function(PhysicsEngine, Player, Box2D, Level, requestAnimFrame)
|
|||
|
||||
ServerProcessor.prototype.createPlayerWithId = function(id) {
|
||||
var player = new Player(this.physicsEngine, id, null);
|
||||
this.players[id] = player;
|
||||
this.players[id] = {
|
||||
player: player,
|
||||
inputController: new InputController(player)
|
||||
};
|
||||
|
||||
player.spawn(100, 0);
|
||||
this.physicsEngine.setCollisionDetector(player);
|
||||
}
|
||||
|
||||
ServerProcessor.prototype.progressGameCommandFromId = function(command, options, id) {
|
||||
var inputController = this.players[id].inputController;
|
||||
if (typeof inputController[command] == 'function') {
|
||||
inputController[command](options);
|
||||
}
|
||||
}
|
||||
|
||||
ServerProcessor.prototype.updateWorld = function() {
|
||||
|
||||
var update = {};
|
||||
var isUpdateNeeded = false;
|
||||
|
||||
var body = world.GetBodyList();
|
||||
var body = this.physicsEngine.world.GetBodyList();
|
||||
do {
|
||||
var userData = body.GetUserData();
|
||||
|
||||
if(userData && userData.bodyId && body.IsAwake()){
|
||||
update[userData.bodyId] = {
|
||||
if(userData && body.IsAwake()){
|
||||
update[userData] = {
|
||||
p: body.GetPosition(),
|
||||
a: body.GetAngle(),
|
||||
lv: body.GetLinearVelocity(),
|
||||
|
|
@ -76,8 +88,10 @@ define(requires, function(PhysicsEngine, Player, Box2D, Level, requestAnimFrame)
|
|||
} while (body = body.GetNext());
|
||||
|
||||
if(isUpdateNeeded) {
|
||||
this.ServerProcessor.updateClientsWorld(update);
|
||||
this.serverGame.updateClientsWorld(update);
|
||||
}
|
||||
|
||||
setTimeout(this.updateWorld.bind(this), 500); // FIXME: do this a different hearbeat
|
||||
}
|
||||
|
||||
return ServerProcessor;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue