From aa1e2c7116bbc38f2b45dbe1ed8f0a500a79ef05 Mon Sep 17 00:00:00 2001 From: Mal Date: Sun, 16 Feb 2020 19:39:44 +0100 Subject: [PATCH] Get parameters from URL are now possible. --- js/UrlParam.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ js/module.js | 18 ++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 js/UrlParam.js diff --git a/js/UrlParam.js b/js/UrlParam.js new file mode 100644 index 0000000..fbdde6f --- /dev/null +++ b/js/UrlParam.js @@ -0,0 +1,59 @@ +export default class UrlParam +{ + constructor() + { + this.url = document.location.toString(); + console.log(this.url); + this.params = UrlParam.getParamsFromUrl(this.url); + } + + get(name) + { + return this.params[name]; + } + + static getParamsFromUrl(url) + { + let urlParts = url.split('?'); + + if (urlParts.length !== 2) { + return []; + } + + let params = urlParts[1].split('&'); + + if (params.length === 0) { + return []; + } + + let pairs = []; + + params.forEach( + (param) => { + let parts = param.split('='); + + if (parts.length !== 2) { + return; + } + + pairs[parts[0]] = parts[1]; + } + ); + + return pairs; + } + + getInt(name) + { + let value = parseInt(this.get(name)); + + return isNaN(value) ? undefined : value; + } + + getFloat(name) + { + let value = parseFloat(this.get(name)); + + return isNaN(value) ? undefined : value; + } +} \ No newline at end of file diff --git a/js/module.js b/js/module.js index 8906249..0c3ae7a 100644 --- a/js/module.js +++ b/js/module.js @@ -11,6 +11,7 @@ import GraphicSet from "./GraphicSet.js"; import ImageLoader from "./ImageLoader.js"; import Level from "./Level.js"; import InterfaceEvent from "./events/InterfaceEvent.js"; +import UrlParam from "./UrlParam.js"; function MainLoop(timestamp) { @@ -124,7 +125,22 @@ function MainLoop(timestamp) window.requestAnimationFrame(MainLoop); } -let level = Level.createFromFile('levels/test(2).json'); +const LEVEL = [ + 'level01.json', + 'moon.json', + 'moonbase.json', + 'terrain8.json', +]; + +let urlGetter = new UrlParam(); + +let levelIndex = urlGetter.getInt('level'); + +if (levelIndex === undefined || levelIndex < 0 || levelIndex >= LEVEL.length) { + levelIndex = 0; +} + +let level = Level.createFromFile(Setting.LEVELS_LOCATION + LEVEL[levelIndex]); const GAME_SPEED = 1; const GRAVITY = level.gravity;