From 4e3f207f525d59900304bbdf8b678079e0f1f9ec Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Sun, 22 Jul 2012 02:02:23 +0200 Subject: [PATCH 1/2] cleaning up Server GameController --- app/Game/Client/GameController.js | 7 +-- app/Game/Server/GameController.js | 78 +++++++++---------------------- 2 files changed, 25 insertions(+), 60 deletions(-) diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index ca20bec..940dba6 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -9,10 +9,11 @@ define([ function(Parent, PhysicsEngine, ViewController, KeyboardController, requestAnimFrame) { function GameController () { - this.me; - this.keyboardController; - Parent.apply(this, new PhysicsEngine()); + + this.me = null; + this.keyboardController = null; + this.viewController = new ViewController(); this.update(); } diff --git a/app/Game/Server/GameController.js b/app/Game/Server/GameController.js index 3c4f6e6..817ddd9 100755 --- a/app/Game/Server/GameController.js +++ b/app/Game/Server/GameController.js @@ -1,41 +1,23 @@ define([ - "Chuck/Physics/Engine", - "Chuck/Settings", - "Chuck/Player", - "Vendor/Box2D", - "Chuck/Loader/Level", + "Game/Core/GameController", + "Game/Server/Physics/Engine", + "Game/Config/Settings", "Chuck/Control/InputController", - "RequestAnimationFrame" + "RequestAnimationFrame", + "NotificationCenter" ], -function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, requestAnimFrame) { +function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter) { - function GameController (channel) { - this.channel = channel; - this.players = {}; - this.init(); - } + function GameController () { + Parent.apply(this, new PhysicsEngine()); - GameController.prototype.init = function() { - this.physicsEngine = this.factory.new(PhysicsEngine); - - this.update(); + this.inputControllers = {}; + + this.update(); this.updateWorld(); } - GameController.prototype.loadLevel = function(path) { - if (this.level) { - this.level.unload(); - } - - this.level = new Level(path, this.physicsEngine); - this.level.loadLevelInToEngine(); - } - - GameController.prototype.getPhysicsEngine = function() { - return this.physicsEngine; - } - GameController.prototype.update = function() { requestAnimFrame(this.update.bind(this)); @@ -46,40 +28,23 @@ function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, request } } - GameController.prototype.destruct = function() { - + GameController.prototype.userJoined = function(user) { + var player = Parent.prototype.userJoined.call(this, user); + this.inputControllers[player.id] = new InputController(player); } - GameController.prototype.createPlayerForUser = function(user) { - var id = user.id; - - var player = new Player(this.physicsEngine, id, null); - this.players[id] = { - player: player, - inputController: new InputController(player) - }; - - player.spawn(100, 0); - this.physicsEngine.setCollisionDetector(player); + GameController.prototype.userLeft = function(user) { + Parent.prototype.userLeft.call(this, user); + delete this.inputControllers[user.id]; } - GameController.prototype.progressGameCommandFromId = function(command, options, id) { - var inputController = this.players[id].inputController; + GameController.prototype.progressGameCommandFromUser = function(command, options, user) { + var inputController = this.inputControllers[user.id]; if (typeof inputController[command] == 'function') { inputController[command](options); } } - GameController.prototype.userIdLeft = function(id) { - var player = this.players[id].player; - player.destroy(); - delete this.players[id]; - } - - GameController.prototype.updateClientsWorld = function(update_world) { - this.channel.sendCommandToAllUsers('gameCommand', {worldUpdate: update_world}); - } - GameController.prototype.updateWorld = function() { var update = {}; @@ -100,9 +65,8 @@ function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, request } } while (body = body.GetNext()); - if(isUpdateNeeded) { - //this.serverGame.updateClientsWorld(update); - this.notificationCenter.trigger("sendCommandToAllUsers", ['gameCommand', {worldUpdate:update}]); + if(isUpdateNeeded) { + NotificationCenter.trigger("sendCommandToAllUsers", ['gameCommand', {worldUpdate:update}]); } setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); From 55505106e43367b265089603b21d2c11e5cd44e0 Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Sun, 22 Jul 2012 02:19:28 +0200 Subject: [PATCH 2/2] singleton NotificationCenter --- app/Game/Server/NotificationCenter.js | 41 ++++++++++++++------------- 1 file changed, 22 insertions(+), 19 deletions(-) mode change 100755 => 100644 app/Game/Server/NotificationCenter.js diff --git a/app/Game/Server/NotificationCenter.js b/app/Game/Server/NotificationCenter.js old mode 100755 new mode 100644 index c8dbc4e..976c450 --- a/app/Game/Server/NotificationCenter.js +++ b/app/Game/Server/NotificationCenter.js @@ -1,30 +1,32 @@ -define( +define(function() { - var NotificationCenter = { - topics: {}, - subUid: -1 - }; + function NotificationCenter() { + this.topics = {}; + this.subUid = -1; + } - NotificationCenter.trigger = function(topic, args) { - if (!NotificationCenter.topics[topic]) { + NotificationCenter.prototype.trigger = function(topic, args) { + if (!this.topics[topic]) { throw "No such topic " + topic + ". Could not trigger."; } - var subscribers = NotificationCenter.topics[topic]; + var subscribers = this.topics[topic]; var len = subscribers ? subscribers.length : 0; while (len--) { subscribers[len].func(topic, args); } + + return this; } - NotificationCenter.on = function(topic, func) { - if (!NotificationCenter.topics[topic]) { - NotificationCenter.topics[topic] = []; + NotificationCenter.prototype.on = function(topic, func) { + if (!this.topics[topic]) { + this.topics[topic] = []; } - var token = ( ++NotificationCenter.subUid ).toString(); - NotificationCenter.topics[topic].push({ + var token = ( ++this.subUid ).toString(); + this.topics[topic].push({ token: token, func: func }); @@ -32,18 +34,19 @@ define( return token; } - NotificationCenter.off = function(token) { + NotificationCenter.prototype.off = function(token) { - for(var m in NotificationCenter.topics) { - if (NotificationCenter.topics[m]) { - for(var i = 0, j = NotificationCenter.topics[m].length; i < j; i++) { - if (NotificationCenter.topics[m][i].token === token) { - NotificationCenter.topics[m].splice(i, 1); + for(var m in this.topics) { + if (this.topics[m]) { + for(var i = 0, j = this.topics[m].length; i < j; i++) { + if (this.topics[m][i].token === token) { + this.topics[m].splice(i, 1); return token; } } } } + return this; } return NotificationCenter;