diff --git a/public/index.html b/public/index.html index 7444d09..f39f3e2 100644 --- a/public/index.html +++ b/public/index.html @@ -46,6 +46,7 @@ +
diff --git a/public/src/js/gamepad.js b/public/src/js/gamepad.js new file mode 100644 index 0000000..2db4f6a --- /dev/null +++ b/public/src/js/gamepad.js @@ -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) + } + } + } +} diff --git a/public/src/js/keyboard.js b/public/src/js/keyboard.js index 78339ee..1088f2b 100644 --- a/public/src/js/keyboard.js +++ b/public/src/js/keyboard.js @@ -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();