From d24ab60756a5aa61f36f20949569e233df518c3a Mon Sep 17 00:00:00 2001 From: logsol Date: Thu, 25 Dec 2014 00:33:00 +0100 Subject: [PATCH] added angular throwing --- app/Game/Channel/Control/PlayerController.js | 2 +- app/Game/Channel/Player.js | 18 +++--- .../Client/Control/Inputs/KeyboardAndMouse.js | 10 +++- app/Game/Client/Control/KeyboardInput.js | 55 +++++++++++++++++++ app/Game/Client/Control/PlayerController.js | 3 +- app/Game/Client/Control/Swiper.js | 4 +- app/Game/Client/GameController.js | 2 +- app/Game/Client/GameObjects/Doll.js | 4 +- app/Game/Config/Settings.js | 4 +- app/Game/Core/GameObjects/Doll.js | 4 +- app/Game/Core/GameObjects/Item.js | 8 +-- app/Game/Core/Player.js | 13 +++-- 12 files changed, 93 insertions(+), 34 deletions(-) create mode 100755 app/Game/Client/Control/KeyboardInput.js diff --git a/app/Game/Channel/Control/PlayerController.js b/app/Game/Channel/Control/PlayerController.js index c8e5c6e..c1f657f 100755 --- a/app/Game/Channel/Control/PlayerController.js +++ b/app/Game/Channel/Control/PlayerController.js @@ -33,7 +33,7 @@ function(Parent, Nc, Parser, Settings) { }; PlayerController.prototype.handActionRequest = function(options) { - if (options) this.player.handActionRequest(options.x, options.y); + if (options) this.player.handActionRequest(options); }; PlayerController.prototype.suicide = function() { diff --git a/app/Game/Channel/Player.js b/app/Game/Channel/Player.js index 7fc60b1..2ac4413 100755 --- a/app/Game/Channel/Player.js +++ b/app/Game/Channel/Player.js @@ -13,7 +13,7 @@ function (Parent, Nc) { Player.prototype = Object.create(Parent.prototype); - Player.prototype.handActionRequest = function(x, y) { + Player.prototype.handActionRequest = function(options) { if(!this.doll) return false; var item = null; @@ -22,29 +22,25 @@ function (Parent, Nc) { if (isHolding) { item = this.holdingItem; } else { - item = this.doll.findCloseItem(x, y); + item = this.doll.findCloseItem(options.x, options.y); } if(item) { - this.handAction(x, y, isHolding, item); + this.handAction(options, isHolding, item); } } - Player.prototype.handAction = function(x, y, isHolding, item) { + Player.prototype.handAction = function(options, isHolding, item) { - var options = { - playerId: this.id, - itemUid: item.uid - } + options.playerId = this.id; + options.itemUid = item.uid; if (isHolding) { // throw if(item.isReleasingAllowed()) { - this.throw(x, y, item); + this.throw(options, item); options.action = "throw"; - options.x = x; - options.y = y; Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, "handActionResponse", options); } } else { diff --git a/app/Game/Client/Control/Inputs/KeyboardAndMouse.js b/app/Game/Client/Control/Inputs/KeyboardAndMouse.js index 202b38a..92d0cc2 100644 --- a/app/Game/Client/Control/Inputs/KeyboardAndMouse.js +++ b/app/Game/Client/Control/Inputs/KeyboardAndMouse.js @@ -105,7 +105,11 @@ function (Parent, KeyboardInput, DomController, Settings, Swiper) { canvas.onmousedown = function(e) { if(!self.playerController.player.isHoldingSomething()) { - self.playerController.handActionRequest(self.x, self.y); + var options = { + x: self.x, + y: self.y + }; + self.playerController.handActionRequest(options); } else { self.swiper = new Swiper(); } @@ -121,8 +125,8 @@ function (Parent, KeyboardInput, DomController, Settings, Swiper) { canvas.onmouseup = function(e) { if(self.swiper) { - var xya = self.swiper.swipeEnd(e.x, e.y); - self.playerController.handActionRequest(xya.x, xya.y); + var options = self.swiper.swipeEnd(e.x, e.y); + self.playerController.handActionRequest(options); self.swiper = null; } } diff --git a/app/Game/Client/Control/KeyboardInput.js b/app/Game/Client/Control/KeyboardInput.js new file mode 100755 index 0000000..8c5484b --- /dev/null +++ b/app/Game/Client/Control/KeyboardInput.js @@ -0,0 +1,55 @@ +define([ + "Game/Client/Control/Key" +], + +function (Key) { + + function KeyboardInput () { + this._registry = {}; + this.init(); + } + + KeyboardInput.prototype.init = function () { + // Using window is ok here because it only runs in the browser + window.onkeydown = this._onKeyDown.bind(this); + window.onkeyup = this._onKeyUp.bind(this); + } + + KeyboardInput.prototype.registerKey = function (keyCode, onKeyDown, onKeyUp) { + var key = new Key(); + if(onKeyDown) key.setKeyDownFunction(onKeyDown); + if(onKeyUp) key.setKeyUpFunction(onKeyUp); + this._registry[keyCode] = key; + } + + KeyboardInput.prototype._getKeyByKeyCode = function (keyCode) { + return this._registry[keyCode]; + } + + KeyboardInput.prototype._onKeyDown = function (e) { + var key = this._getKeyByKeyCode(e.keyCode); + + if (key && !key.getActive()) { + var callback = key.getKeyDownFunction(); + if(callback) callback(); + key.setActive(true); + } + + // Prevent tab from changing focus + if(e.keyCode == 9) return false; + } + + KeyboardInput.prototype._onKeyUp = function (e) { + var key = this._getKeyByKeyCode(e.keyCode); + if (key && key.getActive()) { + var callback = key.getKeyUpFunction(); + if(callback) callback(); + key.setActive(false); + } + + // Prevent tab from changing focus + if(e.keyCode == 9) return false; + } + + return KeyboardInput; +}); \ No newline at end of file diff --git a/app/Game/Client/Control/PlayerController.js b/app/Game/Client/Control/PlayerController.js index 4d4a88e..2e67d96 100755 --- a/app/Game/Client/Control/PlayerController.js +++ b/app/Game/Client/Control/PlayerController.js @@ -67,8 +67,7 @@ function (Parent, Nc, KeyboardAndMouse, Gamepad) { Nc.trigger(Nc.ns.client.to.server.gameCommand.send, "suicide"); }; - PlayerController.prototype.handActionRequest = function(x, y) { - var options = {x:x, y:y}; + PlayerController.prototype.handActionRequest = function(options) { Nc.trigger(Nc.ns.client.to.server.gameCommand.send, "handActionRequest", options); }; diff --git a/app/Game/Client/Control/Swiper.js b/app/Game/Client/Control/Swiper.js index a83235f..84c4d68 100644 --- a/app/Game/Client/Control/Swiper.js +++ b/app/Game/Client/Control/Swiper.js @@ -75,7 +75,7 @@ function (Nc) { } Swiper.prototype.swipeEnd = function(x, y) { - var spin = this.angleSum; + var angularVelocity = this.angleSum; var length = this.lengthSum; var p0x = this.points[0].x; var p0y = this.points[0].y; @@ -106,7 +106,7 @@ function (Nc) { return { x: direction.x * length / 100, y: direction.y * length / 100, - spin: spin + av: angularVelocity / 100 } } diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index 163fa7a..7891d47 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -197,7 +197,7 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque if(item) { if(options.action == "throw") { - player.throw(options.x, options.y, item); + player.throw(options, item); } else if(options.action == "grab") { player.grab(item); } diff --git a/app/Game/Client/GameObjects/Doll.js b/app/Game/Client/GameObjects/Doll.js index 4d374b5..f408e2b 100755 --- a/app/Game/Client/GameObjects/Doll.js +++ b/app/Game/Client/GameObjects/Doll.js @@ -230,8 +230,8 @@ function (Parent, Settings, Nc, Exception, ColorConverter, Layer) { Nc.trigger(Nc.ns.client.view.mesh.update, this.layerId, this.holdingArmMesh, { visible: true }); }; - Doll.prototype.throw = function(item, x, y) { - Parent.prototype.throw.call(this, item, x, y); + Doll.prototype.throw = function(item, options) { + Parent.prototype.throw.call(this, item, options); this.animatedMeshes = this.animatedMeshesContainer.withArms; this.setActionState(this.actionState, true); Nc.trigger(Nc.ns.client.view.mesh.update, this.layerId, this.holdingArmMesh, { visible: false }); diff --git a/app/Game/Config/Settings.js b/app/Game/Config/Settings.js index c8961d9..d46c961 100755 --- a/app/Game/Config/Settings.js +++ b/app/Game/Config/Settings.js @@ -38,8 +38,8 @@ define(function() { FLY_SPEED: 6.2, JUMP_SPEED: 21, JUMP_STOP_DAMPING_FACTOR: 0.5, - MAX_THROW_FORCE: 18 * 3.5, - MAX_THROW_ANGULAR_VELOCITY: 0, + MAX_THROW_FORCE: 28, + MAX_THROW_ANGULAR_VELOCITY: 3, MAX_RUNNING_WEIGHT: 9, RESPAWN_TIME: 5, HEALTH_DISPLAY_TIME: 2, diff --git a/app/Game/Core/GameObjects/Doll.js b/app/Game/Core/GameObjects/Doll.js index 86bc318..6537ce1 100755 --- a/app/Game/Core/GameObjects/Doll.js +++ b/app/Game/Core/GameObjects/Doll.js @@ -354,12 +354,12 @@ function (Parent, Box2D, Settings, CollisionDetector, Item, Nc) { } }; - Doll.prototype.throw = function(item, x, y) { + Doll.prototype.throw = function(item, options) { this.body.GetWorld().DestroyJoint(this.holdingJoint); this.holdingJoint = null; this.holdingItem = null; - item.throw(x, y); + item.throw(options); }; Doll.prototype.isAnotherPlayerNearby = function() { diff --git a/app/Game/Core/GameObjects/Item.js b/app/Game/Core/GameObjects/Item.js index 5f8fafd..c2fcd10 100644 --- a/app/Game/Core/GameObjects/Item.js +++ b/app/Game/Core/GameObjects/Item.js @@ -129,17 +129,17 @@ function (Parent, Box2D, Options, Settings, Exception, Nc) { return this.body.GetWorldCenter(); }; - Item.prototype.throw = function(x, y) { + Item.prototype.throw = function(options) { var body = this.body; body.SetAwake(true); var vector = new Box2D.Common.Math.b2Vec2( - x * Settings.MAX_THROW_FORCE / this.options.weight, - -y * Settings.MAX_THROW_FORCE / this.options.weight + options.x * Settings.MAX_THROW_FORCE / this.options.weight, + -options.y * Settings.MAX_THROW_FORCE / this.options.weight ); this.body.SetLinearVelocity(vector); - body.SetAngularVelocity(Settings.MAX_THROW_ANGULAR_VELOCITY * x); + body.SetAngularVelocity(-options.av * Settings.MAX_THROW_ANGULAR_VELOCITY); }; Item.prototype.destroy = function() { diff --git a/app/Game/Core/Player.js b/app/Game/Core/Player.js index bc63272..4e3a012 100755 --- a/app/Game/Core/Player.js +++ b/app/Game/Core/Player.js @@ -87,9 +87,9 @@ function (Doll, Settings, Nc, Exception, SpectatorDoll, RagDoll) { this.holdingItem = item; }; - Player.prototype.throw = function(x, y, item) { + Player.prototype.throw = function(options, item) { if(!this.isSpawned) return false; - this.doll.throw(item, x, y); + this.doll.throw(item, options); item.beingReleased(this); this.holdingItem = null; }; @@ -99,7 +99,7 @@ function (Doll, Settings, Nc, Exception, SpectatorDoll, RagDoll) { // FIXME: do something better then just respawn in GameController if(this.holdingItem) { - this.throw(0, 0, this.holdingItem) + this.throw(0, 0, 0, this.holdingItem) } // prepare for creating the ragdoll @@ -144,7 +144,12 @@ function (Doll, Settings, Nc, Exception, SpectatorDoll, RagDoll) { Nc.trigger(Nc.ns.core.game.gameObject.remove, 'animated', this); if(this.holdingItem) { - this.throw(0, 0, this.holdingItem); + var options = { + x: 0, + y: 0, + av: 0 + }; + this.throw(options, this.holdingItem); } this.spectatorDoll.destroy();