2018-09-11 06:17:13 +08:00
|
|
|
class loadSong{
|
2018-09-13 01:10:00 +08:00
|
|
|
constructor(selectedSong, autoPlayEnabled, multiplayer){
|
2018-09-11 06:17:13 +08:00
|
|
|
this.selectedSong = selectedSong
|
2018-09-13 01:10:00 +08:00
|
|
|
this.multiplayer = multiplayer
|
2018-09-11 06:17:13 +08:00
|
|
|
this.autoPlayEnabled = autoPlayEnabled
|
2018-09-18 06:37:59 +08:00
|
|
|
loader.changePage("loadsong")
|
|
|
|
this.run()
|
2018-09-11 06:17:13 +08:00
|
|
|
}
|
|
|
|
run(){
|
|
|
|
var id = this.selectedSong.folder
|
|
|
|
var promises = []
|
|
|
|
assets.sounds["start"].play()
|
2015-07-18 10:57:56 +08:00
|
|
|
|
2018-09-11 06:17:13 +08:00
|
|
|
var img = document.createElement("img")
|
2018-09-18 06:37:59 +08:00
|
|
|
promises.push(pageEvents.load(img))
|
2018-09-11 06:17:13 +08:00
|
|
|
img.id = "music-bg"
|
|
|
|
img.src = "/songs/" + id + "/bg.png"
|
|
|
|
document.getElementById("assets").appendChild(img)
|
2015-07-17 16:22:46 +08:00
|
|
|
|
2018-09-11 06:17:13 +08:00
|
|
|
promises.push(new Promise((resolve, reject) => {
|
|
|
|
var songObj
|
|
|
|
assets.songs.forEach(song => {
|
|
|
|
if(song.id == id){
|
|
|
|
songObj = song
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if(songObj.sound){
|
|
|
|
songObj.sound.gain = snd.musicGain
|
|
|
|
resolve()
|
|
|
|
}else{
|
|
|
|
snd.musicGain.load("/songs/" + id + "/main.mp3").then(sound => {
|
|
|
|
songObj.sound = sound
|
|
|
|
resolve()
|
|
|
|
}, reject)
|
2018-08-29 13:55:16 +08:00
|
|
|
}
|
2018-09-11 06:17:13 +08:00
|
|
|
}))
|
2018-09-19 01:33:18 +08:00
|
|
|
promises.push(loader.ajax(this.getOsuPath(this.selectedSong)).then(data => {
|
2018-09-11 06:17:13 +08:00
|
|
|
this.songData = data.replace(/\0/g, "").split("\n")
|
|
|
|
}))
|
|
|
|
Promise.all(promises).then(() => {
|
2018-09-18 06:37:59 +08:00
|
|
|
this.setupMultiplayer()
|
2018-09-13 01:10:00 +08:00
|
|
|
}, error => {
|
|
|
|
console.error(error)
|
2018-09-11 06:17:13 +08:00
|
|
|
alert("An error occurred, please refresh")
|
|
|
|
})
|
2015-07-17 16:22:46 +08:00
|
|
|
}
|
2018-09-19 01:33:18 +08:00
|
|
|
getOsuPath(selectedSong){
|
|
|
|
return "/songs/" + selectedSong.folder + "/" + selectedSong.difficulty + ".osu"
|
|
|
|
}
|
2018-09-13 01:10:00 +08:00
|
|
|
setupMultiplayer(){
|
|
|
|
if(this.multiplayer){
|
2018-09-18 06:37:59 +08:00
|
|
|
var loadingText = document.getElementsByClassName("loading-text")[0]
|
|
|
|
var waitingText = "Waiting for Another Player..."
|
|
|
|
loadingText.firstChild.data = waitingText
|
|
|
|
loadingText.setAttribute("alt", waitingText)
|
|
|
|
|
2018-09-13 01:10:00 +08:00
|
|
|
this.song2Data = this.songData
|
|
|
|
this.selectedSong2 = this.selectedSong
|
2018-09-18 06:37:59 +08:00
|
|
|
pageEvents.add(p2, "message", event => {
|
|
|
|
if(event.type === "gameload"){
|
2018-09-19 01:33:18 +08:00
|
|
|
if(event.value === this.selectedSong.difficulty){
|
2018-09-13 01:10:00 +08:00
|
|
|
p2.send("gamestart")
|
2018-09-18 06:37:59 +08:00
|
|
|
}else{
|
|
|
|
this.selectedSong2 = {
|
|
|
|
title: this.selectedSong.title,
|
|
|
|
folder: this.selectedSong.folder,
|
2018-09-19 01:33:18 +08:00
|
|
|
difficulty: event.value
|
2018-09-18 06:37:59 +08:00
|
|
|
}
|
2018-09-19 01:33:18 +08:00
|
|
|
loader.ajax(this.getOsuPath(this.selectedSong2)).then(data => {
|
2018-09-18 06:37:59 +08:00
|
|
|
this.song2Data = data.replace(/\0/g, "").split("\n")
|
|
|
|
p2.send("gamestart")
|
|
|
|
}, () => {
|
|
|
|
p2.send("gamestart")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}else if(event.type === "gamestart"){
|
|
|
|
this.clean()
|
|
|
|
loader.changePage("game")
|
|
|
|
var taikoGame1 = new Controller(this.selectedSong, this.songData, false, 1)
|
|
|
|
var taikoGame2 = new Controller(this.selectedSong2, this.song2Data, true, 2)
|
|
|
|
taikoGame1.run(taikoGame2)
|
2018-09-13 01:10:00 +08:00
|
|
|
}
|
2018-09-18 06:37:59 +08:00
|
|
|
})
|
2018-09-13 01:10:00 +08:00
|
|
|
p2.send("join", {
|
|
|
|
id: this.selectedSong.folder,
|
2018-09-19 01:33:18 +08:00
|
|
|
diff: this.selectedSong.difficulty
|
2018-09-13 01:10:00 +08:00
|
|
|
})
|
|
|
|
}else{
|
2018-09-18 06:37:59 +08:00
|
|
|
this.clean()
|
|
|
|
loader.changePage("game")
|
2018-09-13 01:10:00 +08:00
|
|
|
var taikoGame = new Controller(this.selectedSong, this.songData, this.autoPlayEnabled)
|
|
|
|
taikoGame.run()
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 06:37:59 +08:00
|
|
|
clean(){
|
|
|
|
pageEvents.remove(p2, "message")
|
|
|
|
}
|
2018-09-13 01:10:00 +08:00
|
|
|
}
|