diff --git a/src/lib.rs b/src/lib.rs index b739e0fe155108612348c9abe0e39f65b61fe87b..68a8242d64e3bec5f657e654be32fb3208205049 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 a8172e084d1c6a4caea507c03aa437e8487b22f7..68faa4baf17edb2b80a7bf97f193246efb4de167 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);