diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/bkcore.coffee/controllers/GamepadController.coffee b/bkcore.coffee/controllers/GamepadController.coffee new file mode 100644 index 0000000..253ecd6 --- /dev/null +++ b/bkcore.coffee/controllers/GamepadController.coffee @@ -0,0 +1,40 @@ +### + GamepadController (Orientation + buttons) for touch devices + + @class bkcore.GamepadController + @author Mahesh Kulkarni +### +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 diff --git a/bkcore.coffee/controllers/GamepadController.js b/bkcore.coffee/controllers/GamepadController.js new file mode 100644 index 0000000..c1d778b --- /dev/null +++ b/bkcore.coffee/controllers/GamepadController.js @@ -0,0 +1,69 @@ +// Generated by CoffeeScript 1.6.3 +/* + GamepadController (Orientation + buttons) for touch devices + + @class bkcore.GamepadController + @author Mahesh Kulkarni +*/ + + +(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); diff --git a/bkcore/hexgl/ShipControls.js b/bkcore/hexgl/ShipControls.js index 31478e5..d03acd4 100644 --- a/bkcore/hexgl/ShipControls.js +++ b/bkcore/hexgl/ShipControls.js @@ -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) diff --git a/css/help-3.png b/css/help-3.png new file mode 100644 index 0000000..40adbb7 Binary files /dev/null and b/css/help-3.png differ diff --git a/index.html b/index.html index b75272f..6a7f204 100644 --- a/index.html +++ b/index.html @@ -88,6 +88,7 @@ + diff --git a/launch.coffee b/launch.coffee index 69592bd..6f23649 100644 --- a/launch.coffee +++ b/launch.coffee @@ -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: '] diff --git a/launch.js b/launch.js index 6b8c834..8e41b00 100644 --- a/launch.js +++ b/launch.js @@ -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;