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

218 lines
5.2 KiB
JavaScript
Raw Normal View History

2018-09-13 01:10:00 +08:00
class Controller{
2018-10-06 01:03:59 +08:00
constructor(selectedSong, songData, autoPlayEnabled, multiplayer, touchEnabled){
2018-09-13 01:10:00 +08:00
this.selectedSong = selectedSong
this.songData = songData
this.autoPlayEnabled = autoPlayEnabled
this.multiplayer = multiplayer
2018-10-06 01:03:59 +08:00
this.touchEnabled = touchEnabled
2018-10-03 17:48:18 +08:00
this.snd = this.multiplayer ? "_p" + this.multiplayer : ""
2018-09-13 01:10:00 +08:00
var backgroundURL = "/songs/" + this.selectedSong.folder + "/bg.png"
2018-10-11 06:13:24 +08:00
if(selectedSong.type === "tja"){
this.parsedSongData = new ParseTja(songData, selectedSong.difficulty, selectedSong.offset)
}else{
this.parsedSongData = new ParseOsu(songData, selectedSong.offset)
}
this.offset = this.parsedSongData.soundOffset
2018-09-13 01:10:00 +08:00
assets.songs.forEach(song => {
if(song.id == this.selectedSong.folder){
this.mainAsset = song.sound
}
})
2015-07-17 16:22:46 +08:00
2018-09-13 01:10:00 +08:00
this.game = new Game(this, this.selectedSong, this.parsedSongData)
this.view = new View(this, backgroundURL, this.selectedSong.title, this.selectedSong.difficulty)
this.mekadon = new Mekadon(this, this.game)
this.keyboard = new Keyboard(this)
}
run(syncWith){
this.loadUIEvents()
this.game.run()
this.view.run()
this.startMainLoop()
if(syncWith){
syncWith.run()
2018-10-03 17:48:18 +08:00
syncWith.elapsedTime = this.game.elapsedTime
syncWith.startDate = this.game.startDate
2018-09-13 01:10:00 +08:00
this.syncWith = syncWith
}
}
loadUIEvents(){
this.continueBtn = document.getElementById("continue-butt")
this.restartBtn = document.getElementById("restart-butt")
2018-09-18 06:37:59 +08:00
this.songSelBtn = document.getElementById("song-selection-butt")
2018-10-13 02:04:28 +08:00
pageEvents.add(this.continueBtn, ["click", "touchend"], this.togglePauseMenu.bind(this))
pageEvents.add(this.restartBtn, ["click", "touchend"], () => {
2018-09-13 01:10:00 +08:00
assets.sounds["don"].play()
this.restartSong()
})
2018-10-13 02:04:28 +08:00
pageEvents.add(this.songSelBtn, ["click", "touchend"], () => {
2018-09-18 06:37:59 +08:00
assets.sounds["don"].play()
this.songSelection()
2018-09-13 01:10:00 +08:00
})
}
startMainLoop(){
this.mainLoopStarted = false
this.mainLoopRunning = true
this.mainLoop()
}
2018-09-15 22:34:53 +08:00
stopMainLoop(){
this.mainLoopRunning = false
this.mainAsset.stop()
}
2018-09-13 01:10:00 +08:00
mainLoop(){
if(this.mainLoopRunning){
2018-09-18 06:37:59 +08:00
if(this.multiplayer !== 2){
2018-09-13 01:10:00 +08:00
requestAnimationFrame(() => {
if(this.syncWith){
2018-10-03 17:48:18 +08:00
this.syncWith.game.elapsedTime = this.game.elapsedTime
2018-09-13 01:10:00 +08:00
}
this.mainLoop()
if(this.syncWith){
this.syncWith.mainLoop()
}
})
}
2018-10-03 17:48:18 +08:00
var ms = this.game.elapsedTime
2018-09-22 04:31:35 +08:00
2018-09-13 01:10:00 +08:00
if(!this.game.isPaused()){
2018-09-22 04:31:35 +08:00
this.keyboard.checkGameKeys()
2018-09-13 01:10:00 +08:00
if(ms >= 0 && !this.mainLoopStarted){
this.mainLoopStarted = true
}
if(ms < 0){
this.game.updateTime()
}
if(this.mainLoopStarted){
this.game.update()
2018-09-18 06:37:59 +08:00
if(!this.mainLoopRunning){
return
}
2018-09-13 01:10:00 +08:00
this.game.playMainMusic()
}
this.view.refresh()
}
this.keyboard.checkMenuKeys()
2018-10-01 15:33:43 +08:00
if(this.scoresheet){
this.scoresheet.redraw()
}
2018-09-13 01:10:00 +08:00
}
}
togglePauseMenu(){
this.togglePause()
this.view.togglePauseMenu()
}
2018-09-18 06:37:59 +08:00
gameEnded(){
2018-09-13 01:10:00 +08:00
var score = this.getGlobalScore()
var vp
2018-10-01 15:33:43 +08:00
if(score.bad === 0){
2018-09-13 01:10:00 +08:00
vp = "fullcombo"
this.playSoundMeka("fullcombo", 1.350)
2018-10-01 15:33:43 +08:00
}else if(score.gauge >= 50){
2018-09-13 01:10:00 +08:00
vp = "clear"
2018-09-15 22:34:53 +08:00
}else{
2018-09-13 01:10:00 +08:00
vp = "fail"
}
2018-10-03 17:48:18 +08:00
this.playSound("game" + vp)
2018-09-18 06:37:59 +08:00
}
displayResults(){
if(this.multiplayer !== 2){
2018-10-01 15:33:43 +08:00
this.scoresheet = new Scoresheet(this, this.getGlobalScore(), this.multiplayer)
2018-09-18 06:37:59 +08:00
}
2018-09-13 01:10:00 +08:00
}
displayScore(score, notPlayed){
this.view.displayScore(score, notPlayed)
}
2018-10-01 15:33:43 +08:00
songSelection(fadeIn){
2018-10-01 18:02:28 +08:00
if(!fadeIn){
this.clean()
}
new SongSelect(false, fadeIn, this.touchEnabled)
2018-09-13 01:10:00 +08:00
}
restartSong(){
2018-09-18 06:37:59 +08:00
this.clean()
if(this.multiplayer){
new loadSong(this.selectedSong, false, true, this.touchEnabled)
2018-09-18 06:37:59 +08:00
}else{
loader.changePage("game")
var taikoGame = new Controller(this.selectedSong, this.songData, this.autoPlayEnabled, false, this.touchEnabled)
2018-09-18 06:37:59 +08:00
taikoGame.run()
}
2018-09-13 01:10:00 +08:00
}
2018-10-03 17:48:18 +08:00
playSound(id, time){
assets.sounds[id + this.snd].play(time)
}
2018-09-13 01:10:00 +08:00
playSoundMeka(soundID, time){
var meka = ""
if(this.autoPlayEnabled && !this.multiplayer){
meka = "-meka"
}
2018-10-03 17:48:18 +08:00
this.playSound(soundID + meka, time)
2018-09-13 01:10:00 +08:00
}
togglePause(){
if(this.syncWith){
this.syncWith.game.togglePause()
}
this.game.togglePause()
}
getKeys(){
return this.keyboard.getKeys()
}
2018-09-22 04:31:35 +08:00
setKey(keyCode, down, ms){
return this.keyboard.setKey(keyCode, down, ms)
2018-09-13 01:10:00 +08:00
}
getBindings(){
return this.keyboard.getBindings()
}
getElapsedTime(){
2018-10-03 17:48:18 +08:00
return this.game.elapsedTime
2018-09-13 01:10:00 +08:00
}
getCircles(){
return this.game.getCircles()
}
getCurrentCircle(){
return this.game.getCurrentCircle()
}
isWaiting(key, type){
return this.keyboard.isWaiting(key, type)
2018-09-13 01:10:00 +08:00
}
waitForKeyup(key, type){
this.keyboard.waitForKeyup(key, type)
}
getKeyTime(){
return this.keyboard.getKeyTime()
}
getCombo(){
return this.game.getCombo()
}
getGlobalScore(){
return this.game.getGlobalScore()
}
autoPlay(circle){
if(this.multiplayer){
p2.play(circle, this.mekadon)
}else{
this.mekadon.play(circle)
}
}
2018-09-18 06:37:59 +08:00
clean(){
2018-10-06 01:03:59 +08:00
if(this.syncWith){
this.syncWith.clean()
}
2018-09-18 06:37:59 +08:00
this.stopMainLoop()
this.keyboard.clean()
this.view.clean()
2018-10-13 02:04:28 +08:00
pageEvents.remove(this.continueBtn, ["click", "touchend"])
2018-09-18 06:37:59 +08:00
delete this.continueBtn
2018-10-13 02:04:28 +08:00
pageEvents.remove(this.restartBtn, ["click", "touchend"])
2018-09-18 06:37:59 +08:00
delete this.restartBtn
2018-10-13 02:04:28 +08:00
pageEvents.remove(this.songSelBtn, ["click", "touchend"])
2018-09-18 06:37:59 +08:00
delete this.songSelBtn
}
2018-09-13 01:10:00 +08:00
}