use bevy::prelude::*;
use big_brain::{evaluators::*, *};
#[derive(Debug)]
pub struct Thirst {
pub per_second: f32,
pub thirst: f32,
}
impl Thirst {
pub fn new(thirst: f32, per_second: f32) -> Self {
Self { thirst, per_second }
}
}
pub fn thirst_system(time: Res<Time>, mut thirsts: Query<&mut Thirst>) {
for mut thirst in thirsts.iter_mut() {
thirst.thirst +=
thirst.per_second * (time.delta().as_micros() as f32 / 1000000.0);
println!("Getting thirstier...{}%", thirst.thirst);
}
}
#[derive(Debug, Action)]
pub struct DrinkAction {}
fn drink_action_system(
mut thirsts: Query<&mut Thirst>,
mut query: Query<(&Parent, &DrinkAction, &mut ActionState)>,
) {
for (Parent(actor), _drink_action, mut state) in query.iter_mut() {
if let Ok(mut thirst) = thirsts.get_mut(*actor) {
match *state {
ActionState::Requested => {
thirst.thirst = 10.0;
println!("drank some water");
*state = ActionState::Success;
}
ActionState::Cancelled => {
*state = ActionState::Failure;
}
_ => {}
}
}
}
}
#[derive(Debug, Consideration)]
pub struct ThirstConsideration {
#[consideration(default)]
pub evaluator: PowerEvaluator,
#[consideration(param)]
pub weight: f32,
}
pub fn thirst_consideration_system(
thirsts: Query<&Thirst>,
mut query: Query<(&Parent, &ThirstConsideration, &mut Utility)>,
) {
for (Parent(actor), conser, mut util) in query.iter_mut() {
if let Ok(thirst) = thirsts.get(*actor) {
*util = Utility {
value: conser.evaluator.evaluate(thirst.thirst),
weight: conser.weight,
};
}
}
}
pub fn init_entities(mut cmd: Commands) {
let actor = cmd.spawn().insert(Thirst::new(80.0, 2.0)).id();
Thinker::load_from_str(
r#"
(
picker: {"FirstToScore": (threshold: 80.0)},
choices: [(
consider: [{"ThirstConsideration": (weight: 2.0)}],
then: {"DrinkAction": ()},
)],
)
"#,
)
.build(actor, &mut cmd);
}
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_startup_system(init_entities.system())
.add_system(thirst_system.system())
.add_system(thirst_consideration_system.system())
.add_system(drink_action_system.system())
.add_system(big_brain::thinker_system.system())
.run();
}