diff --git a/app/Game/Client/Control/Input/MouseInput.js b/app/Game/Client/Control/Input/MouseInput.js index 527ea2f..cbd586a 100755 --- a/app/Game/Client/Control/Input/MouseInput.js +++ b/app/Game/Client/Control/Input/MouseInput.js @@ -37,7 +37,7 @@ function (Parent, DomController, Settings, NotificationCenter) { var x = (((e.clientX - this.offsetLeft) / Settings.STAGE_WIDTH) * 2) - 1; var y = (((Settings.STAGE_HEIGHT - (e.clientY - this.offsetTop)) / Settings.STAGE_HEIGHT) * 2) -1; - NotificationCenter.trigger("input/onHandAction", x, y); + NotificationCenter.trigger("input/onHandActionRequest", x, y); } }; diff --git a/app/Game/Client/Control/PlayerController.js b/app/Game/Client/Control/PlayerController.js index 11f56b6..f3c017e 100755 --- a/app/Game/Client/Control/PlayerController.js +++ b/app/Game/Client/Control/PlayerController.js @@ -15,7 +15,7 @@ function (Parent, KeyboardInput, MouseInput, NotificationCenter) { this.xyInput = new MouseInput(this); NotificationCenter.on("input/onXyChange", this.setXY, this); - NotificationCenter.on("input/onHandAction", this.handAction, this); + NotificationCenter.on("input/onHandActionRequest", this.handActionRequest, this); var keys = { w:87, @@ -75,12 +75,10 @@ function (Parent, KeyboardInput, MouseInput, NotificationCenter) { NotificationCenter.trigger('sendGameCommand', 'lookAt', options); }; - PlayerController.prototype.handAction = function(x, y) { + PlayerController.prototype.handActionRequest = function(x, y) { var options = {x:x, y:y}; - Parent.prototype.handAction.call(this, options); - NotificationCenter.trigger("sendGameCommand", "handAction", options); - }; - + NotificationCenter.trigger("sendGameCommand", "handActionRequest", options); + }; return PlayerController; }); \ No newline at end of file diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index 405584d..172a688 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -93,6 +93,21 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Notificat this.gameObjects.animated.push(player.getDoll()); } + GameController.prototype.onHandActionResponse = function(options) { + var player = this.players[options.playerId]; + + var item = null; + for (var i = 0; i < this.gameObjects.animated.length; i++) { + var currentItem = this.gameObjects.animated[i]; + if(currentItem.uid == options.itemUid) { + item = currentItem; + break; + } + }; + + player.handAction(options.x, options.y, options.isHolding, item); + }; + GameController.prototype.loadLevel = function (path) { Parent.prototype.loadLevel.call(this, path); } diff --git a/app/Game/Client/GameObjects/Doll.js b/app/Game/Client/GameObjects/Doll.js index c0b7135..74a7499 100755 --- a/app/Game/Client/GameObjects/Doll.js +++ b/app/Game/Client/GameObjects/Doll.js @@ -7,7 +7,7 @@ define([ function (Parent, Settings, NotificationCenter, Exception) { - function Doll(physicsEngine, playerId) { + function Doll(physicsEngine, uid, player) { this.animationDef = { "stand": [1,1], "walk": [2,28], @@ -23,7 +23,7 @@ function (Parent, Settings, NotificationCenter, Exception) { this.animatedMeshes = {}; this.headMesh = null; - Parent.call(this, physicsEngine, playerId); + Parent.call(this, physicsEngine, uid, player); } Doll.prototype = Object.create(Parent.prototype); diff --git a/app/Game/Config/Settings.js b/app/Game/Config/Settings.js index 779b534..fa88c81 100755 --- a/app/Game/Config/Settings.js +++ b/app/Game/Config/Settings.js @@ -27,6 +27,8 @@ define({ FLY_SPEED: 6.2, JUMP_SPEED: 20, MAX_THROW_FORCE: 18, + MAX_THROW_ANGULAR_VELOCITY: 8, + MAX_RUNNING_WEIGHT: 9, // restitution: bouncyness, friction: rubbing, density: mass TILE_FRICTION: 0.99, diff --git a/app/Game/Core/Control/PlayerController.js b/app/Game/Core/Control/PlayerController.js index c6bf431..17d4235 100755 --- a/app/Game/Core/Control/PlayerController.js +++ b/app/Game/Core/Control/PlayerController.js @@ -33,10 +33,6 @@ define(function () { if(options) this.player.lookAt(options.x, options.y); } - PlayerController.prototype.handAction = function(options) { - if (options) this.player.handAction(options.x, options.y); - }; - PlayerController.prototype.update = function () { if(this._walkingDirectionStatus != 0) { this.player.move(this._walkingDirectionStatus); diff --git a/app/Game/Core/GameObjects/Doll.js b/app/Game/Core/GameObjects/Doll.js index 21965e4..053701a 100755 --- a/app/Game/Core/GameObjects/Doll.js +++ b/app/Game/Core/GameObjects/Doll.js @@ -8,8 +8,9 @@ define([ function (Parent, Box2D, Settings, CollisionDetector, Item) { - function Doll (physicsEngine, uid) { + function Doll (physicsEngine, uid, player) { + this.player = player; this.height = 43; this.width = 9; this.headHeight = 12; @@ -255,7 +256,14 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) { } }; - Doll.prototype.grab = function(x, y) { + Doll.prototype.grab = function(item) { + + this.holdingItem = item; + this.positionHoldingItem(); + + + + /* var item = null; if (this.lookDirection == -1) { item = this.reachableItems.left.shift(); @@ -272,6 +280,7 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) { return item; + */ }; Doll.prototype.positionHoldingItem = function() { @@ -312,7 +321,7 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) { ), body.GetLocalCenter() ); - body.SetAngularVelocity(8 * x); + body.SetAngularVelocity(Settings.MAX_THROW_ANGULAR_VELOCITY * x); // }; Doll.prototype.onFootSensorDetection = function(isColliding, fixture) { diff --git a/app/Game/Core/GameObjects/Item.js b/app/Game/Core/GameObjects/Item.js index ef9eddb..cf90015 100644 --- a/app/Game/Core/GameObjects/Item.js +++ b/app/Game/Core/GameObjects/Item.js @@ -12,6 +12,7 @@ function (Parent, Box2D, Settings) { this.createFixture(); this.body.ResetMassData(); this.flipDirection = 1; + this.heldByPlayers = []; } Item.prototype = Object.create(Parent.prototype); @@ -67,6 +68,31 @@ 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; + }; + + Item.prototype.beingReleased = function(player) { + if(this.isReleasingAllowed(player)) { + var pos = this.heldByPlayers.indexOf(player); + if(pos >= 0) { + this.heldByPlayers.splice(pos, 1); + } + } + }; return Item; diff --git a/app/Game/Core/Player.js b/app/Game/Core/Player.js index e5383f8..fff8676 100755 --- a/app/Game/Core/Player.js +++ b/app/Game/Core/Player.js @@ -20,7 +20,7 @@ function (Doll, Settings) { }; Player.prototype.spawn = function (x, y) { - this.doll = new Doll(this.physicsEngine, "doll-" + this.id); + this.doll = new Doll(this.physicsEngine, "doll-" + this.id, this); this.doll.spawn(x, y); this.isSpawned = true; } @@ -52,17 +52,21 @@ function (Doll, Settings) { if(this.doll) this.doll.lookAt(x, y); } - Player.prototype.handAction = function(x, y) { + Player.prototype.handAction = function(x, y, isHolding, item) { - if (this.holdingItem) { + if (isHolding) { // throw - this.doll.throw(this.holdingItem, x, y); - this.holdingItem = null; + if(item.isReleasingAllowed()) { + item.beingReleased(this); + this.doll.throw(item, x, y); + this.holdingItem = null; + } } else { // take - var item = this.doll.grab(x, y); - if(item) { - this.holdingItem = item; + if(item.isGrabbingAllowed()) { + item.beingGrabbed(this); + this.doll.grab(item); + this.holdingItem = item; } } }; diff --git a/app/Game/Server/Control/PlayerController.js b/app/Game/Server/Control/PlayerController.js index aebfefc..7f164af 100755 --- a/app/Game/Server/Control/PlayerController.js +++ b/app/Game/Server/Control/PlayerController.js @@ -31,6 +31,10 @@ function(Parent, NotificationCenter, Parser) { } }; + PlayerController.prototype.handActionRequest = function(options) { + if (options) this.player.handActionRequest(options.x, options.y); + }; + return PlayerController; }); \ No newline at end of file diff --git a/app/Game/Server/GameObjects/Doll.js b/app/Game/Server/GameObjects/Doll.js index 4d8e182..214e8d1 100755 --- a/app/Game/Server/GameObjects/Doll.js +++ b/app/Game/Server/GameObjects/Doll.js @@ -2,8 +2,32 @@ define([ "Game/Core/GameObjects/Doll" ], -function(Parent) { +function (Parent) { - return Parent; + function Doll(physicsEngine, uid, player) { + Parent.call(this, physicsEngine, uid, player); + } + + Doll.prototype = Object.create(Parent.prototype); + + Doll.prototype.findCloseItem = function(x, y) { + + function findItem(array) { + for (var i = 0; i < array.length; i++) { + var item = array[i]; + if(item.isGrabbingAllowed(this.player)) { + return item; + } + } + } + + if (x < 0) { // looking left + return findItem(this.reachableItems.left); + } else { + return findItem(this.reachableItems.right); + } + } + + return Doll; }); \ No newline at end of file diff --git a/app/Game/Server/Player.js b/app/Game/Server/Player.js index c5398e5..88cac3f 100755 --- a/app/Game/Server/Player.js +++ b/app/Game/Server/Player.js @@ -1,9 +1,45 @@ define([ - "Game/Core/Player" + "Game/Core/Player", + "Lib/Utilities/NotificationCenter" ], -function(Parent) { +function (Parent, NotificationCenter) { + + function Player(id, physicsEngine) { + Parent.call(this, id, physicsEngine); + } - return Parent; + Player.prototype = Object.create(Parent.prototype); + + Player.prototype.handActionRequest = function(x, y) { + + var item = null; + var isHolding = !!this.holdingItem; + + if (isHolding) { + item = this.holdingItem; + } else { + item = this.doll.findCloseItem(x, y); + } + + 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); + } + } + + return Player; }); \ No newline at end of file