Add gamepad support

This commit is contained in:
LoveEevee 2018-08-29 09:00:15 +03:00
parent 64a93f14ab
commit 121a84911f
3 changed files with 93 additions and 0 deletions

View File

@ -46,6 +46,7 @@
<script type='application/javascript' src='/src/js/view.js'></script>
<script type='application/javascript' src='/src/js/bufferedloop.js'></script>
<script type='application/javascript' src='/src/js/mekadon.js'></script>
<script type='application/javascript' src='/src/js/gamepad.js'></script>
</head>
<body>

86
public/src/js/gamepad.js Normal file
View File

@ -0,0 +1,86 @@
class Gamepad{
constructor(keyboard){
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 = {
"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"
}
this.btn = {}
this.keyboard = keyboard
}
play(menuPlay){
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]){
var buttons = gamepads[i].buttons
this.toRelease = {}
for(var i in bindings){
this.toRelease[i] = bindings[i].length
}
for(var btnName in 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)
break buttonSearch
}
}
}
}
}
}
}
}
checkButton(buttons, btnName, keyCode){
var button = buttons[btnName]
var pressed = !this.btn[btnName] && button.pressed
var released = this.btn[btnName] && !button.pressed
if(pressed){
this.btn[btnName] = true
}else if(released){
delete this.btn[btnName]
}
if(pressed){
if(this.keyboard.getKeys()[keyCode]){
this.keyboard.setKey(keyCode, false)
}
this.keyboard.setKey(keyCode, true)
}else if(!button.pressed && 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)
}
}
}
}

View File

@ -18,6 +18,8 @@ function Keyboard(controller){
return _kbd
}
var _gamepad = new Gamepad(this)
$(document).keydown(function(e){
if (e.which === 8 && !$(e.target).is("input, textarea"))
@ -49,6 +51,9 @@ function Keyboard(controller){
}
this.checkGameKeys = function(){
if(!controller.autoPlayEnabled){
_gamepad.play()
}
_this.checkKeySound(_kbd["don_l"], "note_don")
_this.checkKeySound(_kbd["don_r"], "note_don")
_this.checkKeySound(_kbd["ka_l"], "note_ka")
@ -56,6 +61,7 @@ function Keyboard(controller){
}
this.checkMenuKeys = function(){
_gamepad.play(1)
_this.checkKey(_kbd["back"], "menu", function(){
controller.pauseSound("main-music", true);
controller.songSelection();