From d84fb20f90e1e79d85127f6684fadf7987fcf5de Mon Sep 17 00:00:00 2001 From: Jeena Date: Mon, 10 Feb 2014 14:21:26 +0100 Subject: [PATCH] fixes #12 and added health --- app/Game/Client/GameController.js | 10 ++++++ app/Game/Client/View/DomController.js | 11 +++++++ app/Game/Core/Player.js | 2 +- app/Game/Server/Channel.js | 21 +++++++++--- app/Game/Server/GameController.js | 6 ++-- app/Game/Server/Player.js | 47 ++++++++++++++++++++++----- app/Game/Server/User.js | 4 +++ 7 files changed, 83 insertions(+), 18 deletions(-) diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index 1119ebf..81ac2ef 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -117,6 +117,16 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat }; + GameController.prototype.onUpdateStats = function(options) { + var player = this.players[options.playerId]; + player.stats = options.stats; + + // FIXME: move to canvas later + if(player == this.me) { + DomController.setHealth(player.stats.health); + } + }; + GameController.prototype.loadLevel = function (path) { Parent.prototype.loadLevel.call(this, path); } diff --git a/app/Game/Client/View/DomController.js b/app/Game/Client/View/DomController.js index 795f6bf..a9823eb 100755 --- a/app/Game/Client/View/DomController.js +++ b/app/Game/Client/View/DomController.js @@ -77,6 +77,13 @@ function (Settings, NotificationCenter, Stats, Screenfull) { label.appendChild(checkbox); label.appendChild(document.createTextNode("Debug")); this.devToolsContainer.appendChild(label); + + // create health + this.health = document.createElement("span"); + this.health.innerHTML = "Health: 100"; + p = document.createElement("p"); + p.appendChild(this.health); + this.devToolsContainer.appendChild(p); }; DomController.prototype.statsBegin = function() { @@ -133,6 +140,10 @@ function (Settings, NotificationCenter, Stats, Screenfull) { return this.debugCanvas; } + DomController.prototype.setHealth = function(health) { + this.health.innerHTML = "Health: " + parseInt(health, 10); + }; + return new DomController(); diff --git a/app/Game/Core/Player.js b/app/Game/Core/Player.js index 849d68d..96e82d9 100755 --- a/app/Game/Core/Player.js +++ b/app/Game/Core/Player.js @@ -11,7 +11,7 @@ function (Doll, Settings, NotificationCenter) { this.stats = { health: 100, deaths: 0, - kills: 0 + score: 0 } this.physicsEngine = physicsEngine; diff --git a/app/Game/Server/Channel.js b/app/Game/Server/Channel.js index 83a633f..aa9ff1f 100755 --- a/app/Game/Server/Channel.js +++ b/app/Game/Server/Channel.js @@ -28,8 +28,11 @@ ProtocolHelper.applyCommand(message.data, self); }); - NotificationCenter.on('sendControlCommandToAllUsers', this.sendControlCommandToAllUsers, this); - NotificationCenter.on('sendControlCommandToAllUsersExcept', this.sendControlCommandToAllUsersExcept, this); + NotificationCenter.on('broadcastControlCommand', this.broadcastControlCommand, this); + NotificationCenter.on('broadcastControlCommandExcept', this.broadcastControlCommandExcept, this); + + NotificationCenter.on('broadcastGameCommand', this.broadcastGameCommand, this); + NotificationCenter.on('broadcastGameCommandExcept', this.broadcastGameCommandExcept, this); console.checkpoint('channel ' + name + ' created'); } @@ -69,7 +72,7 @@ Channel.prototype.onReleaseUser = function (userId) { var user = this.users[userId]; - this.sendControlCommandToAllUsersExcept("userLeft", user.id, user); + this.broadcastControlCommandExcept("userLeft", user.id, user); NotificationCenter.trigger('user/left', user); delete this.users[user.id]; } @@ -77,13 +80,13 @@ // Sending commands - Channel.prototype.sendControlCommandToAllUsers = function (command, options) { + Channel.prototype.broadcastControlCommand = function (command, options) { for(var id in this.users) { this.users[id].sendControlCommand(command, options); } } - Channel.prototype.sendControlCommandToAllUsersExcept = function (command, options, exceptUser) { + Channel.prototype.broadcastControlCommandExcept = function (command, options, exceptUser) { for(var id in this.users) { if (id != exceptUser.id) { this.users[id].sendControlCommand(command, options); @@ -91,6 +94,14 @@ } } + Channel.prototype.broadcastGameCommand = function (command, options) { + this.broadcastControlCommand("gameCommand", ProtocolHelper.encodeCommand(command, options)); + } + + Channel.prototype.broadcastGameCommandExcept = function (command, options, exceptUser) { + this.broadcastControlCommandExcept("gameCommand", ProtocolHelper.encodeCommand(command, options), exceptUser); + } + return Channel; }); \ No newline at end of file diff --git a/app/Game/Server/GameController.js b/app/Game/Server/GameController.js index f69f6e4..94ba4d5 100755 --- a/app/Game/Server/GameController.js +++ b/app/Game/Server/GameController.js @@ -67,7 +67,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N y: spawnPoint.y } }; - NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message); + NotificationCenter.trigger("broadcastControlCommand", "gameCommand", message); }; GameController.prototype.createPlayer = function(user) { @@ -82,7 +82,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N var update = this.getWorldUpdateObject(false); if(Object.getOwnPropertyNames(update).length > 0) { - NotificationCenter.trigger("sendControlCommandToAllUsers", 'gameCommand', {worldUpdate:update}); + NotificationCenter.trigger("broadcastControlCommand", 'gameCommand', {worldUpdate:update}); } setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); @@ -138,7 +138,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N GameController.prototype.onResetLevel = function(userId) { Parent.prototype.onResetLevel.call(this); - NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", {resetLevel:true}); + NotificationCenter.trigger("broadcastControlCommand", "gameCommand", {resetLevel:true}); for (var key in this.players) { this.spawnPlayer(this.players[key]); } diff --git a/app/Game/Server/Player.js b/app/Game/Server/Player.js index ca9841c..ed60296 100755 --- a/app/Game/Server/Player.js +++ b/app/Game/Server/Player.js @@ -33,10 +33,6 @@ function (Parent, NotificationCenter) { playerId: this.id, itemUid: item.uid } - - var message = { - handActionResponse: options - } if (isHolding) { // throw @@ -46,7 +42,7 @@ function (Parent, NotificationCenter) { options.action = "throw"; options.x = x; options.y = y; - NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message); + NotificationCenter.trigger("broadcastGameCommand", "handActionResponse", options); } } else { // grab @@ -54,19 +50,52 @@ function (Parent, NotificationCenter) { this.grab(item); options.action = "grab"; - NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message); + NotificationCenter.trigger("broadcastGameCommand", "handActionResponse", options); } } }; Player.prototype.addDamage = function(damage, enemy) { - this.stats.health -= damage; + this.updateHealth(this.stats.health - damage); + if(this.stats.health <= 0) { - this.stats.deaths++; - enemy.stats.kills++; + enemy.score(); this.kill(); } }; + + Player.prototype.spawn = function(x, y) { + Parent.prototype.spawn.call(this, x, y); + this.updateHealth(100); + }; + + Player.prototype.updateHealth = function(health) { + this.stats.health = health; + NotificationCenter.trigger("user/" + this.id + "/gameCommand", "updateStats", { + playerId: this.id, + stats: this.stats + }); + }; + + Player.prototype.kill = function() { + Parent.prototype.kill.call(this); + this.stats.deaths++; + this.broadcastStats(); + }; + + Player.prototype.score = function() { + this.stats.score++; + this.broadcastStats(); + }; + + Player.prototype.broadcastStats = function() { + NotificationCenter.trigger("broadcastGameCommand", "updateStats", { + playerId: this.id, + stats: this.stats + }); + }; + + return Player; diff --git a/app/Game/Server/User.js b/app/Game/Server/User.js index 622ae04..86b93e2 100755 --- a/app/Game/Server/User.js +++ b/app/Game/Server/User.js @@ -27,6 +27,10 @@ function(Parent, NotificationCenter, ProtocolHelper, ProtocolParser) { NotificationCenter.on('user/' + this.id + "/controlCommand", function(message) { ProtocolHelper.applyCommand(message.data, self); }); + + NotificationCenter.on('user/' + this.id + "/gameCommand", function(command, options) { + self.sendGameCommand(command, options); + }); } User.prototype = Object.create(Parent.prototype);