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
|
|
|
|
2019-01-16 20:33:42 +08:00
|
|
|
if(this.multiplayer !== 2){
|
|
|
|
loader.changePage("game", false)
|
|
|
|
}
|
|
|
|
|
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)
|
2018-11-24 00:53:29 +08:00
|
|
|
this.view = new View(this)
|
2018-09-13 01:10:00 +08:00
|
|
|
this.mekadon = new Mekadon(this, this.game)
|
|
|
|
this.keyboard = new Keyboard(this)
|
2018-10-28 02:35:04 +08:00
|
|
|
|
|
|
|
this.playedSounds = {}
|
2018-09-13 01:10:00 +08:00
|
|
|
}
|
|
|
|
run(syncWith){
|
2019-01-25 09:42:05 +08:00
|
|
|
if(syncWith){
|
|
|
|
this.syncWith = syncWith
|
|
|
|
}
|
2018-09-13 01:10:00 +08:00
|
|
|
this.game.run()
|
|
|
|
this.view.run()
|
2019-01-25 09:42:05 +08:00
|
|
|
if(this.multiplayer === 1){
|
|
|
|
syncWith.run(this)
|
2018-10-28 02:35:04 +08:00
|
|
|
syncWith.game.elapsedTime = this.game.elapsedTime
|
|
|
|
syncWith.game.startDate = this.game.startDate
|
2018-09-13 01:10:00 +08:00
|
|
|
}
|
2018-11-24 03:15:10 +08:00
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.startMainLoop()
|
|
|
|
if(!this.multiplayer){
|
|
|
|
debugObj.controller = this
|
|
|
|
if(debugObj.debug){
|
|
|
|
debugObj.debug.updateStatus()
|
|
|
|
}
|
2018-10-15 02:08:05 +08:00
|
|
|
}
|
2018-11-24 03:15:10 +08:00
|
|
|
})
|
2018-09-13 01:10:00 +08:00
|
|
|
}
|
|
|
|
startMainLoop(){
|
|
|
|
this.mainLoopRunning = true
|
2019-01-16 20:33:42 +08:00
|
|
|
this.gameLoop()
|
|
|
|
this.viewLoop()
|
2019-01-25 09:42:05 +08:00
|
|
|
if(this.multiplayer !== 2){
|
|
|
|
this.gameInterval = setInterval(this.gameLoop.bind(this), 1000 / 60)
|
|
|
|
}
|
2018-09-13 01:10:00 +08:00
|
|
|
}
|
2018-09-15 22:34:53 +08:00
|
|
|
stopMainLoop(){
|
|
|
|
this.mainLoopRunning = false
|
|
|
|
this.mainAsset.stop()
|
2019-01-25 09:42:05 +08:00
|
|
|
if(this.multiplayer !== 2){
|
|
|
|
clearInterval(this.gameInterval)
|
|
|
|
}
|
2018-09-15 22:34:53 +08:00
|
|
|
}
|
2019-01-16 20:33:42 +08:00
|
|
|
gameLoop(){
|
|
|
|
if(this.mainLoopRunning){
|
2019-01-25 09:42:05 +08:00
|
|
|
if(this.multiplayer === 1){
|
2019-01-16 20:33:42 +08:00
|
|
|
this.syncWith.game.elapsedTime = this.game.elapsedTime
|
|
|
|
this.syncWith.game.startDate = this.game.startDate
|
|
|
|
}
|
|
|
|
var ms = this.game.elapsedTime
|
|
|
|
|
|
|
|
this.keyboard.checkMenuKeys()
|
|
|
|
if(!this.game.isPaused()){
|
|
|
|
this.keyboard.checkGameKeys()
|
|
|
|
|
|
|
|
if(ms < 0){
|
|
|
|
this.game.updateTime()
|
|
|
|
}else{
|
|
|
|
this.game.update()
|
|
|
|
if(!this.mainLoopRunning){
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.game.playMainMusic()
|
|
|
|
}
|
|
|
|
}
|
2019-01-25 09:42:05 +08:00
|
|
|
if(this.multiplayer === 1){
|
|
|
|
this.syncWith.gameLoop()
|
|
|
|
}
|
2019-01-16 20:33:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
viewLoop(){
|
2018-09-13 01:10:00 +08:00
|
|
|
if(this.mainLoopRunning){
|
2018-09-18 06:37:59 +08:00
|
|
|
if(this.multiplayer !== 2){
|
2018-09-13 01:10:00 +08:00
|
|
|
requestAnimationFrame(() => {
|
2019-01-16 20:33:42 +08:00
|
|
|
this.viewLoop()
|
2019-01-25 09:42:05 +08:00
|
|
|
if(this.multiplayer === 1){
|
2019-01-16 20:33:42 +08:00
|
|
|
this.syncWith.viewLoop()
|
2018-09-13 01:10:00 +08:00
|
|
|
}
|
2018-10-25 22:18:41 +08:00
|
|
|
if(this.scoresheet){
|
|
|
|
if(this.view.ctx){
|
|
|
|
this.view.ctx.save()
|
|
|
|
this.view.ctx.setTransform(1, 0, 0, 1, 0, 0)
|
|
|
|
}
|
|
|
|
this.scoresheet.redraw()
|
|
|
|
if(this.view.ctx){
|
|
|
|
this.view.ctx.restore()
|
|
|
|
}
|
|
|
|
}
|
2018-09-13 01:10:00 +08:00
|
|
|
})
|
|
|
|
}
|
2018-11-12 18:32:02 +08:00
|
|
|
this.view.refresh()
|
2018-09-13 01:10:00 +08:00
|
|
|
}
|
|
|
|
}
|
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-25 22:18:41 +08:00
|
|
|
if(Math.round(score.gauge / 2) - 1 >= 25){
|
|
|
|
if(score.bad === 0){
|
|
|
|
vp = "fullcombo"
|
|
|
|
this.playSoundMeka("fullcombo", 1.350)
|
|
|
|
}else{
|
|
|
|
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-11-02 18:26:46 +08:00
|
|
|
this.scoresheet = new Scoresheet(this, this.getGlobalScore(), this.multiplayer, this.touchEnabled)
|
2018-09-18 06:37:59 +08:00
|
|
|
}
|
2018-09-13 01:10:00 +08:00
|
|
|
}
|
2018-10-25 22:18:41 +08:00
|
|
|
displayScore(score, notPlayed, bigNote){
|
|
|
|
this.view.displayScore(score, notPlayed, bigNote)
|
2018-09-13 01:10:00 +08:00
|
|
|
}
|
2018-10-01 15:33:43 +08:00
|
|
|
songSelection(fadeIn){
|
2018-10-01 18:02:28 +08:00
|
|
|
if(!fadeIn){
|
|
|
|
this.clean()
|
|
|
|
}
|
2018-10-06 21:24:23 +08:00
|
|
|
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){
|
2018-12-13 17:18:52 +08:00
|
|
|
new LoadSong(this.selectedSong, false, true, this.touchEnabled)
|
2018-09-18 06:37:59 +08:00
|
|
|
}else{
|
2018-10-06 02:12:50 +08:00
|
|
|
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){
|
2018-12-13 17:18:52 +08:00
|
|
|
var ms = Date.now() + (time || 0) * 1000
|
2018-10-28 02:35:04 +08:00
|
|
|
if(!(id in this.playedSounds) || ms > this.playedSounds[id] + 30){
|
|
|
|
assets.sounds[id + this.snd].play(time)
|
|
|
|
this.playedSounds[id] = ms
|
|
|
|
}
|
2018-10-03 17:48:18 +08:00
|
|
|
}
|
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(){
|
2019-01-25 09:42:05 +08:00
|
|
|
if(this.multiplayer === 1){
|
2018-09-13 01:10:00 +08:00
|
|
|
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()
|
|
|
|
}
|
2018-09-19 01:33:18 +08:00
|
|
|
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(){
|
2019-01-25 09:42:05 +08:00
|
|
|
if(this.multiplayer === 1){
|
2018-10-06 01:03:59 +08:00
|
|
|
this.syncWith.clean()
|
|
|
|
}
|
2018-09-18 06:37:59 +08:00
|
|
|
this.stopMainLoop()
|
|
|
|
this.keyboard.clean()
|
|
|
|
this.view.clean()
|
|
|
|
|
2018-10-15 04:14:58 +08:00
|
|
|
if(!this.multiplayer){
|
2018-10-15 02:08:05 +08:00
|
|
|
debugObj.controller = null
|
|
|
|
if(debugObj.debug){
|
|
|
|
debugObj.debug.updateStatus()
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 06:37:59 +08:00
|
|
|
}
|
2018-09-13 01:10:00 +08:00
|
|
|
}
|