Skip to content
Snippets Groups Projects
Unverified Commit 35a3156a authored by Kat Marchán's avatar Kat Marchán
Browse files

example: Add an idler action to thirst example

parent 7f8ed12b
No related branches found
No related tags found
No related merge requests found
...@@ -98,6 +98,42 @@ fn drink_action_system( ...@@ -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 // Then, we have something called "Scorers". These are special components that
// run in the background, calculating a "Score" value, which is what Big Brain // run in the background, calculating a "Score" value, which is what Big Brain
// will use to pick which actions to execute. // will use to pick which actions to execute.
...@@ -156,7 +192,8 @@ pub fn init_entities(mut cmd: Commands) { ...@@ -156,7 +192,8 @@ pub fn init_entities(mut cmd: Commands) {
Thinker::build() Thinker::build()
.picker(FirstToScore { threshold: 80.0 }) .picker(FirstToScore { threshold: 80.0 })
// Note that what we pass in are _builders_, not components! // Note that what we pass in are _builders_, not components!
.when(Thirsty::build(), Drink::build()), .when(Thirsty::build(), Drink::build())
.otherwise(Idle::build()),
); );
} }
...@@ -169,5 +206,6 @@ fn main() { ...@@ -169,5 +206,6 @@ fn main() {
.add_system(thirst_system.system()) .add_system(thirst_system.system())
.add_system(drink_action_system.system()) .add_system(drink_action_system.system())
.add_system(thirsty_scorer_system.system()) .add_system(thirsty_scorer_system.system())
.add_system(idle_system.system())
.run(); .run();
} }
...@@ -243,7 +243,6 @@ pub fn thinker_system( ...@@ -243,7 +243,6 @@ pub fn thinker_system(
// ...and then execute it (details below). // ...and then execute it (details below).
exec_picked_action( exec_picked_action(
&mut cmd, &mut cmd,
thinker_ent,
*actor, *actor,
&mut thinker, &mut thinker,
&choice.action, &choice.action,
...@@ -254,7 +253,6 @@ pub fn thinker_system( ...@@ -254,7 +253,6 @@ pub fn thinker_system(
let default_action_ent = default_action_ent.clone(); let default_action_ent = default_action_ent.clone();
exec_picked_action( exec_picked_action(
&mut cmd, &mut cmd,
thinker_ent,
*actor, *actor,
&mut thinker, &mut thinker,
&default_action_ent, &default_action_ent,
...@@ -289,7 +287,6 @@ pub fn thinker_system( ...@@ -289,7 +287,6 @@ pub fn thinker_system(
fn exec_picked_action( fn exec_picked_action(
cmd: &mut Commands, cmd: &mut Commands,
thinker_ent: Entity,
actor: Entity, actor: Entity,
thinker: &mut Mut<Thinker>, thinker: &mut Mut<Thinker>,
picked_action: &ActionBuilderWrapper, picked_action: &ActionBuilderWrapper,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment