2025-10-31 17:27:38 +08:00
|
|
|
import { Entity, Core } from '@esengine/ecs-framework';
|
2025-11-27 20:42:46 +08:00
|
|
|
import { BehaviorTreeData } from './execution/BehaviorTreeData';
|
|
|
|
|
import { BehaviorTreeRuntimeComponent } from './execution/BehaviorTreeRuntimeComponent';
|
|
|
|
|
import { BehaviorTreeAssetManager } from './execution/BehaviorTreeAssetManager';
|
2025-10-27 09:29:11 +08:00
|
|
|
|
|
|
|
|
/**
|
2025-10-31 17:27:38 +08:00
|
|
|
* 行为树启动辅助类
|
2025-10-27 09:29:11 +08:00
|
|
|
*
|
2025-10-31 17:27:38 +08:00
|
|
|
* 提供便捷方法来启动、停止行为树
|
2025-10-27 09:29:11 +08:00
|
|
|
*/
|
|
|
|
|
export class BehaviorTreeStarter {
|
|
|
|
|
/**
|
|
|
|
|
* 启动行为树
|
|
|
|
|
*
|
2025-10-31 17:27:38 +08:00
|
|
|
* @param entity 游戏实体
|
|
|
|
|
* @param treeData 行为树数据
|
|
|
|
|
* @param autoStart 是否自动开始执行
|
2025-10-27 09:29:11 +08:00
|
|
|
*/
|
2025-10-31 17:27:38 +08:00
|
|
|
static start(entity: Entity, treeData: BehaviorTreeData, autoStart: boolean = true): void {
|
|
|
|
|
const assetManager = Core.services.resolve(BehaviorTreeAssetManager);
|
|
|
|
|
assetManager.loadAsset(treeData);
|
2025-10-27 09:29:11 +08:00
|
|
|
|
2025-10-31 17:27:38 +08:00
|
|
|
let runtime = entity.getComponent(BehaviorTreeRuntimeComponent);
|
|
|
|
|
if (!runtime) {
|
|
|
|
|
runtime = new BehaviorTreeRuntimeComponent();
|
|
|
|
|
entity.addComponent(runtime);
|
2025-10-27 09:29:11 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-31 17:27:38 +08:00
|
|
|
runtime.treeAssetId = treeData.id;
|
|
|
|
|
runtime.autoStart = autoStart;
|
2025-10-27 09:29:11 +08:00
|
|
|
|
2025-10-31 17:27:38 +08:00
|
|
|
if (treeData.blackboardVariables) {
|
|
|
|
|
for (const [key, value] of treeData.blackboardVariables.entries()) {
|
|
|
|
|
runtime.setBlackboardValue(key, value);
|
|
|
|
|
}
|
2025-10-27 09:29:11 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-31 17:27:38 +08:00
|
|
|
if (autoStart) {
|
|
|
|
|
runtime.isRunning = true;
|
2025-10-27 09:29:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-31 17:27:38 +08:00
|
|
|
* 停止行为树
|
2025-10-27 09:29:11 +08:00
|
|
|
*
|
2025-10-31 17:27:38 +08:00
|
|
|
* @param entity 游戏实体
|
2025-10-27 09:29:11 +08:00
|
|
|
*/
|
2025-10-31 17:27:38 +08:00
|
|
|
static stop(entity: Entity): void {
|
|
|
|
|
const runtime = entity.getComponent(BehaviorTreeRuntimeComponent);
|
|
|
|
|
if (runtime) {
|
|
|
|
|
runtime.isRunning = false;
|
|
|
|
|
runtime.resetAllStates();
|
2025-10-27 09:29:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-31 17:27:38 +08:00
|
|
|
* 暂停行为树
|
2025-10-27 09:29:11 +08:00
|
|
|
*
|
2025-10-31 17:27:38 +08:00
|
|
|
* @param entity 游戏实体
|
2025-10-27 09:29:11 +08:00
|
|
|
*/
|
2025-10-31 17:27:38 +08:00
|
|
|
static pause(entity: Entity): void {
|
|
|
|
|
const runtime = entity.getComponent(BehaviorTreeRuntimeComponent);
|
|
|
|
|
if (runtime) {
|
|
|
|
|
runtime.isRunning = false;
|
2025-10-27 09:29:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-31 17:27:38 +08:00
|
|
|
* 恢复行为树
|
2025-10-27 09:29:11 +08:00
|
|
|
*
|
2025-10-31 17:27:38 +08:00
|
|
|
* @param entity 游戏实体
|
2025-10-27 09:29:11 +08:00
|
|
|
*/
|
2025-10-31 17:27:38 +08:00
|
|
|
static resume(entity: Entity): void {
|
|
|
|
|
const runtime = entity.getComponent(BehaviorTreeRuntimeComponent);
|
|
|
|
|
if (runtime) {
|
|
|
|
|
runtime.isRunning = true;
|
|
|
|
|
}
|
2025-10-27 09:29:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-10-31 17:27:38 +08:00
|
|
|
* 重启行为树
|
2025-10-27 09:29:11 +08:00
|
|
|
*
|
2025-10-31 17:27:38 +08:00
|
|
|
* @param entity 游戏实体
|
2025-10-27 09:29:11 +08:00
|
|
|
*/
|
2025-10-31 17:27:38 +08:00
|
|
|
static restart(entity: Entity): void {
|
|
|
|
|
const runtime = entity.getComponent(BehaviorTreeRuntimeComponent);
|
|
|
|
|
if (runtime) {
|
|
|
|
|
runtime.resetAllStates();
|
|
|
|
|
runtime.isRunning = true;
|
|
|
|
|
}
|
2025-10-27 09:29:11 +08:00
|
|
|
}
|
|
|
|
|
}
|