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

97 lines
2.8 KiB
JavaScript
Raw Normal View History

2015-07-17 16:22:46 +08:00
function Keyboard(controller){
var _this = this;
var _keys = {};
var _waitKeyupScore = {};
var _waitKeyupSound = {};
var _waitKeyupMenu = {};
$(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 07:56:31 +08:00
if(!controller.autoPlay()){
_this.setKey(e.which, true);
}
2015-07-17 16:22:46 +08:00
});
$(document).keyup(function(e){
2018-08-28 07:56:31 +08:00
if(!controller.autoPlay()){
_this.setKey(e.which, false);
}
2015-07-17 16:22:46 +08:00
});
this.checkGameKeys = function(){
2018-08-28 07:56:31 +08:00
if(_keys[86] && !_this.isWaitingForKeyup(86, "sound")){
// V, play 'don' sound
2018-08-06 19:04:02 +08:00
controller.playSound('note_don');
2015-07-17 16:22:46 +08:00
_this.waitForKeyup(86, "sound");
}
2018-08-28 07:56:31 +08:00
if(_keys[66] && !_this.isWaitingForKeyup(66, "sound")){
// B, play 'don' sound
2018-08-06 19:04:02 +08:00
controller.playSound('note_don');
2015-07-17 16:22:46 +08:00
_this.waitForKeyup(66, "sound");
}
2018-08-28 07:56:31 +08:00
if(_keys[67] && !_this.isWaitingForKeyup(67, "sound")){
// C, play 'ka' sound
2018-08-06 19:04:02 +08:00
controller.playSound('note_ka');
2015-07-17 16:22:46 +08:00
_this.waitForKeyup(67, "sound");
}
2018-08-28 07:56:31 +08:00
if(_keys[78] && !_this.isWaitingForKeyup(78, "sound")){
// N, play 'ka' sound
2018-08-06 19:04:02 +08:00
controller.playSound('note_ka');
2015-07-17 16:22:46 +08:00
_this.waitForKeyup(78, "sound");
}
}
this.checkMenuKeys = function(){
2018-08-28 07:56:31 +08:00
if(_keys[8] && !_this.isWaitingForKeyup(8, "menu")){
// Backspace, go back to song selection
2015-07-17 16:22:46 +08:00
_this.waitForKeyup(8, "menu");
controller.pauseSound("main-music", true);
controller.songSelection();
}
2018-08-28 07:56:31 +08:00
if(_keys[81] && !_this.isWaitingForKeyup(81, "menu")){
// P, pause the game
2018-08-06 19:04:02 +08:00
_this.waitForKeyup(81, "menu");
2015-07-17 16:22:46 +08:00
controller.togglePauseMenu();
}
}
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;
}
}