Source: Thing.UE/Misc/FrameCounter.js


// author : spr1ngd
// desc   : frame-count driven event caller

const UUtils = require("../UUtils");

/**
 * @description 每隔固定帧数F执行回调N次
 * @class FrameCounter
 */
class FrameCounter {

    #uuid = null;
    #intervalFrameCount = 1;
    #loopTime = -1;
    #callee = null;
    #frameCounter = 0;
    #loopCounter = 0;

    /**
     * @param {Function} callee - 回调函数
     * @param {Integer} intervalFrameCount - 每隔N帧执行一次回调
     * @param {Integer} loopTime - 回调执行次数
     */
    constructor(callee, intervalFrameCount = 1, loopTime = -1) {
        this.#uuid = UUtils.generateUUID();
        this.#intervalFrameCount = intervalFrameCount;
        this.#loopTime = loopTime;
        this.#callee = callee;
        this.FrameCounter = 0;
        this.#loopCounter = 0;
        THING.UE.appDelegate.addEventListener('update', () => {
            this.count();
        }, -1, this.#uuid);
    }

    count() {
        ++this.#frameCounter;
        if (this.#frameCounter >= this.#intervalFrameCount) {
            this.#callee();
            this.#loopCounter++;
            this.#frameCounter = 0;
        }
        if (this.#loopTime > 0) {
            if (this.#loopCounter >= this.#loopTime) {
                THING.UE.appDelegate.removeEventListener('update', null, this.#uuid);
            }
        }
    }
}

module.exports = FrameCounter;