Remove hardcoded keyCodes

This commit is contained in:
LoveEevee 2018-08-29 08:58:03 +03:00
parent 5a82469da5
commit 64a93f14ab
5 changed files with 91 additions and 97 deletions

View File

@ -13,6 +13,8 @@ function Controller(selectedSong, songData, autoPlayEnabled){
var _mainLoop; var _mainLoop;
var _pauseMenu = false; var _pauseMenu = false;
this.autoPlayEnabled = autoPlayEnabled
this.run = function(){ this.run = function(){
_this.loadUIEvents(); _this.loadUIEvents();
@ -170,6 +172,10 @@ function Controller(selectedSong, songData, autoPlayEnabled){
return _keyboard.setKey(keyCode, down); return _keyboard.setKey(keyCode, down);
} }
this.getBindings = function(){
return _keyboard.getBindings();
}
this.getSongData = function(){ this.getSongData = function(){
return _game.getSongData(); return _game.getSongData();
} }
@ -215,12 +221,7 @@ function Controller(selectedSong, songData, autoPlayEnabled){
} }
this.autoPlay = function(circle){ this.autoPlay = function(circle){
if(autoPlayEnabled){ _mekadon.play(circle)
if(circle && circle.getStatus() == 450){
_mekadon.play(circle)
}
return true
}
} }
} }

View File

