Merge pull request #17 from maheshkk/gamepad

Add gamepad controller support for the game
This commit is contained in:
Thibaut Despoulain
2014-05-24 15:39:28 -07:00
8 changed files with 148 additions and 14 deletions
+1
View File
@@ -0,0 +1 @@
.DS_Store
@@ -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);
+34 -11
View File
@@ -120,6 +120,7 @@ bkcore.hexgl.ShipControls = function(ctx)
this.touchController = null;
this.orientationController = null;
this.gamepadController = null
if(ctx.controlType == 1 && bkcore.controllers.TouchController.isCompatible())
{
@@ -136,7 +137,7 @@ bkcore.hexgl.ShipControls = function(ctx)
self.key.forward = true;
});
}
else if(ctx.controlType == 3 && bkcore.controllers.OrientationController.isCompatible())
else if(ctx.controlType == 4 && bkcore.controllers.OrientationController.isCompatible())
{
this.orientationController = new bkcore.controllers.OrientationController(
domElement, true,
@@ -151,6 +152,20 @@ bkcore.hexgl.ShipControls = function(ctx)
self.key.forward = true;
});
}
else if(ctx.controlType == 3 && bkcore.controllers.GamepadController.isCompatible())
{
this.gamepadController = new bkcore.controllers.GamepadController(
function(controller){
if (controller.select)
ctx.restart();
else
self.key.forward = controller.acceleration > 0;
self.key.ltrigger = controller.ltrigger > 0;
self.key.rtrigger = controller.rtrigger > 0;
self.key.left = controller.lstickx < -0.1;
self.key.right = controller.lstickx > 0.1;
});
}
else if(ctx.controlType == 2)
{
if(Leap == null)
@@ -376,26 +391,34 @@ bkcore.hexgl.ShipControls.prototype.update = function(dt)
angularAmount += this.orientationController.beta/45 * this.angularSpeed * dt;
rollAmount -= this.orientationController.beta/45 * this.rollAngle;
}
if(this.gamepadController != null && this.gamepadController.updateAvailable())
{
angularAmount -= this.gamepadController.lstickx * 0.2 * this.angularSpeed * dt;
rollAmount += this.gamepadController.lstickx * this.rollAngle;
}
if(this.leapBridge != null && this.leapBridge.hasHands)
{
angularAmount += this.leapBridge.palmNormal[0] * 2 * this.angularSpeed * dt;
this.speed += Math.max(0.0, (0.5 + this.leapBridge.palmNormal[2])) * 3 * this.thrust * dt;
}
else
{
if(this.key.left)
{
angularAmount += this.angularSpeed * dt;
rollAmount -= this.rollAngle;
}
if(this.key.right)
{
angularAmount -= this.angularSpeed * dt;
rollAmount += this.rollAngle;
}
}
if(this.key.forward)
this.speed += this.thrust * dt;
else
this.speed -= this.airResist * dt;
if(this.key.left)
{
angularAmount += this.angularSpeed * dt;
rollAmount -= this.rollAngle;
}
if(this.key.right)
{
angularAmount -= this.angularSpeed * dt;
rollAmount += this.rollAngle;
}
if(this.key.ltrigger)
{
if(this.key.left)
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

+1
View File
@@ -88,6 +88,7 @@
<script src="bkcore.coffee/controllers/TouchController.js"></script>
<script src="bkcore.coffee/controllers/OrientationController.js"></script>
<script src="bkcore.coffee/controllers/GamepadController.js"></script>
<script src="bkcore/Timer.js"></script>
<script src="bkcore/ImageData.js"></script>
+1 -1
View File
@@ -36,7 +36,7 @@ init = (controlType, quality, platform, godmode) ->
u = bkcore.Utils.getURLParameter
s = [
['controlType', ['KEYBOARD', 'TOUCH', 'LEAP MOTION CONTROLLER'], 0, 0, 'Controls: ']
['controlType', ['KEYBOARD', 'TOUCH', 'LEAP MOTION CONTROLLER', 'GAMEPAD'], 0, 0, 'Controls: ']
['quality', ['LOW', 'MID', 'HIGH'], 2, 2, 'Quality: ']
['platform', ['DESKTOP', 'MOBILE'], 0, 0, 'Platform: ']
['godmode', ['OFF', 'ON'], 0, 1, 'Godmode: ']
+2 -2
View File
@@ -1,4 +1,4 @@
// Generated by CoffeeScript 1.6.3
// Generated by CoffeeScript 1.4.0
(function() {
var $, a, getWebGL, hasWebGL, init, s, u, _fn, _i, _len;
@@ -46,7 +46,7 @@
u = bkcore.Utils.getURLParameter;
s = [['controlType', ['KEYBOARD', 'TOUCH', 'LEAP MOTION CONTROLLER'], 0, 0, 'Controls: '], ['quality', ['LOW', 'MID', 'HIGH'], 2, 2, 'Quality: '], ['platform', ['DESKTOP', 'MOBILE'], 0, 0, 'Platform: '], ['godmode', ['OFF', 'ON'], 0, 1, 'Godmode: ']];
s = [['controlType', ['KEYBOARD', 'TOUCH', 'LEAP MOTION CONTROLLER', 'GAMEPAD'], 0, 0, 'Controls: '], ['quality', ['LOW', 'MID', 'HIGH'], 2, 2, 'Quality: '], ['platform', ['DESKTOP', 'MOBILE'], 0, 0, 'Platform: '], ['godmode', ['OFF', 'ON'], 0, 1, 'Godmode: ']];
_fn = function(a) {
var e, f, _ref;