taiko-web/public/src/js/loadsong.js

270 lines
7.2 KiB
JavaScript
Raw Normal View History

2018-09-11 00:17:13 +02:00
class loadSong{
2018-10-05 19:03:59 +02:00
constructor(selectedSong, autoPlayEnabled, multiplayer, touchEnabled){
2018-09-11 00:17:13 +02:00
this.selectedSong = selectedSong
this.autoPlayEnabled = autoPlayEnabled
2018-10-05 19:03:59 +02:00
this.multiplayer = multiplayer
this.touchEnabled = touchEnabled
2018-11-25 23:42:24 +01:00
2018-09-18 00:37:59 +02:00
loader.changePage("loadsong")
this.run()
2018-09-11 00:17:13 +02:00
}
run(){
2018-11-25 23:42:24 +01:00
var song = this.selectedSong
var id = song.folder
2018-09-11 00:17:13 +02:00
var promises = []
assets.sounds["start"].play()
2015-07-18 04:57:56 +02:00
2018-11-25 23:42:24 +01:00
song.songBg = this.randInt(1, 5)
song.songStage = this.randInt(1, 3)
2018-12-02 16:25:42 +01:00
song.donBg = this.randInt(1, 6)
2018-11-25 23:42:24 +01:00
if(song.songSkin && song.songSkin.name){
var imgLoad = []
for(var type in song.songSkin){
var value = song.songSkin[type]
if(type !== "name" && value && value !== "none"){
var filename = "bg_" + type + "_" + song.songSkin.name
if(value === "static"){
imgLoad.push({
filename: filename,
type: type
})
}else{
imgLoad.push({
filename: filename + "_a",
type: type
})
imgLoad.push({
filename: filename + "_b",
type: type
})
}
2018-12-02 16:25:42 +01:00
if(type === "song"){
song.songBg = null
}
2018-11-25 23:42:24 +01:00
}
}
var skinBase = gameConfig.assets_baseurl + "song_skins/"
for(var i = 0; i < imgLoad.length; i++){
let img = document.createElement("img")
2018-11-30 15:47:53 +01:00
if(this.touchEnabled && imgLoad[i].type === "song"){
img.crossOrigin = "Anonymous"
}
2018-11-25 23:42:24 +01:00
let filename = imgLoad[i].filename
let promise = pageEvents.load(img)
if(imgLoad[i].type === "song"){
promises.push(promise.then(() => {
return this.scaleImg(img, filename)
}))
}else{
promises.push(promise.then(() => {
assets.image[filename] = img
}))
}
img.src = skinBase + filename + ".png"
}
}
2018-12-02 16:25:42 +01:00
promises.push(this.loadSongBg(id))
2015-07-17 10:22:46 +02:00
2018-09-11 00:17:13 +02: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-27 23:42:28 +02:00
snd.musicGain.load(gameConfig.songs_baseurl + id + "/main.mp3").then(sound => {
2018-09-11 00:17:13 +02:00
songObj.sound = sound
resolve()
}, reject)
}
2018-09-11 00:17:13 +02:00
}))
2018-11-25 23:42:24 +01:00
promises.push(loader.ajax(this.getSongPath(song)).then(data => {
2018-09-11 00:17:13 +02:00
this.songData = data.replace(/\0/g, "").split("\n")
}))
Promise.all(promises).then(() => {
2018-09-18 00:37:59 +02:00
this.setupMultiplayer()
2018-09-12 19:10:00 +02:00
}, error => {
console.error(error)
2018-09-11 00:17:13 +02:00
alert("An error occurred, please refresh")
})
2015-07-17 10:22:46 +02:00
}
2018-12-02 16:25:42 +01:00
loadSongBg(){
2018-11-23 19:52:24 +01:00
return new Promise((resolve, reject) => {
2018-12-02 16:25:42 +01:00
var promises = []
var filenames = []
if(this.selectedSong.songBg !== null){
filenames.push("bg_song_" + this.selectedSong.songBg)
}
if(this.selectedSong.donBg !== null){
filenames.push("bg_don_" + this.selectedSong.donBg)
if(this.multiplayer){
filenames.push("bg_don2_" + this.selectedSong.donBg)
}
2018-12-02 16:25:42 +01:00
}
for(var i = 0; i < filenames.length; i++){
for(var letter = 0; letter < 2; letter++){
let filenameAb = filenames[i] + (letter === 0 ? "a" : "b")
if(!(filenameAb in assets.image)){
let img = document.createElement("img")
if(filenameAb.startsWith("bg_song_")){
if(this.touchEnabled){
img.crossOrigin = "Anonymous"
}
promises.push(pageEvents.load(img).then(() => {
return this.scaleImg(img, filenameAb)
}))
}else{
promises.push(pageEvents.load(img).then(() => {
assets.image[filenameAb] = img
}))
}
img.src = gameConfig.assets_baseurl + "img/" + filenameAb + ".png"
2018-11-30 15:47:53 +01:00
}
2018-11-23 19:52:24 +01:00
}
}
2018-12-02 16:25:42 +01:00
Promise.all(promises).then(resolve, reject)
2018-11-23 19:52:24 +01:00
})
}
2018-11-25 23:42:24 +01:00
scaleImg(img, filename){
return new Promise((resolve, reject) => {
if(this.touchEnabled){
var canvas = document.createElement("canvas")
var w = Math.floor(img.width / 2)
var h = Math.floor(img.height / 2)
canvas.width = w
canvas.height = h
var ctx = canvas.getContext("2d")
ctx.drawImage(img, 0, 0, w, h)
2018-11-27 00:05:02 +01:00
var saveScaled = url => {
2018-11-25 23:42:24 +01:00
let img2 = document.createElement("img")
pageEvents.load(img2).then(() => {
assets.image[filename] = img2
resolve()
}, reject)
2018-11-27 00:05:02 +01:00
img2.src = url
}
if("toBlob" in canvas){
canvas.toBlob(blob => {
saveScaled(URL.createObjectURL(blob))
})
}else{
saveScaled(canvas.toDataURL())
}
2018-11-25 23:42:24 +01:00
}else{
assets.image[filename] = img
resolve()
}
})
}
2018-11-23 19:52:24 +01:00
randInt(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min
}
2018-10-11 00:13:24 +02:00
getSongPath(selectedSong){
2018-10-27 23:42:28 +02:00
var directory = gameConfig.songs_baseurl + selectedSong.folder + "/"
2018-10-11 00:13:24 +02:00
if(selectedSong.type === "tja"){
return directory + "main.tja"
}else{
return directory + selectedSong.difficulty + ".osu"
}
}
2018-09-12 19:10:00 +02:00
setupMultiplayer(){
2018-11-25 23:42:24 +01:00
var song = this.selectedSong
2018-09-12 19:10:00 +02:00
if(this.multiplayer){
2018-09-18 00:37:59 +02: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 05:36:15 +01: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-12 19:10:00 +02:00
this.song2Data = this.songData
2018-11-25 23:42:24 +01:00
this.selectedSong2 = song
2018-09-18 00:37:59 +02:00
pageEvents.add(p2, "message", event => {
if(event.type === "gameload"){
2018-11-13 05:36:15 +01:00
this.cancelButton.style.display = ""
2018-11-25 23:42:24 +01:00
if(event.value === song.difficulty){
2018-11-13 05:36:15 +01:00
this.startMultiplayer()
2018-09-18 00:37:59 +02:00
}else{
this.selectedSong2 = {
2018-11-25 23:42:24 +01:00
title: song.title,
folder: song.folder,
difficulty: event.value,
2018-11-25 23:42:24 +01:00
type: song.type,
offset: song.offset
2018-09-18 00:37:59 +02:00
}
2018-11-25 23:42:24 +01:00
if(song.type === "tja"){
2018-11-13 05:36:15 +01:00
this.startMultiplayer()
}else{
loader.ajax(this.getSongPath(this.selectedSong2)).then(data => {
this.song2Data = data.replace(/\0/g, "").split("\n")
2018-11-13 05:36:15 +01:00
this.startMultiplayer()
}, () => {
2018-11-13 05:36:15 +01:00
this.startMultiplayer()
})
}
2018-09-18 00:37:59 +02:00
}
}else if(event.type === "gamestart"){
this.clean()
2018-11-01 23:05:18 +01:00
p2.clearMessage("songsel")
2018-09-18 00:37:59 +02:00
loader.changePage("game")
2018-11-25 23:42:24 +01:00
var taikoGame1 = new Controller(song, this.songData, false, 1, this.touchEnabled)
2018-10-25 16:18:41 +02:00
var taikoGame2 = new Controller(this.selectedSong2, this.song2Data, true, 2, this.touchEnabled)
2018-09-18 00:37:59 +02:00
taikoGame1.run(taikoGame2)
2018-11-13 05:36:15 +01:00
}else if(event.type === "left" || event.type === "gameend"){
this.clean()
new SongSelect(false, false, this.touchEnabled)
2018-09-12 19:10:00 +02:00
}
2018-09-18 00:37:59 +02:00
})
2018-09-12 19:10:00 +02:00
p2.send("join", {
2018-11-25 23:42:24 +01:00
id: song.folder,
diff: song.difficulty
2018-09-12 19:10:00 +02:00
})
}else{
2018-09-18 00:37:59 +02:00
this.clean()
loader.changePage("game")
2018-11-25 23:42:24 +01:00
var taikoGame = new Controller(song, this.songData, this.autoPlayEnabled, false, this.touchEnabled)
2018-09-12 19:10:00 +02:00
taikoGame.run()
}
}
2018-11-13 05:36:15 +01:00
startMultiplayer(repeat){
if(document.hasFocus()){
p2.send("gamestart")
}else{
if(!repeat){
2018-11-13 10:50:52 +01:00
assets.sounds["sanka"].play()
2018-11-13 05:36:15 +01: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 00:37:59 +02:00
clean(){
pageEvents.remove(p2, "message")
2018-11-13 05:36:15 +01:00
if(this.cancelButton){
pageEvents.remove(this.cancelButton, ["mousedown", "touchstart"])
delete this.cancelButton
}
2018-09-18 00:37:59 +02:00
}
2018-09-12 19:10:00 +02:00
}