#[derive(Debug, Default, Clone, Copy, Component)]
pub struct Thirsty;
#[scorer_for(Thirsty)]
pub fn score_thirsty(score: Score, thirsts: Query<&Thirst>) -> Score {
score.update(thirsts.get(score.actor()).unwrap().thirst)
}
#[derive(Debug, Clone, Copy, Component)]
pub struct Drink { rate: f32, per: Duration }
impl Default for Drink {
fn default() -> Self {
Self { rate: 0.5, per: Duration::from_millis(500), }
}
}
#[action_for(Drink)]
pub async fn drink_action(
action: Action<Drink>,
mut thirsts: Query<&mut Thirst>
) -> Result<(), ActionFailure> {
while let Ok(mut thirst) = thirsts.get(action.actor()) && thirst.thirst > 10.0 {
action.check_cancelled()?;
thirst.thirst -= action.data().rate;
action.sleep(action.data().per).await;
}
Ok(())
}
fn spawn_entity(cmd: &mut Commands) {
cmd.spawn((
Thirst(70.0, 2.0),
Thinker::new()
.picker(FirstToScore { threshold: 0.8 })
.when<Thirsty, Drink>(),
));
}