From ed3a7cb3c03e27b76b374f75ac179f29c979e4cf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kat=20March=C3=A1n?= <kzm@zkat.tech>
Date: Sat, 17 Apr 2021 16:00:58 -0700
Subject: [PATCH] fix(thinker): add Transform and GlobalTransform

This is... ridiculous, but it happend to be required by the
Bevy Parenting system. Without this, the Thinker will be randomly
removed from the hierarchy :(
---
 src/actions.rs | 4 ++++
 src/pickers.rs | 4 ++--
 src/scorers.rs | 9 ++++++++-
 src/thinker.rs | 7 ++++---
 4 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/actions.rs b/src/actions.rs
index 0e7ad6f..c799eb1 100644
--- a/src/actions.rs
+++ b/src/actions.rs
@@ -92,6 +92,8 @@ pub trait ActionBuilder: std::fmt::Debug + Send + Sync {
     fn attach(&self, cmd: &mut Commands, actor: Entity) -> Entity {
         let action_ent = ActionEnt(cmd.spawn().id());
         cmd.entity(action_ent.0)
+            .insert(Transform::default())
+            .insert(GlobalTransform::default())
             .insert(ActionState::new())
             .insert(Actor(actor));
         self.build(cmd, action_ent.0, actor);
@@ -121,6 +123,8 @@ impl ActionBuilder for StepsBuilder {
     fn build(&self, cmd: &mut Commands, action: Entity, actor: Entity) {
         let child_action = self.steps[0].attach(cmd, actor);
         cmd.entity(action)
+            .insert(Transform::default())
+            .insert(GlobalTransform::default())
             .insert(Steps {
                 active_step: 0,
                 active_ent: ActionEnt(child_action),
diff --git a/src/pickers.rs b/src/pickers.rs
index c54bbea..3a39e47 100644
--- a/src/pickers.rs
+++ b/src/pickers.rs
@@ -38,9 +38,9 @@ impl FirstToScore {
 }
 
 impl Picker for FirstToScore {
-    fn pick(&self, choices: &[Choice], utilities: &Query<&Score>) -> Option<Choice> {
+    fn pick(&self, choices: &[Choice], scores: &Query<&Score>) -> Option<Choice> {
         for choice in choices {
-            let value = choice.calculate(utilities);
+            let value = choice.calculate(scores);
             if value >= self.threshold {
                 return Some(choice.clone());
             }
diff --git a/src/scorers.rs b/src/scorers.rs
index c2c4cc5..be31f35 100644
--- a/src/scorers.rs
+++ b/src/scorers.rs
@@ -66,6 +66,8 @@ pub trait ScorerBuilder: std::fmt::Debug + Sync + Send {
     fn attach(&self, cmd: &mut Commands, actor: Entity) -> Entity {
         let scorer_ent = cmd.spawn().id();
         cmd.entity(scorer_ent)
+            .insert(Transform::default())
+            .insert(GlobalTransform::default())
             .insert(Score::default())
             .insert(Actor(actor));
         self.build(cmd, scorer_ent, actor);
@@ -176,6 +178,8 @@ impl ScorerBuilder for AllOrNothingBuilder {
             .map(|scorer| scorer.attach(cmd, actor))
             .collect();
         cmd.entity(scorer)
+            .insert(Transform::default())
+            .insert(GlobalTransform::default())
             .insert(Score::default())
             .push_children(&scorers[..])
             .insert(AllOrNothing {
@@ -256,7 +260,10 @@ impl ScorerBuilder for SumOfScorersBuilder {
             .iter()
             .map(|scorer| scorer.attach(cmd, actor))
             .collect();
-        cmd.entity(scorer).insert(AllOrNothing {
+        cmd.entity(scorer)
+            .insert(Transform::default())
+            .insert(GlobalTransform::default())
+            .insert(AllOrNothing {
             threshold: self.threshold,
             scorers: scorers.into_iter().map(ScorerEnt).collect(),
         });
diff --git a/src/thinker.rs b/src/thinker.rs
index 84b0099..a8172e0 100644
--- a/src/thinker.rs
+++ b/src/thinker.rs
@@ -118,13 +118,14 @@ impl ThinkerBuilder {
 
 impl ActionBuilder for ThinkerBuilder {
     fn build(&self, cmd: &mut Commands, action_ent: Entity, actor: Entity) {
-        println!("building thinker");
         let choices = self
             .choices
             .iter()
             .map(|choice| choice.build(cmd, actor, action_ent))
             .collect();
         cmd.entity(action_ent)
+            .insert(Transform::default())
+            .insert(GlobalTransform::default())
             .insert(Thinker {
                 // TODO: reasonable default?...
                 picker: self
@@ -190,7 +191,7 @@ pub fn thinker_system(
     mut cmd: Commands,
     mut iterations: Local<ThinkerIterations>,
     mut thinker_q: Query<(Entity, &Actor, &mut Thinker, &ActiveThinker)>,
-    utilities: Query<&Score>,
+    scores: Query<&Score>,
     mut action_states: Query<&mut actions::ActionState>,
 ) {
     let start = Instant::now();
@@ -229,7 +230,7 @@ pub fn thinker_system(
                 }
             }
             ActionState::Requested | ActionState::Executing => {
-                if let Some(choice) = thinker.picker.pick(&thinker.choices, &utilities) {
+                if let Some(choice) = thinker.picker.pick(&thinker.choices, &scores) {
                     // Think about what action we're supposed to be taking. We do this
                     // every tick, because we might change our mind.
                     // ...and then execute it (details below).
-- 
GitLab