Add gamepad controller support for the game
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
###
|
||||
GamepadController (Orientation + buttons) for touch devices
|
||||
|
||||
@class bkcore.GamepadController
|
||||
@author Mahesh Kulkarni <http://twitter.com/maheshkk>
|
||||
###
|
||||
class GamepadController
|
||||
|
||||
@isCompatible: ->
|
||||
return ('getGamepads' of navigator) or ('webkitGetGamepads' of navigator)
|
||||
|
||||
###
|
||||
Creates a new GamepadController
|
||||
###
|
||||
constructor: (@buttonPressCallback) ->
|
||||
@active = true
|
||||
@leftStickArray = []
|
||||
@rightStickArray = []
|
||||
|
||||
###
|
||||
@public
|
||||
###
|
||||
updateAvailable: ->
|
||||
return false if not @active
|
||||
gamepads = if navigator.getGamepads then navigator.getGamepads() else navigator.webkitGetGamepads()
|
||||
return false if not gamepads?[0]
|
||||
gp = gamepads[0]
|
||||
return if not gp.buttons? or not gp.axes?
|
||||
@acceleration = gp.buttons[0]
|
||||
@lstickx = gp.axes[0]
|
||||
@ltrigger = gp.buttons[6]
|
||||
@rtrigger = gp.buttons[7]
|
||||
@select = gp.buttons[8]
|
||||
@buttonPressCallback this
|
||||
true
|
||||
|
||||
exports = exports ? @
|
||||
exports.bkcore ||= {}
|
||||
exports.bkcore.controllers ||= {}
|
||||
exports.bkcore.controllers.GamepadController = GamepadController
|
||||
@@ -0,0 +1,69 @@
|
||||
// Generated by CoffeeScript 1.6.3
|
||||
/*
|
||||
GamepadController (Orientation + buttons) for touch devices
|
||||
|
||||
@class bkcore.GamepadController
|
||||
@author Mahesh Kulkarni <http://twitter.com/maheshkk>
|
||||
*/
|
||||
|
||||
|
||||
(function() {
|
||||
var GamepadController, exports, _base;
|
||||
|
||||
GamepadController = (function() {
|
||||
GamepadController.isCompatible = function() {
|
||||
return ('getGamepads' in navigator) || ('webkitGetGamepads' in navigator);
|
||||
};
|
||||
|
||||
/*
|
||||
Creates a new GamepadController
|
||||
*/
|
||||
|
||||
|
||||
function GamepadController(buttonPressCallback) {
|
||||
this.buttonPressCallback = buttonPressCallback;
|
||||
this.active = true;
|
||||
this.leftStickArray = [];
|
||||
this.rightStickArray = [];
|
||||
}
|
||||
|
||||
/*
|
||||
@public
|
||||
*/
|
||||
|
||||
|
||||
GamepadController.prototype.updateAvailable = function() {
|
||||
var gamepads, gp;
|
||||
if (!this.active) {
|
||||
return false;
|
||||
}
|
||||
gamepads = navigator.getGamepads ? navigator.getGamepads() : navigator.webkitGetGamepads();
|
||||
if (!(gamepads != null ? gamepads[0] : void 0)) {
|
||||
return false;
|
||||
}
|
||||
gp = gamepads[0];
|
||||
if ((gp.buttons == null) || (gp.axes == null)) {
|
||||
return;
|
||||
}
|
||||
this.acceleration = gp.buttons[0];
|
||||
this.lstickx = gp.axes[0];
|
||||
this.ltrigger = gp.buttons[6];
|
||||
this.rtrigger = gp.buttons[7];
|
||||
this.select = gp.buttons[8];
|
||||
this.buttonPressCallback(this);
|
||||
return true;
|
||||
};
|
||||
|
||||
return GamepadController;
|
||||
|
||||
})();
|
||||
|
||||
exports = exports != null ? exports : this;
|
||||
|
||||
exports.bkcore || (exports.bkcore = {});
|
||||
|
||||
(_base = exports.bkcore).controllers || (_base.controllers = {});
|
||||
|
||||
exports.bkcore.controllers.GamepadController = GamepadController;
|
||||
|
||||
}).call(this);
|
||||
Reference in New Issue
Block a user