Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore.coffee/controllers/GamepadController.coffee
4627 views
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
@lstickx = gp.axes[0]
30
accel = gp.buttons[0]
31
lt = gp.buttons[6]
32
rt = gp.buttons[7]
33
sel = gp.buttons[8]
34
# API fallback
35
@acceleration = accel.pressed ? accel
36
@ltrigger = lt.pressed ? lt
37
@rtrigger = rt.pressed ? rt
38
@select = sel.pressed ? sel
39
@buttonPressCallback this
40
true
41
42
exports = exports ? @
43
exports.bkcore ||= {}
44
exports.bkcore.controllers ||= {}
45
exports.bkcore.controllers.GamepadController = GamepadController
46
47