From 38b5023410a94125f44f27733e743e7c9741df22 Mon Sep 17 00:00:00 2001 From: Jeena Date: Mon, 20 Jan 2014 18:15:40 +0100 Subject: [PATCH] refactored grabbing --- app/Game/Client/GameController.js | 11 +++++- app/Game/Client/Player.js | 2 +- app/Game/Core/Collision/Detector.js | 12 +++--- app/Game/Core/GameObjects/Item.js | 31 ++++++--------- app/Game/Core/Player.js | 27 +++++--------- app/Game/Server/GameObjects/Item.js | 58 +++++++++++++++++++++++++++-- app/Game/Server/Player.js | 45 +++++++++++++++------- 7 files changed, 124 insertions(+), 62 deletions(-) diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index 172a688..1119ebf 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -105,7 +105,16 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat } }; - player.handAction(options.x, options.y, options.isHolding, item); + if(item) { + if(options.action == "throw") { + player.throw(options.x, options.y, item); + } else if(options.action == "grab") { + player.grab(item); + } + } else { + console.warn("Item for joint can not be found locally.") + } + }; GameController.prototype.loadLevel = function (path) { diff --git a/app/Game/Client/Player.js b/app/Game/Client/Player.js index 378ad84..4626aa1 100755 --- a/app/Game/Client/Player.js +++ b/app/Game/Client/Player.js @@ -6,4 +6,4 @@ function(Parent) { return Parent; -}); \ No newline at end of file +}); diff --git a/app/Game/Core/Collision/Detector.js b/app/Game/Core/Collision/Detector.js index 529c1d8..81d4cd1 100755 --- a/app/Game/Core/Collision/Detector.js +++ b/app/Game/Core/Collision/Detector.js @@ -7,9 +7,9 @@ function (Box2D) { function Detector () { // FIXME evtl.bind(this) ? this.listener = new Box2D.Dynamics.b2ContactListener(); this.listener.chuckDetector = this; - this.listener.BeginContact = this.BeginContact; - //this.listener.PostSolve = this.PostSolve; - this.listener.EndContact = this.EndContact; + this.listener.BeginContact = this.beginContact; + //this.listener.PostSolve = this.postSolve; + this.listener.EndContact = this.endContact; } Detector.IDENTIFIER = { @@ -34,14 +34,14 @@ function (Box2D) { /** Extension **/ - Detector.prototype.BeginContact = function (point) { + Detector.prototype.beginContact = function (point) { this.chuckDetector.onCollisionChange(point, true); } - Detector.prototype.PostSolve = function (point, impulse) { + Detector.prototype.postSolve = function (point, impulse) { } - Detector.prototype.EndContact = function (point) { + Detector.prototype.endContact = function (point) { this.chuckDetector.onCollisionChange(point, false); } diff --git a/app/Game/Core/GameObjects/Item.js b/app/Game/Core/GameObjects/Item.js index cf90015..b8eb2ec 100644 --- a/app/Game/Core/GameObjects/Item.js +++ b/app/Game/Core/GameObjects/Item.js @@ -12,7 +12,6 @@ function (Parent, Box2D, Settings) { this.createFixture(); this.body.ResetMassData(); this.flipDirection = 1; - this.heldByPlayers = []; } Item.prototype = Object.create(Parent.prototype); @@ -29,6 +28,7 @@ function (Parent, Box2D, Settings) { } Item.prototype.createFixture = function () { + var self = this; var itemShape; var w = this.options.width / Settings.RATIO; @@ -60,6 +60,14 @@ function (Parent, Box2D, Settings) { fixtureDef.isSensor = false; + /* + fixtureDef.userData = { + onCollisionChange: function(isColliding, fixture) { + self.onFixtureWithinReach(isColliding, "right", fixture); + } + } + */ + this.body.CreateFixture(fixtureDef); } @@ -69,29 +77,12 @@ function (Parent, Box2D, Settings) { // FIXME: implement body flip if necessary }; - Item.prototype.isGrabbingAllowed = function(player) { - return this.heldByPlayers.length == 0; - }; - Item.prototype.beingGrabbed = function(player) { - if(this.isGrabbingAllowed(player)) { - this.heldByPlayers.push(player); - return true; - } - return false; - }; - - Item.prototype.isReleasingAllowed = function(player) { - return true; + // overwrite if necessary }; Item.prototype.beingReleased = function(player) { - if(this.isReleasingAllowed(player)) { - var pos = this.heldByPlayers.indexOf(player); - if(pos >= 0) { - this.heldByPlayers.splice(pos, 1); - } - } + // overwrite if necessary }; return Item; diff --git a/app/Game/Core/Player.js b/app/Game/Core/Player.js index fff8676..7256c65 100755 --- a/app/Game/Core/Player.js +++ b/app/Game/Core/Player.js @@ -52,23 +52,16 @@ function (Doll, Settings) { if(this.doll) this.doll.lookAt(x, y); } - Player.prototype.handAction = function(x, y, isHolding, item) { - - if (isHolding) { - // throw - if(item.isReleasingAllowed()) { - item.beingReleased(this); - this.doll.throw(item, x, y); - this.holdingItem = null; - } - } else { - // take - if(item.isGrabbingAllowed()) { - item.beingGrabbed(this); - this.doll.grab(item); - this.holdingItem = item; - } - } + Player.prototype.grab = function(item) { + item.beingGrabbed(this); + this.doll.grab(item); + this.holdingItem = item; + }; + + Player.prototype.throw = function(x, y, item) { + item.beingReleased(this); + this.doll.throw(item, x, y); + this.holdingItem = null; }; Player.prototype.update = function () { diff --git a/app/Game/Server/GameObjects/Item.js b/app/Game/Server/GameObjects/Item.js index 4884a06..6fbcc97 100755 --- a/app/Game/Server/GameObjects/Item.js +++ b/app/Game/Server/GameObjects/Item.js @@ -2,8 +2,58 @@ define([ "Game/Core/GameObjects/Item" ], -function(Parent) { - - return Parent; +function (Parent) { -}); \ No newline at end of file + function Item(physicsEngine, uid, options) { + Parent.call(this, physicsEngine, uid, options); + this.heldByPlayers = []; + this.lastMoved = null; + } + + Item.prototype = Object.create(Parent.prototype); + + + Item.prototype.setLastMovedBy = function(player) { + + if(player) { + this.lastMoved = { + player: player, + timestamp: new Date() + } + } else { + this.lastMoved = null; + } + }; + + Item.prototype.isGrabbingAllowed = function(player) { + return this.heldByPlayers.length == 0; + }; + + Item.prototype.beingGrabbed = function(player) { + Parent.prototype.beingGrabbed.call(this, player); + + if(this.isGrabbingAllowed(player)) { + this.heldByPlayers.push(player); + this.setLastMovedBy(null); + } + }; + + Item.prototype.isReleasingAllowed = function(player) { + return true; + }; + + Item.prototype.beingReleased = function(player) { + Parent.prototype.beingReleased.call(this, player); + + if(this.isReleasingAllowed(player)) { + var pos = this.heldByPlayers.indexOf(player); + if(pos >= 0) { + this.heldByPlayers.splice(pos, 1); + this.setLastMovedBy(player); + } + } + }; + + return Item; + +}); diff --git a/app/Game/Server/Player.js b/app/Game/Server/Player.js index 88cac3f..7d06ea2 100755 --- a/app/Game/Server/Player.js +++ b/app/Game/Server/Player.js @@ -23,22 +23,41 @@ function (Parent, NotificationCenter) { } if(item) { - this.handAction(x, y, isHolding, item); - - var message = { - handActionResponse: { - playerId: this.id, - isHolding: isHolding, - itemUid: item.uid, - x: x, - y: y - } - }; - - NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message); } } + + Player.prototype.handAction = function(x, y, isHolding, item) { + + var options = { + playerId: this.id, + itemUid: item.uid + } + + var message = { + handActionResponse: options + } + + if (isHolding) { + // throw + if(item.isReleasingAllowed()) { + this.throw(x, y, item); + + options.action = "throw"; + options.x = x; + options.y = y; + NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message); + } + } else { + // grab + if(item.isGrabbingAllowed()) { + this.grab(item); + + options.action = "grab"; + NotificationCenter.trigger("sendControlCommandToAllUsers", "gameCommand", message); + } + } + }; return Player;