a82b1762-2ea5-4e85-894e-fd48f5d79339.js 3.28 KB
"use strict";
cc._RF.push(module, 'a82b1diLqVOhYlO/Uj115M5', 'AnimationUtils');
// script/common/utils/AnimationUtils.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnimationUtils = void 0;
const FrameAnimation_1 = require("../components/FrameAnimation");
const ScheduleUtils_1 = require("./ScheduleUtils");
var AnimationUtils;
(function (AnimationUtils) {
    function playFrameAnimation(node, loop, name, callback) {
        let anims = node.getComponents(FrameAnimation_1.default);
        if (!anims || anims.length == 0) {
            return;
        }
        let findOne = anims[0];
        for (let i = 0; i < anims.length; i++) {
            const anim = anims[i];
            if (anim.tag == name) {
                findOne = anim;
            }
            else {
                anim.enabled = false;
            }
        }
        findOne.enabled = true;
        findOne.play(callback, loop);
    }
    AnimationUtils.playFrameAnimation = playFrameAnimation;
    function playFrameAnimationOnce(node, name, callback) {
        playFrameAnimation(node, false, name, callback);
    }
    AnimationUtils.playFrameAnimationOnce = playFrameAnimationOnce;
    function playFrameAnimationLoop(node, name) {
        playFrameAnimation(node, true, name);
    }
    AnimationUtils.playFrameAnimationLoop = playFrameAnimationLoop;
    function animateProgress(progressBar, progress, speed = 1, label) {
        return new Promise((resolve) => {
            let oldProgress = progressBar.progress;
            if (oldProgress == progress) {
                resolve();
                return;
            }
            if (progress > 1)
                progress = 1;
            if (progress < 0)
                progress = 0;
            let delta = progress - oldProgress;
            if (progressBar.updateFunc)
                ScheduleUtils_1.ScheduleUtils.unscheduleUpdate(progressBar.updateFunc);
            const update = (dt) => {
                oldProgress += delta * dt * speed;
                if ((delta > 0 && oldProgress > progress) || (delta < 0 && oldProgress < progress)) {
                    oldProgress = progress;
                    delete progressBar.updateFunc;
                    ScheduleUtils_1.ScheduleUtils.unscheduleUpdate(update);
                    resolve();
                }
                progressBar.progress = oldProgress;
                if (label)
                    label.string = `${Math.round(oldProgress * 1000) / 10}%`;
            };
            progressBar.updateFunc = update;
            ScheduleUtils_1.ScheduleUtils.scheduleUpdate(update);
        });
    }
    AnimationUtils.animateProgress = animateProgress;
    //异步动画执行API. 方便链式调用.
    async function runAction(node, action) {
        return new Promise(resolve => {
            if (!action || !node) {
                console.info("runAction param is null:", node, action);
                resolve();
                return;
            }
            let nd = node instanceof cc.Component ? node.node : node;
            nd.runAction(cc.sequence(action, cc.callFunc(function () {
                resolve();
            })));
        });
    }
    AnimationUtils.runAction = runAction;
})(AnimationUtils = exports.AnimationUtils || (exports.AnimationUtils = {}));

cc._RF.pop();