From ce0d0204ea11e7e34bc7f8a206ced5d057a14ff5 Mon Sep 17 00:00:00 2001 From: Jeena Date: Sat, 31 May 2014 01:49:10 +0200 Subject: [PATCH] Added rudimentary Gamepad controller code --- app/Game/Client/Control/Input/GamepadInput.js | 80 +++++++++++++++++++ app/Game/Client/Control/PlayerController.js | 10 ++- app/Game/Client/GameController.js | 2 + 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 app/Game/Client/Control/Input/GamepadInput.js diff --git a/app/Game/Client/Control/Input/GamepadInput.js b/app/Game/Client/Control/Input/GamepadInput.js new file mode 100644 index 0000000..312b254 --- /dev/null +++ b/app/Game/Client/Control/Input/GamepadInput.js @@ -0,0 +1,80 @@ +define([ + "Game/Client/Control/Input/XyInput", + "Game/Config/Settings", + "Lib/Utilities/NotificationCenter", + "Lib/Utilities/RequestAnimFrame" +], + +function (Parent, Settings, Nc, requestAnimFrame) { + + function GamepadInput(playerController) { + this.playerController = playerController; + Parent.call(this); + + this.gamepad = null; + this.oldHoldingPressure = 0; + this.keyChanged = { + w: false, + a: false, + d: false + }; + var self = this; + + window.addEventListener("gamepadconnected", function(e) { + self.gamepad = e.gamepad; + }); + + window.addEventListener("gamepaddisconnected", function(e) { + self.gamepad = null; + }); + } + + GamepadInput.prototype = Object.create(Parent.prototype); + + GamepadInput.prototype.update = function() { + if(this.gamepad) { + var x = this.gamepad.axes[2]; + var y = -this.gamepad.axes[3]; + + // Looking direction + this.playerController.xyInput.onXyChange(x, y); + + // Pointer finger holding item + var holdingPressure = this.gamepad.axes[5]; + if((holdingPressure > 0 && this.oldHoldingPressure <= 0) + || (holdingPressure <= 0 && this.oldHoldingPressure > 0)) { + this.playerController.handActionRequest(x, y); + } + this.oldHoldingPressure = holdingPressure; + + this.simulateKeyboard(); + } + }; + + GamepadInput.prototype.simulateKeyboard = function() { + // walking + /* + a:0 b:1 x:2 y:3 + lb:4 rb:5 jl:6 jr:7 + start:8 back:9 xbox:10 + u:11 d:12 l:13 r:14 + */ + this.key(11, 87); + this.key(13, 65); + this.key(14, 68); + }; + + GamepadInput.prototype.key = function(i, key) { + if(this.keyChanged[key] != this.gamepad.buttons[i].pressed) { + var evt = {keyCode:key} + if(this.gamepad.buttons[i].pressed) window.onkeydown(evt); + else window.onkeyup(evt) + + this.keyChanged[key] = this.gamepad.buttons[i].pressed; + } + }; + + + return GamepadInput; + +}); diff --git a/app/Game/Client/Control/PlayerController.js b/app/Game/Client/Control/PlayerController.js index 6b86e15..9de126d 100755 --- a/app/Game/Client/Control/PlayerController.js +++ b/app/Game/Client/Control/PlayerController.js @@ -2,10 +2,11 @@ define([ "Game/Core/Control/PlayerController", "Game/Client/Control/KeyboardInput", "Game/Client/Control/Input/MouseInput", - "Lib/Utilities/NotificationCenter" + "Lib/Utilities/NotificationCenter", + "Game/Client/Control/Input/GamepadInput", ], -function (Parent, KeyboardInput, MouseInput, Nc) { +function (Parent, KeyboardInput, MouseInput, Nc, GamepadInput) { function PlayerController (me) { @@ -13,6 +14,7 @@ function (Parent, KeyboardInput, MouseInput, Nc) { this.keyboardInput = new KeyboardInput(this); this.xyInput = new MouseInput(this); + this.gamepadInput = new GamepadInput(this); this.ncTokens = [ Nc.on(Nc.ns.client.input.xy.change, this.setXY, this), @@ -44,6 +46,10 @@ function (Parent, KeyboardInput, MouseInput, Nc) { PlayerController.prototype = Object.create(Parent.prototype); + PlayerController.prototype.update = function() { + this.gamepadInput.update(); + }; + PlayerController.prototype.init = function (keys) { this.keyboardInput.registerKey(keys.a, 'moveLeft', 'stop'); diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index 6121033..5fc74a9 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -50,6 +50,8 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque this.animationRequestId = requestAnimFrame(this.update.bind(this)); + if(this.me && this.me.playerController) this.me.playerController.update(); + this.physicsEngine.update(); if(this.me) {