Skip to content

Commit

Permalink
example: Add an idler action to thirst example
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 23, 2021
1 parent 7f8ed12 commit 35a3156
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
40 changes: 39 additions & 1 deletion examples/thirst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,42 @@ fn drink_action_system(
}
}

#[derive(Debug, Clone)]
struct Idle;

impl Idle {
fn build() -> IdleBuilder {
IdleBuilder
}
}

#[derive(Debug, Clone)]
struct IdleBuilder;

impl ActionBuilder for IdleBuilder {
fn build(&self, cmd: &mut Commands, action: Entity, _actor: Entity) {
cmd.entity(action).insert(Idle);
}
}

fn idle_system(mut query: Query<&mut ActionState, With<Idle>>) {
for mut state in query.iter_mut() {
match *state {
ActionState::Requested => {
println!("Idling...");
*state = ActionState::Executing;
}
ActionState::Cancelled => {
println!("Idling cancelled");
*state = ActionState::Success;
}
ActionState::Executing => {
println!("Idled");
}
_ => {}
}
}
}
// Then, we have something called "Scorers". These are special components that
// run in the background, calculating a "Score" value, which is what Big Brain
// will use to pick which actions to execute.
Expand Down Expand Up @@ -156,7 +192,8 @@ pub fn init_entities(mut cmd: Commands) {
Thinker::build()
.picker(FirstToScore { threshold: 80.0 })
// Note that what we pass in are _builders_, not components!
.when(Thirsty::build(), Drink::build()),
.when(Thirsty::build(), Drink::build())
.otherwise(Idle::build()),
);
}

Expand All @@ -169,5 +206,6 @@ fn main() {
.add_system(thirst_system.system())
.add_system(drink_action_system.system())
.add_system(thirsty_scorer_system.system())
.add_system(idle_system.system())
.run();
}
3 changes: 0 additions & 3 deletions src/thinker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ pub fn thinker_system(
// ...and then execute it (details below).
exec_picked_action(
&mut cmd,
thinker_ent,
*actor,
&mut thinker,
&choice.action,
Expand All @@ -254,7 +253,6 @@ pub fn thinker_system(
let default_action_ent = default_action_ent.clone();
exec_picked_action(
&mut cmd,
thinker_ent,
*actor,
&mut thinker,
&default_action_ent,
Expand Down Expand Up @@ -289,7 +287,6 @@ pub fn thinker_system(

fn exec_picked_action(
cmd: &mut Commands,
thinker_ent: Entity,
actor: Entity,
thinker: &mut Mut<Thinker>,
picked_action: &ActionBuilderWrapper,
Expand Down

0 comments on commit 35a3156

Please sign in to comment.