three-game-engine
Version:
Simple light-weight game engine using three.js, three-mesh-ui and rapier
113 lines (112 loc) • 4.36 kB
JavaScript
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
const { EventEmitter } = require('events');
const Util_1 = require("./Util");
const LOCAL_STORAGE_KEY = 'game_settings';
class Settings {
static on(event, listener) {
_a._emitter.on(event, listener);
}
static off(event, listener) {
_a._emitter.off(event, listener);
}
static _getInitialSettings() {
return {
max_arrows: 30,
bow_in_right_hand: false, // If true, the bow will be held in the right hand, and drawn with the left hand (for left handed users).
draw_distance_min: 0.15, // When the two controllers are this distance or less apart, bow is fully NOT drawn (drawOffset = 0)
draw_distance_max: 0.5 // When the two controllers are this distance or more apart, bow is fully drawn (drawOffset = 1)
};
// Other setttings:
// unlocked_level_<level name>: true
// highest_round_unlocked_for_<level name>: 7
// high_scores_for_<level name>: [ { round: 10, score: 950 }, { round: 9, score: 1000 }, .. ] // must be sorted by round, highest round number first
}
static load() {
_a._promiseChain = _a._promiseChain.then(() => {
console.log(`Settings: loading settings...`);
let savedSettings = null;
try {
savedSettings = window.localStorage.getItem(LOCAL_STORAGE_KEY);
}
catch (error) {
console.error(`Settings: error reading settings from localStorage: ${error}`);
}
if (savedSettings) {
console.log(`Setings: saved settings detected, parsing data...`);
try {
_a._settings = JSON.parse(savedSettings);
_a._validate();
console.log('Settings: saved settings successfully loaded.');
}
catch (error) {
console.error(`Settings: error parsing saved settings: ${error}. Loading default settings instead.`);
_a._settings = _a._getInitialSettings();
}
}
else {
console.log(`Settings: no saved settings detected, loading initial settings...`);
_a._settings = _a._getInitialSettings();
}
console.log(`Settings: loaded values: ${JSON.stringify(_a._settings)}`);
_a._emitter.emit('CHANGE');
});
}
static get(key) {
return _a._settings[key];
}
static set(key, value) {
console.log(`Settings: setting ${key} to ${value}`);
_a._settings[key] = value;
_a._save();
}
static _save() {
_a._debouncedSave();
}
static reset() {
console.log(`Settings: clearing settings...`);
_a._settings = _a._getInitialSettings();
console.log('Settings: initial settings loaded.');
_a._save();
}
static _validate() {
const maxArrows = _a._settings.max_arrows;
if (typeof maxArrows !== 'number' || maxArrows <= 0) {
throw new Error('Settings: invalid settings, max_arrows must be a numeric value > 0');
}
}
}
_a = Settings;
Settings._emitter = new EventEmitter();
Settings._settings = null;
Settings._promiseChain = Promise.resolve(); // allow at most 1 save() or load() operation at a time.
Settings._debouncedSave = Util_1.default.debounce(() => {
// Use promise chain to serialize (only one save/load operation at a time)
_a._promiseChain = _a._promiseChain.then(async () => {
console.log('Settings: saving...');
let saveError = null;
try {
const json = JSON.stringify(_a._settings);
localStorage.setItem(LOCAL_STORAGE_KEY, json);
}
catch (error) {
saveError = error;
}
if (saveError) {
_a._onSaveError(saveError);
}
else {
_a._onSaveSuccess();
}
});
}, 500);
Settings._onSaveSuccess = () => {
console.log('Settings: settings saved successfully.');
_a._emitter.emit('SAVED');
};
Settings._onSaveError = (error) => {
console.warn(`Settings: error saving settings: ${error}`);
_a._emitter.emit('SAVE_ERROR');
};
exports.default = Settings;
;