Skip to content

Commit

Permalink
feat: CommandBuffer::despawn_recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Nov 6, 2024
1 parent e684eb1 commit e92527c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
26 changes: 24 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use anyhow::Context;

use crate::{
buffer::MultiComponentBuffer,
component::{ComponentDesc, ComponentValue},
component::{dummy, ComponentDesc, ComponentValue},
writer::{MissingDyn, SingleComponentWriter, WriteDedupDyn},
BatchSpawn, Component, Entity, EntityBuilder, World,
BatchSpawn, Component, Entity, EntityBuilder, RelationExt, World,
};

type DeferFn = Box<dyn Fn(&mut World) -> anyhow::Result<()> + Send + Sync>;
Expand Down Expand Up @@ -40,6 +40,7 @@ enum Command {
},
/// Despawn an entity
Despawn(Entity),
DespawnRecursive(ComponentDesc, Entity),
/// Remove a component from an entity
Remove {
id: Entity,
Expand Down Expand Up @@ -86,6 +87,12 @@ impl fmt::Debug for Command {
.field("offset", offset)
.finish(),
Self::Despawn(arg0) => f.debug_tuple("Despawn").field(arg0).finish(),
Self::DespawnRecursive(relation, arg0) => f
.debug_tuple("DespawnRecursive")
.field(relation)
.field(arg0)
.finish(),
// Self::DespawnRecursive(arg0) => f.debug_tuple("Despawn").field(arg0).finish(),
Self::Remove {
id,
desc: component,
Expand Down Expand Up @@ -253,6 +260,17 @@ impl CommandBuffer {
self
}

/// Despawn an entity by id recursively
pub fn despawn_recursive<T: ComponentValue>(
&mut self,
relation: impl RelationExt<T>,
id: Entity,
) -> &mut Self {
self.commands
.push(Command::DespawnRecursive(relation.of(dummy()).desc(), id));
self
}

/// Defer a function to execute upon the world.
///
/// Errors will be propagated.
Expand Down Expand Up @@ -326,6 +344,10 @@ impl CommandBuffer {
.despawn(id)
.map_err(|v| v.into_anyhow())
.context("Failed to despawn entity")?,
Command::DespawnRecursive(relation, id) => world
.despawn_recursive_untyped(id, relation.key().id())
.map_err(|v| v.into_anyhow())
.context("Failed to despawn entity")?,
Command::Remove { id, desc } => world
.remove_dyn(id, desc)
.map_err(|v| v.into_anyhow())
Expand Down
17 changes: 16 additions & 1 deletion src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,27 @@ impl World {
Ok(())
}

/// Despawns an entity and all connected entities through the supplied
/// relation
pub fn despawn_recursive_untyped(&mut self, id: Entity, relation: Entity) -> Result<()> {
profile_function!();
self.despawn_children_untyped(id, relation)?;
self.despawn(id)?;

Ok(())
}

/// Despawns all children of an entity recursively
pub fn despawn_children<T: ComponentValue>(
&mut self,
id: Entity,
relation: impl RelationExt<T>,
) -> Result<()> {
self.despawn_children_untyped(id, relation.id())
}

/// Despawns all children of an entity recursively
pub fn despawn_children_untyped(&mut self, id: Entity, relation: Entity) -> Result<()> {
profile_function!();
self.flush_reserved();

Expand All @@ -436,7 +451,7 @@ impl World {
archetypes.extend(
self.archetypes
.index
.find(relation.of(id).key())
.find(ComponentKey::new(relation, Some(id)))
.into_iter()
.flat_map(|v| v.keys().copied()),
);
Expand Down

0 comments on commit e92527c

Please sign in to comment.