feat: implemented Localstorage manipulation and config and save

This commit is contained in:
Renge 2022-04-01 18:25:57 -04:00
parent 869ba41f13
commit fb951259fa
9 changed files with 5246 additions and 20 deletions

5102
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -17,5 +17,8 @@
"typescript": "^3.9.7",
"vinyl-source-stream": "^2.0.0",
"watchify": "^3.11.1"
},
"dependencies": {
"typescript-require": "^0.3.0"
}
}

View File

@ -1,5 +1,15 @@
import Scene from "../../Wolfie2D/Scene/Scene";
import ConfigManager from "../Tools/ConfigManager";
import SaveManager from "../Tools/SaveManager";
export default class MainMenu extends Scene {
protected config: ConfigManager;
protected save: SaveManager;
// TODO
startScene(): void {
this.config = new ConfigManager();
this.save = new SaveManager();
}
}

View File

@ -0,0 +1,50 @@
import Config from "./DataTypes/Config";
import LocalStorageDB from "./LocalStorageDB";
export default class ConfigManager {
private static config: Config;
private db: LocalStorageDB;
constructor() {
this.db = new LocalStorageDB("config");
this.loadConfig();
if (!ConfigManager.config)
this.initConfig();
}
getVolume(): number {
return ConfigManager.config.volume;
}
setVolume(volume: number): void {
ConfigManager.config.volume = volume;
this.saveConfig();
}
// TODOs
// add more functions if needed
resetConfig(callback?: Function): void {
this.initConfig();
callback(ConfigManager.config);
}
private loadConfig(): void {
ConfigManager.config = <Config>this.db.loadJSON();
}
private saveConfig(): void {
this.db.saveJSON(ConfigManager.config);
}
private initConfig(): void {
this.db.readJSON("shattered_sword_assets/jsons/sampleconfig.json", (config: object) => {
if (!config)
throw new Error("Fail to load config file");
ConfigManager.config = <Config>config;
console.log("Initializing Local Storage(config): ", ConfigManager.config);
this.saveConfig();
});
}
}

View File

@ -0,0 +1,3 @@
export default class Config {
volume: number;
}

View File

@ -0,0 +1,4 @@
export default class Save {
name: string;
level: number;
}

View File

@ -1,4 +1,4 @@
import Input from "./../Wolfie2D/Input/Input";
import Input from "../../Wolfie2D/Input/Input";
export default class InputWrapper {
private static isInStoryMode: boolean;

View File

@ -0,0 +1,32 @@
export default class LocalStorageDB {
private key: string;
constructor(key: string) {
this.key = key;
}
/**
* Read a json file from a path
* @param JSONFilePath The path to the JSON file
* @param callback Function to run after getting the JSON
*/
readJSON(JSONFilePath: string, callback: Function): void {
let xobj: XMLHttpRequest = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', JSONFilePath, true);
xobj.onreadystatechange = function () {
if ((xobj.readyState == 4) && (xobj.status == 200)) {
callback(JSON.parse(xobj.responseText));
}
};
xobj.send(null);
}
loadJSON(): object {
return JSON.parse(localStorage.getItem(this.key));
}
saveJSON(value: object): void {
localStorage.setItem(this.key, JSON.stringify(value));
}
}

View File

@ -0,0 +1,60 @@
import Save from "./DataTypes/Save";
import LocalStorageDB from "./LocalStorageDB";
export default class SaveManager {
private static save: Save;
private db: LocalStorageDB;
constructor() {
this.db = new LocalStorageDB("save");
this.loadSave();
if (!SaveManager.save)
this.initSave();
}
getLevel(): number {
return SaveManager.save.level;
}
setLevel(level: number): void {
SaveManager.save.level = level;
this.saveSave();
}
getName(): string {
return SaveManager.save.name;
}
setName(name: string): void {
SaveManager.save.name = name;
this.saveSave();
}
// TODOs
// add more functions if needed
resetSave(callback?: Function): void {
this.initSave();
callback(SaveManager.save);
}
private loadSave(): void {
SaveManager.save = <Save>this.db.loadJSON();
}
private saveSave(): void {
this.db.saveJSON(SaveManager.save);
}
private initSave(): void {
this.db.readJSON("shattered_sword_assets/jsons/samplesave.json", (save: object) => {
if (!save)
throw new Error("Fail to load save file");
SaveManager.save = <Save>save;
console.log("Initializing Local Storage(save): ", SaveManager.save);
this.saveSave();
});
}
}