forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hierarchy.rs
123 lines (113 loc) · 4.43 KB
/
hierarchy.rs
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use bevy::prelude::*;
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.add_system(rotate.system())
.run();
}
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands.spawn(OrthographicCameraBundle::new_2d());
let texture = asset_server.load("branding/icon.png");
// Spawn a root entity with no parent
let parent = commands
.spawn(SpriteBundle {
transform: Transform::from_scale(Vec3::splat(0.75)),
material: materials.add(ColorMaterial {
color: Color::WHITE,
texture: Some(texture.clone()),
}),
..Default::default()
})
// With that entity as a parent, run a lambda that spawns its children
.with_children(|parent| {
// parent is a ChildBuilder, which has a similar API to Commands
parent.spawn(SpriteBundle {
transform: Transform {
translation: Vec3::new(250.0, 0.0, 0.0),
scale: Vec3::splat(0.75),
..Default::default()
},
material: materials.add(ColorMaterial {
color: Color::BLUE,
texture: Some(texture.clone()),
}),
..Default::default()
});
})
// Store parent entity for next sections
.current_entity()
.unwrap();
// Another way to create a hierarchy is to add a Parent component to an entity,
// which would be added automatically to parents with other methods.
// Similarly, adding a Parent component will automatically add a Children component to the parent.
commands
.spawn(SpriteBundle {
transform: Transform {
translation: Vec3::new(-250.0, 0.0, 0.0),
scale: Vec3::splat(0.75),
..Default::default()
},
material: materials.add(ColorMaterial {
color: Color::RED,
texture: Some(texture.clone()),
}),
..Default::default()
})
// Using the entity from the previous section as the parent:
.with(Parent(parent));
// Another way is to use the push_children function to add children after the parent
// entity has already been spawned.
let child = commands
.spawn(SpriteBundle {
transform: Transform {
translation: Vec3::new(0.0, 250.0, 0.0),
scale: Vec3::splat(0.75),
..Default::default()
},
material: materials.add(ColorMaterial {
color: Color::GREEN,
texture: Some(texture),
}),
..Default::default()
})
.current_entity()
.unwrap();
// Pushing takes a slice of children to add:
commands.push_children(parent, &[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>>,
) {
let angle = std::f32::consts::PI / 2.0;
for (parent, children) in parents_query.iter_mut() {
if let Ok(mut transform) = transform_query.get_mut(parent) {
transform.rotate(Quat::from_rotation_z(-angle * time.delta_seconds()));
}
// To iterate through the entities children, just treat the Children component as a Vec
// Alternatively, you could query entities that have a Parent component
for child in children.iter() {
if let Ok(mut transform) = transform_query.get_mut(*child) {
transform.rotate(Quat::from_rotation_z(angle * 2.0 * time.delta_seconds()));
}
}
// To demonstrate removing children, we'll start to remove the children after a couple of seconds
if time.seconds_since_startup() >= 2.0 && children.len() == 3 {
let child = children.last().copied().unwrap();
commands.despawn(child);
}
if time.seconds_since_startup() >= 4.0 {
// This will remove the entity from its parent's list of children, as well as despawn
// any children the entity has.
commands.despawn_recursive(parent);
}
}
}