2018-09-11 00:17:13 +02:00
|
|
|
class Loader{
|
|
|
|
constructor(){
|
|
|
|
this.loadedAssets = 0
|
|
|
|
this.assetsDiv = document.getElementById("assets")
|
2018-09-18 00:37:59 +02:00
|
|
|
this.ajax("src/views/loader.html").then(this.run.bind(this))
|
2018-09-11 00:17:13 +02:00
|
|
|
}
|
2018-09-18 00:37:59 +02:00
|
|
|
run(page){
|
|
|
|
this.promises = []
|
|
|
|
this.screen = document.getElementById("screen")
|
|
|
|
this.screen.innerHTML = page
|
2018-09-11 00:17:13 +02:00
|
|
|
this.loaderPercentage = document.querySelector("#loader .percentage")
|
2018-09-18 00:37:59 +02:00
|
|
|
this.loaderProgress = document.querySelector("#loader .progress")
|
2018-09-05 18:46:26 +02:00
|
|
|
|
2018-09-11 00:17:13 +02:00
|
|
|
assets.fonts.forEach(name => {
|
2018-09-05 18:46:26 +02:00
|
|
|
var font = document.createElement("h1")
|
|
|
|
font.style.fontFamily = name
|
|
|
|
font.appendChild(document.createTextNode("I am a font"))
|
2018-09-11 00:17:13 +02:00
|
|
|
this.assetsDiv.appendChild(font)
|
|
|
|
this.promises.push(new Promise((resolve, reject) => {
|
|
|
|
FontDetect.onFontLoaded(name, resolve, reject, {msTimeout: 90000})
|
|
|
|
}))
|
|
|
|
})
|
2018-09-18 00:37:59 +02:00
|
|
|
var fontDetectDiv = document.getElementById("fontdetectHelper")
|
|
|
|
fontDetectDiv.parentNode.removeChild(fontDetectDiv)
|
2015-07-17 10:22:46 +02:00
|
|
|
|
2018-09-11 00:17:13 +02:00
|
|
|
assets.img.forEach(name => {
|
2018-09-18 00:37:59 +02:00
|
|
|
var id = this.getFilename(name)
|
2018-09-05 18:46:26 +02:00
|
|
|
var image = document.createElement("img")
|
2018-09-18 00:37:59 +02:00
|
|
|
this.promises.push(pageEvents.load(image))
|
2018-09-05 18:46:26 +02:00
|
|
|
image.id = name
|
|
|
|
image.src = "/assets/img/" + name
|
2018-09-11 00:17:13 +02:00
|
|
|
this.assetsDiv.appendChild(image)
|
2018-09-05 18:46:26 +02:00
|
|
|
assets.image[id] = image
|
2018-09-11 00:17:13 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
snd.buffer = new SoundBuffer()
|
|
|
|
snd.musicGain = snd.buffer.createGain()
|
|
|
|
snd.sfxGain = snd.buffer.createGain()
|
|
|
|
snd.buffer.setCrossfade(snd.musicGain, snd.sfxGain, 0.5)
|
|
|
|
snd.previewGain = snd.buffer.createGain()
|
|
|
|
snd.previewGain.setVolume(0.5)
|
2015-07-17 10:22:46 +02:00
|
|
|
|
2018-09-11 00:17:13 +02:00
|
|
|
assets.audioSfx.forEach(name => {
|
2018-09-18 00:37:59 +02:00
|
|
|
this.promises.push(this.loadSound(name, snd.sfxGain))
|
2018-09-11 00:17:13 +02:00
|
|
|
})
|
|
|
|
assets.audioMusic.forEach(name => {
|
2018-09-18 00:37:59 +02:00
|
|
|
this.promises.push(this.loadSound(name, snd.musicGain))
|
2018-09-11 00:17:13 +02:00
|
|
|
})
|
2015-07-17 10:22:46 +02:00
|
|
|
|
2018-09-12 19:10:00 +02:00
|
|
|
p2 = new P2Connection()
|
|
|
|
|
2018-09-18 00:37:59 +02:00
|
|
|
this.promises.push(this.ajax("/api/songs").then(songs => {
|
2018-09-11 00:17:13 +02:00
|
|
|
assets.songs = JSON.parse(songs)
|
|
|
|
}))
|
2015-07-17 10:22:46 +02:00
|
|
|
|
2018-09-18 00:37:59 +02:00
|
|
|
assets.views.forEach(name => {
|
|
|
|
var id = this.getFilename(name)
|
|
|
|
this.promises.push(this.ajax("src/views/" + name).then(page => {
|
|
|
|
assets.pages[id] = page
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2018-09-11 00:17:13 +02:00
|
|
|
this.promises.forEach(promise => {
|
2018-09-18 00:37:59 +02:00
|
|
|
promise.then(this.assetLoaded.bind(this))
|
2018-09-11 00:17:13 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
Promise.all(this.promises).then(() => {
|
2018-09-18 00:37:59 +02:00
|
|
|
this.clean()
|
2018-09-11 00:17:13 +02:00
|
|
|
new Titlescreen()
|
2018-09-18 00:37:59 +02:00
|
|
|
}, this.errorMsg.bind(this))
|
|
|
|
}
|
|
|
|
loadSound(name, gain){
|
|
|
|
var id = this.getFilename(name)
|
|
|
|
return gain.load("/assets/audio/" + name).then(sound => {
|
|
|
|
assets.sounds[id] = sound
|
2018-09-11 00:17:13 +02:00
|
|
|
})
|
2015-07-17 10:22:46 +02:00
|
|
|
}
|
2018-09-18 00:37:59 +02:00
|
|
|
getFilename(name){
|
|
|
|
return name.slice(0, name.lastIndexOf("."))
|
|
|
|
}
|
2018-09-11 00:17:13 +02:00
|
|
|
errorMsg(){
|
2018-09-18 00:37:59 +02:00
|
|
|
this.error = true
|
|
|
|
this.loaderPercentage.appendChild(document.createElement("br"))
|
|
|
|
this.loaderPercentage.appendChild(document.createTextNode("An error occurred, please refresh"))
|
|
|
|
this.clean()
|
2015-07-17 10:22:46 +02:00
|
|
|
}
|
2018-09-11 00:17:13 +02:00
|
|
|
assetLoaded(){
|
2018-09-18 00:37:59 +02:00
|
|
|
if(!this.error){
|
|
|
|
this.loadedAssets++
|
|
|
|
var percentage = Math.floor(this.loadedAssets * 100 / this.promises.length)
|
|
|
|
this.loaderProgress.style.width = percentage + "%"
|
|
|
|
this.loaderPercentage.firstChild.data = percentage + "%"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
changePage(name){
|
|
|
|
document.getElementById("screen").innerHTML = assets.pages[name]
|
|
|
|
}
|
|
|
|
ajax(url, customRequest){
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
var request = new XMLHttpRequest()
|
|
|
|
request.open("GET", url)
|
|
|
|
pageEvents.load(request).then(() => {
|
|
|
|
resolve(request.response)
|
|
|
|
}, reject)
|
|
|
|
if(customRequest){
|
|
|
|
customRequest(request)
|
|
|
|
}
|
|
|
|
request.send()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
clean(){
|
|
|
|
delete this.assetsDiv
|
|
|
|
delete this.loaderPercentage
|
|
|
|
delete this.loaderProgress
|
|
|
|
delete this.promises
|
2015-07-17 10:22:46 +02:00
|
|
|
}
|
2018-09-11 00:17:13 +02:00
|
|
|
}
|
|
|
|
|
2018-09-18 00:37:59 +02:00
|
|
|
var pageEvents = new PageEvents()
|
|
|
|
var loader = new Loader()
|
2018-09-11 00:17:13 +02:00
|
|
|
var snd = {}
|
2018-09-12 19:10:00 +02:00
|
|
|
var p2
|