2018-09-18 06:37:59 +08:00
|
|
|
class Keyboard{
|
|
|
|
constructor(controller){
|
|
|
|
this.controller = controller
|
2018-09-22 04:31:35 +08:00
|
|
|
this.game = this.controller.game
|
|
|
|
|
2018-09-18 06:37:59 +08:00
|
|
|
this.kbd = {
|
|
|
|
"don_l": 86, // V
|
|
|
|
"don_r": 66, // B
|
|
|
|
"ka_l": 67, // C
|
|
|
|
"ka_r": 78, // N
|
|
|
|
"pause": 81, // Q
|
2018-09-27 02:30:57 +08:00
|
|
|
"back": 8, // Backspace
|
|
|
|
"previous": 38, // Up
|
|
|
|
"next": 40, // Down
|
|
|
|
"confirm": 13 // Enter
|
2018-09-18 06:37:59 +08:00
|
|
|
}
|
|
|
|
this.keys = {}
|
|
|
|
this.waitKeyupScore = {}
|
|
|
|
this.waitKeyupSound = {}
|
|
|
|
this.waitKeyupMenu = {}
|
|
|
|
this.keyTime = {
|
|
|
|
"don": -Infinity,
|
|
|
|
"ka": -Infinity
|
|
|
|
}
|
2018-09-27 02:30:57 +08:00
|
|
|
|
|
|
|
var gameBtn = {}
|
|
|
|
gameBtn[this.kbd["don_l"]] = ["u", "d", "l", "r"]
|
|
|
|
gameBtn[this.kbd["don_r"]] = ["a", "b", "x", "y"]
|
|
|
|
gameBtn[this.kbd["ka_l"]] = ["lb", "lt"]
|
|
|
|
gameBtn[this.kbd["ka_r"]] = ["rb", "rt"]
|
|
|
|
this.gamepad = new Gamepad(gameBtn)
|
|
|
|
|
|
|
|
var menuBtn = {
|
|
|
|
"cancel": ["a"],
|
|
|
|
}
|
|
|
|
menuBtn[this.kbd["confirm"]] = ["b"]
|
|
|
|
menuBtn[this.kbd["previous"]] = ["u", "l", "lb", "lt"],
|
|
|
|
menuBtn[this.kbd["next"]] = ["d", "r", "rb", "rt"]
|
|
|
|
menuBtn[this.kbd["pause"]] = ["start"]
|
|
|
|
this.gamepadMenu = new Gamepad(menuBtn)
|
|
|
|
|
|
|
|
|
2018-09-18 06:37:59 +08:00
|
|
|
pageEvents.keyAdd(this, "all", "both", event => {
|
2018-09-22 04:31:35 +08:00
|
|
|
if(event.keyCode === 8){
|
2018-09-18 06:37:59 +08:00
|
|
|
// Disable back navigation when pressing backspace
|
|
|
|
event.preventDefault()
|
|
|
|
}
|
2018-09-22 04:31:35 +08:00
|
|
|
if(!event.repeat && this.buttonEnabled(event.keyCode)){
|
|
|
|
var ms = this.game.getAccurateTime()
|
|
|
|
this.setKey(event.keyCode, event.type === "keydown", ms)
|
2018-09-18 06:37:59 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
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){
|
2018-09-27 02:30:57 +08:00
|
|
|
var ms = this.game.getAccurateTime()
|
|
|
|
this.gamepad.play((pressed, keyCode) => {
|
|
|
|
if(pressed){
|
|
|
|
if(this.keys[keyCode]){
|
|
|
|
this.setKey(keyCode, false)
|
|
|
|
}
|
|
|
|
this.setKey(keyCode, true, ms)
|
|
|
|
}else{
|
|
|
|
this.setKey(keyCode, false)
|
|
|
|
}
|
|
|
|
})
|
2018-09-18 06:37:59 +08:00
|
|
|
}
|
|
|
|
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){
|
2018-09-27 02:30:57 +08:00
|
|
|
var moveMenu = 0
|
|
|
|
var ms = this.game.getAccurateTime()
|
|
|
|
this.gamepadMenu.play((pressed, keyCode) => {
|
|
|
|
if(pressed){
|
|
|
|
if(this.game.isPaused()){
|
|
|
|
if(keyCode === "cancel"){
|
|
|
|
return setTimeout(() => {
|
|
|
|
this.controller.togglePauseMenu()
|
|
|
|
}, 200)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(this.keys[keyCode]){
|
|
|
|
this.setKey(keyCode, false)
|
|
|
|
}
|
|
|
|
this.setKey(keyCode, true, ms)
|
|
|
|
}else{
|
|
|
|
this.setKey(keyCode, false)
|
|
|
|
}
|
|
|
|
})
|
2018-09-18 06:37:59 +08:00
|
|
|
this.checkKey(this.kbd["pause"], "menu", () => {
|
|
|
|
this.controller.togglePauseMenu()
|
|
|
|
})
|
2018-09-27 02:30:57 +08:00
|
|
|
if(this.game.isPaused()){
|
|
|
|
this.checkKey(this.kbd["previous"], "menu", () => {
|
|
|
|
moveMenu = -1
|
|
|
|
})
|
|
|
|
this.checkKey(this.kbd["next"], "menu", () => {
|
|
|
|
moveMenu = 1
|
|
|
|
})
|
|
|
|
this.checkKey(this.kbd["confirm"], "menu", () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
document.getElementsByClassName("selected")[0].click()
|
|
|
|
}, 200)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if(moveMenu){
|
|
|
|
assets.sounds["ka"].play()
|
|
|
|
var selected = document.getElementsByClassName("selected")[0]
|
|
|
|
selected.classList.remove("selected")
|
|
|
|
var next = selected[(moveMenu === 1 ? "next" : "previous") + "ElementSibling"]
|
|
|
|
if(!next){
|
|
|
|
next = selected.parentNode[(moveMenu === 1 ? "first" : "last") + "ElementChild"]
|
|
|
|
}
|
|
|
|
next.classList.add("selected")
|
|
|
|
}
|
2018-09-18 06:37:59 +08:00
|
|
|
}
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 04:31:35 +08:00
|
|
|
checkKey(keyCode, type, callback){
|
|
|
|
if(this.keys[keyCode] && !this.isWaiting(keyCode, type)){
|
|
|
|
this.waitForKeyup(keyCode, type)
|
2018-09-18 06:37:59 +08:00
|
|
|
callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkKeySound(keyCode, sound){
|
|
|
|
this.checkKey(keyCode, "sound", () => {
|
2018-09-22 04:31:35 +08:00
|
|
|
var circles = this.controller.getCircles()
|
|
|
|
var circle = circles[this.controller.getCurrentCircle()]
|
2018-09-18 06:37:59 +08:00
|
|
|
if(
|
|
|
|
(keyCode === this.kbd["don_l"] || keyCode === this.kbd["don_r"])
|
|
|
|
&& circle
|
|
|
|
&& !circle.getPlayed()
|
|
|
|
&& circle.getType() === "balloon"
|
|
|
|
&& circle.requiredHits - circle.timesHit <= 1
|
|
|
|
){
|
|
|
|
assets.sounds["balloon"].play()
|
|
|
|
}else{
|
|
|
|
assets.sounds["note_" + sound].play()
|
|
|
|
}
|
2018-09-22 04:31:35 +08:00
|
|
|
this.keyTime[sound] = this.keyTime[keyCode]
|
2018-09-18 06:37:59 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
getKeys(){
|
|
|
|
return this.keys
|
|
|
|
}
|
2018-09-22 04:31:35 +08:00
|
|
|
setKey(keyCode, down, ms){
|
2018-09-18 06:37:59 +08:00
|
|
|
if(down){
|
|
|
|
this.keys[keyCode] = true
|
2018-09-22 04:31:35 +08:00
|
|
|
this.keyTime[keyCode] = ms
|
2018-09-18 06:37:59 +08:00
|
|
|
}else{
|
2018-09-22 04:31:35 +08:00
|
|
|
this.keys[keyCode] = false
|
|
|
|
this.waitKeyupScore[keyCode] = false
|
|
|
|
this.waitKeyupSound[keyCode] = false
|
|
|
|
this.waitKeyupMenu[keyCode] = false
|
2018-09-18 06:37:59 +08:00
|
|
|
}
|
|
|
|
}
|
2018-09-22 04:31:35 +08:00
|
|
|
isWaiting(keyCode, type){
|
2018-09-18 06:37:59 +08:00
|
|
|
if(type === "score"){
|
2018-09-22 04:31:35 +08:00
|
|
|
return this.waitKeyupScore[keyCode]
|
2018-09-18 06:37:59 +08:00
|
|
|
}else if(type === "sound"){
|
2018-09-22 04:31:35 +08:00
|
|
|
return this.waitKeyupSound[keyCode]
|
2018-09-18 06:37:59 +08:00
|
|
|
}else if(type === "menu"){
|
2018-09-22 04:31:35 +08:00
|
|
|
return this.waitKeyupMenu[keyCode]
|
2018-09-18 06:37:59 +08:00
|
|
|
}
|
|
|
|
}
|
2018-09-22 04:31:35 +08:00
|
|
|
waitForKeyup(keyCode, type){
|
2018-09-18 06:37:59 +08:00
|
|
|
if(type === "score"){
|
2018-09-22 04:31:35 +08:00
|
|
|
this.waitKeyupScore[keyCode] = true
|
2018-09-18 06:37:59 +08:00
|
|
|
}else if(type === "sound"){
|
2018-09-22 04:31:35 +08:00
|
|
|
this.waitKeyupSound[keyCode] = true
|
2018-09-18 06:37:59 +08:00
|
|
|
}else if(type === "menu"){
|
2018-09-22 04:31:35 +08:00
|
|
|
this.waitKeyupMenu[keyCode] = true
|
2018-09-18 06:37:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
getKeyTime(){
|
|
|
|
return this.keyTime
|
|
|
|
}
|
|
|
|
clean(){
|
|
|
|
pageEvents.keyRemove(this, "all")
|
|
|
|
}
|
|
|
|
}
|