Source: ConfigManager.js

ConfigManager.js

//-----------------------------------------------------------------------------
// ConfigManager
//
// The static class that manages the configuration data.

/**
 * The static class that manages player controlled settings in the game
 *
 * @namespace
 */
function ConfigManager() {
    throw new Error("This is a static class");
}

/**
 * Flag for whether the player always tries to dash or not
 *
 * @type boolean
 * @static
 * @name ConfigManager.alwaysDash
 * @default 
 */
ConfigManager.alwaysDash = false;

/**
 * Flag for whether to remember last battle command or not
 *
 * @type boolean
 * @static
 * @name ConfigManager.commandRemember
 * @default 
 */
ConfigManager.commandRemember = false;

/**
 * Flag for whether to use the Touch UI or not
 *
 * @type boolean
 * @static
 * @name ConfigManager.touchUI
 * @default 
 */
ConfigManager.touchUI = true;
ConfigManager._isLoaded = false;

/**
 * Volume for BGMs
 *
 * @type number
 * @static
 * @name ConfigManager.bgmVolume
 */
Object.defineProperty(ConfigManager, "bgmVolume", {
    get: function() {
        return AudioManager._bgmVolume;
    },
    set: function(value) {
        AudioManager.bgmVolume = value;
    },
    configurable: true
});

/**
 * Volume for BGSs
 *
 * @type number
 * @static
 * @name ConfigManager.bgsVolume
 */
Object.defineProperty(ConfigManager, "bgsVolume", {
    get: function() {
        return AudioManager.bgsVolume;
    },
    set: function(value) {
        AudioManager.bgsVolume = value;
    },
    configurable: true
});

/**
 * Volume for MEs
 *
 * @type number
 * @static
 * @name ConfigManager.meVolume
 */
Object.defineProperty(ConfigManager, "meVolume", {
    get: function() {
        return AudioManager.meVolume;
    },
    set: function(value) {
        AudioManager.meVolume = value;
    },
    configurable: true
});

/**
 * Volume for SEs
 *
 * @type number
 * @static
 * @name ConfigManager.seVolume
 */
Object.defineProperty(ConfigManager, "seVolume", {
    get: function() {
        return AudioManager.seVolume;
    },
    set: function(value) {
        AudioManager.seVolume = value;
    },
    configurable: true
});

/**
 * Loads the player's settings from the config save file
 *
 * @static
 */
ConfigManager.load = function() {
    StorageManager.loadObject("config")
        .then(config => this.applyData(config || {}))
        .catch(() => 0)
        .then(() => {
            this._isLoaded = true;
            return 0;
        })
        .catch(() => 0);
};

/**
 * Saves the player's settings to the config save file
 *
 * @static
 */
ConfigManager.save = function() {
    StorageManager.saveObject("config", this.makeData());
};

/**
 * Check if the config file is done loading
 *
 * @static
 * @return {boolean} Whether loading is complete or not
 */
ConfigManager.isLoaded = function() {
    return this._isLoaded;
};

/**
 * Gets the current config settings as an object
 *
 * @static
 * @return {{alwaysDash: boolean, commandRemember: boolean, touchUI: boolean, bgmVolume: number, bgsVolume: number, meVolume: number, seVolume: number}} Config object
 */
ConfigManager.makeData = function() {
    const config = {};
    config.alwaysDash = this.alwaysDash;
    config.commandRemember = this.commandRemember;
    config.touchUI = this.touchUI;
    config.bgmVolume = this.bgmVolume;
    config.bgsVolume = this.bgsVolume;
    config.meVolume = this.meVolume;
    config.seVolume = this.seVolume;
    return config;
};

/**
 * Sets the current config settings to match passed config object. Will use defaults if any property missing
 *
 * @static
 * @param {{alwaysDash: boolean, commandRemember: boolean, touchUI: boolean, bgmVolume: number, bgsVolume: number, meVolume: number, seVolume: number}} config - Config object
 */
ConfigManager.applyData = function(config) {
    this.alwaysDash = this.readFlag(config, "alwaysDash", false);
    this.commandRemember = this.readFlag(config, "commandRemember", false);
    this.touchUI = this.readFlag(config, "touchUI", true);
    this.bgmVolume = this.readVolume(config, "bgmVolume");
    this.bgsVolume = this.readVolume(config, "bgsVolume");
    this.meVolume = this.readVolume(config, "meVolume");
    this.seVolume = this.readVolume(config, "seVolume");
};

/**
 * Tries to access a boolean parameter in the config parameter, if no exist, returns default value instead.
 *
 * @static
 * @param {{alwaysDash: boolean, commandRemember: boolean, touchUI: boolean, bgmVolume: number, bgsVolume: number, meVolume: number, seVolume: number}} config - Config object
 * @param {string} name - The property name
 * @param {boolean} defaultValue - The default setting
 * @return {boolean} config property if exist, else defaultValue
 */
ConfigManager.readFlag = function(config, name, defaultValue) {
    if (name in config) {
        return !!config[name];
    } else {
        return defaultValue;
    }
};

/**
 * Tries to access a number parameter in the config parameter. if no exists, returns 100 instead
 *
 * @static
 * @param {{alwaysDash: boolean, commandRemember: boolean, touchUI: boolean, bgmVolume: number, bgsVolume: number, meVolume: number, seVolume: number}} config - Config object
 * @param {string} name - The property name
 * @return {number} config property between 0 and 100 if exist, else 100
 */
ConfigManager.readVolume = function(config, name) {
    if (name in config) {
        return Number(config[name]).clamp(0, 100);
    } else {
        return 100;
    }
};