1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use bevy_ecs::prelude::*;

/// A marker [`Component`] for entities that should be despawned at the end of
/// the tick.
///
/// In Valence, some entities such as Minecraft entities must not be removed
/// from the [`World`] directly. Valence needs an opportunity to perform
/// deinitialization work while the entity's components still exist.
///
/// To resolve this problem, you must give the entities you wish to despawn the
/// `Despawned` component. At the end of the tick, Valence will despawn all
/// entities with this component for you.
///
/// The `Despawned` component can be used on entities that Valence does not know
/// about. The entity will be despawned regardless.
#[derive(Component, Copy, Clone, Default, PartialEq, Eq, Debug)]
pub struct Despawned;

pub(super) fn despawn_marked_entities(
    entities: Query<Entity, With<Despawned>>,
    mut commands: Commands,
) {
    for entity in &entities {
        commands.entity(entity).despawn();
    }
}