@ -117,48 +117,36 @@ function Game(controller, selectedSong, songData){
var circle = circles[_currentCircle]; var circle = circles[_currentCircle];
if(circle){ if(circle){
if(controller.autoPlayEnabled){
if(controller.autoPlay(circle)){ return controller.autoPlay(circle)
return
} }
var keys = controller.getKeys()
if(controller.getKeys()[86]){ var kbd = controller.getBindings()
if(!circle.getPlayed() && !controller.isWaitingForKeyup(86, "score") && circle.getStatus()!=-1){ if(keys[kbd["don_l"]]){
var score = _this.checkScore(circle); _this.checkKey(kbd["don_l"], circle)
circle.played(score);
_this.updateCurrentCircle();
controller.waitForKeyup(86, "score");
}
} }
if(controller.getKeys()[66]){ if(keys[kbd["don_r"]]){
if(!circle.getPlayed() && !controller.isWaitingForKeyup(66, "score") && circle.getStatus()!=-1){ _this.checkKey(kbd["don_r"], circle)
var score = _this.checkScore(circle);
circle.played(score);
_this.updateCurrentCircle();
controller.waitForKeyup(66, "score");
}
} }
if(controller.getKeys()[67]){ if(keys[kbd["ka_l"]]){
if(!circle.getPlayed() && !controller.isWaitingForKeyup(67, "score") && circle.getStatus()!=-1){ _this.checkKey(kbd["ka_l"], circle)
var score = _this.checkScore(circle);
circle.played(score);
_this.updateCurrentCircle();
controller.waitForKeyup(67, "score");
}
} }
if(controller.getKeys()[78]){ if(keys[kbd["ka_r"]]){
if(!circle.getPlayed() && !controller.isWaitingForKeyup(78, "score") && circle.getStatus()!=-1){ _this.checkKey(kbd["ka_r"], circle)
var score = _this.checkScore(circle);
circle.played(score);
_this.updateCurrentCircle();
controller.waitForKeyup(78, "score");
}
} }
} }
} }
this.checkKey = function(keyCode, circle){
if(!circle.getPlayed() && !controller.isWaitingForKeyup(keyCode, "score") && circle.getStatus()!=-1){
var score = _this.checkScore(circle);
circle.played(score);
_this.updateCurrentCircle();
controller.waitForKeyup(keyCode, "score");
}
}
this.checkScore = function(circle){ this.checkScore = function(circle){
if( if(

View File

@ -1,11 +1,23 @@
function Keyboard(controller){ function Keyboard(controller){
var _kbd = {
"don_l": 86, // V
"don_r": 66, // B
"ka_l": 67, // C
"ka_r": 78, // N
"pause": 81, // Q
"back": 8 // Backspace
}
var _this = this; var _this = this;
var _keys = {}; var _keys = {};
var _waitKeyupScore = {}; var _waitKeyupScore = {};
var _waitKeyupSound = {}; var _waitKeyupSound = {};
var _waitKeyupMenu = {}; var _waitKeyupMenu = {};
this.getBindings = function(){
return _kbd
}
$(document).keydown(function(e){ $(document).keydown(function(e){
if (e.which === 8 && !$(e.target).is("input, textarea")) if (e.which === 8 && !$(e.target).is("input, textarea"))
@ -24,12 +36,12 @@ function Keyboard(controller){
}); });
this.buttonEnabled = function(keyCode){ this.buttonEnabled = function(keyCode){
if(controller.autoPlay()){ if(controller.autoPlayEnabled){
switch(keyCode){ switch(keyCode){
case 86: case _kbd["don_l"]:
case 66: case _kbd["don_r"]:
case 67: case _kbd["ka_l"]:
case 78: case _kbd["ka_r"]:
return false return false
} }
} }
@ -37,45 +49,33 @@ function Keyboard(controller){
} }
this.checkGameKeys = function(){ this.checkGameKeys = function(){
_this.checkKeySound(_kbd["don_l"], "note_don")
if(_keys[86] && !_this.isWaitingForKeyup(86, "sound")){ _this.checkKeySound(_kbd["don_r"], "note_don")
// V, play 'don' sound _this.checkKeySound(_kbd["ka_l"], "note_ka")
controller.playSound('note_don'); _this.checkKeySound(_kbd["ka_r"], "note_ka")
_this.waitForKeyup(86, "sound");
}
if(_keys[66] && !_this.isWaitingForKeyup(66, "sound")){
// B, play 'don' sound
controller.playSound('note_don');
_this.waitForKeyup(66, "sound");
}
if(_keys[67] && !_this.isWaitingForKeyup(67, "sound")){
// C, play 'ka' sound
controller.playSound('note_ka');
_this.waitForKeyup(67, "sound");
}
if(_keys[78] && !_this.isWaitingForKeyup(78, "sound")){
// N, play 'ka' sound
controller.playSound('note_ka');
_this.waitForKeyup(78, "sound");
}
} }
this.checkMenuKeys = function(){ this.checkMenuKeys = function(){
_this.checkKey(_kbd["back"], "menu", function(){
if(_keys[8] && !_this.isWaitingForKeyup(8, "menu")){
// Backspace, go back to song selection
_this.waitForKeyup(8, "menu");
controller.pauseSound("main-music", true); controller.pauseSound("main-music", true);
controller.songSelection(); controller.songSelection();
} })
if(_keys[81] && !_this.isWaitingForKeyup(81, "menu")){ _this.checkKey(_kbd["pause"], "menu", function(){
// Q, pause the game
_this.waitForKeyup(81, "menu");
controller.togglePauseMenu(); controller.togglePauseMenu();
})
}
this.checkKey = function(keyCode, keyup, callback){
if(_keys[keyCode] && !_this.isWaitingForKeyup(keyCode, keyup)){
_this.waitForKeyup(keyCode, keyup);
callback()
} }
}
this.checkKeySound = function(keyCode, sound){
_this.checkKey(keyCode, "sound", function(){
controller.playSound(sound);
})
} }
this.getKeys = function(){ this.getKeys = function(){

View File

@ -6,24 +6,27 @@ class Mekadon{
this.keys = {} this.keys = {}
} }
play(circle){ play(circle){
if(circle.getType() == "don"){ if(circle.getStatus() == 450){
this.setKey(this.lr ? 86 : 66) var kbd = this.controller.getBindings()
this.lr = !this.lr if(circle.getType() == "don"){
}else if(circle.getType() == "daiDon"){ this.setKey(this.lr ? kbd["don_l"] : kbd["don_r"])
this.setKey(86) this.lr = !this.lr
this.setKey(66) }else if(circle.getType() == "daiDon"){
this.lr = false this.setKey(kbd["don_l"])
}else if(circle.getType() == "ka"){ this.setKey(kbd["don_r"])
this.setKey(this.lr ? 67 : 78) this.lr = false
this.lr = !this.lr }else if(circle.getType() == "ka"){
}else if(circle.getType() == "daiKa"){ this.setKey(this.lr ? kbd["ka_l"] : kbd["ka_r"])
this.setKey(67) this.lr = !this.lr
this.setKey(78) }else if(circle.getType() == "daiKa"){
this.lr = false this.setKey(kbd["ka_l"])
this.setKey(kbd["ka_r"])
this.lr = false
}
var score = this.game.checkScore(circle);
circle.played(score);
this.game.updateCurrentCircle();
} }
var score = this.game.checkScore(circle);
circle.played(score);
this.game.updateCurrentCircle();
} }
setKey(keyCode){ setKey(keyCode){
var self = this var self = this

View File

@ -267,23 +267,25 @@ function View(controller, bg, title, diff){
var keyRed = document.getElementById("taiko-key-red"); var keyRed = document.getElementById("taiko-key-red");
var keyBlue = document.getElementById("taiko-key-blue"); var keyBlue = document.getElementById("taiko-key-blue");
var keys = controller.getKeys()
var kbd = controller.getBindings()
if(controller.getKeys()[67]){ if(keys[kbd["ka_l"]]){
var elemW = 0.45*_taikoW; var elemW = 0.45*_taikoW;
_ctx.drawImage(keyBlue, 0, 0, 68, 124, _taikoX+0.05*_taikoW, _taikoY+0.03*_taikoH, elemW, (124/68)*elemW); _ctx.drawImage(keyBlue, 0, 0, 68, 124, _taikoX+0.05*_taikoW, _taikoY+0.03*_taikoH, elemW, (124/68)*elemW);
} }
if(controller.getKeys()[86]){ if(keys[kbd["don_l"]]){
var elemW = 0.35*_taikoW; var elemW = 0.35*_taikoW;
_ctx.drawImage(keyRed, 0, 0, 53, 100, _taikoX+0.15*_taikoW, _taikoY+0.09*_taikoH, elemW, (100/53)*elemW); _ctx.drawImage(keyRed, 0, 0, 53, 100, _taikoX+0.15*_taikoW, _taikoY+0.09*_taikoH, elemW, (100/53)*elemW);
} }
if(controller.getKeys()[66]){ if(keys[kbd["don_r"]]){
var elemW = 0.35*_taikoW; var elemW = 0.35*_taikoW;
_ctx.drawImage(keyRed, 53, 0, 53, 100, (_taikoX+0.15*_taikoW)+elemW, _taikoY+0.09*_taikoH, elemW, (100/53)*elemW); _ctx.drawImage(keyRed, 53, 0, 53, 100, (_taikoX+0.15*_taikoW)+elemW, _taikoY+0.09*_taikoH, elemW, (100/53)*elemW);
} }
if(controller.getKeys()[78]){ if(keys[kbd["ka_r"]]){
var elemW = 0.45*_taikoW; var elemW = 0.45*_taikoW;
_ctx.drawImage(keyBlue, 68, 0, 68, 124, (_taikoX+0.05*_taikoW)+elemW, _taikoY+0.03*_taikoH, elemW, (124/68)*elemW); _ctx.drawImage(keyBlue, 68, 0, 68, 124, (_taikoX+0.05*_taikoW)+elemW, _taikoY+0.03*_taikoH, elemW, (124/68)*elemW);
} }