Check all connected gamepads

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

View File

@ -1,6 +1,6 @@
class Gamepad{
constructor(keyboard){
var kbd=keyboard.getBindings()
var kbd = keyboard.getBindings()
this.gameBtn = {}
this.gameBtn[kbd["don_l"]] = ["u", "d", "l", "r"]
this.gameBtn[kbd["don_r"]] = ["a", "b", "x", "y"]
@ -39,30 +39,38 @@ class Gamepad{
var bindings = menuPlay ? this.menuBtn : this.gameBtn
for(var i = 0; i < gamepads.length; i++){
if(gamepads[i]){
var buttons = gamepads[i].buttons
this.toRelease = {}
for(var i in bindings){
this.toRelease[i] = bindings[i].length
for(var bind in bindings){
this.toRelease[bind] = bindings[bind].length
}
for(var btnName in buttons){
for(var btnName in gamepads[i].buttons){
buttonSearch: {
for(var bind in bindings){
for(var name in bindings[bind]){
if(btnName == this.b[bindings[bind][name]]){
this.checkButton(buttons, btnName, bind)
if(btnName === this.b[bindings[bind][name]]){
this.checkButton(gamepads, btnName, bind)
break buttonSearch
}
}
}
}
}
break
}
}
}
checkButton(buttons, btnName, keyCode){
var button = buttons[btnName]
var pressed = !this.btn[btnName] && button.pressed
var released = this.btn[btnName] && !button.pressed
checkButton(gamepads, btnName, keyCode){
var button = false
for(var i = 0; i < gamepads.length; i++){
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){
this.btn[btnName] = true
}else if(released){
@ -73,12 +81,12 @@ class Gamepad{
this.keyboard.setKey(keyCode, false)
}
this.keyboard.setKey(keyCode, true)
}else if(!button.pressed && this.keyboard.getKeys()[keyCode]){
}else if(!button && this.keyboard.getKeys()[keyCode]){
if(released){
this.toRelease[keyCode+"released"] = true
this.toRelease[keyCode + "released"] = true
}
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)
}
}