Source: EffectManager.js

EffectManager.js

//-----------------------------------------------------------------------------
// EffectManager
//
// The static class that loads Effekseer effects.
/**
 * The static class that loads Effekseer effects.
 *
 * @namespace
 */
function EffectManager() {
    throw new Error("This is a static class");
}

EffectManager._cache = {};
EffectManager._errorUrls = [];

/**
 * Loads the effekseer file from the effects folder
 *
 * @static
 * @param {string} filename - The name of the effect file (no file extension)
 * @return {Graphics.effekseer.effect|null} - The effect object if filename provided, or null if no filename provided
 */
EffectManager.load = function(filename) {
    if (filename) {
        const url = this.makeUrl(filename);
        const cache = this._cache;
        if (!cache[url] && Graphics.effekseer) {
            this.startLoading(url);
        }
        return cache[url];
    } else {
        return null;
    }
};

/**
 * Starts loading process for an effect at the given url
 *
 * @static
 * @param {string} url - The full url of the file (folder + filename + extension)
 * @return {Graphics.effekseer.effect} - The effect object
 */
EffectManager.startLoading = function(url) {
    const onLoad = () => this.onLoad(url);
    const onError = (message, url) => this.onError(url);
    const effect = Graphics.effekseer.loadEffect(url, 1, onLoad, onError);
    this._cache[url] = effect;
    return effect;
};

/**
 * Clears the effect cache
 *
 * @static
 */
EffectManager.clear = function() {
    for (const url in this._cache) {
        const effect = this._cache[url];
        Graphics.effekseer.releaseEffect(effect);
    }
    this._cache = {};
};

/**
 * Handling after an effect is loaded
 *
 * @static
 * @param {string} url - The loaded file's url
 */
EffectManager.onLoad = function(/*url*/) {
    //
};

/**
 * Handling after an effect encounters an error during load
 *
 * @static
 * @param {string} url - The error file's url
 */
EffectManager.onError = function(url) {
    this._errorUrls.push(url);
};

/**
 * Makes a full url from a filename
 *
 * @static
 * @param {string} filename - The filename with no extension
 * @return {string} The full url to the file including folder, filename, and extension
 */
EffectManager.makeUrl = function(filename) {
    return "effects/" + Utils.encodeURI(filename) + ".efkefc";
};

/**
 * Checks if there are any errors that occurred while loading effects
 *
 * @static
 */
EffectManager.checkErrors = function() {
    const url = this._errorUrls.shift();
    if (url) {
        this.throwLoadError(url);
    }
};

/**
 * Throws an error with retry button
 *
 * @static
 * @param {string} url - The url of the file to retry loading
 * @throws Will throw a retry screen error
 */
EffectManager.throwLoadError = function(url) {
    const retry = () => this.startLoading(url);
    throw ["LoadError", url, retry];
};

/**
 * Check if all effects are done loading
 *
 * @static
 * @return {boolean} Whether all effects are done loading or not
 */
EffectManager.isReady = function() {
    this.checkErrors();
    for (const url in this._cache) {
        const effect = this._cache[url];
        if (!effect.isLoaded) {
            return false;
        }
    }
    return true;
};