diff --git a/app/Game/Client/Control/Input/MouseInput.js b/app/Game/Client/Control/Input/MouseInput.js index 493c901..527ea2f 100644 --- a/app/Game/Client/Control/Input/MouseInput.js +++ b/app/Game/Client/Control/Input/MouseInput.js @@ -1,10 +1,11 @@ define([ "Game/Client/Control/Input/XyInput", "Game/Client/View/DomController", - "Game/Config/Settings" + "Game/Config/Settings", + "Lib/Utilities/NotificationCenter" ], -function (Parent, DomController, Settings) { +function (Parent, DomController, Settings, NotificationCenter) { function MouseInput() { Parent.call(this); @@ -30,6 +31,14 @@ function (Parent, DomController, Settings) { self.onXyChange(x, y); } + + canvas.onmousedown = function(e) { + + 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); + } }; diff --git a/app/Game/Client/Control/Input/XyInput.js b/app/Game/Client/Control/Input/XyInput.js index 9f71a77..5848dc4 100644 --- a/app/Game/Client/Control/Input/XyInput.js +++ b/app/Game/Client/Control/Input/XyInput.js @@ -12,10 +12,8 @@ function (NotificationCenter) { XyInput.prototype.onXyChange = function(x, y) { this.x = x; this.y = y; - NotificationCenter.trigger('onXyChange', x, y); + NotificationCenter.trigger('input/onXyChange', x, y); } - - return XyInput; diff --git a/app/Game/Client/Control/PlayerController.js b/app/Game/Client/Control/PlayerController.js index 9fd594a..747f882 100755 --- a/app/Game/Client/Control/PlayerController.js +++ b/app/Game/Client/Control/PlayerController.js @@ -14,7 +14,8 @@ function (Parent, KeyboardInput, MouseInput, NotificationCenter) { this.keyboardInput = new KeyboardInput(this); this.xyInput = new MouseInput(this); - NotificationCenter.on("onXyChange", this.setXY, this); + NotificationCenter.on("input/onXyChange", this.setXY, this); + NotificationCenter.on("input/onHandAction", this.handAction, this); var keys = { w:87, @@ -71,6 +72,12 @@ function (Parent, KeyboardInput, MouseInput, NotificationCenter) { NotificationCenter.trigger('sendGameCommand', 'lookAt', options); }; + PlayerController.prototype.handAction = function(x, y) { + var options = {x:x, y:y}; + Parent.prototype.handAction.call(this, options); + NotificationCenter.trigger("sendGameCommand", "handAction", options); + }; + PlayerController.prototype.update = function () { this.keyboardInput.update(); Parent.prototype.update.call(this); diff --git a/app/Game/Client/View/DomController.js b/app/Game/Client/View/DomController.js index a0a97cc..6145786 100755 --- a/app/Game/Client/View/DomController.js +++ b/app/Game/Client/View/DomController.js @@ -39,7 +39,6 @@ function (Settings) { } DomController.createDebugCanvas = function () { - console.log('setHERE') var container = DomController.getCanvasContainer(); if(DomController.debugCanvas) { container.removeChild(DomController.debugCanvas); diff --git a/app/Game/Core/Control/PlayerController.js b/app/Game/Core/Control/PlayerController.js index f446154..c6bf431 100755 --- a/app/Game/Core/Control/PlayerController.js +++ b/app/Game/Core/Control/PlayerController.js @@ -33,8 +33,11 @@ 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 () { - //console.log(this._walkingDirectionStatus) 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 fed2616..688dba2 100755 --- a/app/Game/Core/GameObjects/Doll.js +++ b/app/Game/Core/GameObjects/Doll.js @@ -22,6 +22,8 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) { left: [], right: [] }; + this.holdingJoint = null; + this.holdingItem = null; this.createFixtures(); this.body.SetActive(false); @@ -201,6 +203,8 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) { } Doll.prototype.lookAt = function(x, y) { + var oldLookDirection = this.lookDirection; + this.body.SetAwake(true); if(x < 0) { this.lookDirection = -1; @@ -210,6 +214,62 @@ function (Parent, Box2D, Settings, CollisionDetector, Item) { this.lookAtXY.x = x; this.lookAtXY.y = y; + + if(oldLookDirection != this.lookDirection) { + this.positionHoldingItem(); + } + }; + + Doll.prototype.grab = function(x, y) { + var item = null; + if (this.lookDirection == -1) { + item = this.reachableItems.left.shift(); + } else { + item = this.reachableItems.right.shift(); + } + + if(item) { + + this.holdingItem = item; + + this.positionHoldingItem(); + } + + + return item; + }; + + Doll.prototype.positionHoldingItem = function() { + if(this.holdingItem) { + + if(this.holdingJoint) { + this.body.GetWorld().DestroyJoint(this.holdingJoint); + this.holdingJoint = null; + } + + var p = this.body.GetPosition(); + this.holdingItem.body.SetPosition(new Box2D.Common.Math.b2Vec2( + p.x + ((this.holdingItem.options.width / Settings.RATIO / 2 + 5 / Settings.RATIO) * this.lookDirection), + p.y - (this.holdingItem.options.height / Settings.RATIO / 2) + )); + //this.holdingItem.body.SetAngle(Math.PI * 2 / 180 * 20 * -this.lookDirection); + this.holdingItem.body.SetAngle(0); + + var jointDef = new Box2D.Dynamics.Joints.b2WeldJointDef(); + jointDef.Initialize(this.body, this.holdingItem.body, this.holdingItem.body.GetWorldCenter()); + + this.holdingJoint = this.body.GetWorld().CreateJoint(jointDef); + } + }; + + Doll.prototype.throw = function(item, x, y) { + this.body.GetWorld().DestroyJoint(this.holdingJoint); + this.holdingJoint = null; + this.holdingItem = null; + + var body = item.body; + body.SetAwake(true); + body.ApplyImpulse(new Box2D.Common.Math.b2Vec2(x * 3, -y * 3), body.GetPosition()); }; Doll.prototype.onFootSensorDetection = function(isColliding, fixture) { diff --git a/app/Game/Core/Player.js b/app/Game/Core/Player.js index 0809f5c..d74dd9c 100755 --- a/app/Game/Core/Player.js +++ b/app/Game/Core/Player.js @@ -12,6 +12,7 @@ function (Doll, Settings) { this.doll; this.id = id; this.isSpawned = false; + this.holdingItem = null; } Player.prototype.getDoll = function() { @@ -45,6 +46,21 @@ function (Doll, Settings) { if(this.doll) this.doll.lookAt(x, y); } + Player.prototype.handAction = function(x, y) { + + if (this.holdingItem) { + // throw + this.doll.throw(this.holdingItem, x, y); + this.holdingItem = null; + } else { + // take + var item = this.doll.grab(x, y); + if(item) { + this.holdingItem = item; + } + } + }; + Player.prototype.update = function () { if(this.doll) { diff --git a/app/Game/Server/Control/PlayerController.js b/app/Game/Server/Control/PlayerController.js index f24bbe3..3ba41d3 100644 --- a/app/Game/Server/Control/PlayerController.js +++ b/app/Game/Server/Control/PlayerController.js @@ -23,7 +23,7 @@ function(Parent, NotificationCenter, Parser) { } else { message = options; } - + for (var command in message) { this[command].call(this, message[command]); } diff --git a/app/Game/Server/PipeToLobby.js b/app/Game/Server/PipeToLobby.js index 4614772..2cef3dc 100755 --- a/app/Game/Server/PipeToLobby.js +++ b/app/Game/Server/PipeToLobby.js @@ -16,22 +16,15 @@ function (NotificationCenter, Channel) { process.on('message', function (message, handle) { - for(var method in message.data) { - switch(method) { - case 'CREATE': - self.channel = new Channel(this, message.data[method]); - break; - - case 'KILL': - self.channel.destroy(); - process.exit(0); - break; - - default: - self.onMessage(message); - break; - } + if(message.data.hasOwnProperty('CREATE')) { + self.channel = new Channel(this, message.data['CREATE']); + } else if (message.data.hasOwnProperty('KILL')) { + self.channel.destroy(); + process.exit(0); + } else { + self.onMessage(message); } + }); } diff --git a/app/Lobby/PipeToChannel.js b/app/Lobby/PipeToChannel.js index d48b465..3b98ba8 100755 --- a/app/Lobby/PipeToChannel.js +++ b/app/Lobby/PipeToChannel.js @@ -26,6 +26,7 @@ function (NotificationCenter, childProcess) { var self = this; } + // While creating user PipeToChannel.prototype.send = function (recipient, data) { var message = { recipient: recipient, @@ -35,6 +36,7 @@ function (NotificationCenter, childProcess) { this.channelPipe.send(message); } + // If user already created PipeToChannel.prototype.sendToUser = function (id, data) { var message = { recipient: "user/" + id,