diff options
author | Logan Hunt <loganhunt@simponic.xyz> | 2022-03-27 02:17:26 -0600 |
---|---|---|
committer | Logan Hunt <loganhunt@simponic.xyz> | 2022-03-27 02:17:26 -0600 |
commit | 4cabba2561f55ee6d068d631e3272ff7bf984b37 (patch) | |
tree | 1635ee664fa16287ec9058657afe5608df6d99a2 /src/bootstrap.js | |
download | bbiy-4cabba2561f55ee6d068d631e3272ff7bf984b37.tar.gz bbiy-4cabba2561f55ee6d068d631e3272ff7bf984b37.zip |
Initial commit - a working dynamic loader with Express
Diffstat (limited to 'src/bootstrap.js')
-rw-r--r-- | src/bootstrap.js | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/bootstrap.js b/src/bootstrap.js new file mode 100644 index 0000000..39453ff --- /dev/null +++ b/src/bootstrap.js @@ -0,0 +1,56 @@ +game.bootstrap = (function() { + const scripts = [ + { src: ['src/game.js'], id: 'game' }, + ]; + const assets = { + bigblue: 'assets/image/bigblue.png', + }; + + const loadScripts = function(onDone) { + while (scripts.length) { + let script = scripts.shift(); + require(script.src, () => { + onDone(script); + }); + } + console.log('scripts loaded'); + } + + const loadAsset = (source) => { + let fileExtension = source.substr(source.lastIndexOf('.') + 1); + return fetch(source) + .then((r) => r.blob()) + .then((r) => { + let asset; + if (["png", "jpg", "jpeg"].includes(fileExtension)) { + asset = new Image(); + } else if (["mp3"].includes(fileExtension)) { + asset = new Audio(); + } + asset.src = URL.createObjectURL(r); + return asset; + }) + } + + const loadAssets = function() { + const promises = []; + for (let key in assets) { + promises.push(loadAsset(assets[key], (asset) => { + game.assets[key] = asset; + }, (error) => { + console.log(error) + })); + } + return Promise.all(Object.keys(assets).map((key) => { + return loadAsset(assets[key]).then((asset) => game.assets[key] = asset); + })); + } + + game.assets = {}; + loadAssets().then(() => { + loadScripts((script) => { + game.initialize(); + }); + }) + +})();
\ No newline at end of file |