Get parameters from URL are now possible.
This commit is contained in:
parent
d4f3ac969f
commit
aa1e2c7116
59
js/UrlParam.js
Normal file
59
js/UrlParam.js
Normal 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;
|
||||
}
|
||||
}
|
18
js/module.js
18
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;
|
||||
|
Loading…
Reference in New Issue
Block a user