bevy/examples/audio/pitch.rs
lelo 42e6dc8987
Refactor EventReader::iter to read (#9631)
# Objective

- The current `EventReader::iter` has been determined to cause confusion
among new Bevy users. It was suggested by @JoJoJet to rename the method
to better clarify its usage.
- Solves #9624 

## Solution

- Rename `EventReader::iter` to `EventReader::read`.
- Rename `EventReader::iter_with_id` to `EventReader::read_with_id`.
- Rename `ManualEventReader::iter` to `ManualEventReader::read`.
- Rename `ManualEventReader::iter_with_id` to
`ManualEventReader::read_with_id`.

---

## Changelog

- `EventReader::iter` has been renamed to `EventReader::read`.
- `EventReader::iter_with_id` has been renamed to
`EventReader::read_with_id`.
- `ManualEventReader::iter` has been renamed to
`ManualEventReader::read`.
- `ManualEventReader::iter_with_id` has been renamed to
`ManualEventReader::read_with_id`.
- Deprecated `EventReader::iter`
- Deprecated `EventReader::iter_with_id`
- Deprecated `ManualEventReader::iter`
- Deprecated `ManualEventReader::iter_with_id`

## Migration Guide

- Existing usages of `EventReader::iter` and `EventReader::iter_with_id`
will have to be changed to `EventReader::read` and
`EventReader::read_with_id` respectively.
- Existing usages of `ManualEventReader::iter` and
`ManualEventReader::iter_with_id` will have to be changed to
`ManualEventReader::read` and `ManualEventReader::read_with_id`
respectively.
2023-08-30 14:20:03 +00:00

55 lines
1.5 KiB
Rust

//! This example illustrates how to play a single-frequency sound (aka a pitch)
use bevy::prelude::*;
use std::time::Duration;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_event::<PlayPitch>()
.add_systems(Startup, setup)
.add_systems(Update, (play_pitch, keyboard_input_system))
.run();
}
#[derive(Event, Default)]
struct PlayPitch;
#[derive(Resource)]
struct PitchFrequency(f32);
fn setup(mut commands: Commands) {
commands.insert_resource(PitchFrequency(220.0));
}
fn play_pitch(
mut pitch_assets: ResMut<Assets<Pitch>>,
frequency: Res<PitchFrequency>,
mut events: EventReader<PlayPitch>,
mut commands: Commands,
) {
for _ in events.read() {
info!("playing pitch with frequency: {}", frequency.0);
commands.spawn(PitchBundle {
source: pitch_assets.add(Pitch::new(frequency.0, Duration::new(1, 0))),
settings: PlaybackSettings::DESPAWN,
});
info!("number of pitch assets: {}", pitch_assets.len());
}
}
fn keyboard_input_system(
keyboard_input: Res<Input<KeyCode>>,
mut frequency: ResMut<PitchFrequency>,
mut events: EventWriter<PlayPitch>,
) {
if keyboard_input.just_pressed(KeyCode::Up) {
frequency.0 *= 2.0f32.powf(1.0 / 12.0);
}
if keyboard_input.just_pressed(KeyCode::Down) {
frequency.0 /= 2.0f32.powf(1.0 / 12.0);
}
if keyboard_input.just_pressed(KeyCode::Space) {
events.send(PlayPitch);
}
}