Skip to content

Commit 5373380

Browse files
committed
Update examples to IDs
1 parent 47a5aa3 commit 5373380

File tree

1 file changed

+36
-43
lines changed

1 file changed

+36
-43
lines changed

examples/animation/animated_transform.rs

+36-43
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,42 @@ fn setup(
2727
..default()
2828
});
2929

30-
// The animation API uses the `Name` component to target entities
31-
let planet = Name::new("planet");
32-
let orbit_controller = Name::new("orbit_controller");
33-
let satellite = Name::new("satellite");
30+
// Spawn satellite
31+
let satellite = commands
32+
.spawn_bundle(PbrBundle {
33+
transform: Transform::from_xyz(1.5, 0.0, 0.0),
34+
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.5 })),
35+
material: materials.add(Color::rgb(0.3, 0.9, 0.3).into()),
36+
..default()
37+
})
38+
.insert(Name::new("satellite"))
39+
// Save entity ID
40+
.id();
41+
// An orbit controller with no visible parts, purely for animation
42+
let orbit_controller = commands
43+
.spawn_bundle(SpatialBundle::VISIBLE_IDENTITY)
44+
.insert(Name::new("orbit_controller"))
45+
// parent the satellite to orbit_controller
46+
.insert_children(0, &[satellite])
47+
// Save entity ID
48+
.id();
49+
// Spawn central planet
50+
let planet = commands
51+
.spawn_bundle(PbrBundle {
52+
mesh: meshes.add(Mesh::from(shape::Icosphere::default())),
53+
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
54+
..default()
55+
})
56+
.insert(Name::new("planet"))
57+
// parent the orbit_controller to planets
58+
.insert_children(0, &[orbit_controller])
59+
.id();
3460

3561
// Creating the animation
3662
let mut animation = AnimationClip::default();
3763
// A curve can modify a single part of a transform, here the translation
3864
animation.add_curve_to_path(
39-
EntityPath {
40-
parts: vec![planet.clone()],
41-
},
65+
planet,
4266
VariableCurve {
4367
keyframe_timestamps: vec![0.0, 1.0, 2.0, 3.0, 4.0],
4468
keyframes: Keyframes::Translation(vec![
@@ -56,9 +80,7 @@ fn setup(
5680
// To find the entity to modify, the hierarchy will be traversed looking for
5781
// an entity with the right name at each level
5882
animation.add_curve_to_path(
59-
EntityPath {
60-
parts: vec![planet.clone(), orbit_controller.clone()],
61-
},
83+
orbit_controller,
6284
VariableCurve {
6385
keyframe_timestamps: vec![0.0, 1.0, 2.0, 3.0, 4.0],
6486
keyframes: Keyframes::Rotation(vec![
@@ -74,9 +96,7 @@ fn setup(
7496
// until all other curves are finished. In that case, another animation should
7597
// be created for each part that would have a different duration / period
7698
animation.add_curve_to_path(
77-
EntityPath {
78-
parts: vec![planet.clone(), orbit_controller.clone(), satellite.clone()],
79-
},
99+
satellite,
80100
VariableCurve {
81101
keyframe_timestamps: vec![0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0],
82102
keyframes: Keyframes::Scale(vec![
@@ -94,9 +114,7 @@ fn setup(
94114
);
95115
// There can be more than one curve targeting the same entity path
96116
animation.add_curve_to_path(
97-
EntityPath {
98-
parts: vec![planet.clone(), orbit_controller.clone(), satellite.clone()],
99-
},
117+
satellite,
100118
VariableCurve {
101119
keyframe_timestamps: vec![0.0, 1.0, 2.0, 3.0, 4.0],
102120
keyframes: Keyframes::Rotation(vec![
@@ -113,31 +131,6 @@ fn setup(
113131
let mut player = AnimationPlayer::default();
114132
player.play(animations.add(animation)).repeat();
115133

116-
// Create the scene that will be animated
117-
// First entity is the planet
118-
commands
119-
.spawn_bundle(PbrBundle {
120-
mesh: meshes.add(Mesh::from(shape::Icosphere::default())),
121-
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
122-
..default()
123-
})
124-
// Add the Name component, and the animation player
125-
.insert_bundle((planet, player))
126-
.with_children(|p| {
127-
// This entity is just used for animation, but doesn't display anything
128-
p.spawn_bundle(SpatialBundle::VISIBLE_IDENTITY)
129-
// Add the Name component
130-
.insert(orbit_controller)
131-
.with_children(|p| {
132-
// The satellite, placed at a distance of the planet
133-
p.spawn_bundle(PbrBundle {
134-
transform: Transform::from_xyz(1.5, 0.0, 0.0),
135-
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.5 })),
136-
material: materials.add(Color::rgb(0.3, 0.9, 0.3).into()),
137-
..default()
138-
})
139-
// Add the Name component
140-
.insert(satellite);
141-
});
142-
});
134+
// The animation player needs to be added to an entity (although it doesn't matter which)
135+
commands.spawn().insert(player);
143136
}

0 commit comments

Comments
 (0)