bevy_hierarchy/
lib.rs

1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2#![forbid(unsafe_code)]
3#![doc(
4    html_logo_url = "https://bevyengine.org/assets/icon.png",
5    html_favicon_url = "https://bevyengine.org/assets/icon.png"
6)]
7
8//! Parent-child relationships for Bevy entities.
9//!
10//! You should use the tools in this crate
11//! whenever you want to organize your entities in a hierarchical fashion,
12//! to make groups of entities more manageable,
13//! or to propagate properties throughout the entity hierarchy.
14//!
15//! This crate introduces various tools, including a [plugin]
16//! for managing parent-child relationships between entities.
17//! It provides two components, [`Parent`] and [`Children`],
18//! to store references to related entities.
19//! It also provides [command] and [world] API extensions
20//! to set and clear those relationships.
21//!
22//! More advanced users may also appreciate
23//! [query extension methods] to traverse hierarchies,
24//! and [events] to notify hierarchical changes.
25//! There is also a [diagnostic plugin] to validate property propagation.
26//!
27//! # Hierarchy management
28//!
29//! The methods defined in this crate fully manage
30//! the components responsible for defining the entity hierarchy.
31//! Mutating these components manually may result in hierarchy invalidation.
32//!
33//! Hierarchical relationships are always managed symmetrically.
34//! For example, assigning a child to an entity
35//! will always set the parent in the other,
36//! and vice versa.
37//! Similarly, unassigning a child in the parent
38//! will always unassign the parent in the child.
39//!
40//! ## Despawning entities
41//!
42//! The commands and methods provided by `bevy_ecs` to despawn entities
43//! are not capable of automatically despawning hierarchies of entities.
44//! In most cases, these operations will invalidate the hierarchy.
45//! Instead, you should use the provided [hierarchical despawn extension methods].
46//!
47//! [command]: BuildChildren
48//! [diagnostic plugin]: ValidParentCheckPlugin
49//! [events]: HierarchyEvent
50//! [hierarchical despawn extension methods]: DespawnRecursiveExt
51//! [plugin]: HierarchyPlugin
52//! [query extension methods]: HierarchyQueryExt
53//! [world]: BuildWorldChildren
54
55mod components;
56pub use components::*;
57
58mod hierarchy;
59pub use hierarchy::*;
60
61mod child_builder;
62pub use child_builder::*;
63
64mod events;
65pub use events::*;
66
67mod valid_parent_check_plugin;
68pub use valid_parent_check_plugin::*;
69
70mod query_extension;
71pub use query_extension::*;
72
73#[doc(hidden)]
74pub mod prelude {
75    #[doc(hidden)]
76    pub use crate::{child_builder::*, components::*, hierarchy::*, query_extension::*};
77
78    #[doc(hidden)]
79    #[cfg(feature = "bevy_app")]
80    pub use crate::{HierarchyPlugin, ValidParentCheckPlugin};
81}
82
83#[cfg(feature = "bevy_app")]
84use bevy_app::prelude::*;
85
86/// Provides hierarchy functionality to a Bevy app.
87///
88/// Check the [crate-level documentation] for all the features.
89///
90/// [crate-level documentation]: crate
91#[cfg(feature = "bevy_app")]
92#[derive(Default)]
93pub struct HierarchyPlugin;
94
95#[cfg(feature = "bevy_app")]
96impl Plugin for HierarchyPlugin {
97    fn build(&self, app: &mut App) {
98        app.register_type::<Children>()
99            .register_type::<Parent>()
100            .add_event::<HierarchyEvent>();
101    }
102}