bevy_hierarchy/components/
parent.rs

1#[cfg(feature = "reflect")]
2use bevy_ecs::reflect::{
3    ReflectComponent, ReflectFromWorld, ReflectMapEntities, ReflectVisitEntities,
4    ReflectVisitEntitiesMut,
5};
6use bevy_ecs::{
7    component::Component,
8    entity::{Entity, VisitEntities, VisitEntitiesMut},
9    traversal::Traversal,
10    world::{FromWorld, World},
11};
12use core::ops::Deref;
13
14/// Holds a reference to the parent entity of this entity.
15/// This component should only be present on entities that actually have a parent entity.
16///
17/// Parent entity must have this entity stored in its [`Children`] component.
18/// It is hard to set up parent/child relationships manually,
19/// consider using higher level utilities like [`BuildChildren::with_children`].
20///
21/// See [`HierarchyQueryExt`] for hierarchy related methods on [`Query`].
22///
23/// [`HierarchyQueryExt`]: crate::query_extension::HierarchyQueryExt
24/// [`Query`]: bevy_ecs::system::Query
25/// [`Children`]: super::children::Children
26/// [`BuildChildren::with_children`]: crate::child_builder::BuildChildren::with_children
27#[derive(Component, Debug, Eq, PartialEq, VisitEntities, VisitEntitiesMut)]
28#[cfg_attr(feature = "reflect", derive(bevy_reflect::Reflect))]
29#[cfg_attr(
30    feature = "reflect",
31    reflect(
32        Component,
33        MapEntities,
34        VisitEntities,
35        VisitEntitiesMut,
36        PartialEq,
37        Debug,
38        FromWorld
39    )
40)]
41pub struct Parent(pub(crate) Entity);
42
43impl Parent {
44    /// Gets the [`Entity`] ID of the parent.
45    #[inline(always)]
46    pub fn get(&self) -> Entity {
47        self.0
48    }
49
50    /// Gets the parent [`Entity`] as a slice of length 1.
51    ///
52    /// Useful for making APIs that require a type or homogeneous storage
53    /// for both [`Children`] & [`Parent`] that is agnostic to edge direction.
54    ///
55    /// [`Children`]: super::children::Children
56    #[inline(always)]
57    pub fn as_slice(&self) -> &[Entity] {
58        core::slice::from_ref(&self.0)
59    }
60}
61
62// TODO: We need to impl either FromWorld or Default so Parent can be registered as Reflect.
63// This is because Reflect deserialize by creating an instance and apply a patch on top.
64// However Parent should only ever be set with a real user-defined entity.  Its worth looking into
65// better ways to handle cases like this.
66impl FromWorld for Parent {
67    #[inline(always)]
68    fn from_world(_world: &mut World) -> Self {
69        Parent(Entity::PLACEHOLDER)
70    }
71}
72
73impl Deref for Parent {
74    type Target = Entity;
75
76    #[inline(always)]
77    fn deref(&self) -> &Self::Target {
78        &self.0
79    }
80}
81
82/// This provides generalized hierarchy traversal for use in [event propagation].
83///
84/// `Parent::traverse` will never form loops in properly-constructed hierarchies.
85///
86/// [event propagation]: bevy_ecs::observer::Trigger::propagate
87impl Traversal for &Parent {
88    fn traverse(item: Self::Item<'_>) -> Option<Entity> {
89        Some(item.0)
90    }
91}