Skip to content

Commit

Permalink
Add utility constructors for Event and UpdateArgs for easy 0.0 delta …
Browse files Browse the repository at this point in the history
…time. Modify RepeatSequence doc code to make use of them.
  • Loading branch information
kaphula committed Jan 27, 2024
1 parent 81eafc9 commit f117536
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
8 changes: 5 additions & 3 deletions bonsai/src/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,20 @@ pub enum Behavior<A> {
///use bonsai_bt::Event;
///
///#[derive(Clone, Debug)]
///
///enum Ex { A, B, C }
///
///let rs = RepeatSequence(
/// Box::new(Action(Ex::A)),
/// vec![Action(Ex::B), Action(Ex::C)],
///);
///let mut bt = BT::new(rs, ());
///
///let (SUCCESS, FAILURE, RUNNING ) = ((Success, 0.0), (Failure, 0.0), (Running, 0.0));
///
///let e: Event = UpdateArgs { dt: 0.0 }.into();
///let mut bt = BT::new(rs, ());
///
///let mut i = 0;
///let status = bt.tick(&e, &mut |args: ActionArgs<Event, Ex>, blackboard| {
///let status = bt.tick(&Event::zero_dt_args(), &mut |args: ActionArgs<Event, Ex>, _| {
/// match args.action {
/// Ex::A => {
/// i += 1;
Expand Down
13 changes: 13 additions & 0 deletions bonsai/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ pub struct UpdateArgs {
pub dt: f64,
}

impl UpdateArgs {
/// Creates [UpdateArgs] with `0.0` delta time.
pub fn zero_dt() -> UpdateArgs {
Self { dt: 0.0 }
}
}

/// Models loop events.
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize)]
pub enum Loop {
Expand All @@ -30,6 +37,12 @@ pub enum Event {
/// Events that commonly used by event loops.
Loop(Loop),
}
impl Event {
/// Creates [Event] from [UpdateArgs] with `0.0` delta time.
pub fn zero_dt_args() -> Self {
UpdateArgs::zero_dt().into()
}
}

/// When the application state should be updated.
pub trait UpdateEvent: Sized {
Expand Down

0 comments on commit f117536

Please sign in to comment.