Skip to content

Commit

Permalink
clean up examples
Browse files Browse the repository at this point in the history
  • Loading branch information
maniwani committed Aug 20, 2022
1 parent 4bad8c2 commit a605409
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 38 deletions.
8 changes: 4 additions & 4 deletions examples/2d/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ fn animate_translation(
mut query: Query<&mut Transform, (With<Text>, With<AnimateTranslation>)>,
) {
for mut transform in &mut query {
transform.translation.x = 100.0 * time.seconds_since_startup().sin() as f32 - 400.0;
transform.translation.y = 100.0 * time.seconds_since_startup().cos() as f32;
transform.translation.x = 100.0 * time.seconds_since_startup().sin() - 400.0;
transform.translation.y = 100.0 * time.seconds_since_startup().cos();
}
}

Expand All @@ -101,7 +101,7 @@ fn animate_rotation(
mut query: Query<&mut Transform, (With<Text>, With<AnimateRotation>)>,
) {
for mut transform in &mut query {
transform.rotation = Quat::from_rotation_z(time.seconds_since_startup().cos() as f32);
transform.rotation = Quat::from_rotation_z(time.seconds_since_startup().cos());
}
}

Expand All @@ -113,6 +113,6 @@ fn animate_scale(
// rendered quad, resulting in a pixellated look.
for mut transform in &mut query {
transform.translation = Vec3::new(400.0, 0.0, 0.0);
transform.scale = Vec3::splat((time.seconds_since_startup().sin() as f32 + 1.1) * 2.0);
transform.scale = Vec3::splat((time.seconds_since_startup().sin() + 1.1) * 2.0);
}
}
2 changes: 1 addition & 1 deletion examples/3d/load_gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn animate_light_direction(
transform.rotation = Quat::from_euler(
EulerRot::ZYX,
0.0,
time.seconds_since_startup() as f32 * std::f32::consts::TAU / 10.0,
time.seconds_since_startup() * std::f32::consts::TAU / 10.0,
-std::f32::consts::FRAC_PI_4,
);
}
Expand Down
7 changes: 3 additions & 4 deletions examples/3d/spotlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,11 @@ fn light_sway(time: Res<Time>, mut query: Query<(&mut Transform, &mut SpotLight)
for (mut transform, mut angles) in query.iter_mut() {
transform.rotation = Quat::from_euler(
EulerRot::XYZ,
-std::f32::consts::FRAC_PI_2
+ (time.seconds_since_startup() * 0.67 * 3.0).sin() as f32 * 0.5,
(time.seconds_since_startup() * 3.0).sin() as f32 * 0.5,
-std::f32::consts::FRAC_PI_2 + (time.seconds_since_startup() * 0.67 * 3.0).sin() * 0.5,
(time.seconds_since_startup() * 3.0).sin() * 0.5,
0.0,
);
let angle = ((time.seconds_since_startup() * 1.2).sin() as f32 + 1.0)
let angle = ((time.seconds_since_startup() * 1.2).sin() + 1.0)
* (std::f32::consts::FRAC_PI_4 - 0.1);
angles.inner_angle = angle * 0.8;
angles.outer_angle = angle;
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/transparency_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn setup(
/// when the alpha value goes back below the threshold.
/// - `Blend`: Object fades in and out smoothly.
pub fn fade_transparency(time: Res<Time>, mut materials: ResMut<Assets<StandardMaterial>>) {
let alpha = (time.time_since_startup().as_secs_f32().sin() / 2.0) + 0.5;
let alpha = (time.seconds_since_startup().sin() / 2.0) + 0.5;
for (_, material) in materials.iter_mut() {
material.base_color.set_a(alpha);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/3d/update_gltf_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ fn move_scene_entities(
iter_hierarchy(moved_scene_entity, &children, &mut |entity| {
if let Ok(mut transform) = transforms.get_mut(entity) {
transform.translation = Vec3::new(
offset * time.seconds_since_startup().sin() as f32 / 20.,
offset * time.seconds_since_startup().sin() / 20.,
0.,
time.seconds_since_startup().cos() as f32 / 20.,
time.seconds_since_startup().cos() / 20.,
);
offset += 1.0;
}
Expand Down
6 changes: 2 additions & 4 deletions examples/animation/custom_skinned_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ fn setup(
/// Animate the joint marked with [`AnimatedJoint`] component.
fn joint_animation(time: Res<Time>, mut query: Query<&mut Transform, With<AnimatedJoint>>) {
for mut transform in &mut query {
transform.rotation = Quat::from_axis_angle(
Vec3::Z,
0.5 * PI * time.time_since_startup().as_secs_f32().sin(),
);
transform.rotation =
Quat::from_axis_angle(Vec3::Z, 0.5 * PI * time.seconds_since_startup().sin());
}
}
6 changes: 2 additions & 4 deletions examples/animation/gltf_skinned_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ fn joint_animation(
// Get `Transform` in the second joint.
let mut second_joint_transform = transform_query.get_mut(second_joint_entity).unwrap();

second_joint_transform.rotation = Quat::from_axis_angle(
Vec3::Z,
0.5 * PI * time.time_since_startup().as_secs_f32().sin(),
);
second_joint_transform.rotation =
Quat::from_axis_angle(Vec3::Z, 0.5 * PI * time.seconds_since_startup().sin());
}
}
2 changes: 1 addition & 1 deletion examples/audio/audio_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn update_speed(
time: Res<Time>,
) {
if let Some(sink) = audio_sinks.get(&music_controller.0) {
sink.set_speed(((time.seconds_since_startup() / 5.0).sin() as f32 + 1.0).max(0.1));
sink.set_speed(((time.seconds_since_startup() / 5.0).sin() + 1.0).max(0.1));
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/component_change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
}

#[derive(Component, Debug)]
struct MyComponent(f64);
struct MyComponent(f32);

fn setup(mut commands: Commands) {
commands.spawn().insert(MyComponent(0.));
Expand Down
30 changes: 19 additions & 11 deletions examples/ecs/fixed_timestep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,30 @@ fn main() {
.run();
}

fn frame_update(mut last_time: Local<f64>, time: Res<Time>) {
info!("update: {}", time.seconds_since_startup() - *last_time);
*last_time = time.seconds_since_startup();
fn frame_update(mut last_time: Local<f32>, time: Res<Time>) {
info!(
"time since last frame_update: {}",
time.raw_seconds_since_startup() - *last_time
);
*last_time = time.raw_seconds_since_startup();
}

fn fixed_update(mut last_time: Local<f64>, time: Res<Time>, fixed_timesteps: Res<FixedTimesteps>) {
fn fixed_update(mut last_time: Local<f32>, time: Res<Time>, fixed_timesteps: Res<FixedTimesteps>) {
info!(
"fixed_update: {}",
time.seconds_since_startup() - *last_time,
"time since last fixed_update: {}\n",
time.raw_seconds_since_startup() - *last_time
);

let fixed_timestep = fixed_timesteps.get(LABEL).unwrap();
let state = fixed_timesteps.get(LABEL).unwrap();

info!("fixed timestep: {}\n", 0.5);
info!(
" overstep_percentage: {}",
fixed_timestep.overstep_percentage()
"time accrued toward next fixed_update: {}\n",
state.accumulator()
);

*last_time = time.seconds_since_startup();
info!(
"time accrued toward next fixed_update (% of timestep): {}",
state.overstep_percentage()
);
*last_time = time.raw_seconds_since_startup();
}
2 changes: 1 addition & 1 deletion examples/games/alien_cake_addict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ fn rotate_bonus(game: Res<Game>, time: Res<Time>, mut transforms: Query<&mut Tra
if let Ok(mut cake_transform) = transforms.get_mut(entity) {
cake_transform.rotate_y(time.delta_seconds());
cake_transform.scale = Vec3::splat(
1.0 + (game.score as f32 / 10.0 * time.seconds_since_startup().sin() as f32).abs(),
1.0 + (game.score as f32 / 10.0 * time.seconds_since_startup().sin()).abs(),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/scene/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl FromWorld for ComponentB {
fn from_world(world: &mut World) -> Self {
let time = world.resource::<Time>();
ComponentB {
_time_since_startup: time.time_since_startup(),
_time_since_startup: time.elapsed_since_startup(),
value: "Default Value".to_string(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/shader/animate_shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl ExtractResource for ExtractedTime {

fn extract_resource(time: &Self::Source) -> Self {
ExtractedTime {
seconds_since_startup: time.seconds_since_startup() as f32,
seconds_since_startup: time.seconds_since_startup(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/tools/scene_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ fn update_lights(
transform.rotation = Quat::from_euler(
EulerRot::ZYX,
0.0,
time.seconds_since_startup() as f32 * TAU / 30.0,
time.seconds_since_startup() * TAU / 30.0,
-TAU / 8.,
);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {

fn text_color_system(time: Res<Time>, mut query: Query<&mut Text, With<ColorText>>) {
for mut text in &mut query {
let seconds = time.seconds_since_startup() as f32;
let seconds = time.seconds_since_startup();

// Update the color of the first and only section.
text.sections[0].style.color = Color::Rgba {
Expand Down

0 comments on commit a605409

Please sign in to comment.