//! Demonstrates techniques for creating a hierarchy of parent and child entities. //! //! When [`DefaultPlugins`] are added to your app, systems are automatically added to propagate //! [`Transform`] and [`Visibility`] from parents to children down the hierarchy, //! resulting in a final [`GlobalTransform`] and [`InheritedVisibility`] component for each entity. use std::{f32::consts::*, time::Duration}; use bevy::{color::palettes::css::*, ecs::relationship::RelatedSpawner, prelude::*}; fn main() { App::new() .add_plugins(DefaultPlugins) .add_systems(Startup, setup) .init_state::() .insert_resource(Delta(Duration::ZERO)) .add_systems(OnEnter(Showcase::WithChildren), setup_with_children) .add_systems(OnEnter(Showcase::ChildrenSpawn), setup_children_spawn) .add_systems(OnEnter(Showcase::ChildrenMacro), spawn_children_macro) .add_systems(OnEnter(Showcase::ChildrenIter), setup_children_iter) .add_systems(OnEnter(Showcase::Related), setup_children_related) .add_systems(OnEnter(Showcase::Bsn), setup_bsn) .add_systems(Update, (rotate, switch_scene)) .run(); } #[derive(Debug, Clone, Eq, PartialEq, Hash, States, Default)] enum Showcase { #[default] WithChildren, ChildrenSpawn, ChildrenMacro, ChildrenIter, Related, Bsn, } impl Showcase { fn next(&self) -> Self { match self { Showcase::WithChildren => Showcase::ChildrenSpawn, Showcase::ChildrenSpawn => Showcase::ChildrenMacro, Showcase::ChildrenMacro => Showcase::ChildrenIter, Showcase::ChildrenIter => Showcase::Related, Showcase::Related => Showcase::Bsn, Showcase::Bsn => Showcase::WithChildren, } } } fn switch_scene( keyboard: Res>, scene: Res>, mut next_scene: ResMut>, ) { if keyboard.just_pressed(KeyCode::Space) { info!("Switching scene"); next_scene.set(scene.get().next()); } } fn setup(mut commands: Commands) { commands.spawn(Camera2d); } #[derive(Resource)] struct Delta(Duration); fn setup_common( commands: &mut Commands, time: &Res