首页 > 其他 > 详细

Behavior Tree 用 Lua 实现一个最简行为树

时间:2017-06-23 09:33:58      阅读:529      评论:0      收藏:0      [点我收藏+]

 

 1 local SELECTOR = 1
 2 local SEQUENCE = 2
 3 local CONDITION = 3
 4 local ACTION = 4
 5 
 6 local function Traverse(node, ...)
 7     local t = node.type
 8     if t == SELECTOR then
 9         for i=1, #node do
10             if Traverse(node[i], ...) then
11                 return true
12             end
13         end
14         return false
15     elseif t == SEQUENCE then
16         for i=1, #node do
17             if not Traverse(node[i], ...) then
18                 return false
19             end
20         end
21         return true
22     elseif t == CONDITION then
23         for i=1, #node do
24             if not node[i](...) then
25                 return false
26             end
27         end
28         return true
29     elseif t == ACTION then
30         for i=1, #node do
31             node[i](...)
32         end
33         return true
34     end
35 end
36 
37 local root =
38 {
39     type = SELECTOR,
40     {
41         type = SEQUENCE,
42         {
43             type = CONDITION,
44             function(i,j) return math.random() > i end,
45             function(i,j) return math.random() < j end,
46         },
47         {
48             type = ACTION,
49             function() print("random") end,
50         },
51     },
52     {
53         type = ACTION,
54         function() print("idle") end,
55     },
56 }
57 
58 local input1 = 0.2
59 local input2 = 0.7
60 Traverse(root, input1, input2)

 

有没有比 C++ 代码简单一万倍,有没有? 

 

Behavior Tree 用 Lua 实现一个最简行为树

原文:http://www.cnblogs.com/hangj/p/7067945.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!