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/battle/BattleService.ts

83 lines
2.4 KiB

3 weeks ago
import type { Player } from '../game/Player';
import { BattleBuffCarryStore } from '../manager/battleBuff/BattleBuffCarryStore';
import { AiBattleBridge } from './ai/bridge/AiBattleBridge';
3 weeks ago
import { createBattleRuntime, type ProjectBattleHandle } from './runtime/BattleRuntime';
import { TableBattleConfigProvider } from './config/TableBattleConfigProvider';
import { BattleUiPauseGate } from './BattleUiPauseGate';
import { installAtkDebugHooks } from './debug/AtkDebugLog';
import type { IBattleService } from './IBattleService';
import type {
BattleDeployParams,
BattleStartParams,
IBattleRuntimeHandle,
} from './types/ProjectBattleTypes';
/** 战斗模块实现:对局生命周期,由 gg.battle 持有单实例 */
export class BattleService implements IBattleService {
readonly config = new TableBattleConfigProvider();
private _match: ProjectBattleHandle | null = null;
get match(): IBattleRuntimeHandle | null {
return this._match;
}
get localPlayer() {
return this._match?.player;
}
get players(): readonly Player[] {
return this._match?.players ?? [];
}
get remotePlayer() {
return this._match?.remotePlayer;
}
init(): void {
this.config.loadStaticTables();
installAtkDebugHooks();
}
start(params: BattleStartParams): IBattleRuntimeHandle | null {
installAtkDebugHooks();
this.end();
// 建波读灾厄盒等:先清旧局携带,再按已缓存匹配档案预挂 AI 被动
BattleBuffCarryStore.clear();
AiBattleBridge.inst.prefetchCarryForWaveBuild();
3 weeks ago
const runtime = createBattleRuntime(params);
if (!runtime) {
console.error(`[BattleService] start failed level=${params.levelId} rank=${params.playerRankId} opp=${params.opponentRankId}`);
return null;
}
this._match = runtime;
return runtime;
}
end(): void {
BattleUiPauseGate.clear();
const levelId = this._match?.levelId;
this._match?.destroy();
this._match = null;
if (levelId) {
this.config.releaseBattleSession(levelId);
}
}
tick(dt: number): void {
this._match?.tick(dt);
}
deploy(params: BattleDeployParams): boolean {
return this._match?.deploy(params) ?? false;
}
startCombat(): void {
this._match?.startCombat();
}
startPrep(): void {
this._match?.startPrep();
}
}