diff --git a/app/Game/Client/Control/KeyboardController.js b/app/Game/Client/Control/KeyboardController.js index a6ba55a..6c36bb8 100755 --- a/app/Game/Client/Control/KeyboardController.js +++ b/app/Game/Client/Control/KeyboardController.js @@ -1,4 +1,10 @@ -define(["Game/Core/Control/InputController", "Game/Client/Control/KeyboardInput"], function (InputController, KeyboardInput) { +define([ + "Game/Core/Control/InputController", + "Game/Client/Control/KeyboardInput", + "Game/Core/NotificationCenter" +], + +function (InputController, KeyboardInput, NotificationCenter) { function KeyboardController (me, gameController) { @@ -42,22 +48,22 @@ define(["Game/Core/Control/InputController", "Game/Client/Control/KeyboardInput" KeyboardController.prototype.moveLeft = function () { this.inputController.moveLeft(); - this.gameController.sendGameCommand('moveLeft'); + NotificationCenter.trigger('sendGameCommand', 'moveLeft'); } KeyboardController.prototype.moveRight = function () { this.inputController.moveRight(); - this.gameController.sendGameCommand('moveRight'); + NotificationCenter.trigger('sendGameCommand', 'moveRight'); } KeyboardController.prototype.stop = function () { this.inputController.stop(); - this.gameController.sendGameCommand('stop'); + NotificationCenter.trigger('sendGameCommand', 'stop'); } KeyboardController.prototype.jump = function () { this.inputController.jump(); - this.gameController.sendGameCommand('jump'); + NotificationCenter.trigger('sendGameCommand', 'jump'); } KeyboardController.prototype.jumped = function () { @@ -70,7 +76,7 @@ define(["Game/Core/Control/InputController", "Game/Client/Control/KeyboardInput" KeyboardController.prototype.duck = function () { this.inputController.duck(); - this.gameController.sendGameCommand('duck'); + NotificationCenter.trigger('sendGameCommand', 'duck'); } KeyboardController.prototype.standUp = function () { @@ -79,7 +85,7 @@ define(["Game/Core/Control/InputController", "Game/Client/Control/KeyboardInput" KeyboardController.prototype.activateShift = function () { this.inputController.activateShift(); - this.gameController.sendGameCommand('activateShift'); + NotificationCenter.trigger('sendGameCommand', 'activateShift'); } KeyboardController.prototype.deactivateShift = function () { diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index f4cf72d..3f2c75d 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -22,6 +22,9 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio GameController.prototype = Object.create(Parent.prototype); + GameController.prototype.destruct = function() { + //destroy box2d world etc. + }; GameController.prototype.getMe = function () { return this.me; diff --git a/app/Game/Client/Networker.js b/app/Game/Client/Networker.js index e5e0da6..dd4523c 100755 --- a/app/Game/Client/Networker.js +++ b/app/Game/Client/Networker.js @@ -1,10 +1,11 @@ define([ "Game/Core/Protocol/Helper", "Game/Client/GameController", - "Game/Core/User" + "Game/Core/User", + "Game/Core/NotificationCenter" ], -function (ProtocolHelper, GameController, User) { +function (ProtocolHelper, GameController, User, NotificationCenter) { function Networker (socketLink) { this.socketLink = socketLink; @@ -29,6 +30,7 @@ function (ProtocolHelper, GameController, User) { self.onDisconnect(); }); + NotificationCenter.on("sendGameCommand", this.sendGameCommand, this); } Networker.prototype.onConnect = function () { @@ -123,6 +125,11 @@ function (ProtocolHelper, GameController, User) { } } + Networker.prototype.sendGameCommand = function(command, options) { + var message = ProtocolHelper.encodeCommand(command, options); + this.sendCommand('gameCommand', message); + }; + return Networker; }); \ No newline at end of file diff --git a/app/Game/Server/Channel.js b/app/Game/Server/Channel.js index 30f29b5..f51d8c1 100755 --- a/app/Game/Server/Channel.js +++ b/app/Game/Server/Channel.js @@ -20,37 +20,17 @@ this.gameController.loadLevel("default.json"); - /* + var self = this; NotificationCenter.on("processGameCommandFromUser", function (topic, args) { self.processGameCommandFromUser.apply(self, args); }); - */ + NotificationCenter.on('channel/message', function (message) { - - switch(message.recipient) { - case 'user': - console.log(message); - var user = self.users[message.id]; - ProtocolHelper.runCommands(message.data, function (command, options) { - user[command].call(user, options); - }); - break; - - case 'id': // Do nothing, it is needed by the user - break; - - case 'channel': - ProtocolHelper.runCommands(message.data, function (command, options) { - self[command].call(self, options); - }); - break; - - default: - throw 'unknown recipient'; - break; - } + ProtocolHelper.runCommands(message.data, function (command, options) { + self[command].call(self, options); + }); }); NotificationCenter.on('sendControlCommandToAllUsers', this.sendControlCommandToAllUsers, this); diff --git a/app/Game/Server/GameController.js b/app/Game/Server/GameController.js index 3ff4a6a..eaa2c2f 100755 --- a/app/Game/Server/GameController.js +++ b/app/Game/Server/GameController.js @@ -41,8 +41,10 @@ function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, No var id = user.id; var player = this.players[id]; + user.setPlayer(player); player.spawn(50, 50); this.inputControllers[id] = new InputController(player); + player.inputController = this.inputControllers[id]; // FIXME move this to Server/Player } GameController.prototype.userLeft = function (user) { diff --git a/app/Game/Server/PipeToLobby.js b/app/Game/Server/PipeToLobby.js index f40f738..0cb22cf 100755 --- a/app/Game/Server/PipeToLobby.js +++ b/app/Game/Server/PipeToLobby.js @@ -45,7 +45,7 @@ function (NotificationCenter, Channel) { }; PipeToLobby.prototype.onMessage = function (message) { - NotificationCenter.trigger(message.recipient + '/message', message); + NotificationCenter.trigger(message.recipient + '/message', message); } return PipeToLobby; diff --git a/app/Game/Server/User.js b/app/Game/Server/User.js index 4ff79f9..436088d 100644 --- a/app/Game/Server/User.js +++ b/app/Game/Server/User.js @@ -10,6 +10,7 @@ function(Parent, NotificationCenter, ProtocolHelper) { Parent.call(this, id); this.channel = channel; + this.player = null; var self = this; NotificationCenter.on('user/joined', function(user) { @@ -21,6 +22,12 @@ function(Parent, NotificationCenter, ProtocolHelper) { NotificationCenter.on('user/' + this.id + "/joinSuccess", function(options) { self.sendControlCommand("joinSuccess", options); }); + + NotificationCenter.on('user/' + this.id + "/message", function(message) { // FIXME: right now only game commands? + ProtocolHelper.runCommands(message.data, function (command, options) { + self.gameCommand(command, options); + }); + }); } User.prototype = Object.create(Parent.prototype); @@ -37,6 +44,14 @@ function(Parent, NotificationCenter, ProtocolHelper) { this.sendControlCommand("gameCommand", data); }; + User.prototype.gameCommand = function(command, options) { + this.player.inputController[command].call(this.player.inputController); + }; + + User.prototype.setPlayer = function(player) { + this.player = player; + }; + return User; }); \ No newline at end of file diff --git a/app/Lobby/Coordinator.js b/app/Lobby/Coordinator.js index 8e08f2c..1d49c37 100755 --- a/app/Lobby/Coordinator.js +++ b/app/Lobby/Coordinator.js @@ -84,6 +84,10 @@ function (User, Channel, PipeToChannel, NotificationCenter) { NotificationCenter.on('user/left', function (user) { channelPipe.send('channel', { releaseUser: user.id }); }, this); + + NotificationCenter.on('user/gameCommand', function (userId, data) { + channelPipe.sendToUser(userId, data); + }, this); return channelPipe; }; diff --git a/app/Lobby/PipeToChannel.js b/app/Lobby/PipeToChannel.js index c653ef9..ec02331 100755 --- a/app/Lobby/PipeToChannel.js +++ b/app/Lobby/PipeToChannel.js @@ -35,6 +35,15 @@ function (NotificationCenter, childProcess) { this.channelPipe.send(message); } + PipeToChannel.prototype.sendToUser = function (id, data) { + var message = { + recipient: "user/" + id, + data: data + } + + this.channelPipe.send(message); + } + PipeToChannel.prototype.onMessage = function (message) { NotificationCenter.trigger(message.recipient + '/message', message.data); } diff --git a/app/Lobby/User.js b/app/Lobby/User.js index 1dbb622..bc510b0 100755 --- a/app/Lobby/User.js +++ b/app/Lobby/User.js @@ -54,10 +54,7 @@ function (Parent, ProtocolHelper, NotificationCenter) { break; case 'gameCommand': - for(var gameCommand in options) { - //NotificationCenter.trigger("processGameCommandFromUser", [gameCommand, options[gameCommand], this]); - //this.channel.processGameCommandFromUser(gameCommand, options[gameCommand], this); - } + NotificationCenter.trigger("user/gameCommand", this.id, options); break; default: