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

174 lines
5.0 KiB
JavaScript
Raw Normal View History

2018-09-11 06:17:13 +08:00
class loadSong{
2018-10-06 01:03:59 +08:00
constructor(selectedSong, autoPlayEnabled, multiplayer, touchEnabled){
2018-09-11 06:17:13 +08:00
this.selectedSong = selectedSong
this.autoPlayEnabled = autoPlayEnabled
2018-10-06 01:03:59 +08:00
this.multiplayer = multiplayer
this.touchEnabled = touchEnabled
2018-09-18 06:37:59 +08:00
loader.changePage("loadsong")
this.run()
2018-09-11 06:17:13 +08:00
}
2018-11-24 00:53:29 +08:00
songBg(){
return new Promise((resolve, reject) => {
var id = Math.floor(Math.random() * (5 - 1) + 1)
this.selectedSong.songBg = id
var filename = "bg_song_" + id
if(filename + "a" in assets.image && filename + "b" in assets.image){
resolve()
}else{
var promises = []
for(var i = 0; i < 2; i++){
let filenameAb = filename + (i === 0 ? "a" : "b")
let img = document.createElement("img")
promises.push(pageEvents.load(img).then(() => {
assets.image[filenameAb] = img
}))
img.src = gameConfig.assets_baseurl + "img/" + filenameAb + ".png"
}
return Promise.all(promises).then(resolve, reject)
}
})
}
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-11-24 00:53:29 +08:00
promises.push(new Promise(resolve => {
2018-09-27 09:04:01 +08:00
var img = document.createElement("img")
2018-11-24 00:53:29 +08:00
pageEvents.load(img).then(() => {
this.selectedSong.customBg = true
}, () => this.songBg(id)).then(resolve)
2018-09-27 09:04:01 +08:00
img.id = "music-bg"
2018-10-28 05:42:28 +08:00
img.src = gameConfig.songs_baseurl + id + "/bg.png"
2018-09-27 09:04:01 +08:00
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{
2018-10-28 05:42:28 +08:00
snd.musicGain.load(gameConfig.songs_baseurl + id + "/main.mp3").then(sound => {
2018-09-11 06:17:13 +08:00
songObj.sound = sound
resolve()
}, reject)
}
2018-09-11 06:17:13 +08:00
}))
2018-10-11 06:13:24 +08:00
promises.push(loader.ajax(this.getSongPath(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-10-11 06:13:24 +08:00
getSongPath(selectedSong){
2018-10-28 05:42:28 +08:00
var directory = gameConfig.songs_baseurl + selectedSong.folder + "/"
2018-10-11 06:13:24 +08:00
if(selectedSong.type === "tja"){
return directory + "main.tja"
}else{
return directory + 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-11-13 12:36:15 +08:00
this.cancelButton = document.getElementById("p2-cancel-button")
this.cancelButton.style.display = "inline-block"
pageEvents.add(this.cancelButton, ["mousedown", "touchstart"], this.cancelLoad.bind(this))
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-11-13 12:36:15 +08:00
this.cancelButton.style.display = ""
if(event.value === this.selectedSong.difficulty){
2018-11-13 12:36:15 +08:00
this.startMultiplayer()
2018-09-18 06:37:59 +08:00
}else{
this.selectedSong2 = {
title: this.selectedSong.title,
folder: this.selectedSong.folder,
difficulty: event.value,
type: this.selectedSong.type,
offset: this.selectedSong.offset
2018-09-18 06:37:59 +08:00
}
if(this.selectedSong.type === "tja"){
2018-11-13 12:36:15 +08:00
this.startMultiplayer()
}else{
loader.ajax(this.getSongPath(this.selectedSong2)).then(data => {
this.song2Data = data.replace(/\0/g, "").split("\n")
2018-11-13 12:36:15 +08:00
this.startMultiplayer()
}, () => {
2018-11-13 12:36:15 +08:00
this.startMultiplayer()
})
}
2018-09-18 06:37:59 +08:00
}
}else if(event.type === "gamestart"){
this.clean()
2018-11-02 06:05:18 +08:00
p2.clearMessage("songsel")
2018-09-18 06:37:59 +08:00
loader.changePage("game")
2018-10-06 01:03:59 +08:00
var taikoGame1 = new Controller(this.selectedSong, this.songData, false, 1, this.touchEnabled)
2018-10-25 22:18:41 +08:00
var taikoGame2 = new Controller(this.selectedSong2, this.song2Data, true, 2, this.touchEnabled)
2018-09-18 06:37:59 +08:00
taikoGame1.run(taikoGame2)
2018-11-13 12:36:15 +08:00
}else if(event.type === "left" || event.type === "gameend"){
this.clean()
new SongSelect(false, false, this.touchEnabled)
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,
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-10-06 01:03:59 +08:00
var taikoGame = new Controller(this.selectedSong, this.songData, this.autoPlayEnabled, false, this.touchEnabled)
2018-09-13 01:10:00 +08:00
taikoGame.run()
}
}
2018-11-13 12:36:15 +08:00
startMultiplayer(repeat){
if(document.hasFocus()){
p2.send("gamestart")
}else{
if(!repeat){
2018-11-13 17:50:52 +08:00
assets.sounds["sanka"].play()
2018-11-13 12:36:15 +08:00
}
setTimeout(() => {
this.startMultiplayer(true)
}, 100)
}
}
cancelLoad(event){
if(event.type === "mousedown"){
if(event.which !== 1){
return
}
}else{
event.preventDefault()
}
p2.send("leave")
assets.sounds["don"].play()
this.cancelButton.style.pointerEvents = "none"
}
2018-09-18 06:37:59 +08:00
clean(){
pageEvents.remove(p2, "message")
2018-11-13 12:36:15 +08:00
if(this.cancelButton){
pageEvents.remove(this.cancelButton, ["mousedown", "touchstart"])
delete this.cancelButton
}
2018-09-18 06:37:59 +08:00
}
2018-09-13 01:10:00 +08:00
}