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

239 lines
6.1 KiB
JavaScript
Raw Normal View History

2018-09-18 06:37:59 +08:00
class SongSelect{
constructor(){
this.songs
this.selectedSong = {
"title": "",
"folder": "",
"difficulty": ""
}
this.previewId = 0
this.diffNames={
"easy": "かんたん",
"normal": "ふつう",
"hard": "むずかしい",
"oni": "おに"
}
loader.changePage("songselect")
this.run()
2018-09-06 00:46:26 +08:00
}
2018-09-18 06:37:59 +08:00
startPreview(id, prvtime, switchedTo){
this.endPreview()
2018-09-11 22:50:10 +08:00
var startLoad = +new Date
2018-09-18 06:37:59 +08:00
var currentId = this.previewId
if(!switchedTo){
2018-09-11 06:17:13 +08:00
snd.musicGain.fadeOut(0.4)
}
2018-09-18 06:37:59 +08:00
var songObj = assets.songs.find(song => song.id == id)
2018-09-11 06:17:13 +08:00
if(songObj.sound){
2018-09-18 06:37:59 +08:00
this.preview = songObj.sound
this.preview.gain = snd.previewGain
this.previewLoaded(startLoad, prvtime, switchedTo)
2018-09-11 06:17:13 +08:00
}else{
snd.previewGain.load("/songs/" + id + "/main.mp3").then(sound => {
2018-09-18 06:37:59 +08:00
if(currentId == this.previewId){
2018-09-11 06:17:13 +08:00
songObj.sound = sound
2018-09-18 06:37:59 +08:00
this.preview = sound
this.previewLoaded(startLoad, prvtime, switchedTo)
2018-09-11 06:17:13 +08:00
}
})
2018-08-27 00:14:56 +08:00
}
2018-09-18 06:37:59 +08:00
}
previewLoaded(startLoad, prvtime, switchedTo){
2018-09-11 06:17:13 +08:00
var endLoad = +new Date
2018-09-11 22:50:10 +08:00
var difference = endLoad - startLoad
2018-09-18 06:37:59 +08:00
var minDelay = switchedTo ? 300 : 1000
2018-09-11 22:50:10 +08:00
var delay = minDelay - Math.min(minDelay, difference)
2018-09-18 06:37:59 +08:00
this.preview.playLoop(delay / 1000, false, prvtime / 1000)
2018-09-11 06:17:13 +08:00
}
2018-09-18 06:37:59 +08:00
endPreview() {
this.previewId++
if(this.preview){
this.preview.stop()
}
}
run(){
this.createCode()
this.startP2()
2015-07-17 16:22:46 +08:00
2018-09-18 06:37:59 +08:00
this.songselHelp = document.getElementById("songsel-help")
pageEvents.once(this.songselHelp, "click").then(() => {
this.clean()
2018-09-11 06:17:13 +08:00
assets.sounds["don"].play()
2018-09-18 06:37:59 +08:00
new Tutorial()
})
this.diffElements = document.getElementsByClassName("difficulty")
for(let difficulty of this.diffElements){
pageEvents.once(difficulty, "click").then(this.onDifficulty.bind(this))
}
this.songElements = document.getElementsByClassName("song")
for(let song of this.songElements){
pageEvents.add(song, "click", this.onSong.bind(this))
}
this.songSelect = document.getElementById("song-select")
}
onDifficulty(event){
this.clean()
var target = event.currentTarget
var song = target.parentNode.parentNode
assets.sounds["don"].play()
2015-07-17 16:22:46 +08:00
2018-09-18 06:37:59 +08:00
this.selectedSong.difficulty = target.classList[1] + ".osu"
this.selectedSong.title = song.dataset.title
this.selectedSong.folder = song.dataset.songId
2015-07-17 16:22:46 +08:00
2018-09-18 06:37:59 +08:00
new loadSong(this.selectedSong, event.shiftKey, event.ctrlKey)
}
onSong(event){
var target = event.currentTarget
var opened = document.getElementsByClassName("opened")[0]
if(!opened){
this.startPreview(target.dataset.songId, target.dataset.preview)
assets.sounds["don"].play()
assets.sounds["song-select"].stop()
assets.sounds["diffsel"].play(0.3)
target.classList.add("opened")
this.songSelect.classList.add("difficulty-select")
}else if(opened == target){
this.endPreview()
snd.musicGain.fadeIn(0.4)
assets.sounds["diffsel"].stop()
assets.sounds["cancel"].play()
assets.sounds["song-select"].play(0.3)
opened.classList.remove("opened")
this.songSelect.classList.remove("difficulty-select")
}else{
this.startPreview(target.dataset.songId, target.dataset.preview, true)
assets.sounds["ka"].play()
opened.classList.remove("opened")
target.classList.add("opened")
}
2015-07-17 16:22:46 +08:00
}
2018-09-18 06:37:59 +08:00
createCode(){
2018-09-11 22:50:10 +08:00
assets.sounds["bgm_songsel"].playLoop(0.1, false, 0, 1.442, 3.506)
2018-09-18 06:37:59 +08:00
assets.sounds["song-select"].play(0.2)
2018-09-09 12:11:05 +08:00
var songElements = [0]
2018-09-18 06:37:59 +08:00
assets.songs.forEach(song => {
var songTitle = song.title
2018-09-09 12:11:05 +08:00
var charElements = [0]
var diffElements = [0]
2015-07-17 16:22:46 +08:00
2018-09-18 06:37:59 +08:00
for(var charIndex = 0; charIndex < songTitle.length; charIndex++){
var ch = songTitle.charAt(charIndex)
var cl = "song-title-char"
if(ch == " "){
ch = "\xa0"
cl += " song-title-space"
}else if(songTitle.charAt(charIndex + 1) == "'"){
ch = ch + "'"
cl += " song-title-apos"
charIndex++
}
2018-09-09 12:11:05 +08:00
charElements.push(
["span", {
class: cl,
alt: ch
}, ch]
)
2018-09-18 06:37:59 +08:00
}
for(var diff in this.diffNames){
var diffName = diff
var diffLevel = song.stars[diff]
2018-08-27 00:14:56 +08:00
if (!diffLevel) {
2018-09-18 06:37:59 +08:00
continue
2018-08-27 00:14:56 +08:00
}
2018-09-09 12:11:05 +08:00
var starsDisplay = [0]
2018-09-18 06:37:59 +08:00
for(var star = 1; star <= diffLevel; star++){
2018-09-09 12:11:05 +08:00
starsDisplay.push("\u2605")
starsDisplay.push(["br"])
2015-07-17 16:22:46 +08:00
}
2018-09-18 06:37:59 +08:00
var diffTxt = this.diffNames[diffName]
2018-09-09 12:11:05 +08:00
diffElements.push(
["li", {
class: "difficulty " + diffName
},
["span", {
class: "diffname"
}, diffTxt],
["span", {
class: "stars"
}, starsDisplay]
]
)
2015-07-17 16:22:46 +08:00
}
2018-09-09 12:11:05 +08:00
songElements.push(
["div", {
id: "song-" + song.id,
class: "song",
"data-title": songTitle,
"data-song-id": song.id,
"data-preview": song.preview
},
["div", {
class: /^[\x00-\xFF]*$/.test(songTitle) ? "song-title alpha-title" : "song-title"
}, charElements],
["ul", {
class: "difficulties"
}, diffElements]
]
)
2018-09-18 06:37:59 +08:00
})
2018-09-09 12:11:05 +08:00
element(
document.getElementById("song-container"),
songElements
)
2015-07-17 16:22:46 +08:00
}
2018-09-18 06:37:59 +08:00
onusers(response){
2018-09-13 01:10:00 +08:00
var oldP2Elements = document.getElementsByClassName("p2")
for(var i = oldP2Elements.length; i--;){
oldP2Elements[i].classList.remove("p2")
}
if(response){
response.forEach(idDiff => {
2018-09-18 06:37:59 +08:00
var id = idDiff.id |0
var diff = idDiff.diff
if(diff in this.diffNames){
2018-09-13 01:10:00 +08:00
var idElement = document.getElementById("song-" + id)
if(idElement){
idElement.classList.add("p2")
var diffElement = idElement.getElementsByClassName("difficulty " + diff)[0]
if(diffElement){
diffElement.classList.add("p2")
}
}
}
})
}
}
2018-09-18 06:37:59 +08:00
startP2(){
this.onusers(p2.getMessage("users"))
pageEvents.add(p2, "message", response => {
if(response.type == "users"){
this.onusers(response.value)
}
2018-09-13 01:10:00 +08:00
})
if(p2.closed){
p2.open()
}
}
2018-09-18 06:37:59 +08:00
clean(){
assets.sounds["bgm_songsel"].stop()
assets.sounds["song-select"].stop()
assets.sounds["diffsel"].stop()
this.endPreview()
snd.musicGain.fadeIn()
pageEvents.remove(p2, "message")
for(let difficulty of this.diffElements){
pageEvents.remove(difficulty, "click")
}
delete this.diffElements
for(let song of this.songElements){
pageEvents.remove(song, "click")
}
delete this.songElements
pageEvents.remove(this.songselHelp, "click")
delete this.songselHelp
delete this.songSelect
}
}