Check all connected gamepads

This commit is contained in:
LoveEevee 2018-09-17 19:25:47 +03:00
parent 938931d7af
commit 6b34c19a30

View File

@ -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)
} }
} }