2018-08-29 14:00:15 +08:00
|
|
|
class Gamepad{
|
2018-09-18 00:25:47 +08:00
|
|
|
constructor(keyboard){
|
2018-09-22 04:31:35 +08:00
|
|
|
this.keyboard = keyboard
|
|
|
|
this.game = this.keyboard.controller.game
|
|
|
|
|
2018-09-18 00:25:47 +08:00
|
|
|
var kbd = keyboard.getBindings()
|
|
|
|
this.gameBtn = {}
|
|
|
|
this.gameBtn[kbd["don_l"]] = ["u", "d", "l", "r"]
|
|
|
|
this.gameBtn[kbd["don_r"]] = ["a", "b", "x", "y"]
|
|
|
|
this.gameBtn[kbd["ka_l"]] = ["lb", "lt"]
|
|
|
|
this.gameBtn[kbd["ka_r"]] = ["rb", "rt"]
|
|
|
|
this.menuBtn = {}
|
|
|
|
this.menuBtn[kbd["pause"]] = ["start"]
|
|
|
|
this.b = {
|
2018-09-18 01:33:52 +08:00
|
|
|
"a": 0,
|
|
|
|
"b": 1,
|
|
|
|
"x": 2,
|
|
|
|
"y": 3,
|
|
|
|
"lb": 4,
|
|
|
|
"rb": 5,
|
|
|
|
"lt": 6,
|
|
|
|
"rt": 7,
|
|
|
|
"back": 8,
|
|
|
|
"start": 9,
|
|
|
|
"ls": 10,
|
|
|
|
"rs": 11,
|
|
|
|
"u": 12,
|
|
|
|
"d": 13,
|
|
|
|
"l": 14,
|
|
|
|
"r": 15,
|
|
|
|
"guide": 16
|
2018-09-18 00:25:47 +08:00
|
|
|
}
|
|
|
|
this.btn = {}
|
|
|
|
}
|
|
|
|
play(menuPlay){
|
2018-09-22 04:31:35 +08:00
|
|
|
var ms = this.game.getAccurateTime()
|
2018-09-18 00:25:47 +08:00
|
|
|
if("getGamepads" in navigator){
|
|
|
|
var gamepads = navigator.getGamepads()
|
|
|
|
}else{
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var bindings = menuPlay ? this.menuBtn : this.gameBtn
|
|
|
|
for(var i = 0; i < gamepads.length; i++){
|
|
|
|
if(gamepads[i]){
|
|
|
|
this.toRelease = {}
|
|
|
|
for(var bind in bindings){
|
|
|
|
this.toRelease[bind] = bindings[bind].length
|
|
|
|
}
|
2018-09-18 01:33:52 +08:00
|
|
|
for(var btnName = 0; btnName <= 16; btnName++){
|
2018-09-18 00:25:47 +08:00
|
|
|
buttonSearch: {
|
|
|
|
for(var bind in bindings){
|
|
|
|
for(var name in bindings[bind]){
|
|
|
|
if(btnName === this.b[bindings[bind][name]]){
|
2018-09-22 04:31:35 +08:00
|
|
|
this.checkButton(gamepads, btnName, bind, ms)
|
2018-09-18 00:25:47 +08:00
|
|
|
break buttonSearch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 04:31:35 +08:00
|
|
|
checkButton(gamepads, btnName, keyCode, ms){
|
2018-09-18 00:25:47 +08:00
|
|
|
var button = false
|
2018-09-22 04:31:35 +08:00
|
|
|
|
2018-09-18 00:25:47 +08:00
|
|
|
for(var i = 0; i < gamepads.length; i++){
|
|
|
|
if(gamepads[i]){
|
2018-09-18 01:33:52 +08:00
|
|
|
var btn = gamepads[i].buttons[btnName]
|
|
|
|
if(btn){
|
|
|
|
var btnPressed = btn.pressed || btn.value >= 0.5
|
|
|
|
if(btnPressed){
|
|
|
|
button = btnPressed
|
|
|
|
}
|
2018-09-18 00:25:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 04:31:35 +08:00
|
|
|
|
|
|
|
var keys = this.keyboard.getKeys()
|
2018-09-18 00:25:47 +08:00
|
|
|
var pressed = !this.btn[btnName] && button
|
|
|
|
var released = this.btn[btnName] && !button
|
2018-09-22 04:31:35 +08:00
|
|
|
|
2018-09-18 00:25:47 +08:00
|
|
|
if(pressed){
|
|
|
|
this.btn[btnName] = true
|
|
|
|
}else if(released){
|
|
|
|
delete this.btn[btnName]
|
|
|
|
}
|
2018-09-22 04:31:35 +08:00
|
|
|
|
2018-09-18 00:25:47 +08:00
|
|
|
if(pressed){
|
2018-09-22 04:31:35 +08:00
|
|
|
if(keys[keyCode]){
|
2018-09-18 00:25:47 +08:00
|
|
|
this.keyboard.setKey(keyCode, false)
|
|
|
|
}
|
2018-09-22 04:31:35 +08:00
|
|
|
this.keyboard.setKey(keyCode, true, ms)
|
|
|
|
|
|
|
|
}else if(!button && keys[keyCode]){
|
2018-09-18 00:25:47 +08:00
|
|
|
if(released){
|
|
|
|
this.toRelease[keyCode + "released"] = true
|
|
|
|
}
|
|
|
|
this.toRelease[keyCode]--
|
|
|
|
if(this.toRelease[keyCode] === 0 && this.toRelease[keyCode + "released"]){
|
|
|
|
this.keyboard.setKey(keyCode, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-29 14:00:15 +08:00
|
|
|
}
|