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.
52 lines
1.6 KiB
52 lines
1.6 KiB
|
5 days ago
|
import { PhysicsSystem2D } from 'cc';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 2D 物理开关唯一入口。
|
||
|
|
*
|
||
|
|
* 根因:广告 / 离局 / 分辨率稳定等各自 `enable = false` 再按本地 wasEnabled 恢复,
|
||
|
|
* 快照会互相覆盖,最终永久关物理 → 子弹无碰撞 → 怪不掉血。
|
||
|
|
*
|
||
|
|
* 规则:任一 reason 挂起则关;集合空才开。开战/离局走 beginBattle/endBattle。
|
||
|
|
*/
|
||
|
|
export class Physics2DGate {
|
||
|
|
private static _reasons = new Set<string>();
|
||
|
|
|
||
|
|
static readonly VideoAd = 'videoAd';
|
||
|
|
|
||
|
|
static suspend(reason: string): void {
|
||
|
|
if (!reason) return;
|
||
|
|
this._reasons.add(reason);
|
||
|
|
PhysicsSystem2D.instance.enable = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
static resume(reason: string): void {
|
||
|
|
if (!reason) return;
|
||
|
|
this._reasons.delete(reason);
|
||
|
|
this._applyEnable();
|
||
|
|
}
|
||
|
|
|
||
|
|
static isSuspended(reason?: string): boolean {
|
||
|
|
if (reason) return this._reasons.has(reason);
|
||
|
|
return this._reasons.size > 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static _applyEnable(): void {
|
||
|
|
const on = this._reasons.size === 0;
|
||
|
|
PhysicsSystem2D.instance.enable = on;
|
||
|
|
if (on) PhysicsSystem2D.instance.maxSubSteps = 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 进入战斗:清空挂起并开物理 */
|
||
|
|
static beginBattle(): void {
|
||
|
|
this._reasons.clear();
|
||
|
|
PhysicsSystem2D.instance.enable = true;
|
||
|
|
PhysicsSystem2D.instance.maxSubSteps = 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 离开战斗:清空其它挂起并关物理(避免离局后广告回调误开) */
|
||
|
|
static endBattle(): void {
|
||
|
|
this._reasons.clear();
|
||
|
|
PhysicsSystem2D.instance.enable = false;
|
||
|
|
}
|
||
|
|
}
|