之前运行的行为树,都是一颗总树,那么实际上会有很多的总树,因此需要对行为树进行管理。

BTBehaviorManager.lua

 BTBehaviorManager = {};

 local this = BTBehaviorManager;
this.isEachFrameCall = false;--是否是每帧调用
this.printTreeStr = "";
this.trees = {}; function this.RunTree()
if (isEachFrameCall) then
else
while (true) do
local isAllFinish = this.OnUpdate();
if (isAllFinish) then
break;
end
end
end
end function this.OnUpdate()
local count = ; for i=,#this.trees do
local tree = this.trees[i];
if (tree.executionStatus == BTTaskStatus.Inactive) then --第一次执行
print("第一次执行:" .. tree.name);
tree:OnUpdate();
elseif (tree.executionStatus == BTTaskStatus.Running) then --第二次以及以后执行
print("第二次以及以后执行:" .. tree.name);
tree:OnUpdate();
else
count = count + ;
end
end if (count == #this.trees) then
return true;
else
return false;
end
end --添加行为树
function this.AddTree(task)
table.insert(this.trees, task);
end --深度优先,打印树结构
function this.PrintTree(task)
this.printTreeStr = "";
this.AddToPrintTreeStr(task);
print(this.printTreeStr);
end function this.AddToPrintTreeStr(task)
local taskType = task.taskType; this.printTreeStr = this.printTreeStr .. task:ToString() .. "\n"; if (taskType == BTTaskType.Root) then
this.AddToPrintTreeStr(task.startTask);
elseif (taskType == BTTaskType.Composite or taskType == BTTaskType.Decorator) then
for i=,#task.childTasks do
this.AddToPrintTreeStr(task.childTasks[i]);
end
else end
end

说明:

1.因为是运行在Sublime环境下的,所以这里使用while循环模拟每帧调用

2.关于AddTree和OnUpdate是否会冲突的问题。AddTree会增加trees的长度,但是在lua中for的三个表达式在循环开始前一次性求值,以后不再进行求值。比如#this.trees这个表达式只会在循环开始前执行一次,其结果用在后面的循环中。也就是说,如果在for循环过程中执行了AddTree方法,新增的元素会添加在后面,不会影响for循环的。

测试:

1.多行为树执行

TestBehaviorTree.lua

 TestBehaviorTree = BTBehaviorTree:New();

 local this = TestBehaviorTree;
this.name = "TestBehaviorTree"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o:Init();
return o;
end function this:Init()
local parallel = BTParallel:New();
local action = self:GetBTActionUniversal();
local action2 = self:GetBTActionUniversal2(); self:SetStartTask(parallel); parallel:AddChild(action);
parallel:AddChild(action2);
end function this:GetBTActionUniversal()
local count = ;
local a = function ()
if (count <= ) then
count = count + ;
print("");
return BTTaskStatus.Running;
else
return BTTaskStatus.Success;
end
end
local universal = BTActionUniversal:New(nil, a);
return universal;
end function this:GetBTActionUniversal2()
local universal = BTActionUniversal:New(nil, function ()
return BTTaskStatus.Success;
end);
return universal;
end

TestBehaviorTree2.lua

 TestBehaviorTree2 = BTBehaviorTree:New();

 local this = TestBehaviorTree2;
this.name = "TestBehaviorTree2"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o:Init();
return o;
end function this:Init()
local repeater = BTRepeater:New();
local sequence = BTSequence:New();
local log = BTLog:New("This is a other tree!!!");
local log2 = BTLog:New("This is a other tree 2!!!"); self:SetStartTask(repeater); repeater:AddChild(sequence); sequence:AddChild(log);
sequence:AddChild(log2);
end

TestMain.lua

 require "BehaviorTree/Core/Init"
require "BehaviorTree/Test/TestBehaviorTree"
require "BehaviorTree/Test/TestBehaviorTree2" local tree = TestBehaviorTree:New();
local tree2 = TestBehaviorTree2:New();
tree.name = "tree";
tree2.name = "tree2";
BTBehaviorManager.AddTree(tree);
BTBehaviorManager.AddTree(tree2);
BTBehaviorManager.RunTree();

输出如下:

2.for循环中添加行为树

TestBehaviorTree3.lua

 TestBehaviorTree3 = BTBehaviorTree:New();

 local this = TestBehaviorTree3;
this.name = "TestBehaviorTree3"; function this:New()
local o = {};
setmetatable(o, self);
self.__index = self;
o:Init();
return o;
end function this:Init()
local repeater = BTRepeater:New();
local log = BTLog:New("This is TestBehaviorTree3 tree!!!"); self:SetStartTask(repeater); repeater:AddChild(log);
end

BTBehaviorManager.lua

 BTBehaviorManager = {};

 local this = BTBehaviorManager;
this.isEachFrameCall = false;--是否是每帧调用
this.printTreeStr = "";
this.trees = {}; function this.RunTree()
if (isEachFrameCall) then
else
while (true) do
local isAllFinish = this.OnUpdate();
if (isAllFinish) then
break;
end
end
end
end local temp = false; function this.OnUpdate()
local count = ; for i=,#this.trees do
if (not temp) then
temp = true;
require "BehaviorTree/Test/TestBehaviorTree3"
local tree3 = TestBehaviorTree3:New();
tree3.name = "tree3";
this.AddTree(tree3);
end local tree = this.trees[i];
if (tree.executionStatus == BTTaskStatus.Inactive) then --第一次执行
print("第一次执行:" .. tree.name);
tree:OnUpdate();
elseif (tree.executionStatus == BTTaskStatus.Running) then --第二次以及以后执行
print("第二次以及以后执行:" .. tree.name);
tree:OnUpdate();
else
count = count + ;
end
end print("------------------------------"); if (count == #this.trees) then
return true;
else
return false;
end
end --添加行为树
function this.AddTree(task)
table.insert(this.trees, task);
end --深度优先,打印树结构
function this.PrintTree(task)
this.printTreeStr = "";
this.AddToPrintTreeStr(task);
print(this.printTreeStr);
end function this.AddToPrintTreeStr(task)
local taskType = task.taskType; this.printTreeStr = this.printTreeStr .. task:ToString() .. "\n"; if (taskType == BTTaskType.Root) then
this.AddToPrintTreeStr(task.startTask);
elseif (taskType == BTTaskType.Composite or taskType == BTTaskType.Decorator) then
for i=,#task.childTasks do
this.AddToPrintTreeStr(task.childTasks[i]);
end
else end
end

输出如下。可以看到,在第一帧时,添加了行为树tree3,但是并不影响tree和tree2的执行,而在第二帧以及以后的帧时,tree3也开始执行了。

[Unity插件]Lua行为树(十二):行为树管理的更多相关文章

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

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

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

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

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

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

  4. [Unity插件]Lua行为树(一):BehaviorDesigner源码分析

    BehaviorDesigner是Unity上的一款行为树插件,不过这个插件是用C#编写的,编写出来的行为树也是依赖于C#的,不利于热更,所以有必要写一个lua版本的. 首先下载BehaviorDes ...

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

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

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

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

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

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

  8. [Unity插件]Lua行为树(十一):组合节点Parallel

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

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

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

随机推荐

  1. [转载] C# DllImport用法和路径问题

    DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息. DllImport属性应用于方法,要求最少 ...

  2. CentOS7安装部署zabbix3.4操作记录

    CentOS7安装部署zabbix3.4操作记录 1.安装前准备 1.1 查看centos的系统版本 [root@zabbix ~]# cat /etc/redhat-release CentOS L ...

  3. golang里json的处理配合struct是相当方便

    type Feed struct { Name string `json:"site"` URI string `json:"link"` Type strin ...

  4. Debian 7.0(Wheezy) 安装配置笔记

    1. 下载光盘镜像 ftp://debian.ustc.edu.cn/debian-cd/7.1.0/amd64/iso-dvd/ [2012.3.25]  1.1 去官网 http://cdimag ...

  5. Linux LVM 简单操作

    查看当前磁盘分区情况fdisk -l 磁盘分区fdisk /dev/sdb# 可能用到的Type :# 8e Linux LVM# fd Linux raid auto 创建PVpvcreate /d ...

  6. SpringSecurity-ConcurrentSessionFilter的作用

    ConcurrentSessionFilter主要有两个功能: (1)每次request时调用SessionRegistry的refreshLastRequest(String)更新session的最 ...

  7. 【linux】安装docker

    硬件Centos6.9 x86_64 1.查看centos内核,uname -r 目前,CentOS 仅发行版本中的内核支持 Docker. Docker 运行在 CentOS 7 上,要求系统为64 ...

  8. Ngui Tween 组合动画 group

    使用NGUI的Tween做补间动画,难免会涉及组合各种Tween.最常用的就是 Scale+Alpha组合 做淡入淡出了.那么如何控制 播放完一个Tween 后在 播放另一个Tween呢? 利用del ...

  9. maven不同环境的profile配置

    1.开发的时候经常需要加载不同的环境,比如本地开发环境dev,生产环境product.如果需要手动去修改的话就太麻烦了,自己实现了maven资源替换,然后多环境下的配置文件管理的demo,在此贴出来. ...

  10. spring boot (入门简介 demo)

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过 ...