@@ -4,36 +4,79 @@ fn main() {
4
4
App :: build ( )
5
5
. add_plugins ( DefaultPlugins )
6
6
. add_startup_system ( setup. system ( ) )
7
- . add_system ( animate. system ( ) )
7
+ . add_system ( animate_translation. system ( ) )
8
+ . add_system ( animate_rotation. system ( ) )
9
+ . add_system ( animate_scale. system ( ) )
8
10
. run ( ) ;
9
11
}
10
12
13
+ struct AnimateTranslation ;
14
+ struct AnimateRotation ;
15
+ struct AnimateScale ;
16
+
11
17
fn setup ( mut commands : Commands , asset_server : Res < AssetServer > ) {
18
+ let font = asset_server. load ( "fonts/FiraSans-Bold.ttf" ) ;
19
+ let text_style = TextStyle {
20
+ font,
21
+ font_size : 60.0 ,
22
+ color : Color :: WHITE ,
23
+ } ;
24
+ let text_alignment = TextAlignment {
25
+ vertical : VerticalAlign :: Center ,
26
+ horizontal : HorizontalAlign :: Center ,
27
+ } ;
12
28
// 2d camera
13
29
commands. spawn_bundle ( OrthographicCameraBundle :: new_2d ( ) ) ;
14
- commands. spawn_bundle ( Text2dBundle {
15
- text : Text :: with_section (
16
- "This text is in the 2D scene." ,
17
- TextStyle {
18
- font : asset_server. load ( "fonts/FiraSans-Bold.ttf" ) ,
19
- font_size : 60.0 ,
20
- color : Color :: WHITE ,
21
- } ,
22
- TextAlignment {
23
- vertical : VerticalAlign :: Center ,
24
- horizontal : HorizontalAlign :: Center ,
25
- } ,
26
- ) ,
27
- ..Default :: default ( )
28
- } ) ;
30
+ // Demonstrate changing translation
31
+ commands
32
+ . spawn_bundle ( Text2dBundle {
33
+ text : Text :: with_section ( "translation" , text_style. clone ( ) , text_alignment) ,
34
+ ..Default :: default ( )
35
+ } )
36
+ . insert ( AnimateTranslation ) ;
37
+ // Demonstrate changing rotation
38
+ commands
39
+ . spawn_bundle ( Text2dBundle {
40
+ text : Text :: with_section ( "rotation" , text_style. clone ( ) , text_alignment) ,
41
+ ..Default :: default ( )
42
+ } )
43
+ . insert ( AnimateRotation ) ;
44
+ // Demonstrate changing scale
45
+ commands
46
+ . spawn_bundle ( Text2dBundle {
47
+ text : Text :: with_section ( "scale" , text_style, text_alignment) ,
48
+ ..Default :: default ( )
49
+ } )
50
+ . insert ( AnimateScale ) ;
29
51
}
30
52
31
- fn animate ( time : Res < Time > , mut query : Query < & mut Transform , With < Text > > ) {
32
- // `Transform.translation` will determine the location of the text.
33
- // `Transform.scale` and ` Transform.rotation` do not yet affect text (though you can set the
34
- // size of the text via `Text.style.font_size`)
53
+ fn animate_translation (
54
+ time : Res < Time > ,
55
+ mut query : Query < & mut Transform , ( With < Text > , With < AnimateTranslation > ) > ,
56
+ ) {
35
57
for mut transform in query. iter_mut ( ) {
36
- transform. translation . x = 100.0 * time. seconds_since_startup ( ) . sin ( ) as f32 ;
58
+ transform. translation . x = 100.0 * time. seconds_since_startup ( ) . sin ( ) as f32 - 400.0 ;
37
59
transform. translation . y = 100.0 * time. seconds_since_startup ( ) . cos ( ) as f32 ;
38
60
}
39
61
}
62
+
63
+ fn animate_rotation (
64
+ time : Res < Time > ,
65
+ mut query : Query < & mut Transform , ( With < Text > , With < AnimateRotation > ) > ,
66
+ ) {
67
+ for mut transform in query. iter_mut ( ) {
68
+ transform. rotation = Quat :: from_rotation_z ( time. seconds_since_startup ( ) . cos ( ) as f32 ) ;
69
+ }
70
+ }
71
+
72
+ fn animate_scale (
73
+ time : Res < Time > ,
74
+ mut query : Query < & mut Transform , ( With < Text > , With < AnimateScale > ) > ,
75
+ ) {
76
+ // Consider changing font-size instead of scaling the transform. Scaling a Text2D will scale the
77
+ // rendered quad, resulting in a pixellated look.
78
+ for mut transform in query. iter_mut ( ) {
79
+ transform. translation = Vec3 :: new ( 400.0 , 0.0 , 0.0 ) ;
80
+ transform. scale = Vec3 :: splat ( ( time. seconds_since_startup ( ) . sin ( ) as f32 + 1.1 ) * 2.0 ) ;
81
+ }
82
+ }
0 commit comments