behave_fun 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +44 -42
- data/lib/behave_fun/task.rb +6 -6
- data/lib/behave_fun/tree.rb +6 -1
- data/lib/behave_fun/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa6285d17ba7200bca3e11fb9cdb7c6fb538a7461f8cb2d9c4496e5a1f885a90
|
4
|
+
data.tar.gz: 196808930dec23a365d3825da90c20e85e3a4f780a3857b67f768f718595a4fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1cd5d323dfd2db92b4d9ac41b12cabf14ac9a56a14a317db64f32b23513f92ce0ce31285881d3e2feb0560603627891d392534ffec63ba378303eae244738fc
|
7
|
+
data.tar.gz: '01139c43255bb5130e327d2152b42964266adac83752314de0923898f2eb543b438e306a0027321b0139b120c2ac06fb6f0aa17138e8f2957df0b70fc36001f8'
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
BehaveFun is a behavior tree library for Ruby.
|
4
4
|
|
5
|
+
Inspired by [gdx-ai's behavior trees](https://github.com/libgdx/gdx-ai/wiki/Behavior-Trees).
|
6
|
+
|
5
7
|
Main features:
|
6
8
|
|
7
9
|
* Build behavior tree from Ruby and JSON.
|
@@ -17,80 +19,80 @@ gem 'behave_fun'
|
|
17
19
|
|
18
20
|
And then execute:
|
19
21
|
|
20
|
-
|
22
|
+
$ bundle install
|
21
23
|
|
22
24
|
Or install it yourself as:
|
23
25
|
|
24
|
-
|
26
|
+
$ gem install behave_fun
|
25
27
|
|
26
28
|
## Usage
|
27
29
|
|
28
30
|
To build a behavior tree:
|
29
31
|
|
30
32
|
``` ruby
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
# ruby dsl
|
34
|
+
tree = BehaveFun.build_tree { success }
|
35
|
+
# from hash
|
36
|
+
tree = BehaveFun.build_tree_from_hash(type: :success)
|
37
|
+
# from json
|
38
|
+
tree = BehaveFun.build_tree_from_json(json_string)
|
37
39
|
```
|
38
40
|
|
39
41
|
To build a complex behavior tree:
|
40
42
|
|
41
43
|
``` ruby
|
42
|
-
|
43
|
-
|
44
|
+
# write_spec, write_code, run_spec, git_push and release_gem are customized tasks
|
45
|
+
tree = BehaveFun.build_tree {
|
46
|
+
sequence {
|
47
|
+
until_success {
|
44
48
|
sequence {
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
write_code
|
49
|
-
run_spec
|
50
|
-
}
|
51
|
-
}
|
52
|
-
git_push
|
53
|
-
release_gem
|
49
|
+
write_spec
|
50
|
+
write_code
|
51
|
+
run_spec
|
54
52
|
}
|
55
53
|
}
|
54
|
+
git_push
|
55
|
+
release_gem
|
56
|
+
}
|
57
|
+
}
|
56
58
|
```
|
57
59
|
|
58
60
|
To create customized task, create a class that extends `BehaveFun::Task`. Don't forget call `running` `success` or `fail` in `#execute` method at the end.
|
59
61
|
|
60
62
|
``` ruby
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
63
|
+
# a task that increase data by 1, always success
|
64
|
+
class Counter < BehaveFun::Task
|
65
|
+
def execute
|
66
|
+
context[:counter] += 1
|
67
|
+
success
|
68
|
+
end
|
69
|
+
|
70
|
+
add_to_task_builder
|
71
|
+
end
|
72
|
+
|
73
|
+
# a task that detect tree data is even or not
|
74
|
+
class IsCounterEven < BehaveFun::Task
|
75
|
+
def execute
|
76
|
+
context[:counter].even? ? success : fail
|
77
|
+
end
|
78
|
+
|
79
|
+
add_to_task_builder
|
80
|
+
end
|
79
81
|
```
|
80
82
|
|
81
83
|
To run a tree:
|
82
84
|
|
83
85
|
``` ruby
|
84
|
-
|
85
|
-
|
86
|
-
|
86
|
+
tree.context = { ... } # provide your context (data) for the tree
|
87
|
+
tree.run
|
88
|
+
tree.status # :running, :succeeded or :failed
|
87
89
|
```
|
88
90
|
|
89
91
|
To dump and restore status:
|
90
92
|
|
91
93
|
``` ruby
|
92
|
-
|
93
|
-
|
94
|
+
status = tree.dump_status # a hash that contains entire tree's status
|
95
|
+
tree.restore_status(status)
|
94
96
|
```
|
95
97
|
|
96
98
|
For more detail, see spec examples.
|
data/lib/behave_fun/task.rb
CHANGED
@@ -2,8 +2,8 @@ module BehaveFun
|
|
2
2
|
class Task
|
3
3
|
include TaskSerializer
|
4
4
|
|
5
|
-
attr_accessor :guard
|
6
|
-
attr_reader :
|
5
|
+
attr_accessor :control, :guard
|
6
|
+
attr_reader :context, :children, :params, :status
|
7
7
|
|
8
8
|
def initialize(**params)
|
9
9
|
@params = params
|
@@ -19,9 +19,9 @@ module BehaveFun
|
|
19
19
|
BehaveFun::TaskBuilder.add_task_type(self, name: name)
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
23
|
-
@
|
24
|
-
|
22
|
+
def context=(context)
|
23
|
+
@context = context
|
24
|
+
children.each { _1.context = context }
|
25
25
|
end
|
26
26
|
|
27
27
|
def running
|
@@ -71,7 +71,7 @@ module BehaveFun
|
|
71
71
|
def guard_passed?
|
72
72
|
return true unless guard
|
73
73
|
|
74
|
-
guard.
|
74
|
+
guard.context = context
|
75
75
|
guard.reset
|
76
76
|
guard.run
|
77
77
|
|
data/lib/behave_fun/tree.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module BehaveFun
|
2
2
|
class Tree < Task
|
3
|
-
attr_accessor :root
|
3
|
+
attr_accessor :root
|
4
4
|
|
5
5
|
def root=(root)
|
6
6
|
@root = root
|
@@ -11,6 +11,11 @@ module BehaveFun
|
|
11
11
|
self
|
12
12
|
end
|
13
13
|
|
14
|
+
def context=(context)
|
15
|
+
@context = context
|
16
|
+
@root.context = context
|
17
|
+
end
|
18
|
+
|
14
19
|
def execute
|
15
20
|
root.run
|
16
21
|
end
|
data/lib/behave_fun/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: behave_fun
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ayaya zhao
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-05-
|
11
|
+
date: 2020-05-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: zeitwerk
|