Skip to content

Commit 6100ecf

Browse files
author
mahesh.kk
committed
Add gamepad controller support for the game
1 parent 855dac6 commit 6100ecf

File tree

8 files changed

+148
-14
lines changed

8 files changed

+148
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
###
2+
GamepadController (Orientation + buttons) for touch devices
3+
4+
@class bkcore.GamepadController
5+
@author Mahesh Kulkarni <http://twitter.com/maheshkk>
6+
###
7+
class GamepadController
8+
9+
@isCompatible: ->
10+
return ('getGamepads' of navigator) or ('webkitGetGamepads' of navigator)
11+
12+
###
13+
Creates a new GamepadController
14+
###
15+
constructor: (@buttonPressCallback) ->
16+
@active = true
17+
@leftStickArray = []
18+
@rightStickArray = []
19+
20+
###
21+
@public
22+
###
23+
updateAvailable: ->
24+
return false if not @active
25+
gamepads = if navigator.getGamepads then navigator.getGamepads() else navigator.webkitGetGamepads()
26+
return false if not gamepads?[0]
27+
gp = gamepads[0]
28+
return if not gp.buttons? or not gp.axes?
29+
@acceleration = gp.buttons[0]
30+
@lstickx = gp.axes[0]
31+
@ltrigger = gp.buttons[6]
32+
@rtrigger = gp.buttons[7]
33+
@select = gp.buttons[8]
34+
@buttonPressCallback this
35+
true
36+
37+
exports = exports ? @
38+
exports.bkcore ||= {}
39+
exports.bkcore.controllers ||= {}
40+
exports.bkcore.controllers.GamepadController = GamepadController

bkcore.coffee/controllers/GamepadController.js

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bkcore/hexgl/ShipControls.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ bkcore.hexgl.ShipControls = function(ctx)
120120

121121
this.touchController = null;
122122
this.orientationController = null;
123+
this.gamepadController = null
123124

124125
if(ctx.controlType == 1 && bkcore.controllers.TouchController.isCompatible())
125126
{
@@ -136,7 +137,7 @@ bkcore.hexgl.ShipControls = function(ctx)
136137
self.key.forward = true;
137138
});
138139
}
139-
else if(ctx.controlType == 3 && bkcore.controllers.OrientationController.isCompatible())
140+
else if(ctx.controlType == 4 && bkcore.controllers.OrientationController.isCompatible())
140141
{
141142
this.orientationController = new bkcore.controllers.OrientationController(
142143
domElement, true,
@@ -151,6 +152,20 @@ bkcore.hexgl.ShipControls = function(ctx)
151152
self.key.forward = true;
152153
});
153154
}
155+
else if(ctx.controlType == 3 && bkcore.controllers.GamepadController.isCompatible())
156+
{
157+
this.gamepadController = new bkcore.controllers.GamepadController(
158+
function(controller){
159+
if (controller.select)
160+
ctx.restart();
161+
else
162+
self.key.forward = controller.acceleration > 0;
163+
self.key.ltrigger = controller.ltrigger > 0;
164+
self.key.rtrigger = controller.rtrigger > 0;
165+
self.key.left = controller.lstickx < -0.1;
166+
self.key.right = controller.lstickx > 0.1;
167+
});
168+
}
154169
else if(ctx.controlType == 2)
155170
{
156171
if(Leap == null)
@@ -376,26 +391,34 @@ bkcore.hexgl.ShipControls.prototype.update = function(dt)
376391
angularAmount += this.orientationController.beta/45 * this.angularSpeed * dt;
377392
rollAmount -= this.orientationController.beta/45 * this.rollAngle;
378393
}
394+
if(this.gamepadController != null && this.gamepadController.updateAvailable())
395+
{
396+
angularAmount -= this.gamepadController.lstickx * 0.2 * this.angularSpeed * dt;
397+
rollAmount += this.gamepadController.lstickx * this.rollAngle;
398+
}
379399
if(this.leapBridge != null && this.leapBridge.hasHands)
380400
{
381401
angularAmount += this.leapBridge.palmNormal[0] * 2 * this.angularSpeed * dt;
382402
this.speed += Math.max(0.0, (0.5 + this.leapBridge.palmNormal[2])) * 3 * this.thrust * dt;
383403
}
404+
else
405+
{
406+
if(this.key.left)
407+
{
408+
angularAmount += this.angularSpeed * dt;
409+
rollAmount -= this.rollAngle;
410+
}
411+
if(this.key.right)
412+
{
413+
angularAmount -= this.angularSpeed * dt;
414+
rollAmount += this.rollAngle;
415+
}
416+
}
384417

385418
if(this.key.forward)
386419
this.speed += this.thrust * dt;
387420
else
388421
this.speed -= this.airResist * dt;
389-
if(this.key.left)
390-
{
391-
angularAmount += this.angularSpeed * dt;
392-
rollAmount -= this.rollAngle;
393-
}
394-
if(this.key.right)
395-
{
396-
angularAmount -= this.angularSpeed * dt;
397-
rollAmount += this.rollAngle;
398-
}
399422
if(this.key.ltrigger)
400423
{
401424
if(this.key.left)

css/help-3.png

39 KB
Loading

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888

8989
<script src="bkcore.coffee/controllers/TouchController.js"></script>
9090
<script src="bkcore.coffee/controllers/OrientationController.js"></script>
91+
<script src="bkcore.coffee/controllers/GamepadController.js"></script>
9192

9293
<script src="bkcore/Timer.js"></script>
9394
<script src="bkcore/ImageData.js"></script>

launch.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ init = (controlType, quality, platform, godmode) ->
3636

3737
u = bkcore.Utils.getURLParameter
3838
s = [
39-
['controlType', ['KEYBOARD', 'TOUCH', 'LEAP MOTION CONTROLLER'], 0, 0, 'Controls: ']
39+
['controlType', ['KEYBOARD', 'TOUCH', 'LEAP MOTION CONTROLLER', 'GAMEPAD'], 0, 0, 'Controls: ']
4040
['quality', ['LOW', 'MID', 'HIGH'], 2, 2, 'Quality: ']
4141
['platform', ['DESKTOP', 'MOBILE'], 0, 0, 'Platform: ']
4242
['godmode', ['OFF', 'ON'], 0, 1, 'Godmode: ']

launch.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)