diff --git a/bonsai/src/bt.rs b/bonsai/src/bt.rs
index 8b03709..74f091c 100644
--- a/bonsai/src/bt.rs
+++ b/bonsai/src/bt.rs
@@ -1,7 +1,7 @@
use std::fmt::Debug;
use crate::visualizer::NodeType;
-use crate::{ActionArgs, Behavior, State, Status, UpdateEvent};
+use crate::{state::State, ActionArgs, Behavior, Status, UpdateEvent};
use petgraph::dot::{Config, Dot};
use petgraph::Graph;
@@ -14,7 +14,7 @@ use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BT {
/// constructed behavior tree
- pub state: State,
+ state: State,
/// keep the initial state
initial_behavior: Behavior,
/// The data storage shared by all nodes in the tree. This is generally
@@ -35,7 +35,8 @@ impl BT {
}
}
- /// Updates the cursor that tracks an event.
+ /// Updates the cursor that tracks an event. If the behavior tree previously
+ /// finished (either success or failure), it will automatically be restarted.
///
/// The action need to return status and remaining delta time.
/// Returns status and the remaining delta time.
@@ -53,7 +54,13 @@ impl BT {
E: UpdateEvent,
F: FnMut(ActionArgs, &mut B) -> (Status, f64),
{
- self.state.tick(e, &mut self.bb, f)
+ match self.state.tick(e, &mut self.bb, f) {
+ result @ (Status::Success | Status::Failure, _) => {
+ self.reset_bt();
+ result
+ }
+ result => result,
+ }
}
/// Retrieve a mutable reference to the blackboard for
@@ -62,12 +69,6 @@ impl BT {
&mut self.bb
}
- /// Retrieve a mutable reference to the internal state
- /// of the Behavior Tree
- pub fn get_state(bt: &mut BT) -> &mut State {
- &mut bt.state
- }
-
/// The behavior tree is a stateful data structure in which the immediate
/// state of the BT is allocated and updated in heap memory through the lifetime
/// of the BT. The state of the BT is said to be `transient` meaning upon entering
diff --git a/bonsai/src/lib.rs b/bonsai/src/lib.rs
index 88747e5..512672b 100644
--- a/bonsai/src/lib.rs
+++ b/bonsai/src/lib.rs
@@ -124,7 +124,7 @@ pub use behavior::Behavior::{
pub use bt::BT;
pub use event::{Event, Timer, UpdateArgs, UpdateEvent};
-pub use state::{ActionArgs, State, RUNNING};
+pub use state::{ActionArgs, RUNNING};
pub use status::Status::{self, Failure, Running, Success};
mod behavior;
diff --git a/bonsai/src/sequence.rs b/bonsai/src/sequence.rs
index e51f697..15e3139 100644
--- a/bonsai/src/sequence.rs
+++ b/bonsai/src/sequence.rs
@@ -1,5 +1,5 @@
use crate::status::Status::*;
-use crate::{event::UpdateEvent, ActionArgs, Behavior, State, Status, RUNNING};
+use crate::{event::UpdateEvent, state::State, ActionArgs, Behavior, Status, RUNNING};
pub struct SequenceArgs<'a, A, E, F, B> {
pub select: bool,
diff --git a/bonsai/src/state.rs b/bonsai/src/state.rs
index 0df0037..73064a7 100644
--- a/bonsai/src/state.rs
+++ b/bonsai/src/state.rs
@@ -28,19 +28,19 @@ pub struct ActionArgs<'a, E: 'a, A: 'a> {
/// Keeps track of a behavior.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
-pub enum State {
+pub(crate) enum State {
/// Executes an action.
- ActionState(A),
+ Action(A),
/// Converts `Success` into `Failure` and vice versa.
- InvertState(Box>),
+ Invert(Box>),
/// Ignores failures and always return `Success`.
- AlwaysSucceedState(Box>),
+ AlwaysSucceed(Box>),
/// Keeps track of waiting for a period of time before continuing.
- WaitState { time_to_wait: f64, elapsed_time: f64 },
+ Wait { time_to_wait: f64, elapsed_time: f64 },
/// Waits forever.
- WaitForeverState,
+ WaitForever,
/// Keeps track of an `If` behavior.
- IfState {
+ If {
/// The behavior to run if the status is a success.
on_success: Box>,
/// The behavior to run if the status is a failure.
@@ -52,7 +52,7 @@ pub enum State {
current_state: Box>,
},
/// Keeps track of a `Select` behavior.
- SelectState {
+ Select {
/// The behaviors that will be selected across in order.
behaviors: Vec>,
/// The index of the behavior currently being executed.
@@ -61,7 +61,7 @@ pub enum State {
current_state: Box>,
},
/// Keeps track of an `Sequence` behavior.
- SequenceState {
+ Sequence {
/// The behaviors that will be executed in order.
behaviors: Vec>,
/// The index of the behavior currently being executed.
@@ -70,7 +70,7 @@ pub enum State {
current_state: Box>,
},
/// Keeps track of a `While` behavior.
- WhileState {
+ While {
/// The state of the condition of the loop. The loop continues to run
/// while this state is running.
condition_state: Box>,
@@ -82,7 +82,7 @@ pub enum State {
loop_body_state: Box>,
},
/// Keeps track of a `WhileAll` behavior.
- WhileAllState {
+ WhileAll {
/// The state of the condition of the loop. The loop continues to run
/// while this state is running, though this is only checked once at the
/// start of each loop.
@@ -98,12 +98,12 @@ pub enum State {
},
/// Keeps track of a `WhenAll` behavior. As the states finish, they are set
/// to [`None`].
- WhenAllState(Vec