From a1e94e1ed0d8de462173b894ad20e5dd269d6c9c Mon Sep 17 00:00:00 2001 From: Logsol Date: Sat, 5 Jan 2013 22:38:29 +0100 Subject: [PATCH] added getSpawnedPlayersAndTheirPositions in order to get already spawned players to spawn the moment you join and spawn yourself. fixes #26 --- app/Game/Client/GameController.js | 2 -- app/Game/Client/Networker.js | 14 +++++++++++--- app/Game/Core/Physics/Doll.js | 8 +++++++- app/Game/Core/Player.js | 4 ++++ app/Game/Server/Channel.js | 6 ++++-- app/Game/Server/GameController.js | 29 +++++++++++++++++++++++++++++ 6 files changed, 55 insertions(+), 8 deletions(-) diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index 9b7f572..7849e41 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -72,8 +72,6 @@ function (Parent, PhysicsEngine, ViewController, KeyboardController, Notificatio x = options.x, y = options.y; - console.log(this.players, options); - var player = this.players[playerId]; player.spawn(x, y); diff --git a/app/Game/Client/Networker.js b/app/Game/Client/Networker.js index 7dc875b..c938712 100755 --- a/app/Game/Client/Networker.js +++ b/app/Game/Client/Networker.js @@ -49,9 +49,17 @@ function (ProtocolHelper, GameController, User, NotificationCenter) { this.onUserJoined(options.userId); this.gameController.onJoinMe(options.userId); - if (options.others) { - for(var i = 0; i < options.others.length; i++) { - this.onUserJoined(options.others[i]); + if (options.joinedUsers) { + for(var i = 0; i < options.joinedUsers.length; i++) { + this.onUserJoined(options.joinedUsers[i]); + } + } + + if (options.spawnedPlayers) { + for(var i = 0; i < options.spawnedPlayers.length; i++) { + this.gameController.onSpawnPlayer(options.spawnedPlayers[i]); + + console.log("already spawned player, options: ", options.spawnedPlayers[i]) } } } diff --git a/app/Game/Core/Physics/Doll.js b/app/Game/Core/Physics/Doll.js index 052c70b..287d689 100755 --- a/app/Game/Core/Physics/Doll.js +++ b/app/Game/Core/Physics/Doll.js @@ -1,4 +1,10 @@ -define(["Lib/Vendor/Box2D", "Game/Config/Settings", "Game/Core/Collision/Detector"], function (Box2D, Settings, CollisionDetector) { +define([ + "Lib/Vendor/Box2D", + "Game/Config/Settings", + "Game/Core/Collision/Detector" +], + +function (Box2D, Settings, CollisionDetector) { function Doll (physicsEngine, id) { this.id = id; diff --git a/app/Game/Core/Player.js b/app/Game/Core/Player.js index f37600d..3c2b400 100755 --- a/app/Game/Core/Player.js +++ b/app/Game/Core/Player.js @@ -32,6 +32,10 @@ function (Doll, Settings) { return this.doll.getBody(); } + Player.prototype.getPosition = function () { + return this.getBody().GetPosition(); + } + Player.prototype.setStanding = function (isStanding) { var resetStates = ['jump', 'jumploop']; if (resetStates.indexOf(this.currentAnimationState)>=0 && !this.standing && isStanding) { diff --git a/app/Game/Server/Channel.js b/app/Game/Server/Channel.js index a82d875..de6b9ed 100755 --- a/app/Game/Server/Channel.js +++ b/app/Game/Server/Channel.js @@ -38,14 +38,16 @@ Channel.prototype.onAddUser = function (userId) { var user = new User(userId, this); - var others = Object.keys(this.users); + var joinedUsers = Object.keys(this.users); + var spawnedPlayers = this.gameController.getSpawnedPlayersAndTheirPositions(); this.users[user.id] = user; var options = { userId: user.id, channelName: this.name, - others: others + joinedUsers: joinedUsers, + spawnedPlayers: spawnedPlayers }; NotificationCenter.trigger('user/' + user.id + "/joinSuccess", options); diff --git a/app/Game/Server/GameController.js b/app/Game/Server/GameController.js index 30b7abc..1a46e55 100755 --- a/app/Game/Server/GameController.js +++ b/app/Game/Server/GameController.js @@ -91,5 +91,34 @@ function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, No setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); } + GameController.prototype.getSpawnedPlayers = function() { + var spawnedPlayers = {}; + for(player in this.players) { + if(player.isSpawned) { + spawnedPlayers[player.id] = player; + } + } + return spawnedPlayers; + }; + + GameController.prototype.getSpawnedPlayersAndTheirPositions = function() { + var spawnedPlayers = []; + for(id in this.players) { + var player = this.players[id]; + if(player.isSpawned) { + spawnedPlayers.push({ + id: id, + x: player.getPosition().x * Settings.RATIO, + y: player.getPosition().y * Settings.RATIO + }); + } + } + + // FIXME: OR: use get Spawned Players and fetch them into a sort of transfer objects + // that contains only necessary data + + return spawnedPlayers; + }; + return GameController; });