 7989cb2650
			
		
	
	
		7989cb2650
		
	
	
	
	
		
			
			# Objective - Make `Time` API more consistent. - Support time accel/decel/pause. ## Solution This is just the `Time` half of #3002. I was told that part isn't controversial. - Give the "delta time" and "total elapsed time" methods `f32`, `f64`, and `Duration` variants with consistent naming. - Implement accelerating / decelerating the passage of time. - Implement stopping time. --- ## Changelog - Changed `time_since_startup` to `elapsed` because `time.time_*` is just silly. - Added `relative_speed` and `set_relative_speed` methods. - Added `is_paused`, `pause`, `unpause` , and methods. (I'd prefer `resume`, but `unpause` matches `Timer` API.) - Added `raw_*` variants of the "delta time" and "total elapsed time" methods. - Added `first_update` method because there's a non-zero duration between startup and the first update. ## Migration Guide - `time.time_since_startup()` -> `time.elapsed()` - `time.seconds_since_startup()` -> `time.elapsed_seconds_f64()` - `time.seconds_since_startup_wrapped_f32()` -> `time.elapsed_seconds_wrapped()` If you aren't sure which to use, most systems should continue to use "scaled" time (e.g. `time.delta_seconds()`). The realtime "unscaled" time measurements (e.g. `time.raw_delta_seconds()`) are mostly for debugging and profiling.
		
			
				
	
	
		
			68 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //! This example illustrates how to load and play an audio file, and control how it's played.
 | |
| 
 | |
| use bevy::{audio::AudioSink, prelude::*};
 | |
| 
 | |
| fn main() {
 | |
|     App::new()
 | |
|         .add_plugins(DefaultPlugins)
 | |
|         .add_startup_system(setup)
 | |
|         .add_system(update_speed)
 | |
|         .add_system(pause)
 | |
|         .add_system(volume)
 | |
|         .run();
 | |
| }
 | |
| 
 | |
| fn setup(
 | |
|     mut commands: Commands,
 | |
|     asset_server: Res<AssetServer>,
 | |
|     audio: Res<Audio>,
 | |
|     audio_sinks: Res<Assets<AudioSink>>,
 | |
| ) {
 | |
|     let music = asset_server.load("sounds/Windless Slopes.ogg");
 | |
|     let handle = audio_sinks.get_handle(audio.play(music));
 | |
|     commands.insert_resource(MusicController(handle));
 | |
| }
 | |
| 
 | |
| #[derive(Resource)]
 | |
| struct MusicController(Handle<AudioSink>);
 | |
| 
 | |
| fn update_speed(
 | |
|     audio_sinks: Res<Assets<AudioSink>>,
 | |
|     music_controller: Res<MusicController>,
 | |
|     time: Res<Time>,
 | |
| ) {
 | |
|     if let Some(sink) = audio_sinks.get(&music_controller.0) {
 | |
|         sink.set_speed(((time.elapsed_seconds() / 5.0).sin() + 1.0).max(0.1));
 | |
|     }
 | |
| }
 | |
| 
 | |
| fn pause(
 | |
|     keyboard_input: Res<Input<KeyCode>>,
 | |
|     audio_sinks: Res<Assets<AudioSink>>,
 | |
|     music_controller: Res<MusicController>,
 | |
| ) {
 | |
|     if keyboard_input.just_pressed(KeyCode::Space) {
 | |
|         if let Some(sink) = audio_sinks.get(&music_controller.0) {
 | |
|             if sink.is_paused() {
 | |
|                 sink.play();
 | |
|             } else {
 | |
|                 sink.pause();
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| fn volume(
 | |
|     keyboard_input: Res<Input<KeyCode>>,
 | |
|     audio_sinks: Res<Assets<AudioSink>>,
 | |
|     music_controller: Res<MusicController>,
 | |
| ) {
 | |
|     if let Some(sink) = audio_sinks.get(&music_controller.0) {
 | |
|         if keyboard_input.just_pressed(KeyCode::Plus) {
 | |
|             sink.set_volume(sink.volume() + 0.1);
 | |
|         } else if keyboard_input.just_pressed(KeyCode::Minus) {
 | |
|             sink.set_volume(sink.volume() - 0.1);
 | |
|         }
 | |
|     }
 | |
| }
 |