change breakout to use FixedTime::period (#8076)

This commit is contained in:
Mike 2023-03-13 14:12:53 -07:00 committed by GitHub
parent 7c4a0eb768
commit 8a2502ce0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);
@ -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();
@ -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;
@ -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
@ -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();
}
}