BasicTreePreconditionNode.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { _ECommonState } from "../Data/CommonDataType";
  2. import BasicObject from "./BasicObject";
  3. import BasicTreeBehaviorNode from "./BasicTreeBehaviorNode";
  4. import BasicTreeControlNode from "./BasicTreeControlNode";
  5. /**
  6. * 基础行为树前提节点 - 逻辑层
  7. */
  8. export default abstract class BasicTreePreconditionNode extends BasicObject {
  9. /**优先级 */
  10. protected _priority: number;
  11. /**前提节点 */
  12. protected _precondition: BasicTreePreconditionNode;
  13. constructor (priority: number = 0, precondition?: BasicTreePreconditionNode) {
  14. super();
  15. this._priority = priority;
  16. if (precondition != null) {
  17. this._precondition = precondition;
  18. this._precondition.priority = this._priority;
  19. this.addComponentIntance(this._precondition);
  20. }
  21. }
  22. public set priority (priority: number) {
  23. this._priority = priority;
  24. }
  25. public get priority (): number {
  26. return this._priority;
  27. }
  28. /**设置前提节点 */
  29. public set precondition (precondition: BasicTreePreconditionNode) {
  30. if (this._precondition != null) {
  31. this.removeComponentIntance(this._precondition, true);
  32. }
  33. this._precondition = precondition;
  34. this.addComponentIntance(this._precondition);
  35. }
  36. /**获得前提节点 */
  37. public get precondition (): BasicTreePreconditionNode {
  38. return this._precondition;
  39. }
  40. /**获得前提结果 */
  41. private getPreconditionResule (behavior: any, ...args: any): _ECommonState {
  42. return (this._precondition == null)? _ECommonState.Success:this._precondition.enter(behavior, ...args);
  43. }
  44. /**进入前提 */
  45. public enter (behavior: any, ...args: any): _ECommonState {
  46. let parent = (this.parent.parent as BasicTreeControlNode);
  47. let compatibilityChild: Array<string> = [];
  48. if (parent.behaviorTreeConfig != null) {
  49. compatibilityChild = parent.behaviorTreeConfig.compatibilityChild[(this.parent as BasicTreeBehaviorNode).behaviorType];
  50. }
  51. let control: BasicTreeBehaviorNode = parent.getChild(behavior);
  52. if (control != null) {
  53. // 通过优先级判断
  54. if (this.priority <= control.precondition.priority || (compatibilityChild != null && compatibilityChild.includes(behavior) == true)) {
  55. if (this.getPreconditionResule(behavior, ...args) == _ECommonState.Success && this._enter(behavior, ...args) == _ECommonState.Success) {
  56. return _ECommonState.Success;
  57. }
  58. }
  59. }
  60. else {
  61. return _ECommonState.Success;
  62. }
  63. return _ECommonState.Fail;
  64. }
  65. /**进入前提 */
  66. protected abstract _enter (behavior: any, ...args: any): _ECommonState;
  67. }