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.
 
 
 
 
AdLike2048/assets/script/game/AppLifecycle.ts

76 lines
2.6 KiB

import { game, Game } from 'cc';
import { EDITOR } from 'cc/env';
import { GEvent } from '../../mx/module/event/GEvent';
import { BattleUiPauseGate } from '../battle/BattleUiPauseGate';
import { LoadScheduler } from '../../mx/module/res/LoadScheduler';
import { getResourcePhase, ResourcePhase } from '../../mx/module/res/ResourcePhase';
import { MemoryDowngrade } from './MemoryDowngrade';
/** 切后台暂停门闩 token(与 BattleUiPauseGate 一致) */
const BACKGROUND_PAUSE_TOKEN = 'app-background';
/**
* 小游戏前后台生命周期:战斗暂停、预载降压、屏幕常亮。
* 由 BootPipeline 安装;战斗进/退由 GameController 切换常亮意图。
*/
export class AppLifecycle {
private static _installed = false;
private static _inBackground = false;
private static _battleKeepScreen = false;
static install(): void {
if (this._installed) return;
this._installed = true;
game.on(Game.EVENT_HIDE, this._onHide, this);
game.on(Game.EVENT_SHOW, this._onShow, this);
GEvent.Ins.on(GEvent.PLATFORM_SHOW, this._onPlatformShow, this);
}
/** 战斗进行中保持屏幕常亮;退战斗须显式关闭 */
static setBattleKeepScreen(enabled: boolean): void {
this._battleKeepScreen = enabled;
if (!this._inBackground) {
gg.sdk.setKeepScreenOn(enabled);
}
}
/** 平台 onShow 回调:容器恢复后重设常亮(离开小游戏会失效) */
static onPlatformShow(): void {
if (this._battleKeepScreen && !this._inBackground) {
gg.sdk.setKeepScreenOn(true);
}
}
static isInBattle(): boolean {
const battle = gg.game?.CurentBattle;
if (battle && !battle.IsGameOver) return true;
return getResourcePhase() === ResourcePhase.Battle;
}
private static _onPlatformShow(): void {
this.onPlatformShow();
}
private static _onHide(): void {
if (EDITOR) return;
this._inBackground = true;
if (this.isInBattle()) {
BattleUiPauseGate.acquire(BACKGROUND_PAUSE_TOKEN);
}
LoadScheduler.getInstance().setSilentPaused(true);
gg.sdk.pfTriggerGC();
}
private static _onShow(): void {
if (EDITOR) return;
this._inBackground = false;
BattleUiPauseGate.release(BACKGROUND_PAUSE_TOKEN);
this._syncSilentPreload();
this.onPlatformShow();
}
/** 恢复 silent:仅内存降档暂停,战斗中允许局内补载 */
private static _syncSilentPreload(): void {
LoadScheduler.getInstance().setSilentPaused(MemoryDowngrade.level >= 1);
}
}