From fb5de522e5754386253e68ba79e509642795d2e1 Mon Sep 17 00:00:00 2001 From: Logsol Date: Sat, 5 Jan 2013 08:19:49 +0100 Subject: [PATCH] Added spawning gameCommand logic --- app/Game/Client/GameController.js | 31 ++++++++++++---------- app/Game/Client/Networker.js | 14 +++++----- app/Game/Core/GameController.js | 8 +++--- app/Game/Core/Player.js | 18 +++---------- app/Game/Server/Channel.js | 4 +-- app/Game/Server/GameController.js | 43 +++++++++++++++++-------------- app/Game/Server/Player.js | 21 +++++++++++++++ app/Game/Server/User.js | 4 +-- 8 files changed, 84 insertions(+), 59 deletions(-) create mode 100644 app/Game/Server/Player.js diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index 29bffbf..9b7f572 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -43,17 +43,6 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio } } - GameController.prototype.meJoined = function (user) { - this.me = this.userJoined(user); - this.keyboardController = new KeyboardController(this.me, this); - } - - GameController.prototype.userJoined = function (user) { - var player = Parent.prototype.userJoined.call(this, user); - //player.spawn(50, 50); - return player; - } - GameController.prototype.onWorldUpdate = function (updateData) { var body = this.physicsEngine.world.GetBodyList(); @@ -71,8 +60,24 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio } - GameController.prototype.onPlayerSpawn = function(user) { - + + GameController.prototype.onJoinMe = function (playerId) { + //this.onSpawnPlayer(options); + this.me = this.players[playerId]; + this.keyboardController = new KeyboardController(this.me, this); + } + + GameController.prototype.onSpawnPlayer = function(options) { + var playerId = options.id, + x = options.x, + y = options.y; + + console.log(this.players, options); + + var player = this.players[playerId]; + player.spawn(x, y); + + }; return GameController; diff --git a/app/Game/Client/Networker.js b/app/Game/Client/Networker.js index b744ff6..7dc875b 100755 --- a/app/Game/Client/Networker.js +++ b/app/Game/Client/Networker.js @@ -46,14 +46,12 @@ function (ProtocolHelper, GameController, User, NotificationCenter) { this.gameController = new GameController(); this.gameController.loadLevel("default.json"); - var user = new User(options.userId); - this.gameController.meJoined(user); - - console.log("Joined Success", options); + this.onUserJoined(options.userId); + this.gameController.onJoinMe(options.userId); if (options.others) { for(var i = 0; i < options.others.length; i++) { - this.users[userId] = new User(options.others[i]); + this.onUserJoined(options.others[i]); } } } @@ -73,10 +71,14 @@ function (ProtocolHelper, GameController, User, NotificationCenter) { // Commands from server Networker.prototype.onUserJoined = function (userId) { - this.users[userId] = new User(userId); + var user = new User(userId); + this.users[userId] = user; + this.gameController.userJoined(user); } Networker.prototype.onUserLeft = function (userId) { + var user = this.users[userId]; + this.gameController.userLeft(user); delete this.users[userId]; } diff --git a/app/Game/Core/GameController.js b/app/Game/Core/GameController.js index 6d489b3..bce0d71 100755 --- a/app/Game/Core/GameController.js +++ b/app/Game/Core/GameController.js @@ -36,9 +36,7 @@ function (Engine, Level, Player) { } GameController.prototype.userJoined = function (user) { - var player = new Player(user.id, this.physicsEngine); - this.players[user.id] = player; - return player; + this.players[user.id] = this.createPlayer(user); } GameController.prototype.userLeft = function (user) { @@ -47,6 +45,10 @@ function (Engine, Level, Player) { delete this.players[user.id]; } + GameController.prototype.createPlayer = function(user) { + return new Player(user.id, this.physicsEngine); + }; + return GameController; }); diff --git a/app/Game/Core/Player.js b/app/Game/Core/Player.js index c60fdf3..f37600d 100755 --- a/app/Game/Core/Player.js +++ b/app/Game/Core/Player.js @@ -5,31 +5,23 @@ define([ function (Doll, Settings) { - function Player (id, physicsEngine, repository) { + function Player (id, physicsEngine) { this.physicsEngine = physicsEngine; this.id = id; - this.repository = repository; this.standing = false; this.doll; this.mc; this.currentAnimationState = 'stand'; this.lookDirection = 1; this.moveDirection = 0; + this.isSpawned = false; - this.init(id); - } - - Player.prototype.init = function (id) { - this.doll = new Doll(this.physicsEngine, id); - //this.mc = EmbedHandler.load(EmbedHandler.CHUCK); - //this.mc.stop(); - //var mclp = new MovieClipLabelParser(); - //mclp.parse(this.mc); } Player.prototype.spawn = function (x, y) { - //this.repository.createModel(this.mc, this.doll.getBody()); + this.doll = new Doll(this.physicsEngine, this.id); this.doll.spawn(x, y); + this.isSpawned = true; } Player.prototype.getDoll = function () { @@ -113,8 +105,6 @@ function (Doll, Settings) { return; } - //this.mc.gotoAndPlay(type); - this.currentAnimationState = type; } diff --git a/app/Game/Server/Channel.js b/app/Game/Server/Channel.js index 5679ff5..27477ac 100755 --- a/app/Game/Server/Channel.js +++ b/app/Game/Server/Channel.js @@ -38,10 +38,10 @@ Channel.prototype.onAddUser = function (userId) { var user = new User(userId, this); - var others = Object.keys(this.users); + var othersJoined = Object.keys(this.users); this.users[user.id] = user; - NotificationCenter.trigger('user/' + user.id + "/joinSuccess", {userId: user.id, channelName: this.name, others: others}); + NotificationCenter.trigger('user/' + user.id + "/joinSuccess", {userId: user.id, channelName: this.name, others: othersJoined}); NotificationCenter.trigger('user/joined', user); } diff --git a/app/Game/Server/GameController.js b/app/Game/Server/GameController.js index 2fbdffd..30b7abc 100755 --- a/app/Game/Server/GameController.js +++ b/app/Game/Server/GameController.js @@ -4,15 +4,15 @@ define([ "Game/Config/Settings", "Game/Server/Control/InputController", "Lib/Utilities/RequestAnimFrame", - "Game/Core/NotificationCenter" + "Game/Core/NotificationCenter", + "Game/Server/Player" ], -function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter) { +function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter, Player) { function GameController (channel) { Parent.call(this, new PhysicsEngine()); - this.inputControllers = {}; this.channel = channel; this.update(); @@ -38,26 +38,31 @@ function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, No GameController.prototype.userJoined = function (user) { Parent.prototype.userJoined.call(this, user); - - var id = user.id; - var player = this.players[id]; + var player = this.players[user.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 + this.spawnPlayer(player); } - GameController.prototype.userLeft = function (user) { - Parent.prototype.userLeft.call(this, user); - delete this.inputControllers[user.id]; - } + GameController.prototype.spawnPlayer = function(player) { + var x = 150, + y = 50; + player.spawn(x, y); - GameController.prototype.progressGameCommandFromUser = function (command, options, user) { - var inputController = this.inputControllers[user.id]; - if (typeof inputController[command] == 'function') { - inputController[command](options); - } - } + var message = { + spawnPlayer: { + id: player.id, + x: x, + y: y + } + }; + NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message); + }; + + GameController.prototype.createPlayer = function(user) { + var player = new Player(user.id, this.physicsEngine); + player.setInputController(new InputController(player)) + return player; + }; GameController.prototype.updateWorld = function () { diff --git a/app/Game/Server/Player.js b/app/Game/Server/Player.js new file mode 100644 index 0000000..7922336 --- /dev/null +++ b/app/Game/Server/Player.js @@ -0,0 +1,21 @@ +define([ + "Game/Core/Player" +], + +function(Parent) { + + function Player(id, physicsEngine) { + Parent.call(this, id, physicsEngine); + + this.inputController = null; + } + + Player.prototype = Object.create(Parent.prototype); + + Player.prototype.setInputController = function(inputController) { + this.inputController = inputController; + } + + return Player; + +}); \ No newline at end of file diff --git a/app/Game/Server/User.js b/app/Game/Server/User.js index a7e060c..ebcf8f5 100644 --- a/app/Game/Server/User.js +++ b/app/Game/Server/User.js @@ -13,7 +13,7 @@ function(Parent, NotificationCenter, ProtocolHelper) { this.player = null; var self = this; - NotificationCenter.on('user/joined', function(user) { + NotificationCenter.on('user/joined', function(user) { // FIXME: use sendToAllUsersExcept instead if(user.id != self.id) { self.sendControlCommand("userJoined", user.id); } @@ -23,7 +23,7 @@ function(Parent, NotificationCenter, ProtocolHelper) { self.sendControlCommand("joinSuccess", options); }); - NotificationCenter.on('user/' + this.id + "/controlCommand", function(message) { // FIXME: right now only game commands? + NotificationCenter.on('user/' + this.id + "/controlCommand", function(message) { ProtocolHelper.applyCommand(message.data, self); }); }