LocalStorage for levels
This commit is contained in:
parent
89a504641a
commit
b807825832
@ -29,6 +29,7 @@ export class GameField {
|
|||||||
this.map = null;
|
this.map = null;
|
||||||
|
|
||||||
this.soundManager = new SoundManager();
|
this.soundManager = new SoundManager();
|
||||||
|
this.mapParser = new MapParser();
|
||||||
|
|
||||||
this.collisionManager = new CollisionManager(this.soundManager);
|
this.collisionManager = new CollisionManager(this.soundManager);
|
||||||
this.tank = new Tank(Constants.PLAYER_TANK_ID_IMAGE.getValue(), 0, 18 * 16, this.collisionManager);
|
this.tank = new Tank(Constants.PLAYER_TANK_ID_IMAGE.getValue(), 0, 18 * 16, this.collisionManager);
|
||||||
@ -82,7 +83,7 @@ export class GameField {
|
|||||||
await this.setupTileset();
|
await this.setupTileset();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.map = await new MapParser().getMap(level, this.tileset);
|
this.map = await this.mapParser.getMap(level, this.tileset);
|
||||||
this.canvas.height = this.height;
|
this.canvas.height = this.height;
|
||||||
this.canvas.width = this.width;
|
this.canvas.width = this.width;
|
||||||
this.collisionManager.attachMap(this.map);
|
this.collisionManager.attachMap(this.map);
|
||||||
|
|||||||
18
src/Graphics/Map/levelStorage.js
Normal file
18
src/Graphics/Map/levelStorage.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
export class LevelStorage {
|
||||||
|
constructor(){
|
||||||
|
this.localStorage = window.localStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} level
|
||||||
|
* @param {string} json
|
||||||
|
*/
|
||||||
|
setLevel(level, json){
|
||||||
|
this.localStorage.setItem(level.toString(), json.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
getLevel(level) {
|
||||||
|
return this.localStorage.getItem(level.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -2,17 +2,26 @@ import { BlockBuilder } from "./../../Blocks/blockBuilder"
|
|||||||
import { Map } from "./map";
|
import { Map } from "./map";
|
||||||
import { Constants } from "./../../constants"
|
import { Constants } from "./../../constants"
|
||||||
import { BlockTypeEnum } from "./../../Blocks/blockTypeEnum"
|
import { BlockTypeEnum } from "./../../Blocks/blockTypeEnum"
|
||||||
|
import { LevelStorage } from "./levelStorage"
|
||||||
|
|
||||||
|
|
||||||
export class MapParser {
|
export class MapParser {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this.levelStorage = new LevelStorage();
|
||||||
}
|
}
|
||||||
|
|
||||||
async getMap(levelNumber, tileset) {
|
async getMap(levelNumber, tileset) {
|
||||||
|
let json = this.levelStorage.getLevel(levelNumber);
|
||||||
|
|
||||||
|
if (!json) {
|
||||||
let fetchResult = await fetch(`./stages/stage-${levelNumber}.json`);
|
let fetchResult = await fetch(`./stages/stage-${levelNumber}.json`);
|
||||||
if (!fetchResult.ok)
|
if (!fetchResult.ok)
|
||||||
return null;
|
return null;
|
||||||
let mapObject = await fetchResult.json();
|
json = await fetchResult.text();
|
||||||
|
this.levelStorage.setLevel(levelNumber, json);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mapObject = JSON.parse(json);
|
||||||
|
|
||||||
var map = new Map(mapObject.size.width, mapObject.size.height, mapObject.bots, mapObject.name);
|
var map = new Map(mapObject.size.width, mapObject.size.height, mapObject.bots, mapObject.name);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user