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

244 lines
6.1 KiB
JavaScript
Raw Normal View History

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 = {
2018-10-23 22:46:26 +08:00
"don_l": 70, // F
"don_r": 74, // J
"ka_l": 68, // D
"ka_r": 75, // K
2018-09-18 06:37:59 +08:00
"pause": 81, // Q
2018-09-27 02:30:57 +08:00
"back": 8, // Backspace
2018-11-12 18:32:02 +08:00
"previous": 37, // Left
"next": 39, // Right
2018-09-27 02:30:57 +08:00
"confirm": 13 // Enter
2018-09-18 06:37:59 +08:00
}
2018-11-12 18:32:02 +08:00
this.kbdAlias = {
"pause": [27], // Esc
"previous": [38], // Up
"next": [40], // Down
"confirm": [32] // Space
}
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 = {}
2018-10-09 04:32:25 +08:00
gameBtn[this.kbd["don_l"]] = ["u", "d", "l", "r", "ls"]
gameBtn[this.kbd["don_r"]] = ["a", "b", "x", "y", "rs"]
2018-09-27 02:30:57 +08:00
gameBtn[this.kbd["ka_l"]] = ["lb", "lt"]
gameBtn[this.kbd["ka_r"]] = ["rb", "rt"]
this.gamepad = new Gamepad(gameBtn)
var menuBtn = {
"cancel": ["a"],
}
2018-10-09 04:32:25 +08:00
menuBtn[this.kbd["confirm"]] = ["b", "ls", "rs"]
2018-10-09 14:59:36 +08:00
menuBtn[this.kbd["previous"]] = ["u", "l", "lb", "lt", "lsu", "lsl"],
menuBtn[this.kbd["next"]] = ["d", "r", "rb", "rt", "lsd", "lsr"]
2018-09-27 02:30:57 +08:00
menuBtn[this.kbd["pause"]] = ["start"]
this.gamepadMenu = new Gamepad(menuBtn)
2018-11-12 18:32:02 +08:00
this.kbdSearch = {}
for(var name in this.kbdAlias){
var list = this.kbdAlias[name]
for(var i in list){
this.kbdSearch[list[i]] = this.kbd[name]
}
}
for(var name in this.kbd){
this.kbdSearch[this.kbd[name]] = this.kbd[name]
}
2018-09-27 02:30:57 +08:00
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-11-12 18:32:02 +08:00
var key = this.kbdSearch[event.keyCode]
if(key && !event.repeat && this.buttonEnabled(key)){
2018-09-22 04:31:35 +08:00
var ms = this.game.getAccurateTime()
2018-11-12 18:32:02 +08:00
this.setKey(key, 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-10-06 01:03:59 +08:00
}else{
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")
2018-09-18 06:37:59 +08:00
}
}
checkMenuKeys(){
2018-12-13 17:18:52 +08:00
if(!this.controller.multiplayer && !this.locked){
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()){
2018-09-27 02:30:57 +08:00
if(keyCode === "cancel"){
2018-12-13 17:18:52 +08:00
this.locked = true
2018-09-27 02:30:57 +08:00
return setTimeout(() => {
2018-11-12 18:32:02 +08:00
this.controller.togglePause()
2018-12-13 17:18:52 +08:00
this.locked = false
2018-09-27 02:30:57 +08:00
}, 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", () => {
2018-11-12 18:32:02 +08:00
this.controller.togglePause()
for(var key in this.keyTime){
this.keys[key] = null
2018-10-25 22:18:41 +08:00
this.keyTime[key] = -Infinity
2018-09-27 05:12:50 +08:00
}
})
var moveMenuMinus = () => {
moveMenu = -1
}
var moveMenuPlus = () => {
moveMenu = 1
}
var moveMenuConfirm = () => {
if(this.game.isPaused()){
2018-12-13 17:18:52 +08:00
this.locked = true
2018-09-27 02:30:57 +08:00
setTimeout(() => {
2018-11-12 18:32:02 +08:00
this.controller.view.pauseConfirm()
2018-12-13 17:18:52 +08:00
this.locked = false
2018-09-27 02:30:57 +08:00
}, 200)
2018-09-27 05:12:50 +08:00
}
2018-09-27 02:30:57 +08:00
}
this.checkKey(this.kbd["previous"], "menu", moveMenuMinus)
this.checkKey(this.kbd["ka_l"], "menu", moveMenuMinus)
this.checkKey(this.kbd["next"], "menu", moveMenuPlus)
this.checkKey(this.kbd["ka_r"], "menu", moveMenuPlus)
this.checkKey(this.kbd["confirm"], "menu", moveMenuConfirm)
this.checkKey(this.kbd["don_l"], "menu", moveMenuConfirm)
this.checkKey(this.kbd["don_r"], "menu", moveMenuConfirm)
if(moveMenu && this.game.isPaused()){
2018-09-27 02:30:57 +08:00
assets.sounds["ka"].play()
2018-11-12 18:32:02 +08:00
this.controller.view.pauseMove(moveMenu)
2018-09-27 02:30:57 +08:00
}
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(
2018-10-06 01:03:59 +08:00
sound === "don"
2018-09-18 06:37:59 +08:00
&& circle
&& !circle.getPlayed()
&& circle.getType() === "balloon"
&& circle.requiredHits - circle.timesHit <= 1
){
2018-10-03 17:48:18 +08:00
this.controller.playSound("balloon")
2018-09-18 06:37:59 +08:00
}else{
2018-10-03 17:48:18 +08:00
this.controller.playSound("note_" + sound)
2018-09-18 06:37:59 +08:00
}
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-12-13 17:18:52 +08:00
if(this.game.isPaused()){
return
}
2018-09-22 04:31:35 +08:00
this.keyTime[keyCode] = ms
2018-10-06 01:03:59 +08:00
if(keyCode == this.kbd.don_l || keyCode == this.kbd.don_r){
this.checkKeySound(keyCode, "don")
}else if(keyCode == this.kbd.ka_l || keyCode == this.kbd.ka_r){
this.checkKeySound(keyCode, "ka")
}
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")
}
}