You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.3 KiB
72 lines
2.3 KiB
import { createMembershipSet, membershipAdd } from '../../mx/tools/MembershipSet';
|
|
|
|
/**
|
|
* 局内 UI 暂停门(引用计数)。
|
|
* 打开需暂停的弹窗 → acquire;关闭 → release;持有数归零才 resume。
|
|
*
|
|
* 登记方式:在 `ConfigRes.UIConfigs[UIID.xxx]` 加 `pauseBattle: true`。
|
|
* 也可手动 `BattleUiPauseGate.acquire('token')` / `release('token')`(如广告)。
|
|
*
|
|
* 注意:本文件不得静态 import gg.battle / UIManager,避免与 mx UI 循环依赖。
|
|
*/
|
|
export class BattleUiPauseGate {
|
|
private static _holds = createMembershipSet<string>();
|
|
|
|
static get holdCount(): number {
|
|
return this._holds.size;
|
|
}
|
|
|
|
static get isHeld(): boolean {
|
|
return this._holds.size > 0;
|
|
}
|
|
|
|
/** 打开需暂停的 UI / 临时占用(广告等)时调用 */
|
|
static acquire(token: string): void {
|
|
if (!token) return;
|
|
const first = this._holds.size === 0;
|
|
membershipAdd(this._holds, token);
|
|
if (first) this._applyPause();
|
|
}
|
|
|
|
/** 关闭对应 UI / 结束临时占用时调用;全部释放后才恢复 */
|
|
static release(token: string): void {
|
|
if (!token || !this._holds.has(token)) return;
|
|
this._holds.delete(token);
|
|
if (this._holds.size === 0) this._applyResume();
|
|
}
|
|
|
|
/** 对局结束时清空,避免脏引用。默认不 resume(退出/销毁时不应再恢复战斗) */
|
|
static clear(opts?: { resume?: boolean }): void {
|
|
const had = this._holds.size > 0;
|
|
this._holds.clear();
|
|
if (had && opts?.resume === true) {
|
|
this._applyResume();
|
|
}
|
|
}
|
|
|
|
private static _battle(): {
|
|
IsGameOver: boolean;
|
|
IsGamePause: boolean;
|
|
pause(): void;
|
|
resume(): void;
|
|
} | null {
|
|
return (gg.game?.CurentBattle as {
|
|
IsGameOver: boolean;
|
|
IsGamePause: boolean;
|
|
pause(): void;
|
|
resume(): void;
|
|
} | null) ?? null;
|
|
}
|
|
|
|
private static _applyPause(): void {
|
|
const b = this._battle();
|
|
if (!b || b.IsGameOver) return;
|
|
if (!b.IsGamePause) b.pause();
|
|
}
|
|
|
|
private static _applyResume(): void {
|
|
const b = this._battle();
|
|
if (!b || b.IsGameOver) return;
|
|
if (b.IsGamePause) b.resume();
|
|
}
|
|
}
|
|
|