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

145 lines
4.1 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-09-06 00:46:26 +08:00
var _keyTime = {
"don": -Infinity,
"ka": -Infinity
}
2015-07-17 16:22:46 +08:00
2018-08-29 13:58:03 +08:00
this.getBindings = function(){
return _kbd
}
2018-08-29 14:00:15 +08:00
var _gamepad = new Gamepad(this)
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 14:00:15 +08:00
if(!controller.autoPlayEnabled){
_gamepad.play()
}
2018-09-06 00:46:26 +08:00
_this.checkKeySound(_kbd["don_l"], "don")
_this.checkKeySound(_kbd["don_r"], "don")
_this.checkKeySound(_kbd["ka_l"], "ka")
_this.checkKeySound(_kbd["ka_r"], "ka")
2015-07-17 16:22:46 +08:00
}
this.checkMenuKeys = function(){
2018-09-13 01:10:00 +08:00
if(!controller.multiplayer){
_gamepad.play(1)
_this.checkKey(_kbd["pause"], "menu", function(){
controller.togglePauseMenu();
})
}
if(controller.multiplayer != 2){
_this.checkKey(_kbd["back"], "menu", function(){
if(controller.multiplayer == 1){
p2.send("gameend")
}
controller.togglePause();
controller.songSelection();
})
}
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(){
2018-09-15 22:34:53 +08:00
var circles = controller.parsedSongData.circles
var circle = circles[controller.game.getCurrentCircle()]
if(
(keyCode == _kbd["don_l"] || keyCode == _kbd["don_r"])
&& circle
&& !circle.getPlayed()
&& circle.getStatus() != -1
&& circle.getType() == "balloon"
&& circle.requiredHits - circle.timesHit <= 1
){
assets.sounds["balloon"].play()
}else{
assets.sounds["note_"+sound].play()
}
2018-09-13 01:10:00 +08:00
_keyTime[sound] = controller.getElapsedTime().ms
2018-08-29 13:58:03 +08:00
})
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;
}
2018-09-06 00:46:26 +08:00
this.getKeyTime = function(){
return _keyTime;
}
2015-07-17 16:22:46 +08:00
}