update of client data by server

This commit is contained in:
Jeena Paradies 2012-07-13 22:49:15 +02:00
parent 2cb401bbd3
commit c8d0a48489
11 changed files with 239 additions and 120 deletions

View file

@ -12,7 +12,8 @@ var requires = [
define(requires,
function(ViewController, PhysicsEngine, Player, InputControlUnit, Settings, Box2D, Level, requestAnimFrame) {
function ClientProcessor () {
function ClientProcessor (clientGame) {
this.clientGame = clientGame;
this.init();
};
@ -46,6 +47,7 @@ define(requires,
this.physicsEngine.update();
this.viewController.update();
if(this.me) {
this.inputControlUnit.update();
this.me.update();
@ -65,7 +67,35 @@ define(requires,
ClientProcessor.prototype.spawnMeWithId = function(id) {
this.me = this.spawnNewPlayerWithId(id);
this.inputControlUnit = new InputControlUnit(this.me);
this.inputControlUnit = new InputControlUnit(this.me, this);
}
ClientProcessor.prototype.sendGameCommand = function(command, options) {
this.clientGame.sendGameCommand(command, options);
}
ClientProcessor.prototype.processGameCommand = function(command, options) {
if (command == "worldUpdate") {
var body = this.physicsEngine.world.GetBodyList();
do {
var userData = body.GetUserData();
if(userData && options[userData]) {
var update = options[userData];
//console.log('position difference:', (body.GetPosition().y - update.p.y) * 30, body.GetLinearVelocity().y);
body.SetAwake(true);
body.SetPosition(update.p);
body.SetAngle(update.a);
body.SetLinearVelocity(update.lv);
body.SetAngularVelocity(update.av);
}
} while (body = body.GetNext());
}
}
return ClientProcessor;

View file

@ -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;