/** @format */ import {ComBehaviorTree} from '../components/ComBehaviorTree' import {ECSSystem} from '../lib/ECSSystem' import {BT} from '../../behaviorTree/BehaviorTree' import {FightWorld} from '../worlds/FightWorld' import {ComCocosNode} from '../components/ComCocosNode' import {GenFilterKey} from '../lib/ECSComponent' import {Log} from '../../utils/LogUtils' const FILTER_BEHAVIORTREE = GenFilterKey([ComBehaviorTree, ComCocosNode]) export class SysBehaviorTree extends ECSSystem { private _context: BT.ExecuteContext /** 连接 */ public onAdd(world: FightWorld): void { this._context = new BT.ExecuteContext() this._context.init(this, world) } /** 断开连接 */ public onRemove(world: FightWorld): void {} /** 添加实体 */ public onEntityEnter(world: FightWorld, entity: number): void {} /** */ public onEntityLeave(world: FightWorld, entity: number): void {} /** 更新 */ public onUpdate(world: FightWorld, dt: number): void { this._context.dt = dt world.getFilter(FILTER_BEHAVIORTREE).walk((entity: number) => { let comBehavior = world.getComponent(entity, ComBehaviorTree) if (!comBehavior.root) return this._context.set(entity, dt, comBehavior.bb) if (comBehavior.root.state !== BT.NodeState.Executing) { this.onEnterBTNode(comBehavior.root, this._context) } else { this.updateBTNode(comBehavior.root, this._context) } return false }) } /** 进入节点 */ public onEnterBTNode(node: BT.NodeBase, context: BT.ExecuteContext) { let handler = BT.NodeHandlers[node.type] handler.onEnter(node, context) } /** 更新节点状态 */ public updateBTNode(node: BT.NodeBase, context: BT.ExecuteContext) { let handler = BT.NodeHandlers[node.type] handler.onUpdate(node, context) } public canExecuteBTNode(node: BT.NodeBase, context: BT.ExecuteContext): boolean { return true } }