86449bea-7b4f-45c9-b8ec-afd43345e746.js 9.34 KB
"use strict";
cc._RF.push(module, '86449vqe09Fybjsr9QzRedG', 'ActionManager');
// script/avg/ActionManager.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActionManager = void 0;
const ActionModel_1 = require("./model/ActionModel");
const EditorEvents_1 = require("./EditorEvents");
const GameRecord_1 = require("./game-data/GameRecord");
const simba_random_1 = require("simba-random");
const AudioManager_1 = require("../common/gameplay/managers/AudioManager");
const runningExecutors = new Set();
let _affectRecord = true;
class ActionExecutor {
    constructor(action) {
        this.sleep = (t) => {
            return new Promise((resolve) => {
                this._timerHandle = setTimeout(() => {
                    this._timerHandle = undefined;
                    resolve();
                }, t * 1000);
            });
        };
        this.execute = async () => {
            runningExecutors.add(this);
            if (this._action.trigger === ActionModel_1.TriggerType.Click) { // wait for click events
                // await new Promise((resolve) => {
                //     this._disposable = ClickEvent.once(() => {
                //         resolve();
                //     })
                // });
            }
            if (this._action.trigger === ActionModel_1.TriggerType.Event) {
                if (!this._action.triggerEvent)
                    return;
                return new Promise((resolve) => {
                    this._disposable = EditorEvents_1.EditorEvents.emitter.once(this._action.triggerEvent, async () => {
                        let result = await this.executeInternal();
                        resolve();
                        return result;
                    });
                });
            }
            else {
                return this.executeInternal();
            }
        };
        this.executeInternal = async () => {
            if (this._action.delay) {
                await this.sleep(this._action.delay);
            }
            let finished = true;
            if (this.nextExecutor && this.nextExecutor._action.trigger === ActionModel_1.TriggerType.PreviousStart) {
                [finished] = await Promise.all([this.onExec(), this.nextExecutor.execute()]);
            }
            else {
                finished = await this.onExec();
                if (this.nextExecutor) {
                    await this.nextExecutor.execute();
                }
            }
            if (finished) {
                runningExecutors.delete(this);
            }
        };
        this.stop = () => {
            this.onStop();
            if (this._disposable)
                this._disposable.dispose();
            if (this._timerHandle)
                clearTimeout(this._timerHandle);
        };
        this._action = action;
    }
}
class ModifyVariableExecutor extends ActionExecutor {
    onExec() {
        if (!_affectRecord)
            return Promise.resolve(true);
        try {
            let action = this._action;
            let varValue = GameRecord_1.GameRecord.getVariableValue(action.target);
            let value = action.oprand.value;
            if (action.oprand.type === ActionModel_1.OperandType.Const) {
                if (typeof varValue === "number") {
                    value = Number.parseFloat(value);
                }
            }
            else if (action.oprand.type === ActionModel_1.OperandType.Random) {
                let arr = value.split("-");
                let min = Number.parseFloat(arr[0]);
                let max = Number.parseFloat(arr[1]);
                value = simba_random_1.Random.range(min, max);
            }
            else if (action.oprand.type === ActionModel_1.OperandType.Variable) {
                value = GameRecord_1.GameRecord.getVariableValue(value);
            }
            switch (action.operator) {
                case ActionModel_1.VariableOperator.Assign:
                    break;
                case ActionModel_1.VariableOperator.Plus:
                    varValue += value;
                    break;
                case ActionModel_1.VariableOperator.Minus:
                    varValue -= value;
                    break;
                case ActionModel_1.VariableOperator.Multiply:
                    varValue *= value;
                    break;
                case ActionModel_1.VariableOperator.Divide:
                    varValue /= value;
                    break;
                case ActionModel_1.VariableOperator.Modulo:
                    varValue %= value;
                    break;
            }
            GameRecord_1.GameRecord.setVariableValue(action.target, varValue);
        }
        catch (e) {
            console.error(e);
        }
        return Promise.resolve(true);
    }
    onStop() {
    }
}
class PlayAudioExecutor extends ActionExecutor {
    async onExec() {
        let action = this._action;
        if (action.audioType === "effect") {
            if (action.stopPreviousSound) {
                AudioManager_1.AudioManager.stopAllEffect();
            }
            if (action.loopCount <= 0) {
                this.audioId = await AudioManager_1.AudioManager.playEffect(action.filePath, 0);
                return false;
            }
            else {
                AudioManager_1.AudioManager.playEffect(action.filePath, action.loopCount);
            }
        }
        else {
            if (!action.filePath) {
                AudioManager_1.AudioManager.stopMusic();
            }
            else {
                AudioManager_1.AudioManager.playMusic(action.filePath);
            }
            if (_affectRecord) {
                GameRecord_1.GameRecord.recordVariables.bgm = action.filePath;
            }
        }
        return true;
    }
    onStop() {
        if (this.audioId !== undefined) {
            AudioManager_1.AudioManager.stopEffect(this.audioId);
        }
    }
}
class EmitEventExecutor extends ActionExecutor {
    async onExec() {
        if (this._action.emitEvent) {
            await Promise.all(EditorEvents_1.EditorEvents.emitter.emit(this._action.emitEvent, this._action.param));
        }
        return Promise.resolve(true);
    }
    onStop() {
    }
}
var ActionManager;
(function (ActionManager) {
    function getExecutor(action) {
        if (action.type === ActionModel_1.ActionType.ModifyVariable) {
            return new ModifyVariableExecutor(action);
        }
        else if (action.type === ActionModel_1.ActionType.PlayAudio) {
            return new PlayAudioExecutor(action);
        }
        else if (action.type === ActionModel_1.ActionType.EmitEvent) {
            return new EmitEventExecutor(action);
        }
        return undefined;
    }
    let stopped = false;
    let prevActions;
    async function executeActions(actionData, filter, affectRecord = true) {
        // if (prevActions === actionData) {
        //     throw new Error("Action already executed.");
        // }
        // prevActions = actionData;
        _affectRecord = affectRecord;
        if (!stopped) {
            stop();
        }
        stopped = false;
        let firstActionExecutor;
        let triggerType = ActionModel_1.TriggerType.PreviousStart;
        let executors = [];
        let noWaitExecutors = [];
        if (actionData.actions) {
            let currExecutor;
            for (let i = 0; i < actionData.actions.length; i++) {
                if (i > 0 && actionData.actions[i].trigger === ActionModel_1.TriggerType.Event) {
                    if (firstActionExecutor) {
                        executors.push(firstActionExecutor);
                        firstActionExecutor = undefined;
                    }
                }
                let action = actionData.actions[i];
                if (filter && !filter(action)) {
                    continue;
                }
                let executor = getExecutor(action);
                if (executor) {
                    if (!firstActionExecutor) {
                        firstActionExecutor = executor;
                        triggerType = action.trigger;
                        currExecutor = executor;
                    }
                    else if (currExecutor) {
                        currExecutor.nextExecutor = executor;
                        currExecutor = executor;
                    }
                }
            }
            if (firstActionExecutor) {
                if (triggerType === ActionModel_1.TriggerType.PreviousStart || triggerType === ActionModel_1.TriggerType.PreviousEnd) {
                    executors.push(firstActionExecutor);
                }
                else {
                    noWaitExecutors.push(firstActionExecutor);
                }
            }
        }
        if (noWaitExecutors.length) {
            noWaitExecutors.forEach((v) => v.execute());
        }
        let promises = executors.map(v => v.execute());
        if (executors.length) {
            await Promise.all(promises);
        }
        if (stopped)
            return;
    }
    ActionManager.executeActions = executeActions;
    function stop() {
        if (stopped)
            return;
        stopped = true;
        for (let executor of runningExecutors) {
            executor.stop();
        }
        runningExecutors.clear();
        //add line
        // prevActions = undefined;
    }
    ActionManager.stop = stop;
})(ActionManager = exports.ActionManager || (exports.ActionManager = {}));

cc._RF.pop();