22
33use bevy:: prelude:: * ;
44
5- const TIME_STEP : f32 = 1.0 / 60.0 ;
65const BOUNDS : Vec2 = Vec2 :: new ( 1200.0 , 640.0 ) ;
76
87fn main ( ) {
98 App :: new ( )
109 . add_plugins ( DefaultPlugins )
11- . insert_resource ( FixedTime :: new_from_secs ( TIME_STEP ) )
10+ . insert_resource ( Time :: < Fixed > :: from_hz ( 60.0 ) )
1211 . add_systems ( Startup , setup)
1312 . add_systems (
1413 FixedUpdate ,
@@ -117,6 +116,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
117116
118117/// Demonstrates applying rotation and movement based on keyboard input.
119118fn player_movement_system (
119+ time : Res < Time > ,
120120 keyboard_input : Res < Input < KeyCode > > ,
121121 mut query : Query < ( & Player , & mut Transform ) > ,
122122) {
@@ -138,12 +138,14 @@ fn player_movement_system(
138138 }
139139
140140 // update the ship rotation around the Z axis (perpendicular to the 2D plane of the screen)
141- transform. rotate_z ( rotation_factor * ship. rotation_speed * TIME_STEP ) ;
141+ transform. rotate_z ( rotation_factor * ship. rotation_speed * time . delta_seconds ( ) ) ;
142142
143- // get the ship's forward vector by applying the current rotation to the ships initial facing vector
143+ // get the ship's forward vector by applying the current rotation to the ships initial facing
144+ // vector
144145 let movement_direction = transform. rotation * Vec3 :: Y ;
145- // get the distance the ship will move based on direction, the ship's movement speed and delta time
146- let movement_distance = movement_factor * ship. movement_speed * TIME_STEP ;
146+ // get the distance the ship will move based on direction, the ship's movement speed and delta
147+ // time
148+ let movement_distance = movement_factor * ship. movement_speed * time. delta_seconds ( ) ;
147149 // create the change in translation using the new movement direction and distance
148150 let translation_delta = movement_direction * movement_distance;
149151 // update the ship translation with our new translation delta
@@ -182,8 +184,8 @@ fn snap_to_player_system(
182184/// if not, which way to rotate to face the player. The dot product on two unit length vectors
183185/// will return a value between -1.0 and +1.0 which tells us the following about the two vectors:
184186///
185- /// * If the result is 1.0 the vectors are pointing in the same direction, the angle between them
186- /// is 0 degrees.
187+ /// * If the result is 1.0 the vectors are pointing in the same direction, the angle between them is
188+ /// 0 degrees.
187189/// * If the result is 0.0 the vectors are perpendicular, the angle between them is 90 degrees.
188190/// * If the result is -1.0 the vectors are parallel but pointing in opposite directions, the angle
189191/// between them is 180 degrees.
@@ -198,6 +200,7 @@ fn snap_to_player_system(
198200/// floating point precision loss, so it pays to clamp your dot product value before calling
199201/// `acos`.
200202fn rotate_to_player_system (
203+ time : Res < Time > ,
201204 mut query : Query < ( & RotateToPlayer , & mut Transform ) , Without < Player > > ,
202205 player_query : Query < & Transform , With < Player > > ,
203206) {
@@ -242,7 +245,8 @@ fn rotate_to_player_system(
242245 let max_angle = forward_dot_player. clamp ( -1.0 , 1.0 ) . acos ( ) ; // clamp acos for safety
243246
244247 // calculate angle of rotation with limit
245- let rotation_angle = rotation_sign * ( config. rotation_speed * TIME_STEP ) . min ( max_angle) ;
248+ let rotation_angle =
249+ rotation_sign * ( config. rotation_speed * time. delta_seconds ( ) ) . min ( max_angle) ;
246250
247251 // rotate the enemy to face the player
248252 enemy_transform. rotate_z ( rotation_angle) ;
0 commit comments