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/view/CocosBattleRoot.ts

96 lines
2.7 KiB

3 weeks ago
import { _decorator, Component } from 'cc';
import { BattleFlowMode } from 'db://assets/mx/module/battle';
import type { BattleEvent, BattleEventSubscription } from 'db://assets/mx/module/battle';
import { CocosBattleMapHost } from '../map/CocosBattleMapHost';
import { RankDataManager } from '../../manager/RankDataManager';
import type { ProjectBattleMode } from '../types/ProjectBattleTypes';
import type { ProjectBattleHandle } from '../runtime/BattleRuntime';
const { ccclass, property } = _decorator;
/**
* 战斗场景根组件 — 初始化、tick、事件转发
* 挂到战斗场景空节点即可
*/
@ccclass('CocosBattleRoot')
export class CocosBattleRoot extends Component {
@property(CocosBattleMapHost)
mapHost: CocosBattleMapHost | null = null;
@property
levelId = 1;
@property
mode: ProjectBattleMode = 'solo';
@property
autoStartCombat = false;
/** 调试:强制连续战斗流程(覆盖关卡/常量配置) */
@property
useContinuousFlow = true;
@property
debugDeployWeapon1Id = 0;
@property
debugDeployRow = 8;
@property
debugDeployCol = 4;
onLoad(): void {
const playerRankId = RankDataManager.getPlayerRankId();
const battle = gg.battle.start({
levelId: this.levelId,
playerRankId,
opponentRankId: playerRankId,
mode: this.mode,
flowMode: this.useContinuousFlow ? BattleFlowMode.Continuous : undefined,
});
if (!battle) return;
const handle = battle as ProjectBattleHandle;
handle.on('*' as BattleEventSubscription, (e: BattleEvent) => this.onBattleEvent(e));
const mapHost = this.mapHost ?? this.getComponent(CocosBattleMapHost);
mapHost?.loadMap(battle.mapPrefabName);
if (this.debugDeployWeapon1Id > 0) {
gg.battle.deploy({
weapon1Id: this.debugDeployWeapon1Id,
row: this.debugDeployRow,
col: this.debugDeployCol,
});
}
if (this.autoStartCombat || this.useContinuousFlow) {
gg.battle.startCombat();
}
}
update(dt: number): void {
try {
gg.battle.tick(dt);
} catch (e) {
console.error('[CocosBattleRoot] tick', String(e));
}
}
onDestroy(): void {
gg.battle.end();
}
/** 供 UI / 子类监听 */
protected onBattleEvent(_event: BattleEvent): void {}
public beginCombat(): void {
gg.battle.startCombat();
}
public deployTower(weapon1Id: number, row: number, col: number): boolean {
return gg.battle.deploy({ weapon1Id, row, col });
}
}