@@ -27,18 +27,42 @@ fn setup(
27
27
..default ( )
28
28
} ) ;
29
29
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 ( ) ;
34
60
35
61
// Creating the animation
36
62
let mut animation = AnimationClip :: default ( ) ;
37
63
// A curve can modify a single part of a transform, here the translation
38
64
animation. add_curve_to_path (
39
- EntityPath {
40
- parts : vec ! [ planet. clone( ) ] ,
41
- } ,
65
+ planet,
42
66
VariableCurve {
43
67
keyframe_timestamps : vec ! [ 0.0 , 1.0 , 2.0 , 3.0 , 4.0 ] ,
44
68
keyframes : Keyframes :: Translation ( vec ! [
@@ -56,9 +80,7 @@ fn setup(
56
80
// To find the entity to modify, the hierarchy will be traversed looking for
57
81
// an entity with the right name at each level
58
82
animation. add_curve_to_path (
59
- EntityPath {
60
- parts : vec ! [ planet. clone( ) , orbit_controller. clone( ) ] ,
61
- } ,
83
+ orbit_controller,
62
84
VariableCurve {
63
85
keyframe_timestamps : vec ! [ 0.0 , 1.0 , 2.0 , 3.0 , 4.0 ] ,
64
86
keyframes : Keyframes :: Rotation ( vec ! [
@@ -74,9 +96,7 @@ fn setup(
74
96
// until all other curves are finished. In that case, another animation should
75
97
// be created for each part that would have a different duration / period
76
98
animation. add_curve_to_path (
77
- EntityPath {
78
- parts : vec ! [ planet. clone( ) , orbit_controller. clone( ) , satellite. clone( ) ] ,
79
- } ,
99
+ satellite,
80
100
VariableCurve {
81
101
keyframe_timestamps : vec ! [ 0.0 , 0.5 , 1.0 , 1.5 , 2.0 , 2.5 , 3.0 , 3.5 , 4.0 ] ,
82
102
keyframes : Keyframes :: Scale ( vec ! [
@@ -94,9 +114,7 @@ fn setup(
94
114
) ;
95
115
// There can be more than one curve targeting the same entity path
96
116
animation. add_curve_to_path (
97
- EntityPath {
98
- parts : vec ! [ planet. clone( ) , orbit_controller. clone( ) , satellite. clone( ) ] ,
99
- } ,
117
+ satellite,
100
118
VariableCurve {
101
119
keyframe_timestamps : vec ! [ 0.0 , 1.0 , 2.0 , 3.0 , 4.0 ] ,
102
120
keyframes : Keyframes :: Rotation ( vec ! [
@@ -113,31 +131,6 @@ fn setup(
113
131
let mut player = AnimationPlayer :: default ( ) ;
114
132
player. play ( animations. add ( animation) ) . repeat ( ) ;
115
133
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) ;
143
136
}
0 commit comments