Merge pull request #19 from LoveEevee/all-gamepads

Check all connected gamepads
This commit is contained in:
Bui 2018-09-17 17:38:41 +01:00 committed by GitHub
commit f1560bd036

View File

@ -1,86 +1,94 @@
class Gamepad{ class Gamepad{
constructor(keyboard){ constructor(keyboard){
var kbd=keyboard.getBindings() var kbd = keyboard.getBindings()
this.gameBtn = {} this.gameBtn = {}
this.gameBtn[kbd["don_l"]] = ["u", "d", "l", "r"] this.gameBtn[kbd["don_l"]] = ["u", "d", "l", "r"]
this.gameBtn[kbd["don_r"]] = ["a", "b", "x", "y"] this.gameBtn[kbd["don_r"]] = ["a", "b", "x", "y"]
this.gameBtn[kbd["ka_l"]] = ["lb", "lt"] this.gameBtn[kbd["ka_l"]] = ["lb", "lt"]
this.gameBtn[kbd["ka_r"]] = ["rb", "rt"] this.gameBtn[kbd["ka_r"]] = ["rb", "rt"]
this.menuBtn = {} this.menuBtn = {}
this.menuBtn[kbd["pause"]] = ["start"] this.menuBtn[kbd["pause"]] = ["start"]
this.b = { this.b = {
"a": "0", "a": "0",
"b": "1", "b": "1",
"x": "2", "x": "2",
"y": "3", "y": "3",
"lb": "4", "lb": "4",
"rb": "5", "rb": "5",
"lt": "6", "lt": "6",
"rt": "7", "rt": "7",
"back": "8", "back": "8",
"start": "9", "start": "9",
"ls": "10", "ls": "10",
"rs": "11", "rs": "11",
"u": "12", "u": "12",
"d": "13", "d": "13",
"l": "14", "l": "14",
"r": "15", "r": "15",
"guide": "16" "guide": "16"
} }
this.btn = {} this.btn = {}
this.keyboard = keyboard this.keyboard = keyboard
} }
play(menuPlay){ play(menuPlay){
if("getGamepads" in navigator){ if("getGamepads" in navigator){
var gamepads = navigator.getGamepads() var gamepads = navigator.getGamepads()
}else{ }else{
return return
} }
var bindings = menuPlay ? this.menuBtn : this.gameBtn var bindings = menuPlay ? this.menuBtn : this.gameBtn
for(var i = 0; i < gamepads.length; i++){ for(var i = 0; i < gamepads.length; i++){
if(gamepads[i]){ if(gamepads[i]){
var buttons = gamepads[i].buttons this.toRelease = {}
this.toRelease = {} for(var bind in bindings){
for(var i in bindings){ this.toRelease[bind] = bindings[bind].length
this.toRelease[i] = bindings[i].length }
} for(var btnName in gamepads[i].buttons){
for(var btnName in buttons){ buttonSearch: {
buttonSearch: { for(var bind in bindings){
for(var bind in bindings){ for(var name in bindings[bind]){
for(var name in bindings[bind]){ if(btnName === this.b[bindings[bind][name]]){
if(btnName == this.b[bindings[bind][name]]){ this.checkButton(gamepads, btnName, bind)
this.checkButton(buttons, btnName, bind) break buttonSearch
break buttonSearch }
} }
} }
} }
} }
} break
} }
} }
} }
checkButton(buttons, btnName, keyCode){ checkButton(gamepads, btnName, keyCode){
var button = buttons[btnName] var button = false
var pressed = !this.btn[btnName] && button.pressed for(var i = 0; i < gamepads.length; i++){
var released = this.btn[btnName] && !button.pressed if(gamepads[i]){
if(pressed){ var btnPressed = gamepads[i].buttons[btnName].pressed
this.btn[btnName] = true if(btnPressed){
}else if(released){ button = btnPressed
delete this.btn[btnName] }
} }
if(pressed){ }
if(this.keyboard.getKeys()[keyCode]){ var pressed = !this.btn[btnName] && button
this.keyboard.setKey(keyCode, false) var released = this.btn[btnName] && !button
} if(pressed){
this.keyboard.setKey(keyCode, true) this.btn[btnName] = true
}else if(!button.pressed && this.keyboard.getKeys()[keyCode]){ }else if(released){
if(released){ delete this.btn[btnName]
this.toRelease[keyCode+"released"] = true }
} if(pressed){
this.toRelease[keyCode]-- if(this.keyboard.getKeys()[keyCode]){
if(this.toRelease[keyCode] == 0 && this.toRelease[keyCode+"released"]){ this.keyboard.setKey(keyCode, false)
this.keyboard.setKey(keyCode, false) }
} this.keyboard.setKey(keyCode, true)
} }else if(!button && this.keyboard.getKeys()[keyCode]){
} if(released){
this.toRelease[keyCode + "released"] = true
}
this.toRelease[keyCode]--
if(this.toRelease[keyCode] === 0 && this.toRelease[keyCode + "released"]){
this.keyboard.setKey(keyCode, false)
}
}
}
} }