Permalink
Browse files

Added rudimentary Gamepad controller code

  • Loading branch information...
Jeena
Jeena committed May 30, 2014
1 parent 281cbdc commit ce0d0204ea11e7e34bc7f8a206ced5d057a14ff5
@@ -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;
+
+});
@@ -2,17 +2,19 @@ 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) {
Parent.call(this, me);
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');
@@ -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) {

0 comments on commit ce0d020

Please sign in to comment.