Get parameters from URL are now possible.

This commit is contained in:
Mal 2020-02-16 19:39:44 +01:00
parent d4f3ac969f
commit aa1e2c7116
2 changed files with 76 additions and 1 deletions

59
js/UrlParam.js Normal file
View File

@ -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;
}
}

View File

@ -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;