japanese-drum-game/public/src/js/keyboard.js

110 lines
3.0 KiB
JavaScript
Raw Normal View History

2015-07-17 16:22:46 +08:00
function Keyboard(controller){
2018-08-29 13:58:03 +08:00
var _kbd = {
"don_l": 86, // V
"don_r": 66, // B
"ka_l": 67, // C
"ka_r": 78, // N
"pause": 81, // Q
"back": 8 // Backspace
}
2015-07-17 16:22:46 +08:00
var _this = this;
var _keys = {};
var _waitKeyupScore = {};
var _waitKeyupSound = {};
var _waitKeyupMenu = {};
2018-08-29 13:58:03 +08:00
this.getBindings = function(){
return _kbd
}
2015-07-17 16:22:46 +08:00
$(document).keydown(function(e){
2018-08-28 07:56:31 +08:00
if (e.which === 8 && !$(e.target).is("input, textarea"))
// Disable back navigation when pressing backspace
2015-07-17 16:22:46 +08:00
e.preventDefault();
2018-08-28 08:44:17 +08:00
if(_this.buttonEnabled(e.which)){
2018-08-28 07:56:31 +08:00
_this.setKey(e.which, true);
}
2015-07-17 16:22:46 +08:00
});
$(document).keyup(function(e){
2018-08-28 08:44:17 +08:00
if(_this.buttonEnabled(e.which)){
2018-08-28 07:56:31 +08:00
_this.setKey(e.which, false);
}
2015-07-17 16:22:46 +08:00
});
2018-08-28 08:44:17 +08:00
this.buttonEnabled = function(keyCode){
2018-08-29 13:58:03 +08:00
if(controller.autoPlayEnabled){
2018-08-28 08:44:17 +08:00
switch(keyCode){
2018-08-29 13:58:03 +08:00
case _kbd["don_l"]:
case _kbd["don_r"]:
case _kbd["ka_l"]:
case _kbd["ka_r"]:
2018-08-28 08:44:17 +08:00
return false
}
}
return true
}
2015-07-17 16:22:46 +08:00
this.checkGameKeys = function(){
2018-08-29 13:58:03 +08:00
_this.checkKeySound(_kbd["don_l"], "note_don")
_this.checkKeySound(_kbd["don_r"], "note_don")
_this.checkKeySound(_kbd["ka_l"], "note_ka")
_this.checkKeySound(_kbd["ka_r"], "note_ka")
2015-07-17 16:22:46 +08:00
}
this.checkMenuKeys = function(){
2018-08-29 13:58:03 +08:00
_this.checkKey(_kbd["back"], "menu", function(){
2015-07-17 16:22:46 +08:00
controller.pauseSound("main-music", true);
controller.songSelection();
2018-08-29 13:58:03 +08:00
})
_this.checkKey(_kbd["pause"], "menu", function(){
2015-07-17 16:22:46 +08:00
controller.togglePauseMenu();
2018-08-29 13:58:03 +08:00
})
}
this.checkKey = function(keyCode, keyup, callback){
if(_keys[keyCode] && !_this.isWaitingForKeyup(keyCode, keyup)){
_this.waitForKeyup(keyCode, keyup);
callback()
2015-07-17 16:22:46 +08:00
}
2018-08-29 13:58:03 +08:00
}
this.checkKeySound = function(keyCode, sound){
_this.checkKey(keyCode, "sound", function(){
controller.playSound(sound);
})
2015-07-17 16:22:46 +08:00
}
this.getKeys = function(){
return _keys;
}
2018-08-28 07:56:31 +08:00
this.setKey=function(keyCode, down){
if(down){
_keys[keyCode]=true;
}else{
delete _keys[keyCode];
delete _waitKeyupScore[keyCode];
delete _waitKeyupSound[keyCode];
delete _waitKeyupMenu[keyCode];
}
}
2015-07-17 16:22:46 +08:00
this.isWaitingForKeyup = function(key, type){
var isWaiting;
if(type == "score") isWaiting = _waitKeyupScore[key];
else if(type == "sound") isWaiting = _waitKeyupSound[key];
else if(type == "menu") isWaiting = _waitKeyupMenu[key];
return isWaiting;
}
this.waitForKeyup = function(key, type){
if(type == "score") _waitKeyupScore[key] = true;
else if(type == "sound") _waitKeyupSound[key] = true;
else if(type == "menu") _waitKeyupMenu[key] = true;
}
}