ground work for different users on server and players on client

This commit is contained in:
Jeena Paradies 2012-07-13 21:10:08 +02:00
parent e37d1eeb2e
commit 2cb401bbd3
9 changed files with 122 additions and 54 deletions

View file

@ -8,39 +8,77 @@ var requires = [
define(requires, function(PhysicsEngine, Player, Box2D, Level, requestAnimFrame){
function ServerGame () {
function ServerProcessor (ServerProcessor) {
this.ServerProcessor = ServerProcessor;
this.players = {};
this.init();
};
ServerGame.prototype.init = function() {
ServerProcessor.prototype.init = function() {
this.physicsEngine = new PhysicsEngine();
this.update();
}
ServerGame.prototype.loadLevel = function(path) {
ServerProcessor.prototype.loadLevel = function(path) {
if (this.level) {
this.level.unload();
}
this.level = new Level(path, this.physicsEngine);
this.level.loadLevelInToEngine();
console.log(this.level);
}
ServerGame.prototype.getPhysicsEngine = function() {
ServerProcessor.prototype.getPhysicsEngine = function() {
return this.physicsEngine;
}
ServerGame.prototype.update = function() {
ServerProcessor.prototype.update = function() {
requestAnimFrame(this.update.bind(this));
this.physicsEngine.update();
for(var id in this.players) {
this.players[id].update();
}
}
ServerGame.prototype.destruct = function() {
ServerProcessor.prototype.destruct = function() {
}
return ServerGame;
ServerProcessor.prototype.createPlayerWithId = function(id) {
var player = new Player(this.physicsEngine, id, null);
this.players[id] = player;
player.spawn(100, 0);
this.physicsEngine.setCollisionDetector(player);
}
ServerProcessor.prototype.updateWorld = function() {
var update = {};
var isUpdateNeeded = false;
var body = world.GetBodyList();
do {
var userData = body.GetUserData();
if(userData && userData.bodyId && body.IsAwake()){
update[userData.bodyId] = {
p: body.GetPosition(),
a: body.GetAngle(),
lv: body.GetLinearVelocity(),
av: body.GetAngularVelocity()
};
isUpdateNeeded = true;
}
} while (body = body.GetNext());
if(isUpdateNeeded) {
this.ServerProcessor.updateClientsWorld(update);
}
}
return ServerProcessor;
});