From aa4535cb0c2692d8d7a5c3c5bea89fb76f127a99 Mon Sep 17 00:00:00 2001 From: Jeena Date: Mon, 24 Feb 2014 19:26:31 +0100 Subject: [PATCH] fixed #53 --- app/Game/Client/GameController.js | 19 ++++++++++ app/Game/Core/GameObjects/Doll.js | 4 -- app/Game/Core/GameObjects/GameObject.js | 4 ++ app/Game/Core/GameObjects/Items/RagDoll.js | 4 +- app/Game/Core/GameObjects/Items/Skateboard.js | 15 ++++++++ app/Game/Core/Loader/Level.js | 2 +- app/Game/Core/Player.js | 1 - app/Game/Server/Channel.js | 10 ++++- app/Game/Server/GameController.js | 38 +++++++++---------- app/Game/Server/User.js | 8 +++- 10 files changed, 73 insertions(+), 32 deletions(-) diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index a993544..e9c15c1 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -68,6 +68,25 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat this.onWorldUpdate(options.worldUpdate); } + if (options.runtimeItems) { + for (var i = 0; i < options.runtimeItems.length; i++) { + var itemDef = options.runtimeItems[i]; + + var alreadyExists = false; + for (var i = 0; i < this.gameObjects.animated.length; i++) { + if(this.gameObjects.animated[i].uid == itemDef.uid) { + alreadyExists = true; + break; + } + }; + + if(!alreadyExists) { + var item = this.level.createItem(itemDef.uid, itemDef.options); + this.onGameObjectAdd("animated", item); + } + }; + } + this.createMe(options.userId); }; diff --git a/app/Game/Core/GameObjects/Doll.js b/app/Game/Core/GameObjects/Doll.js index 21e6051..b63b7d7 100755 --- a/app/Game/Core/GameObjects/Doll.js +++ b/app/Game/Core/GameObjects/Doll.js @@ -159,10 +159,6 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) { this.setActionState("fall"); } - Doll.prototype.getPosition = function() { - return this.body.GetPosition().Copy(); - }; - Doll.prototype.getHeadPosition = function() { var pos = this.body.GetPosition(); return { diff --git a/app/Game/Core/GameObjects/GameObject.js b/app/Game/Core/GameObjects/GameObject.js index 11703cf..20553a1 100755 --- a/app/Game/Core/GameObjects/GameObject.js +++ b/app/Game/Core/GameObjects/GameObject.js @@ -28,6 +28,10 @@ function (Box2D, Exception) { GameObject.prototype.getBody = function() { return this.body; }; + + GameObject.prototype.getPosition = function() { + return this.body.GetPosition().Copy(); + }; return GameObject; diff --git a/app/Game/Core/GameObjects/Items/RagDoll.js b/app/Game/Core/GameObjects/Items/RagDoll.js index 6a9afe3..bf718cb 100644 --- a/app/Game/Core/GameObjects/Items/RagDoll.js +++ b/app/Game/Core/GameObjects/Items/RagDoll.js @@ -345,8 +345,8 @@ function (Parent, Box2D, Settings, NotificationCenter) { body.SetAwake(true); var vector = new Box2D.Common.Math.b2Vec2( - x * Settings.MAX_THROW_FORCE * limbDampingFactor, - -y * Settings.MAX_THROW_FORCE * 1.5 *limbDampingFactor // 1.5 is to throw higher then far + x * Settings.MAX_THROW_FORCE * limbDampingFactor / this.options.weight, + -y * Settings.MAX_THROW_FORCE * limbDampingFactor / this.options.weight ); body.SetLinearVelocity(vector); // body.SetAngularVelocity(Settings.MAX_THROW_ANGULAR_VELOCITY * x); diff --git a/app/Game/Core/GameObjects/Items/Skateboard.js b/app/Game/Core/GameObjects/Items/Skateboard.js index 72b0b92..67fc387 100755 --- a/app/Game/Core/GameObjects/Items/Skateboard.js +++ b/app/Game/Core/GameObjects/Items/Skateboard.js @@ -85,6 +85,21 @@ function (Parent, Box2D, Settings) { // FIXME: implement body flip if necessary }; + Skateboard.prototype.throw = function(x, y) { + Parent.prototype.throw.call(this, x, y); + + for (var i = 0; i < this.wheels.length; i++) { + var body = this.wheels[i]; + body.SetAwake(true); + + var vector = new Box2D.Common.Math.b2Vec2( + x * Settings.MAX_THROW_FORCE / this.options.weight, + -y * Settings.MAX_THROW_FORCE / this.options.weight + ); + body.SetLinearVelocity(vector); + } + }; + Skateboard.prototype.destroy = function() { for (var i = 0; i < this.wheels.length; i++) { this.body.GetWorld().DestroyBody(this.wheels[i]); diff --git a/app/Game/Core/Loader/Level.js b/app/Game/Core/Loader/Level.js index 71aec73..5885327 100755 --- a/app/Game/Core/Loader/Level.js +++ b/app/Game/Core/Loader/Level.js @@ -67,7 +67,7 @@ define([ var options = items[i]; var uid = "item-" + i; var item = this.createItem(uid, options); - this.gameObjects.animated.push(item); + this.gameObjects.animated.push(item); // FIXME: use NotificationCenter }; }; diff --git a/app/Game/Core/Player.js b/app/Game/Core/Player.js index 957d847..3962615 100755 --- a/app/Game/Core/Player.js +++ b/app/Game/Core/Player.js @@ -116,7 +116,6 @@ function (Doll, Settings, NotificationCenter, Exception, SpectatorDoll, RagDoll) var ragDoll = new RagDoll(this.physicsEngine, "ragDoll-" + this.id + "-" + ragDollId, options); ragDoll.setVelocities(this.doll.getVelocities()); - console.log(ragDoll.uid) this.isSpawned = false; diff --git a/app/Game/Server/Channel.js b/app/Game/Server/Channel.js index 76d8dc6..10e4d52 100755 --- a/app/Game/Server/Channel.js +++ b/app/Game/Server/Channel.js @@ -104,11 +104,17 @@ } Channel.prototype.broadcastGameCommand = function (command, options) { - this.broadcastControlCommand("gameCommand", ProtocolHelper.encodeCommand(command, options)); + for(var id in this.users) { + this.users[id].sendGameCommand(command, options); + } } Channel.prototype.broadcastGameCommandExcept = function (command, options, exceptUser) { - this.broadcastControlCommandExcept("gameCommand", ProtocolHelper.encodeCommand(command, options), exceptUser); + for(var id in this.users) { + if (id != exceptUser.id) { + this.users[id].sendGameCommand(command, options); + } + } } return Channel; diff --git a/app/Game/Server/GameController.js b/app/Game/Server/GameController.js index 47f9d3e..15d368a 100755 --- a/app/Game/Server/GameController.js +++ b/app/Game/Server/GameController.js @@ -8,10 +8,11 @@ define([ "Lib/Vendor/Box2D", "Game/Server/Player", "Game/Server/GameObjects/GameObject", - "Game/Server/GameObjects/Doll" + "Game/Server/GameObjects/Doll", + "Game/Server/GameObjects/Items/RagDoll" ], -function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, NotificationCenter, Box2D, Player, GameObject, Doll) { +function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, NotificationCenter, Box2D, Player, GameObject, Doll, RagDoll) { function GameController (channel) { @@ -77,15 +78,13 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N // put it into self.gameObjects.animated.push(player); - var message = { - spawnPlayer: { - id: player.id, - x: spawnPoint.x, - y: spawnPoint.y - } + var options = { + id: player.id, + x: spawnPoint.x, + y: spawnPoint.y }; - NotificationCenter.trigger("broadcastControlCommand", "gameCommand", message); + NotificationCenter.trigger("broadcastGameCommand", "spawnPlayer", options); }, respawnTime * 1000); }; @@ -103,7 +102,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N var update = this.getWorldUpdateObject(false); if(Object.getOwnPropertyNames(update).length > 0) { - NotificationCenter.trigger("broadcastControlCommand", 'gameCommand', {worldUpdate:update}); + NotificationCenter.trigger("broadcastGameCommand", 'worldUpdate', update); } setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); @@ -143,7 +142,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N GameController.prototype.getSpawnedPlayersAndTheirPositions = function() { var spawnedPlayers = []; - for(id in this.players) { + for(var id in this.players) { var player = this.players[id]; if(player.isSpawned) { @@ -164,23 +163,21 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N return spawnedPlayers; }; - GameController.prototype.getAdditionalObjects = function() { + GameController.prototype.getRuntimeItems = function() { var objects = [] for (var i = 0; i < this.gameObjects.animated.length; i++) { if(this.gameObjects.animated[i] instanceof RagDoll) { var object = this.gameObjects.animated[i]; + var options = object.options; + options.x = object.getPosition().x; + options.y = object.getPosition().y; objects.push({ - uid: objects.uid, - options: object.options, - x: object.getPosition().x, - y: object.getPosition().y + uid: object.uid, + options: object.options }); } }; - var object = { - // FIXME: finish it! - } return objects; }; @@ -192,6 +189,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N var options = { spawnedPlayers: this.getSpawnedPlayersAndTheirPositions(), worldUpdate: this.getWorldUpdateObject(true), + runtimeItems: this.getRuntimeItems(), userId: userId } @@ -200,7 +198,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N GameController.prototype.onResetLevel = function(userId) { Parent.prototype.onResetLevel.call(this); - NotificationCenter.trigger("broadcastControlCommand", "gameCommand", {resetLevel:true}); + NotificationCenter.trigger("broadcastGameCommand", "resetLevel", true); for (var key in this.players) { this.spawnPlayer(this.players[key]); } diff --git a/app/Game/Server/User.js b/app/Game/Server/User.js index d02c373..d66b140 100755 --- a/app/Game/Server/User.js +++ b/app/Game/Server/User.js @@ -12,6 +12,7 @@ function(Parent, NotificationCenter, ProtocolHelper, ProtocolParser) { this.channel = channel; this.player = null; + this.isReady = false; var self = this; NotificationCenter.on('user/joined', function(user) { // FIXME: use sendToAllUsersExcept instead @@ -51,6 +52,7 @@ function(Parent, NotificationCenter, ProtocolHelper, ProtocolParser) { if(command.hasOwnProperty("resetLevel")) { NotificationCenter.trigger("user/resetLevel", this.id); } else if(command.hasOwnProperty("clientReady")) { + this.isReady = true; NotificationCenter.trigger("user/clientReady", this.id); } else { this.player.playerController.applyCommand(command); @@ -69,8 +71,10 @@ function(Parent, NotificationCenter, ProtocolHelper, ProtocolParser) { }; User.prototype.sendGameCommand = function(command, options) { - var data = ProtocolHelper.encodeCommand(command, options); - this.sendControlCommand("gameCommand", data); + if(this.isReady) { + var data = ProtocolHelper.encodeCommand(command, options); + this.sendControlCommand("gameCommand", data); + } };