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,6 +1,6 @@
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"]
@ -39,30 +39,38 @@ class Gamepad{
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 i in bindings){ for(var bind in bindings){
this.toRelease[i] = bindings[i].length this.toRelease[bind] = bindings[bind].length
} }
for(var btnName in buttons){ for(var btnName in gamepads[i].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(buttons, btnName, bind) this.checkButton(gamepads, 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]){
var btnPressed = gamepads[i].buttons[btnName].pressed
if(btnPressed){
button = btnPressed
}
}
}
var pressed = !this.btn[btnName] && button
var released = this.btn[btnName] && !button
if(pressed){ if(pressed){
this.btn[btnName] = true this.btn[btnName] = true
}else if(released){ }else if(released){
@ -73,12 +81,12 @@ class Gamepad{
this.keyboard.setKey(keyCode, false) this.keyboard.setKey(keyCode, false)
} }
this.keyboard.setKey(keyCode, true) this.keyboard.setKey(keyCode, true)
}else if(!button.pressed && this.keyboard.getKeys()[keyCode]){ }else if(!button && this.keyboard.getKeys()[keyCode]){
if(released){ if(released){
this.toRelease[keyCode+"released"] = true this.toRelease[keyCode + "released"] = true
} }
this.toRelease[keyCode]-- this.toRelease[keyCode]--
if(this.toRelease[keyCode] == 0 && this.toRelease[keyCode+"released"]){ if(this.toRelease[keyCode] === 0 && this.toRelease[keyCode + "released"]){
this.keyboard.setKey(keyCode, false) this.keyboard.setKey(keyCode, false)
} }
} }