orion-engine
Version:
A simple and lightweight web based game development library
82 lines (81 loc) • 2.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GameDataManager = void 0;
class GameDataManager {
_uuid;
_game_data = null;
_format;
/**
* Creates a new GameDataManager instance.
* @param uuid UUID of the game data to manage.
*/
constructor(uuid, format = 'json') {
this._uuid = uuid;
this._format = format;
}
/**
* Gets the game data.
*/
get game_data() {
return this._game_data;
}
/**
* Gets the UUID of the game data.
*/
get uuid() {
return this._uuid;
}
/**
* Saves the game data to local storage. Saves the latest data set by setGameData(). Ensure you have set the data first!
*/
save_game() {
if (this._game_data) {
localStorage.setItem(this._uuid, this._format === 'json' ? JSON.stringify(this._game_data) : btoa(JSON.stringify(this._game_data)));
}
}
/**
* Loads the game data from local storage.
* @returns T of Game Data
*/
load_game() {
if (this._format === 'json') {
return JSON.parse(localStorage.getItem(this._uuid) || 'null');
}
else {
return JSON.parse(atob(localStorage.getItem(this._uuid) || 'null'));
}
}
/**
* Sets the game data manually.
* @param data T of Game Data
*/
set_game_data(data) {
this._game_data = data;
}
/**
* Retrieves the raw game data from localStorage.
* @returns The raw game data as a string.
*/
get_raw() {
return localStorage.getItem(this._uuid) || '';
}
/**
* Creates a download URL for the current game data in the specified format.
* @param format The format of the game data to download ('json' or 'text').
* @returns The download URL for the game data.
*/
create_save_download(format) {
let url = '';
if (format === 'json') {
const new_data = JSON.stringify(this._game_data, null, 2);
const blob = new Blob([new_data], { type: 'application/json' });
url = window.URL.createObjectURL(blob);
}
else {
const blob = new Blob([this._game_data], { type: 'text/plain' });
url = window.URL.createObjectURL(blob);
}
return url;
}
}
exports.GameDataManager = GameDataManager;