From c18384f50287b5bbb7f0527bb35b8246d98fd554 Mon Sep 17 00:00:00 2001 From: logsol Date: Sat, 24 May 2014 17:39:19 +0200 Subject: [PATCH] begin Round Bug improvements, player renders doll. fixes #64 --- app/Game/Channel/GameController.js | 11 +++++-- app/Game/Channel/Player.js | 4 ++- app/Game/Client/GameController.js | 34 +++++++++++++++------- app/Game/Client/Networker.js | 10 +++++-- app/Game/Client/Player.js | 4 ++- app/Game/Config/Settings.js | 6 ++-- app/Game/Core/GameController.js | 20 +++++++++---- app/Game/Core/GameObjects/Doll.js | 2 -- app/Game/Core/GameObjects/Items/RagDoll.js | 3 -- app/Game/Core/Player.js | 14 +++++---- app/Lib/Utilities/NotificationCenter.js | 10 ++++--- 11 files changed, 76 insertions(+), 42 deletions(-) diff --git a/app/Game/Channel/GameController.js b/app/Game/Channel/GameController.js index df17d06..8547514 100755 --- a/app/Game/Channel/GameController.js +++ b/app/Game/Channel/GameController.js @@ -28,7 +28,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N Nc.on(Nc.ns.channel.events.user.level.reset, this.onResetLevel, this), Nc.on(Nc.ns.channel.events.user.client.ready, this.onClientReady, this), Nc.on(Nc.ns.core.game.events.level.loaded, this.onLevelLoaded, this), - Nc.on(Nc.ns.core.game.player.killed, this.onPlayerKilled, this), // FIXME: move to events + Nc.on(Nc.ns.channel.events.game.player.killed, this.onPlayerKilled, this), ]); console.checkpoint('starting game controller for channel (' + options.channelName + ')'); @@ -66,8 +66,9 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N GameController.prototype.onPlayerKilled = function(player, killedByPlayer) { if(killedByPlayer.stats.score >= this.options.scoreLimit) { Nc.trigger(Nc.ns.channel.events.round.end); + } else { + this.spawnPlayer(player, Settings.RESPAWN_TIME); } - this.spawnPlayer(player, Settings.RESPAWN_TIME); }; GameController.prototype.spawnPlayer = function(player, respawnTime) { @@ -186,7 +187,6 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N GameController.prototype.onClientReady = function(userId) { var player = this.players[userId]; - this.spawnPlayer(player, 0); var options = { spawnedPlayers: this.getSpawnedPlayersAndTheirPositions(), @@ -196,9 +196,14 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N } Nc.trigger(Nc.ns.channel.to.client.user.gameCommand.send + userId, "clientReadyResponse", options); + + this.spawnPlayer(player, 0); }; GameController.prototype.onResetLevel = function(userId) { + + console.log('OH NO!!! ON RESET LEVEL IS CALLED AND RESPAWNES PLAYERS'); + Parent.prototype.onResetLevel.call(this); Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, "resetLevel", true); for (var key in this.players) { diff --git a/app/Game/Channel/Player.js b/app/Game/Channel/Player.js index 1c25e59..6fbe770 100755 --- a/app/Game/Channel/Player.js +++ b/app/Game/Channel/Player.js @@ -66,7 +66,7 @@ function (Parent, Nc) { if(this.stats.health < 0) this.stats.health = 0; if(this.stats.health <= 0) { - /*if(enemy != this) for testing! */ enemy.score(); + /*if(enemy != this) self killing scores for testing! */ enemy.score(); this.kill(enemy); } else { this.broadcastStats(); @@ -91,6 +91,8 @@ function (Parent, Nc) { ragDollId: ragDollId }); + Nc.trigger(Nc.ns.channel.events.game.player.killed, this, killedByPlayer); // sends endround + if(this.ragDoll) { this.ragDoll.delayedDestroy(); } diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index ee89f89..f1c853c 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -9,10 +9,11 @@ define([ "Game/Config/Settings", "Game/Client/GameObjects/GameObject", "Game/Client/GameObjects/Doll", - "Game/Client/View/DomController" + "Game/Client/View/DomController", + "Lib/Utilities/Protocol/Helper" ], -function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, requestAnimFrame, Settings, GameObject, Doll, DomController) { +function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, requestAnimFrame, Settings, GameObject, Doll, DomController, ProtocolHelper) { if (!window.cancelAnimationFrame) { window.cancelAnimationFrame = function(id) { @@ -20,8 +21,9 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque }; } - function GameController (options) { + + this.clientIsReady = false; this.view = ViewManager.createView(); this.me = null; this.animationRequestId = null; @@ -64,12 +66,6 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque GameController.prototype.onClientReadyResponse = function(options) { - if (options.spawnedPlayers) { - for(var i = 0; i < options.spawnedPlayers.length; i++) { - this.onSpawnPlayer(options.spawnedPlayers[i]); - } - } - if (options.worldUpdate) { this.onWorldUpdate(options.worldUpdate); } @@ -88,12 +84,19 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque if(!alreadyExists) { var item = this.level.createItem(itemDef.uid, itemDef.options); - //this.onGameObjectAdd("animated", item); } }; } this.createMe(options.userId); + + this.clientIsReady = true; // needs to stay before onSpawnPlayer + + if (options.spawnedPlayers) { + for(var i = 0; i < options.spawnedPlayers.length; i++) { + this.onSpawnPlayer(options.spawnedPlayers[i]); + } + } }; GameController.prototype.onWorldUpdate = function (updateData) { @@ -128,14 +131,23 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque this.view.setMe(this.me); } + GameController.prototype.onGameCommand = function(message) { + ProtocolHelper.applyCommand(message, this); + }; + GameController.prototype.onSpawnPlayer = function(options) { + + if(!this.clientIsReady) { + return; + } + var playerId = options.id, x = options.x, y = options.y; var player = this.players[playerId]; player.spawn(x, y); - this.gameObjects.animated.push(player); + this.onGameObjectAdd('animated', player); if(options.holdingItemUid) { this.onHandActionResponse({ diff --git a/app/Game/Client/Networker.js b/app/Game/Client/Networker.js index 0a32cde..07faa7e 100755 --- a/app/Game/Client/Networker.js +++ b/app/Game/Client/Networker.js @@ -20,8 +20,12 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) { var self = this; this.socketLink.on('message', function (message) { var m = JSON.parse(message) - if(Settings.NETWORK_LOG_INCOMING && !m.gameCommand) { - console.log('INCOMING', message); + if(Settings.NETWORK_LOG_INCOMING) { + + if (message.indexOf('worldUpdate') == -1 && message.indexOf('pong') == -1) { + console.log('INCOMING', message); + } + } ProtocolHelper.applyCommand(message, self); }); @@ -147,7 +151,7 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) { } Networker.prototype.onGameCommand = function(message) { - ProtocolHelper.applyCommand(message, this.gameController); + this.gameController.onGameCommand(message); } Networker.prototype.onPong = function(timestamp) { diff --git a/app/Game/Client/Player.js b/app/Game/Client/Player.js index e11dc6b..5fb7ce7 100755 --- a/app/Game/Client/Player.js +++ b/app/Game/Client/Player.js @@ -87,7 +87,9 @@ function (Parent, Nc, Settings) { Player.prototype.render = function() { - // dolls are self responsible + if(this.doll) { + this.doll.render(); + } if(this.playerInfoViewVisible) { var position = this.getPosition(); diff --git a/app/Game/Config/Settings.js b/app/Game/Config/Settings.js index 4aac03e..ce6cc82 100755 --- a/app/Game/Config/Settings.js +++ b/app/Game/Config/Settings.js @@ -63,12 +63,12 @@ define(function() { // NETWORKING WORLD_UPDATE_BROADCAST_INTERVAL: 70, CHANNEL_DESTRUCTION_TIME: 30, - NETWORK_LOG_INCOMING: false, - NETWORK_LOG_OUTGOING: true, + NETWORK_LOG_INCOMING: true, + NETWORK_LOG_OUTGOING: false, NETWORK_LOG_FILTER: ['ping', 'pong', 'worldUpdate', 'lookAt'], // CHANNEL - CHANNEL_END_ROUND_TIME: 10, + CHANNEL_END_ROUND_TIME: 4, //10, CHANNEL_DEFAULT_MAX_USERS: 40, CHANNEL_DEFAULT_SCORE_LIMIT: 10, CHANNEL_DEFAULT_LEVELS: ['stones2', 'debug', 'stones2', 'debug'] diff --git a/app/Game/Core/GameController.js b/app/Game/Core/GameController.js index 1e00239..0a20439 100755 --- a/app/Game/Core/GameController.js +++ b/app/Game/Core/GameController.js @@ -2,10 +2,11 @@ define([ "Game/" + GLOBALS.context + "/Physics/Engine", "Game/" + GLOBALS.context + "/Loader/TiledLevel", "Game/" + GLOBALS.context + "/Player", - "Lib/Utilities/NotificationCenter" + "Lib/Utilities/NotificationCenter", + "Game/" + GLOBALS.context + "/GameObjects/Doll", ], -function (PhysicsEngine, TiledLevel, Player, Nc) { +function (PhysicsEngine, TiledLevel, Player, Nc, Doll) { function GameController (options) { @@ -79,8 +80,7 @@ function (PhysicsEngine, TiledLevel, Player, Nc) { return; } - var i = this.gameObjects.animated.indexOf(player); - if(i>=0) this.gameObjects.animated.splice(i, 1); + this.onGameObjectRemove('animated', player); player.destroy(); delete this.players[userId]; @@ -105,14 +105,24 @@ function (PhysicsEngine, TiledLevel, Player, Nc) { Nc.off(this.ncTokens[i]); }; + /* + * Contents of gameObject: Players, Items, Tiles, RagDolls + * No Dolls. + */ + for (var key in this.gameObjects) { for (var i = 0; i < this.gameObjects[key].length; i++) { var gameObject = this.gameObjects[key][i]; - this.onGameObjectRemove(key, gameObject); + gameObject.destroy(); }; }; + this.gameObjects = { + fixed: [], + animated: [] + }; + this.physicsEngine.destroy(); } diff --git a/app/Game/Core/GameObjects/Doll.js b/app/Game/Core/GameObjects/Doll.js index 6f2e89e..ea1ed30 100755 --- a/app/Game/Core/GameObjects/Doll.js +++ b/app/Game/Core/GameObjects/Doll.js @@ -38,7 +38,6 @@ function (Parent, Box2D, Settings, CollisionDetector, Item, Nc) { this.createFixtures(); this.body.SetActive(false); - Nc.trigger(Nc.ns.core.game.gameObject.add, 'animated', this); } Doll.prototype = Object.create(Parent.prototype); @@ -372,7 +371,6 @@ function (Parent, Box2D, Settings, CollisionDetector, Item, Nc) { }; Doll.prototype.destroy = function() { - Nc.trigger(Nc.ns.core.game.gameObject.remove, 'animated', this); }; return Doll; diff --git a/app/Game/Core/GameObjects/Items/RagDoll.js b/app/Game/Core/GameObjects/Items/RagDoll.js index 8b6f751..ea7ca36 100644 --- a/app/Game/Core/GameObjects/Items/RagDoll.js +++ b/app/Game/Core/GameObjects/Items/RagDoll.js @@ -161,9 +161,6 @@ function (Parent, Box2D, Settings, Nc) { 0, options.limbs.upperRightArm.height / 2 ); - - - Nc.trigger(Nc.ns.core.game.gameObject.add, 'animated', this); } RagDoll.prototype = Object.create(Parent.prototype); diff --git a/app/Game/Core/Player.js b/app/Game/Core/Player.js index f16c644..5575b76 100755 --- a/app/Game/Core/Player.js +++ b/app/Game/Core/Player.js @@ -99,7 +99,7 @@ function (Doll, Settings, Nc, Exception, SpectatorDoll, RagDoll) { this.throw(0, 0, this.holdingItem) } - // get forces + // prepare for creating the ragdoll var options = { x: this.getPosition().x * Settings.RATIO, y: this.getPosition().y * Settings.RATIO, @@ -123,9 +123,6 @@ function (Doll, Settings, Nc, Exception, SpectatorDoll, RagDoll) { this.doll = null; this.ragDoll = ragDoll; - - - Nc.trigger(Nc.ns.core.game.player.killed, this, killedByPlayer); }; Player.prototype.update = function () { @@ -145,9 +142,14 @@ function (Doll, Settings, Nc, Exception, SpectatorDoll, RagDoll) { } this.spectatorDoll.destroy(); - if(this.doll) this.doll.destroy(); - this.playerController.destroy(); + if(this.doll) { + this.doll.destroy(); + } + + if(this.playerController) { + this.playerController.destroy(); + } } Player.prototype.setPlayerController = function(playerController) { diff --git a/app/Lib/Utilities/NotificationCenter.js b/app/Lib/Utilities/NotificationCenter.js index e6edd6d..6536eee 100755 --- a/app/Lib/Utilities/NotificationCenter.js +++ b/app/Lib/Utilities/NotificationCenter.js @@ -79,9 +79,6 @@ function (Exception) { add: null, remove: null }, - player: { - killed: null - }, events: { level: { loaded: null @@ -107,6 +104,11 @@ function (Exception) { }, round: { end: null + }, + game: { + player: { + killed: null + } } }, engine: { @@ -231,5 +233,5 @@ function (Exception) { } - return new NotificationCenter(); // making it singletone + return new NotificationCenter(); // making it singleton }); \ No newline at end of file