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/ai/runtime/AiSkillHost.ts

72 lines
2.4 KiB

3 weeks ago
import { Node } from 'cc';
import { aiLog } from './AiLog';
import type { ProjectBattleHandle } from '../../runtime/BattleRuntime';
import type { MapManager } from '../../map/legacy/MapManager';
import { SkillController } from '../../skill/SkillController';
import { AiHeadlessBagHost } from './AiHeadlessBagHost';
2 weeks ago
import type { TextGrid } from '../../view/TextGrid';
3 weeks ago
/**
* AI 技能表现宿主:独立 SkillController,承接 AI 塔 PresentationCue。
* 缺绑则 PlayPresentation 上锁后无人 finish,AI 塔永不攻击。
*/
export class AiSkillHost {
private _hostNode: Node | null = null;
private _controller: SkillController | null = null;
/**
* 绑定 AI 玩家的 SkillController。
* @param battle 战斗
* @param map 地图
* @param parent UI 节点(挂载组件)
2 weeks ago
* @param mapGrids AI 地图 TextGrid
3 weeks ago
*/
2 weeks ago
bind(battle: ProjectBattleHandle, map: MapManager, parent: Node, mapGrids?: ReadonlyMap<number, TextGrid>): boolean {
3 weeks ago
this.dispose();
const aiPlayer = battle.getPlayer(AiHeadlessBagHost.AI_PLAYER_ID);
if (!aiPlayer) {
aiLog('[AiSkillHost] bind failed: AI player missing');
return false;
}
if (!parent?.isValid) {
aiLog('[AiSkillHost] bind failed: parent invalid');
return false;
}
let host = parent.getChildByName('aiSkillHost');
if (!host?.isValid) {
host = new Node('aiSkillHost');
parent.addChild(host);
}
let sc = host.getComponent(SkillController);
if (!sc) sc = host.addComponent(SkillController);
sc.bind(aiPlayer, battle);
2 weeks ago
sc.refreshEntitySkillBindings(map, mapGrids);
3 weeks ago
aiPlayer.skillController = sc;
this._hostNode = host;
this._controller = sc;
aiLog('[AiSkillHost] bound AI SkillController');
return true;
}
/**
* 部署变化后刷新 entity→Skill 映射。
* @param map 地图
2 weeks ago
* @param mapGrids AI 地图 TextGrid
3 weeks ago
*/
2 weeks ago
refresh(map: MapManager, mapGrids?: ReadonlyMap<number, TextGrid>): void {
this._controller?.refreshEntitySkillBindings(map, mapGrids);
3 weeks ago
}
/** 解除绑定 */
dispose(): void {
if (this._controller) {
this._controller.unbind();
this._controller = null;
}
if (this._hostNode?.isValid) {
this._hostNode.destroy();
}
this._hostNode = null;
}
}