From db16e2f6ee97777b4df12e4ae435bf27b8012c7c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kat=20March=C3=A1n?= <kzm@zkat.tech>
Date: Sat, 17 Apr 2021 20:07:24 -0700
Subject: [PATCH] fix(thinker): stop using the child/parent system for toplevel
 thinkers

---
 src/lib.rs     |  1 +
 src/thinker.rs | 17 ++++++++++++++---
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs
index b739e0f..68a8242 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -194,6 +194,7 @@ impl Plugin for BigBrainPlugin {
         app.add_system(thinker::thinker_system.system());
         app.add_system(thinker::thinker_component_attach_system.system());
         app.add_system(thinker::thinker_component_detach_system.system());
+        app.add_system(thinker::actor_gone_cleanup.system());
         app.add_system(actions::steps_system.system());
         app.add_system(scorers::fixed_score_system.system());
         app.add_system(scorers::all_or_nothing_system.system());
diff --git a/src/thinker.rs b/src/thinker.rs
index a8172e0..68faa4b 100644
--- a/src/thinker.rs
+++ b/src/thinker.rs
@@ -147,9 +147,7 @@ pub fn thinker_component_attach_system(
 ) {
     for (entity, thinker_builder) in q.iter() {
         let thinker = thinker_builder.attach(&mut cmd, entity);
-        cmd.entity(entity)
-            .insert(HasThinker(thinker))
-            .push_children(&[thinker]);
+        cmd.entity(entity).insert(HasThinker(thinker));
     }
 }
 
@@ -163,6 +161,19 @@ pub fn thinker_component_detach_system(
     }
 }
 
+pub fn actor_gone_cleanup(
+    mut cmd: Commands,
+    actors: Query<&ThinkerBuilder>,
+    q: Query<(Entity, &Actor)>,
+) {
+    for (child, Actor(actor)) in q.iter() {
+        if actors.get(*actor).is_err() {
+            // Actor is gone. Let's clean up.
+            cmd.entity(child).despawn_recursive();
+        }
+    }
+}
+
 #[derive(Debug)]
 pub struct HasThinker(Entity);
 
-- 
GitLab