Clean up classes before exiting them
This commit is contained in:
@@ -1,145 +1,134 @@
|
||||
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 _keys = {};
|
||||
var _waitKeyupScore = {};
|
||||
var _waitKeyupSound = {};
|
||||
var _waitKeyupMenu = {};
|
||||
var _keyTime = {
|
||||
"don": -Infinity,
|
||||
"ka": -Infinity
|
||||
}
|
||||
|
||||
this.getBindings = function(){
|
||||
return _kbd
|
||||
}
|
||||
|
||||
var _gamepad = new Gamepad(this)
|
||||
|
||||
$(document).keydown(function(e){
|
||||
|
||||
if (e.which === 8 && !$(e.target).is("input, textarea"))
|
||||
// Disable back navigation when pressing backspace
|
||||
e.preventDefault();
|
||||
|
||||
if(_this.buttonEnabled(e.which)){
|
||||
_this.setKey(e.which, true);
|
||||
}
|
||||
});
|
||||
|
||||
$(document).keyup(function(e){
|
||||
if(_this.buttonEnabled(e.which)){
|
||||
_this.setKey(e.which, false);
|
||||
}
|
||||
});
|
||||
|
||||
this.buttonEnabled = function(keyCode){
|
||||
if(controller.autoPlayEnabled){
|
||||
switch(keyCode){
|
||||
case _kbd["don_l"]:
|
||||
case _kbd["don_r"]:
|
||||
case _kbd["ka_l"]:
|
||||
case _kbd["ka_r"]:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
this.checkGameKeys = function(){
|
||||
if(!controller.autoPlayEnabled){
|
||||
_gamepad.play()
|
||||
}
|
||||
_this.checkKeySound(_kbd["don_l"], "don")
|
||||
_this.checkKeySound(_kbd["don_r"], "don")
|
||||
_this.checkKeySound(_kbd["ka_l"], "ka")
|
||||
_this.checkKeySound(_kbd["ka_r"], "ka")
|
||||
}
|
||||
|
||||
this.checkMenuKeys = function(){
|
||||
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();
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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(){
|
||||
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()
|
||||
}
|
||||
_keyTime[sound] = controller.getElapsedTime().ms
|
||||
})
|
||||
}
|
||||
|
||||
this.getKeys = function(){
|
||||
return _keys;
|
||||
}
|
||||
|
||||
this.setKey=function(keyCode, down){
|
||||
if(down){
|
||||
_keys[keyCode]=true;
|
||||
}else{
|
||||
delete _keys[keyCode];
|
||||
delete _waitKeyupScore[keyCode];
|
||||
delete _waitKeyupSound[keyCode];
|
||||
delete _waitKeyupMenu[keyCode];
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
this.getKeyTime = function(){
|
||||
return _keyTime;
|
||||
}
|
||||
|
||||
}
|
||||
class Keyboard{
|
||||
constructor(controller){
|
||||
this.controller = controller
|
||||
this.kbd = {
|
||||
"don_l": 86, // V
|
||||
"don_r": 66, // B
|
||||
"ka_l": 67, // C
|
||||
"ka_r": 78, // N
|
||||
"pause": 81, // Q
|
||||
"back": 8 // Backspace
|
||||
}
|
||||
this.keys = {}
|
||||
this.waitKeyupScore = {}
|
||||
this.waitKeyupSound = {}
|
||||
this.waitKeyupMenu = {}
|
||||
this.keyTime = {
|
||||
"don": -Infinity,
|
||||
"ka": -Infinity
|
||||
}
|
||||
this.gamepad = new Gamepad(this)
|
||||
pageEvents.keyAdd(this, "all", "both", event => {
|
||||
if (event.keyCode === 8){
|
||||
// Disable back navigation when pressing backspace
|
||||
event.preventDefault()
|
||||
}
|
||||
if(this.buttonEnabled(event.keyCode)){
|
||||
this.setKey(event.keyCode, event.type === "keydown")
|
||||
}
|
||||
})
|
||||
}
|
||||
getBindings(){
|
||||
return this.kbd
|
||||
}
|
||||
buttonEnabled(keyCode){
|
||||
if(this.controller.autoPlayEnabled){
|
||||
switch(keyCode){
|
||||
case this.kbd["don_l"]:
|
||||
case this.kbd["don_r"]:
|
||||
case this.kbd["ka_l"]:
|
||||
case this.kbd["ka_r"]:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
checkGameKeys(){
|
||||
if(!this.controller.autoPlayEnabled){
|
||||
this.gamepad.play()
|
||||
}
|
||||
this.checkKeySound(this.kbd["don_l"], "don")
|
||||
this.checkKeySound(this.kbd["don_r"], "don")
|
||||
this.checkKeySound(this.kbd["ka_l"], "ka")
|
||||
this.checkKeySound(this.kbd["ka_r"], "ka")
|
||||
}
|
||||
checkMenuKeys(){
|
||||
if(!this.controller.multiplayer){
|
||||
this.gamepad.play(true)
|
||||
this.checkKey(this.kbd["pause"], "menu", () => {
|
||||
this.controller.togglePauseMenu()
|
||||
})
|
||||
}
|
||||
if(this.controller.multiplayer !== 2){
|
||||
this.checkKey(this.kbd["back"], "menu", () => {
|
||||
if(this.controller.multiplayer === 1){
|
||||
p2.send("gameend")
|
||||
}
|
||||
this.controller.togglePause()
|
||||
this.controller.songSelection()
|
||||
})
|
||||
}
|
||||
}
|
||||
checkKey(keyCode, keyup, callback){
|
||||
if(this.keys[keyCode] && !this.isWaitingForKeyup(keyCode, keyup)){
|
||||
this.waitForKeyup(keyCode, keyup)
|
||||
callback()
|
||||
}
|
||||
}
|
||||
checkKeySound(keyCode, sound){
|
||||
this.checkKey(keyCode, "sound", () => {
|
||||
var circles = this.controller.parsedSongData.circles
|
||||
var circle = circles[this.controller.game.getCurrentCircle()]
|
||||
if(
|
||||
(keyCode === this.kbd["don_l"] || keyCode === this.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()
|
||||
}
|
||||
this.keyTime[sound] = this.controller.getElapsedTime().ms
|
||||
})
|
||||
}
|
||||
getKeys(){
|
||||
return this.keys
|
||||
}
|
||||
setKey(keyCode, down){
|
||||
if(down){
|
||||
this.keys[keyCode] = true
|
||||
}else{
|
||||
delete this.keys[keyCode]
|
||||
delete this.waitKeyupScore[keyCode]
|
||||
delete this.waitKeyupSound[keyCode]
|
||||
delete this.waitKeyupMenu[keyCode]
|
||||
}
|
||||
}
|
||||
isWaitingForKeyup(key, type){
|
||||
if(type === "score"){
|
||||
return this.waitKeyupScore[key]
|
||||
}else if(type === "sound"){
|
||||
return this.waitKeyupSound[key]
|
||||
}else if(type === "menu"){
|
||||
return this.waitKeyupMenu[key]
|
||||
}
|
||||
}
|
||||
waitForKeyup(key, type){
|
||||
if(type === "score"){
|
||||
this.waitKeyupScore[key] = true
|
||||
}else if(type === "sound"){
|
||||
this.waitKeyupSound[key] = true
|
||||
}else if(type === "menu"){
|
||||
this.waitKeyupMenu[key] = true
|
||||
}
|
||||
}
|
||||
getKeyTime(){
|
||||
return this.keyTime
|
||||
}
|
||||
clean(){
|
||||
pageEvents.keyRemove(this, "all")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user