From 8641a2dc0b53f3424e08441a2513e5875d725337 Mon Sep 17 00:00:00 2001 From: logsol Date: Sat, 1 Oct 2016 16:44:40 +0200 Subject: [PATCH] On destroy unsubscribe missing GameObjects from render event fixes #71 --- app/Game/Channel/GameController.js | 42 ++++++++++++++++++++--------- app/Game/Client/GameObjects/Item.js | 4 ++- app/Game/Client/Player.js | 5 +++- static/html/index.html | 6 ----- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/app/Game/Channel/GameController.js b/app/Game/Channel/GameController.js index 4d0adbd..c41897a 100755 --- a/app/Game/Channel/GameController.js +++ b/app/Game/Channel/GameController.js @@ -177,25 +177,36 @@ function (Parent, PhysicsEngine, Settings, requestAnimFrame, Nc, Box2D, Player, return spawnedPlayers; }; - GameController.prototype.getRuntimeItems = function() { - var objects = []; + GameController.prototype._getRuntimeItems = function() { - // This is using the level.createItem mechanism to - // create the RubeDoll from its ItemSettings + var runtimeItems = []; for (var uid in this.worldUpdateObjects) { if(this.worldUpdateObjects[uid] instanceof RubeDoll) { var object = this.worldUpdateObjects[uid]; - var options = object.options; - options.x = object.getPosition().x; - options.y = object.getPosition().y; - objects.push({ - uid: object.uid, - options: object.options - }); + runtimeItems.push(object); } } + return runtimeItems; + }; - return objects; + GameController.prototype.gatherRuntimeItemsForWorldUpdate = function() { + var infos = []; + var runtimeItems = this._getRuntimeItems(); + + // On the other side this is using the level.createItem mechanism to + // create the RubeDoll from its ItemSettings + for (var i = 0; i < runtimeItems.length; i++) { + var object = runtimeItems[i]; + var options = object.options; + options.x = object.getPosition().x; + options.y = object.getPosition().y; + infos.push({ + uid: object.uid, + options: object.options + }); + } + + return infos; }; GameController.prototype.onClientReady = function(userId) { @@ -204,7 +215,7 @@ function (Parent, PhysicsEngine, Settings, requestAnimFrame, Nc, Box2D, Player, var options = { spawnedPlayers: this.getSpawnedPlayersAndTheirPositions(), worldUpdate: this.getWorldUpdateObject(true), - runtimeItems: this.getRuntimeItems(), + runtimeItems: this.gatherRuntimeItemsForWorldUpdate(), userId: userId }; @@ -241,6 +252,11 @@ function (Parent, PhysicsEngine, Settings, requestAnimFrame, Nc, Box2D, Player, clearTimeout(this.spawnTimeouts[i]); }; + var runtimeItems = this._getRuntimeItems(); + for (var i = 0; i < runtimeItems.length; i++) { + runtimeItems[i].destroy(); + } + Parent.prototype.destroy.call(this); }; diff --git a/app/Game/Client/GameObjects/Item.js b/app/Game/Client/GameObjects/Item.js index 8115858..3770dce 100755 --- a/app/Game/Client/GameObjects/Item.js +++ b/app/Game/Client/GameObjects/Item.js @@ -13,7 +13,9 @@ function (Parent, Settings, Nc, Layer) { this.layerId = Layer.ID.ITEM; Parent.call(this, physicsEngine, uid, options); - Nc.on(Nc.ns.client.game.events.render, this.render, this); + this.ncTokens = this.ncTokens.concat([ + Nc.on(Nc.ns.client.game.events.render, this.render, this) + ]); } Item.prototype = Object.create(Parent.prototype); diff --git a/app/Game/Client/Player.js b/app/Game/Client/Player.js index 4ad6bac..0af2d58 100755 --- a/app/Game/Client/Player.js +++ b/app/Game/Client/Player.js @@ -16,7 +16,9 @@ function (Parent, Nc, Settings) { this.healthBarViewVisible = false; this.initHealthBar(); - Nc.on(Nc.ns.client.game.events.render, this.render, this); + this.ncTokens = [ + Nc.on(Nc.ns.client.game.events.render, this.render, this) + ]; } Player.prototype = Object.create(Parent.prototype); @@ -109,6 +111,7 @@ function (Parent, Nc, Settings) { Player.prototype.destroy = function() { clearTimeout(this.healthBarViewVisibleTimeout); Nc.trigger(Nc.ns.client.view.healthBar.remove, this.healthBarView); + Nc.off(this.ncTokens); Parent.prototype.destroy.call(this); }; diff --git a/static/html/index.html b/static/html/index.html index f42bc8d..47b486f 100644 --- a/static/html/index.html +++ b/static/html/index.html @@ -26,12 +26,6 @@
Maps