Parallel节点类似Sequence节点,不同在于Parallel会每帧执行所有的节点。当所有节点返回成功时返回成功,当其中一个节点返回失败时,返回失败并且结束所有的子节点运行。

例如说,给Sequence节点插入一个不断返回Running的行为节点,那么就会造成后面的子节点无法执行,而对于Parallel来说,是不会存在这种阻塞情况的。

Parallel.cs

  1. namespace BehaviorDesigner.Runtime.Tasks
  2. {
  3. [TaskDescription("Similar to the sequence task, the parallel task will run each child task until a child task returns failure. " +
  4. "The difference is that the parallel task will run all of its children tasks simultaneously versus running each task one at a time. " +
  5. "Like the sequence class, the parallel task will return success once all of its children tasks have return success. " +
  6. "If one tasks returns failure the parallel task will end all of the child tasks and return failure.")]
  7. [HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=27")]
  8. [TaskIcon("{SkinColor}ParallelIcon.png")]
  9. public class Parallel : Composite
  10. {
  11. // The index of the child that is currently running or is about to run.
  12. private int currentChildIndex;
  13. // The task status of every child task.
  14. private TaskStatus[] executionStatus;
  15.  
  16. public override void OnAwake()
  17. {
  18. // Create a new task status array that will hold the execution status of all of the children tasks.
  19. executionStatus = new TaskStatus[children.Count];
  20. }
  21.  
  22. public override void OnChildStarted(int childIndex)
  23. {
  24. // One of the children has started to run. Increment the child index and set the current task status of that child to running.
  25. currentChildIndex++;
  26. executionStatus[childIndex] = TaskStatus.Running;
  27. }
  28.  
  29. public override bool CanRunParallelChildren()
  30. {
  31. // This task can run parallel children.
  32. return true;
  33. }
  34.  
  35. public override int CurrentChildIndex()
  36. {
  37. return currentChildIndex;
  38. }
  39.  
  40. public override bool CanExecute()
  41. {
  42. // We can continue executing if we have more children that haven't been started yet.
  43. return currentChildIndex < children.Count;
  44. }
  45.  
  46. public override void OnChildExecuted(int childIndex, TaskStatus childStatus)
  47. {
  48. // One of the children has finished running. Set the task status.
  49. executionStatus[childIndex] = childStatus;
  50. }
  51.  
  52. public override TaskStatus OverrideStatus(TaskStatus status)
  53. {
  54. // Assume all of the children have finished executing. Loop through the execution status of every child and check to see if any tasks are currently running
  55. // or failed. If a task is still running then all of the children are not done executing and the parallel task should continue to return a task status of running.
  56. // If a task failed then return failure. The Behavior Manager will stop all of the children tasks. If no child task is running or has failed then the parallel
  57. // task succeeded and it will return success.
  58. bool childrenComplete = true;
  59. for (int i = ; i < executionStatus.Length; ++i) {
  60. if (executionStatus[i] == TaskStatus.Running) {
  61. childrenComplete = false;
  62. } else if (executionStatus[i] == TaskStatus.Failure) {
  63. return TaskStatus.Failure;
  64. }
  65. }
  66. return (childrenComplete ? TaskStatus.Success : TaskStatus.Running);
  67. }
  68.  
  69. public override void OnConditionalAbort(int childIndex)
  70. {
  71. // Start from the beginning on an abort
  72. currentChildIndex = ;
  73. for (int i = ; i < executionStatus.Length; ++i) {
  74. executionStatus[i] = TaskStatus.Inactive;
  75. }
  76. }
  77.  
  78. public override void OnEnd()
  79. {
  80. // Reset the execution status and the child index back to their starting values.
  81. for (int i = ; i < executionStatus.Length; ++i) {
  82. executionStatus[i] = TaskStatus.Inactive;
  83. }
  84. currentChildIndex = ;
  85. }
  86. }
  87. }

BTParallel.lua

  1. BTParallel = BTComposite:New();
  2.  
  3. local this = BTParallel;
  4. this.name = "BTParallel";
  5.  
  6. function this:New()
  7. local o = {};
  8. setmetatable(o, self);
  9. self.__index = self;
  10. o.childTasks = {};
  11. o.executionStatus = {};
  12. return o;
  13. end
  14.  
  15. function this:OnUpdate()
  16. if (not self:HasChild()) then
  17. return BTTaskStatus.Failure;
  18. end
  19.  
  20. for i=,#self.childTasks do
  21. local childTask = self.childTasks[i];
  22. if (not self.executionStatus[i]) then --第一次执行
  23. self.executionStatus[i] = childTask:OnUpdate();
  24. if (self.executionStatus[i] == BTTaskStatus.Failure) then
  25. return BTTaskStatus.Failure;
  26. end
  27. elseif (self.executionStatus[i] == BTTaskStatus.Running) then --第二次以及以后执行
  28. self.executionStatus[i] = childTask:OnUpdate();
  29. if (self.executionStatus[i] == BTTaskStatus.Failure) then
  30. return BTTaskStatus.Failure;
  31. end
  32. end
  33. end
  34.  
  35. local childrenComplete = true;
  36. for i=,#self.executionStatus do
  37. if (self.executionStatus[i] == BTTaskStatus.Running) then
  38. childrenComplete = false;
  39. break;
  40. end
  41. end
  42. if (childrenComplete) then
  43. return BTTaskStatus.Success;
  44. else
  45. return BTTaskStatus.Running;
  46. end
  47. end

测试如下:

1.BTSequence和BTParallel的对比

BTSequence:

  1. TestBehaviorTree = BTBehaviorTree:New();
  2.  
  3. local this = TestBehaviorTree;
  4. this.name = "TestBehaviorTree";
  5.  
  6. function this:New()
  7. local o = {};
  8. setmetatable(o, self);
  9. self.__index = self;
  10. o:Init();
  11. return o;
  12. end
  13.  
  14. function this:Init()
  15. local sequence = BTSequence:New();
  16. local action = self:GetBTActionUniversal();
  17. local log = BTLog:New("This is log!!!");
  18. log.name = "log";
  19.  
  20. self:SetStartTask(sequence);
  21.  
  22. sequence:AddChild(action);
  23. sequence:AddChild(log);
  24. end
  25.  
  26. function this:GetBTActionUniversal()
  27. local count = ;
  28. local a = function ()
  29. if (count <= ) then
  30. count = count + ;
  31. print("");
  32. return BTTaskStatus.Running;
  33. else
  34. return BTTaskStatus.Success;
  35. end
  36. end
  37. local universal = BTActionUniversal:New(nil, a);
  38. return universal;
  39. end

BTParallel:

  1. TestBehaviorTree = BTBehaviorTree:New();
  2.  
  3. local this = TestBehaviorTree;
  4. this.name = "TestBehaviorTree";
  5.  
  6. function this:New()
  7. local o = {};
  8. setmetatable(o, self);
  9. self.__index = self;
  10. o:Init();
  11. return o;
  12. end
  13.  
  14. function this:Init()
  15. local parallel = BTParallel:New();
  16. local action = self:GetBTActionUniversal();
  17. local log = BTLog:New("This is log!!!");
  18. log.name = "log";
  19.  
  20. self:SetStartTask(parallel);
  21.  
  22. parallel:AddChild(action);
  23. parallel:AddChild(log);
  24. end
  25.  
  26. function this:GetBTActionUniversal()
  27. local count = ;
  28. local a = function ()
  29. if (count <= ) then
  30. count = count + ;
  31. print("");
  32. return BTTaskStatus.Running;
  33. else
  34. return BTTaskStatus.Success;
  35. end
  36. end
  37. local universal = BTActionUniversal:New(nil, a);
  38. return universal;
  39. end

2.BTParallel中返回失败中断执行的情况

  1. TestBehaviorTree = BTBehaviorTree:New();
  2.  
  3. local this = TestBehaviorTree;
  4. this.name = "TestBehaviorTree";
  5.  
  6. function this:New()
  7. local o = {};
  8. setmetatable(o, self);
  9. self.__index = self;
  10. o:Init();
  11. return o;
  12. end
  13.  
  14. function this:Init()
  15. local parallel = BTParallel:New();
  16. local action = self:GetBTActionUniversal();
  17. local action2 = self:GetBTActionUniversal2();
  18.  
  19. self:SetStartTask(parallel);
  20.  
  21. parallel:AddChild(action);
  22. parallel:AddChild(action2);
  23. end
  24.  
  25. function this:GetBTActionUniversal()
  26. local count = ;
  27. local a = function ()
  28. if (count <= ) then
  29. count = count + ;
  30. print("");
  31. return BTTaskStatus.Running;
  32. else
  33. return BTTaskStatus.Success;
  34. end
  35. end
  36. local universal = BTActionUniversal:New(nil, a);
  37. return universal;
  38. end
  39.  
  40. function this:GetBTActionUniversal2()
  41. local universal = BTActionUniversal:New(nil, function ()
  42. return BTTaskStatus.Failure;
  43. end);
  44. return universal;
  45. end

[Unity插件]Lua行为树(十一):组合节点Parallel的更多相关文章

  1. [Unity插件]Lua行为树(三):组合节点Sequence

    Sequence的继承关系如下: Sequence->Composite->ParentTask->Task 上一篇已经实现了简单版本的ParentTask和Task(基于Behav ...

  2. [Unity插件]Lua行为树(十三):装饰节点完善

    之前介绍了组合节点中三大常用的节点:BTSequence.BTSelector和BTParallel,一般来说,这三种就够用了,可以满足很多的需求. 接下来可以完善一下装饰节点,增加几种新的节点. 1 ...

  3. [Unity插件]Lua行为树(七):行为树嵌套

    在上一篇的基础上,可以测试下行为树的嵌套,所谓的行为树嵌套,就是在一棵行为树下的某一个分支,接入另一棵行为树. 以下面这棵行为树为例: TestBehaviorTree2.lua TestBehavi ...

  4. [Unity插件]Lua行为树(六):打印树结构

    经过前面的文章,已经把行为树中的四种基本类型节点介绍了下.接下来可以整理一下,打印一下整棵行为树.注意点如下: 1.可以把BTBehaviorTree也当作一种节点,这样就可以方便地进行行为树嵌套了 ...

  5. [Unity插件]Lua行为树(二):树结构

    参考链接:https://blog.csdn.net/u012740992/article/details/79366251 在行为树中,有四种最基本的节点,其继承结构如下: Action->T ...

  6. [Unity插件]Lua行为树(四):条件节点和行为节点

    条件节点和行为节点,这两种节点本身的设计比较简单,项目中编写行为树节点一般就是扩展这两种节点,而Decorator和Composite节点只需要使用内置的就足够了. 它们的继承关系如下: Condit ...

  7. [Unity插件]Lua行为树(十):通用行为和通用条件节点

    在行为树中,需要扩展的主要是行为节点和条件节点.一般来说,每当要创建一个节点时,就要新建一个节点文件.而对于一些简单的行为节点和条件节点,为了去掉新建文件的过程,可以写一个通用版本的行为节点和条件节点 ...

  8. [Unity插件]Lua行为树(九):条件节点调整

    先看一下之前的条件节点是怎么设计的: BTConditional.lua BTConditional = BTTask:New(); local this = BTConditional; this. ...

  9. [Unity插件]Lua行为树(八):行为节点扩展

    先看一下之前的行为节点是怎么设计的: BTAction.lua BTAction = BTTask:New(); local this = BTAction; this.taskType = BTTa ...

随机推荐

  1. RedHat6.5安装单机flume1.6

    版本号: RedHat6.5   JDK1.8   apache-flume-1.6.0 1.apache-flume-1.6.0-bin.tar.gz 下载 官网下载地址:http://archiv ...

  2. Spring Cloud(Dalston.SR5)--Eureka 注册中心搭建

    基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务治理可以说是微服务架构中最为核心和基础的模块,他主要用来实现各个微服务实例的自动化注册与发现 服务注册:在 ...

  3. 【java_需阅读】Java中static关键字用法总结

    1.     静态方法 通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法 声明为static的方法有以下几条限制: · 它们仅能调用其他的static 方法. · 它 ...

  4. 在Win32程序中显示Dos调试窗口

    在很多程序中,都可以看到程序运行中,会有一个Dos窗口,实时显示一些运行信息,这里就告诉大家是如何实现的,我们做个简单的,其实对控制台的操作还有很多,有兴趣的可以去查资料. 用到的API函数如下: / ...

  5. XE5 Android 开发数据访问server端[转]

    建立一个webservices  stand-alone vcl application 作为手机访问的服务端 1.new->other->webservices 2.选择 stand-a ...

  6. 初识React:使用React完成Hello World程序

    正式学习React之前,通过一个简单的Hello Word程序来感受一下. <!DOCTYPE html> <html lang="zh-cn"> < ...

  7. java锁在等待唤醒机制中作用

    等待的线程放在线程池wait().notify().notifyall()都使用在同步中,因为要对持有监视器(锁)的线程操作.所以要使用在同步中,因为只有同步才具有锁. 为什么这些操作的线程的方法要定 ...

  8. 黄聪:微信小程序(应用号)资源汇总整理(转)

    微信小应用资源汇总整理 开源项目 WeApp - 微信小程序版的微信 wechat-weapp-redux-todos - 微信小程序集成Redux实现的Todo list wechat-weapp- ...

  9. VS2012 创建的entityframework 4.1版本

    起因:部署网站提示错误“Method not found: 'Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullRe ...

  10. 制作OpenStack云平台centos6.5镜像

    创建虚拟镜像 # qemu-img create -f raw Cloud_Centos6.5_64bit.img 10G [root@localhost ~]# ll /opt/CentOS-6.5 ...