From 8cf8f057bbf69b187e0a16e42b6a35f210b68c8e Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Sat, 21 Jul 2012 01:29:58 +0200 Subject: [PATCH] fiusing notificationCenter for sendCommandToAllUsers for testing, seems to work --- lib/Chuck/Processors/ServerProcessor.js | 7 ++++--- lib/Chuck/ServerGame.js | 2 +- lib/Server/Coordinator.js | 3 --- lib/Server/User.js | 16 +++++++++++++++- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/Chuck/Processors/ServerProcessor.js b/lib/Chuck/Processors/ServerProcessor.js index 8b8d53c..7da278c 100755 --- a/lib/Chuck/Processors/ServerProcessor.js +++ b/lib/Chuck/Processors/ServerProcessor.js @@ -14,10 +14,10 @@ define(requires, function(PhysicsEngine, Settings, Player, Box2D, Level, InputCo this.serverGame = serverGame; this.players = {}; this.init(); - }; + } ServerProcessor.prototype.init = function() { - this.physicsEngine = new PhysicsEngine(); + this.physicsEngine = this.factory.new(PhysicsEngine); this.update(); this.updateWorld(); @@ -95,7 +95,8 @@ define(requires, function(PhysicsEngine, Settings, Player, Box2D, Level, InputCo } while (body = body.GetNext()); if(isUpdateNeeded) { - this.serverGame.updateClientsWorld(update); + //this.serverGame.updateClientsWorld(update); + this.notificationCenter.trigger("sendCommandToAllUsers", ['gameCommand', {worldUpdate:update}]); } setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); diff --git a/lib/Chuck/ServerGame.js b/lib/Chuck/ServerGame.js index b2f14b1..f57ab65 100644 --- a/lib/Chuck/ServerGame.js +++ b/lib/Chuck/ServerGame.js @@ -2,7 +2,7 @@ define(["Chuck/Processors/ServerProcessor"], function(ServerProcessor) { function ServerGame(channel) { this.channel = channel; - this.serverProcessor = new ServerProcessor(this); + this.serverProcessor = this.factory.new(ServerProcessor, this); } ServerGame.prototype.loadLevel = function(path) { diff --git a/lib/Server/Coordinator.js b/lib/Server/Coordinator.js index c88816a..3a71f80 100644 --- a/lib/Server/Coordinator.js +++ b/lib/Server/Coordinator.js @@ -35,9 +35,6 @@ define(["Server/User", "Server/Channel", "Server/Factory"], function(User, Chann this.channels[channelName] = channel; } - user.notificationCenter = channel.notificationCenter; - user.factory = channel.factory; - channel.addUser(user); user.setChannel(channel); diff --git a/lib/Server/User.js b/lib/Server/User.js index 792e4aa..f30c9d9 100644 --- a/lib/Server/User.js +++ b/lib/Server/User.js @@ -23,11 +23,25 @@ define(["Protocol/Helper"], function(ProtocolHelper) { }); } - User.prototype.setChannel = function(channel){ + User.prototype.setChannel = function(channel) { + if (this.notificationCenter) { + this.notificationCenter.off("updateClientsWorld"); + } + this.channel = channel; + + // Use the right factory and nc + this.notificationCenter = this.channel.notificationCenter; + this.factory = this.channel.factory; + + var self = this; + this.notificationCenter.on("sendCommandToAllUsers", function(topic, args) { + self.sendCommand.apply(self, args); + }); } User.prototype.sendCommand = function(command, options) { + var message = ProtocolHelper.encodeCommand(command, options); this.socketLink.send(message); }