-
Notifications
You must be signed in to change notification settings - Fork 45
/
test.lua
145 lines (119 loc) · 3.1 KB
/
test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
local behavior_tree = require "behavior3.behavior_tree"
local behavior_node = require "behavior3.behavior_node"
local process = require "example.process"
process.Listen = {
run = function()
print("Listen not defined")
return "success"
end
}
behavior_node.process(process)
local json = require "json"
local function load_tree(path)
local file, err = io.open(path, 'r')
assert(file, err)
local str = file:read('*a')
file:close()
return json.decode(str)
end
local monster = {
hp = 100,
x = 200,
y = 0,
}
local hero = {
hp = 100,
x = 0,
y = 0,
}
local ctx = {
time = 0,
avatars = {monster, hero},
}
function ctx:find(func)
local list = {}
for _, v in pairs(ctx.avatars) do
if func(v) then
list[#list+1] = v
end
end
return list
end
local function test_hero()
print("=================== test hero ========================")
local btree = behavior_tree.new("hero", load_tree("workspace/trees/hero.json"), {
ctx = ctx,
owner = hero,
})
-- 移动到目标并攻击
btree:run()
btree:run()
btree:run()
btree:run()
btree:run()
btree:run()
-- 后摇
btree:run()
btree:interrupt()
btree:run()
ctx.time = 20
btree:run()
end
test_hero()
local function test_moster()
print("=================== test monster ========================")
local btree = behavior_tree.new("monster", load_tree("workspace/trees/monster.json"), {
ctx = ctx,
owner = monster,
})
monster.hp = 100
btree:run()
monster.hp = 20
btree:run()
ctx.time = 40
btree:run()
btree:run()
end
test_moster()
local function test_repeat_until_success()
print("=================== test repeat until success ========================")
local btree = behavior_tree.new("repeat-until-success", load_tree("workspace/trees/test-repeat-until-success.json"), {
ctx = ctx,
})
for i = 1, 7 do
ctx.time = ctx.time + 1
btree:run()
end
end
test_repeat_until_success()
local function test_repeat_until_fail()
print("=================== test repeat until fail ========================")
local btree = behavior_tree.new("repeat-until-fail", load_tree("workspace/trees/test-repeat-until-failure.json"), {
ctx = ctx,
})
for i = 1, 7 do
ctx.time = ctx.time + 1
btree:run()
end
end
test_repeat_until_fail()
local function test_repeat_until_fail()
print("=================== test parallel ========================")
local btree = behavior_tree.new("parallel", load_tree("workspace/trees/test-parallel.json"), {
ctx = ctx,
})
for i = 1, 6 do
ctx.time = ctx.time + 1
btree:run()
end
end
test_repeat_until_fail()
local function test_parallel_with_wait()
print("=================== test parallel with wait ========================")
local btree = behavior_tree.new("parallel-with-wait", load_tree("workspace/trees/test-parallel-with-wait.json"), {
})
for i = 1, 8 do
btree:run()
end
end
test_parallel_with_wait()