# Objective After #17398, Bevy now has relations! We don't teach users how to make / work with these in the examples yet though, but we definitely should. ## Solution - Add a simple abstract example that goes over defining, spawning, traversing and removing a custom relations. - ~~Add `Relationship` and `RelationshipTarget` to the prelude: the trait methods are really helpful here.~~ - this causes subtle ambiguities with method names and weird compiler errors. Not doing it here! - Clean up related documentation that I referenced when writing this example. ## Testing `cargo run --example relationships` ## Notes to reviewers 1. Yes, I know that the cycle detection code could be more efficient. I decided to reduce the caching to avoid distracting from the broader point of "here's how you traverse relationships". 2. Instead of using an `App`, I've decide to use `World::run_system_once` + system functions defined inside of `main` to do something closer to literate programming. --------- Co-authored-by: Joona Aalto <jondolf.dev@gmail.com> Co-authored-by: MinerSebas <66798382+MinerSebas@users.noreply.github.com> Co-authored-by: Kristoffer Søholm <k.soeholm@gmail.com>
		
			
				
	
	
		
			94 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
//! 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::*;
 | 
						|
 | 
						|
use bevy::{color::palettes::css::*, prelude::*};
 | 
						|
 | 
						|
fn main() {
 | 
						|
    App::new()
 | 
						|
        .add_plugins(DefaultPlugins)
 | 
						|
        .add_systems(Startup, setup)
 | 
						|
        .add_systems(Update, rotate)
 | 
						|
        .run();
 | 
						|
}
 | 
						|
 | 
						|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
 | 
						|
    commands.spawn(Camera2d);
 | 
						|
    let texture = asset_server.load("branding/icon.png");
 | 
						|
 | 
						|
    // Spawn a root entity with no parent
 | 
						|
    let parent = commands
 | 
						|
        .spawn((
 | 
						|
            Sprite::from_image(texture.clone()),
 | 
						|
            Transform::from_scale(Vec3::splat(0.75)),
 | 
						|
        ))
 | 
						|
        // With that entity as a parent, run a lambda that spawns its children
 | 
						|
        .with_children(|parent| {
 | 
						|
            // parent is a ChildSpawnerCommands, which has a similar API to Commands
 | 
						|
            parent.spawn((
 | 
						|
                Transform::from_xyz(250.0, 0.0, 0.0).with_scale(Vec3::splat(0.75)),
 | 
						|
                Sprite {
 | 
						|
                    image: texture.clone(),
 | 
						|
                    color: BLUE.into(),
 | 
						|
                    ..default()
 | 
						|
                },
 | 
						|
            ));
 | 
						|
        })
 | 
						|
        // Store parent entity for next sections
 | 
						|
        .id();
 | 
						|
 | 
						|
    // Another way is to use the add_child function to add children after the parent
 | 
						|
    // entity has already been spawned.
 | 
						|
    let child = commands
 | 
						|
        .spawn((
 | 
						|
            Sprite {
 | 
						|
                image: texture,
 | 
						|
                color: LIME.into(),
 | 
						|
                ..default()
 | 
						|
            },
 | 
						|
            Transform::from_xyz(0.0, 250.0, 0.0).with_scale(Vec3::splat(0.75)),
 | 
						|
        ))
 | 
						|
        .id();
 | 
						|
 | 
						|
    // Add child to the parent.
 | 
						|
    commands.entity(parent).add_child(child);
 | 
						|
}
 | 
						|
 | 
						|
// A simple system to rotate the root entity, and rotate all its children separately
 | 
						|
fn rotate(
 | 
						|
    mut commands: Commands,
 | 
						|
    time: Res<Time>,
 | 
						|
    mut parents_query: Query<(Entity, &Children), With<Sprite>>,
 | 
						|
    mut transform_query: Query<&mut Transform, With<Sprite>>,
 | 
						|
) {
 | 
						|
    for (parent, children) in &mut parents_query {
 | 
						|
        if let Ok(mut transform) = transform_query.get_mut(parent) {
 | 
						|
            transform.rotate_z(-PI / 2. * time.delta_secs());
 | 
						|
        }
 | 
						|
 | 
						|
        // To iterate through the entities children, just treat the Children component as a Vec
 | 
						|
        // Alternatively, you could query entities that have a ChildOf component
 | 
						|
        for child in children {
 | 
						|
            if let Ok(mut transform) = transform_query.get_mut(*child) {
 | 
						|
                transform.rotate_z(PI * time.delta_secs());
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        // To demonstrate removing children, we'll remove a child after a couple of seconds.
 | 
						|
        if time.elapsed_secs() >= 2.0 && children.len() == 2 {
 | 
						|
            let child = children.last().unwrap();
 | 
						|
            commands.entity(*child).despawn();
 | 
						|
        }
 | 
						|
 | 
						|
        if time.elapsed_secs() >= 4.0 {
 | 
						|
            // This will remove the entity from its parent's list of children, as well as despawn
 | 
						|
            // any children the entity has.
 | 
						|
            commands.entity(parent).despawn();
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |