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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b98bf7be91dd32a369c4d44b7b24fa889a795eecce544e2f9ff1e061dcd9d1b9
4
- data.tar.gz: 79a1c994205b9161b1ca79529740a465e189dbab64d5819617dd42541753698b
3
+ metadata.gz: fa6285d17ba7200bca3e11fb9cdb7c6fb538a7461f8cb2d9c4496e5a1f885a90
4
+ data.tar.gz: 196808930dec23a365d3825da90c20e85e3a4f780a3857b67f768f718595a4fb
5
5
  SHA512:
6
- metadata.gz: 8d9b5140ad575eff28249a80b3ef03ed56b5d21fabce0a2c941122697832d5f323de817b4176de3a7d9896ac5dfbf450ef39742e5e5f6ac925c1fa2d3a76ffac
7
- data.tar.gz: 948a0acfc738d0536eb3c8ea6414a421f4d9faa86539e0d7e02b9991b8594ea5477fa33dcb04799ef9dec8118c017117852b714ce2a4d367bf1d38529282d100
6
+ metadata.gz: f1cd5d323dfd2db92b4d9ac41b12cabf14ac9a56a14a317db64f32b23513f92ce0ce31285881d3e2feb0560603627891d392534ffec63ba378303eae244738fc
7
+ data.tar.gz: '01139c43255bb5130e327d2152b42964266adac83752314de0923898f2eb543b438e306a0027321b0139b120c2ac06fb6f0aa17138e8f2957df0b70fc36001f8'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- behave_fun (0.1.0)
4
+ behave_fun (0.1.1)
5
5
  activesupport (~> 6.0.0)
6
6
  zeitwerk (~> 2.3.0)
7
7
 
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
- $ bundle install
22
+ $ bundle install
21
23
 
22
24
  Or install it yourself as:
23
25
 
24
- $ gem install behave_fun
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
- # ruby dsl
32
- tree = BehaveFun.build_tree { success }
33
- # from hash
34
- tree = BehaveFun.build_tree_from_hash(type: :success)
35
- # from json
36
- tree = BehaveFun.build_tree_from_json(json_string)
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
- # write_spec, write_code, run_spec, git_push and release_gem are customized tasks
43
- tree = BehaveFun.build_tree {
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
- until_success {
46
- sequence {
47
- write_spec
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
- # a task that increase data by 1, always success
62
- class Counter < BehaveFun::Task
63
- def execute
64
- tree.data += 1
65
- success
66
- end
67
-
68
- add_to_task_builder
69
- end
70
-
71
- # a task that detect tree data is even or not
72
- class IsCounterEven < BehaveFun::Task
73
- def execute
74
- tree.data.even? ? success : fail
75
- end
76
-
77
- add_to_task_builder
78
- end
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
- tree.data = { ... } # provide your data (context) for the tree
85
- tree.run
86
- tree.status # :running, :succeeded or :failed
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
- status = tree.dump_status
93
- tree.restore_status(status)
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.
@@ -2,8 +2,8 @@ module BehaveFun
2
2
  class Task
3
3
  include TaskSerializer
4
4
 
5
- attr_accessor :guard
6
- attr_reader :tree, :control, :children, :params, :status
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 control=(control)
23
- @control = control
24
- @tree = control.tree
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.data = tree.data
74
+ guard.context = context
75
75
  guard.reset
76
76
  guard.run
77
77
 
@@ -1,6 +1,6 @@
1
1
  module BehaveFun
2
2
  class Tree < Task
3
- attr_accessor :root, :data
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
@@ -1,3 +1,3 @@
1
1
  module BehaveFun
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
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.0
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-15 00:00:00.000000000 Z
11
+ date: 2020-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk