BattleManager.ts 2.31 KB
import { SaveDataManager } from "../../component/SaveDataManager";
import { PhysicsControl } from "../physics/PhysicsControl";
import { Battle } from "./Battle";
import { BattleEvent, getGlobalNode } from "./BattleConst";

export class BattleManager extends cc.Component {
    private static _ins: BattleManager;
    static get ins(): BattleManager {
        if (this._ins == null) {
            this._ins = getGlobalNode().addComponent(BattleManager);
            getGlobalNode().addChild(this._ins.eventNode);
            // this._ins.eventNode.event
        }

        return this._ins;
    }

    private _curBattle: Battle;
    get curBattle(): Battle { return this._curBattle; }

    private _eventNode: cc.Node = new cc.Node("battleManagerEventNode");
    /**事件节点,BattleEvent的派发和监听都在此节点进行 */
    get eventNode(): cc.Node { return this._eventNode; }

    private _paused: boolean = false;
    get paused(): boolean { return this._paused; }
    set paused(value: boolean) {
        this._paused = value;

        if (this.curBattle && this.curBattle.roundRunning) {

            // PhysicsControl.ins.setPhysicsEnable(!value);
        }
    }

    /**是否可以滑动控制发射指示器 */
    shootTouchEnable: boolean = false;

    // private _prefabCollection:cc.Prefab

    startBattle(levelId: number, callBack: Function, thisObj: any, prefab: cc.Prefab) {

        this.shootTouchEnable = false;
        this._paused = false;

        if (this.curBattle) {
            //销毁
            this._curBattle.destroy();
            this._curBattle = null;
        }

        this._curBattle = new Battle();
        this._curBattle.init(levelId, () => {
            callBack.call(thisObj);
        }, this, prefab);

        this.eventNode.emit(BattleEvent.BATTLE_START);
    }

    /**直接离开战斗,不会触发战斗结算 */
    leaveBattle() {

        //#region 恢复试用数据
        SaveDataManager.ins.runtimeData.ballSkinTry = 0;
        SaveDataManager.ins.runtimeData.goldenAimTry = false;
        SaveDataManager.ins.saveData();
        //#endregion

        this._curBattle.destroy();
        this._curBattle = null;
    }

    protected update(dt: number): void {
        // super.update(dt);
        if (this._curBattle && !this._paused) {
            this._curBattle.update(dt);
        }
    }
}