fixed server bugs

This commit is contained in:
logsol 2012-07-22 15:01:50 +02:00
parent f85469b97e
commit 3afc2fa66e
5 changed files with 24 additions and 27 deletions

View file

@ -1,9 +1,10 @@
define([ define([
"Game/Core/Physics/Engine", "Game/Core/Physics/Engine",
"Game/Core/Loader/Level" "Game/Core/Loader/Level",
"Game/Core/Player"
], ],
function(Engine, Level) { function(Engine, Level, Player) {
function GameController(physicsEngine) { function GameController(physicsEngine) {
console.log('constructor called'); console.log('constructor called');
@ -36,7 +37,7 @@ function(Engine, Level) {
} }
GameController.prototype.userJoined = function(user) { GameController.prototype.userJoined = function(user) {
var player = new Player(id, this.physicsEngine); var player = new Player(user.id, this.physicsEngine);
this.players[user.id] = player; this.players[user.id] = player;
return player; return player;
} }

View file

@ -29,7 +29,7 @@ function(GameController, NotificationCenter) {
user.sendCommand('joinSuccess', {channelName: this.name, id: user.id, userIds: userIds}); user.sendCommand('joinSuccess', {channelName: this.name, id: user.id, userIds: userIds});
this.sendCommandToAllUsersExcept('userJoined', user.id, user); this.sendCommandToAllUsersExcept('userJoined', user.id, user);
this.gameController.createPlayerForUser(user) NotificationCenter.trigger('user/joined', user);
} }
Channel.prototype.releaseUser = function(user) { Channel.prototype.releaseUser = function(user) {

View file

@ -16,6 +16,9 @@ function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, Not
this.update(); this.update();
this.updateWorld(); this.updateWorld();
NotificationCenter.on('user/joined', this.userJoined, this);
NotificationCenter.on('user/left', this.userLeft, this);
} }
GameController.prototype = Object.create(Parent.prototype); GameController.prototype = Object.create(Parent.prototype);
@ -26,13 +29,16 @@ function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, Not
this.physicsEngine.update(); this.physicsEngine.update();
for(var id in this.players) { for(var id in this.players) {
this.players[id].player.update(); this.players[id].update();
} }
} }
GameController.prototype.userJoined = function(user) { GameController.prototype.userJoined = function(user) {
var player = Parent.prototype.userJoined.call(this, user); Parent.prototype.userJoined.call(this, user);
this.inputControllers[player.id] = new InputController(player);
var id = user.id;
var player = this.players[id];
this.inputControllers[id] = new InputController(player);
} }
GameController.prototype.userLeft = function(user) { GameController.prototype.userLeft = function(user) {
@ -68,7 +74,7 @@ function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, Not
} while (body = body.GetNext()); } while (body = body.GetNext());
if(isUpdateNeeded) { if(isUpdateNeeded) {
NotificationCenter.trigger("sendCommandToAllUsers", ['gameCommand', {worldUpdate:update}]); //NotificationCenter.trigger("sendCommandToAllUsers", ['gameCommand', {worldUpdate:update}]);
} }
setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);

View file

@ -8,21 +8,23 @@ function() {
this.subUid = -1; this.subUid = -1;
} }
NotificationCenter.prototype.trigger = function(topic, args) { NotificationCenter.prototype.trigger = function(topic) {
if (!this.topics[topic]) { if (!this.topics[topic]) {
throw "No such topic " + topic + ". Could not trigger."; throw "No such topic " + topic + ". Could not trigger.";
} }
var args = Array.prototype.slice.call(arguments, 1);
var subscribers = this.topics[topic]; var subscribers = this.topics[topic];
var len = subscribers ? subscribers.length : 0; var len = subscribers ? subscribers.length : 0;
while (len--) { while (len--) {
subscribers[len].func(topic, args); var subscriber = subscribers[len];
subscriber.func.apply(subscriber.context, args);
} }
} }
NotificationCenter.prototype.on = function(topic, func) { NotificationCenter.prototype.on = function(topic, func, context) {
if (!this.topics[topic]) { if (!this.topics[topic]) {
this.topics[topic] = []; this.topics[topic] = [];
@ -31,7 +33,8 @@ function() {
var token = ( ++this.subUid ).toString(); var token = ( ++this.subUid ).toString();
this.topics[topic].push({ this.topics[topic].push({
token: token, token: token,
func: func func: func,
context: context
}); });
return token; return token;

View file

@ -29,20 +29,7 @@ function(ProtocolHelper, NotificationCenter) {
} }
User.prototype.setChannel = function(channel) { User.prototype.setChannel = function(channel) {
if (NotificationCenter) {
NotificationCenter.off("updateClientsWorld");
}
this.channel = channel; this.channel = channel;
// Use the right factory and nc
NotificationCenter = this.channel.notificationCenter;
this.factory = this.channel.factory;
var self = this;
NotificationCenter.on("sendCommandToAllUsers", function(topic, args) {
self.sendCommand.apply(self, args);
});
} }
User.prototype.sendCommand = function(command, options) { User.prototype.sendCommand = function(command, options) {
@ -75,7 +62,7 @@ function(ProtocolHelper, NotificationCenter) {
case 'gameCommand': case 'gameCommand':
for(var gameCommand in options) { for(var gameCommand in options) {
NotificationCenter.trigger("processGameCommandFromUser", [gameCommand, options[gameCommand], this]); //NotificationCenter.trigger("processGameCommandFromUser", [gameCommand, options[gameCommand], this]);
//this.channel.processGameCommandFromUser(gameCommand, options[gameCommand], this); //this.channel.processGameCommandFromUser(gameCommand, options[gameCommand], this);
} }
break; break;