import { _ECommonState } from "../Data/CommonDataType"; import BasicObject from "./BasicObject"; import BasicTreeBehaviorNode from "./BasicTreeBehaviorNode"; import BasicTreeControlNode from "./BasicTreeControlNode"; /** * 基础行为树前提节点 - 逻辑层 */ export default abstract class BasicTreePreconditionNode extends BasicObject { /**优先级 */ protected _priority: number; /**前提节点 */ protected _precondition: BasicTreePreconditionNode; constructor (priority: number = 0, precondition?: BasicTreePreconditionNode) { super(); this._priority = priority; if (precondition != null) { this._precondition = precondition; this._precondition.priority = this._priority; this.addComponentIntance(this._precondition); } } public set priority (priority: number) { this._priority = priority; } public get priority (): number { return this._priority; } /**设置前提节点 */ public set precondition (precondition: BasicTreePreconditionNode) { if (this._precondition != null) { this.removeComponentIntance(this._precondition, true); } this._precondition = precondition; this.addComponentIntance(this._precondition); } /**获得前提节点 */ public get precondition (): BasicTreePreconditionNode { return this._precondition; } /**获得前提结果 */ private getPreconditionResule (behavior: any, ...args: any): _ECommonState { return (this._precondition == null)? _ECommonState.Success:this._precondition.enter(behavior, ...args); } /**进入前提 */ public enter (behavior: any, ...args: any): _ECommonState { let parent = (this.parent.parent as BasicTreeControlNode); let compatibilityChild: Array = []; if (parent.behaviorTreeConfig != null) { compatibilityChild = parent.behaviorTreeConfig.compatibilityChild[(this.parent as BasicTreeBehaviorNode).behaviorType]; } let control: BasicTreeBehaviorNode = parent.getChild(behavior); if (control != null) { // 通过优先级判断 if (this.priority <= control.precondition.priority || (compatibilityChild != null && compatibilityChild.includes(behavior) == true)) { if (this.getPreconditionResule(behavior, ...args) == _ECommonState.Success && this._enter(behavior, ...args) == _ECommonState.Success) { return _ECommonState.Success; } } } else { return _ECommonState.Success; } return _ECommonState.Fail; } /**进入前提 */ protected abstract _enter (behavior: any, ...args: any): _ECommonState; }