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

more example improvements

parent e54e8d37
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,9 @@ impl Thirst { ...@@ -21,6 +21,9 @@ impl Thirst {
pub fn thirst_system(time: Res<Time>, mut thirsts: Query<&mut Thirst>) { pub fn thirst_system(time: Res<Time>, mut thirsts: Query<&mut Thirst>) {
for mut thirst in thirsts.iter_mut() { for mut thirst in thirsts.iter_mut() {
thirst.thirst += thirst.per_second * (time.delta().as_micros() as f32 / 1000000.0); thirst.thirst += thirst.per_second * (time.delta().as_micros() as f32 / 1000000.0);
if thirst.thirst >= 100.0 {
thirst.thirst = 100.0;
}
println!("Thirst: {}", thirst.thirst); println!("Thirst: {}", thirst.thirst);
} }
} }
...@@ -34,15 +37,15 @@ pub fn thirst_system(time: Res<Time>, mut thirsts: Query<&mut Thirst>) { ...@@ -34,15 +37,15 @@ pub fn thirst_system(time: Res<Time>, mut thirsts: Query<&mut Thirst>) {
// These actions will be spawned and queued by the game engine when their // These actions will be spawned and queued by the game engine when their
// conditions trigger (we'll configure what these are later). // conditions trigger (we'll configure what these are later).
#[derive(Debug, Action)] #[derive(Debug, Action)]
pub struct DrinkAction; pub struct Drink;
// Associated with that DrinkAction, you then need to have a system that will // Associated with that Drink Action, you then need to have a system that will
// actually execute those actions when they're "spawned" by the Big Brain // actually execute those actions when they're "spawned" by the Big Brain
// engine. // engine.
// //
// In our case, we want the Thirst components, since we'll be changing those. // In our case, we want the Thirst components, since we'll be changing those.
// Additionally, we want to pick up the DrinkAction components, as well as // Additionally, we want to pick up the DrinkAction components, as well as
// their associated ActionState. Note that the DrinkAction belongs to a // their associated ActionState. Note that the Drink Action belongs to a
// *separate entity* from the owner of the Thirst component! // *separate entity* from the owner of the Thirst component!
fn drink_action_system( fn drink_action_system(
mut thirsts: Query<&mut Thirst>, mut thirsts: Query<&mut Thirst>,
...@@ -55,9 +58,9 @@ fn drink_action_system( ...@@ -55,9 +58,9 @@ fn drink_action_system(
// usually happen because the target action changed (due to a different // usually happen because the target action changed (due to a different
// Scorer winning). But you can also cancel the actions yourself by // Scorer winning). But you can also cancel the actions yourself by
// setting the state in the Action system. // setting the state in the Action system.
mut query: Query<(&Parent, &DrinkAction, &mut ActionState)>, mut query: Query<(&Parent, &mut ActionState), With<Drink>>,
) { ) {
for (Parent(actor), _drink_action, mut state) in query.iter_mut() { for (Parent(actor), mut state) in query.iter_mut() {
// Use the drink_action's actor to look up the corresponding Thirst. // Use the drink_action's actor to look up the corresponding Thirst.
if let Ok(mut thirst) = thirsts.get_mut(*actor) { if let Ok(mut thirst) = thirsts.get_mut(*actor) {
match *state { match *state {
...@@ -84,21 +87,26 @@ fn drink_action_system( ...@@ -84,21 +87,26 @@ fn drink_action_system(
// in the docs (later), but for now, just put them in there and trust the // in the docs (later), but for now, just put them in there and trust the
// system. :) // system. :)
#[derive(Debug, Scorer)] #[derive(Debug, Scorer)]
pub struct ScoreThirst; pub struct Thirsty;
// Look familiar? Similar dance to Actions here. // Look familiar? Similar dance to Actions here.
pub fn score_thirst_system( pub fn thirsty_scorer_system(
thirsts: Query<&Thirst>, thirsts: Query<&Thirst>,
// Same dance with the Parent here, but now we've added a Utility! // Same dance with the Parent here, but now we've added a Score!
mut query: Query<(&Parent, &mut Score), With<ScoreThirst>>, mut query: Query<(&Parent, &mut Score), With<Thirsty>>,
) { ) {
for (Parent(actor), mut score) in query.iter_mut() { for (Parent(actor), mut score) in query.iter_mut() {
if let Ok(thirst) = thirsts.get(*actor) { if let Ok(thirst) = thirsts.get(*actor) {
// This is really what the job of a Scorer is. To calculate // This is really what the job of a Scorer is. To calculate a
// a generic Utility value that the Big Brain engine will compare // generic Utility value that the Big Brain engine will compare
// against others, over time, and use to make decisions. This is // against others, over time, and use to make decisions. This is
// generally "the higher the better", and "first across the finish // generally "the higher the better", and "first across the finish
// line", but that's all configurable using Pickers! // line", but that's all configurable using Pickers!
//
// In a real-world application, you might want to do a fancier
// calculation here, possibly to clamp the value to a range, add a
// curve, etc. In our case, we'll just assume thirst goes from
// 0.0..100.0, to keep things simple.
*score = Score(thirst.thirst); *score = Score(thirst.thirst);
} }
} }
...@@ -126,9 +134,9 @@ pub fn init_entities(mut cmd: Commands) { ...@@ -126,9 +134,9 @@ pub fn init_entities(mut cmd: Commands) {
( (
picker: {"FirstToScore": (threshold: 80.0)}, picker: {"FirstToScore": (threshold: 80.0)},
choices: [( choices: [(
when: {"ScoreThirst": ()}, when: {"Thirsty": ()},
// This action will fire when (and as long as) ScoreThirst scores >=80.0. // This action will fire when (and as long as) Thirsty scores >=80.0.
then: {"DrinkAction": ()}, then: {"Drink": ()},
)], )],
) )
"#, "#,
...@@ -143,7 +151,7 @@ fn main() { ...@@ -143,7 +151,7 @@ fn main() {
.add_plugin(BigBrainPlugin) .add_plugin(BigBrainPlugin)
.add_startup_system(init_entities.system()) .add_startup_system(init_entities.system())
.add_system(thirst_system.system()) .add_system(thirst_system.system())
.add_system(score_thirst_system.system())
.add_system(drink_action_system.system()) .add_system(drink_action_system.system())
.add_system(thirsty_scorer_system.system())
.run(); .run();
} }
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