Skip to content

Commit

Permalink
change breakout to use FixedTime::period (#8076)
Browse files Browse the repository at this point in the history
  • Loading branch information
hymm authored Mar 13, 2023
1 parent 7c4a0eb commit 8a2502c
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions examples/games/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ use bevy::{
sprite::MaterialMesh2dBundle,
};

// Defines the amount of time that should elapse between each physics step.
const TIME_STEP: f32 = 1.0 / 60.0;

// These constants are defined in `Transform` units.
// Using the default 2D camera they correspond 1:1 with screen pixels.
const PADDLE_SIZE: Vec3 = Vec3::new(120.0, 20.0, 0.0);
Expand Down Expand Up @@ -70,7 +67,7 @@ fn main() {
.in_schedule(CoreSchedule::FixedUpdate),
)
// Configure how frequently our gameplay systems are run
.insert_resource(FixedTime::new_from_secs(TIME_STEP))
.insert_resource(FixedTime::new_from_secs(1.0 / 60.0))
.add_system(update_scoreboard)
.add_system(bevy::window::close_on_esc)
.run();
Expand Down Expand Up @@ -315,6 +312,7 @@ fn setup(
fn move_paddle(
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<&mut Transform, With<Paddle>>,
time_step: Res<FixedTime>,
) {
let mut paddle_transform = query.single_mut();
let mut direction = 0.0;
Expand All @@ -328,7 +326,8 @@ fn move_paddle(
}

// Calculate the new horizontal paddle position based on player input
let new_paddle_position = paddle_transform.translation.x + direction * PADDLE_SPEED * TIME_STEP;
let new_paddle_position =
paddle_transform.translation.x + direction * PADDLE_SPEED * time_step.period.as_secs_f32();

// Update the paddle position,
// making sure it doesn't cause the paddle to leave the arena
Expand All @@ -338,10 +337,10 @@ fn move_paddle(
paddle_transform.translation.x = new_paddle_position.clamp(left_bound, right_bound);
}

fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>) {
fn apply_velocity(mut query: Query<(&mut Transform, &Velocity)>, time_step: Res<FixedTime>) {
for (mut transform, velocity) in &mut query {
transform.translation.x += velocity.x * TIME_STEP;
transform.translation.y += velocity.y * TIME_STEP;
transform.translation.x += velocity.x * time_step.period.as_secs_f32();
transform.translation.y += velocity.y * time_step.period.as_secs_f32();
}
}

Expand Down

0 comments on commit 8a2502c

Please sign in to comment.