From 5fd1cf093f552390b9ad5e12e22e420ab9972a09 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kat=20March=C3=A1n?= <kzm@zkat.tech>
Date: Thu, 8 Apr 2021 19:38:05 -0700
Subject: [PATCH] privatize score values, and require they be betweeen 0.0 and
 100.0

---
 README.md          |  2 +-
 examples/thirst.rs |  7 ++-----
 src/scorers.rs     | 11 ++++++++++-
 3 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/README.md b/README.md
index b143e25..7b597df 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 73c3099..914003e 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 1d17d84..1343bdd 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.
-- 
GitLab