diff --git a/README.md b/README.md index b143e254dde889c8fe2a96fedf30f32007bf373d..7b597df4678db7418cabba407e4c2234b8636813 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ pub fn score_thirst_system( ) { for (Parent(actor), mut score) in query.iter_mut() { if let Ok(thirst) = thirsts.get(*actor) { - score.0 = thirst.thirst; + score.set(thirst.thirst); } } } diff --git a/examples/thirst.rs b/examples/thirst.rs index 73c3099c7a63de3243f4a3547112eb77991a8e58..914003e4d2e7d20c030e1e49667e344ec44e27f2 100644 --- a/examples/thirst.rs +++ b/examples/thirst.rs @@ -103,11 +103,8 @@ pub fn thirsty_scorer_system( // generally "the higher the better", and "first across the finish // 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.0 = thirst.thirst; + // The score here must be between 0.0 and 100.0. + score.set(thirst.thirst); } } } diff --git a/src/scorers.rs b/src/scorers.rs index 1d17d84d76a3e43238ed9284ddcfa1b7863a5370..1343bdd1c4dfb05c29314424c649334cda2bad8b 100644 --- a/src/scorers.rs +++ b/src/scorers.rs @@ -3,7 +3,16 @@ use bevy::prelude::*; use crate::ScorerEnt; #[derive(Debug, Clone, Default)] -pub struct Score(pub f32); +pub struct Score(pub(crate) f32); + +impl Score { + pub fn set(&mut self, value: f32) { + if !(0.0..=100.0).contains(&value) { + panic!("Score value must be between 0.0 and 100.0"); + } + self.0 = value; + } +} /** This trait defines new Scorers. In general, you should use the [derive macro](derive.Scorer.html) instead.