Skip to content

Commit b3d72f7

Browse files
authored
Revert "Started to work on soundfx volume (#14)"
This reverts commit 8969811.
1 parent 8969811 commit b3d72f7

File tree

8 files changed

+29
-46
lines changed

8 files changed

+29
-46
lines changed

CHANGELOG.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
<!-- next-header -->
22
## [Unreleased] - ReleaseDate
33

4-
## BREAKING CHANGES
5-
6-
- `.play_sfx()` now takes a volume level from `0.0` to `1.0` as a second argument, e.g. `.play_sfx(SfxPreset::Congratulations, 1.0)`
7-
8-
## Other Changes
9-
104
- (meta) Improved CI times by using sccache together with GitHub Actions caching
115

126
## [2.0.1] - 2021-11-15

examples/collision.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ fn logic(game_state: &mut GameState) {
4444
match event.state {
4545
CollisionState::Begin => {
4646
text_actor.text = format!("{:?}", event.pair);
47-
game_state.audio_manager.play_sfx(SfxPreset::Switch1, 1.0)
47+
game_state.audio_manager.play_sfx(SfxPreset::Switch1)
4848
}
4949
CollisionState::End => {
5050
text_actor.text = "".into();
51-
game_state.audio_manager.play_sfx(SfxPreset::Switch2, 1.0)
51+
game_state.audio_manager.play_sfx(SfxPreset::Switch2)
5252
}
5353
}
5454
}

examples/scenarios/car_shoot.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn game_logic(game_state: &mut GameState) {
6363
marble.translation.x = player_x;
6464
marble.layer = 5.0;
6565
marble.collision = true;
66-
game_state.audio_manager.play_sfx(SfxPreset::Impact2, 0.7);
66+
game_state.audio_manager.play_sfx(SfxPreset::Impact2);
6767
}
6868
}
6969

@@ -151,8 +151,6 @@ fn game_logic(game_state: &mut GameState) {
151151
if event.pair.1.starts_with("marble") {
152152
game_state.string_vec.push(event.pair.1);
153153
}
154-
game_state
155-
.audio_manager
156-
.play_sfx(SfxPreset::Confirmation1, 0.5);
154+
game_state.audio_manager.play_sfx(SfxPreset::Confirmation1);
157155
}
158156
}

examples/scenarios/extreme_drivers_ed.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,9 +1180,7 @@ fn logic(game_state: &mut GameState) {
11801180
event.pair.1.clone()
11811181
};
11821182
game_state.actors.remove(&shiny_label);
1183-
game_state
1184-
.audio_manager
1185-
.play_sfx(SfxPreset::Confirmation1, 1.0);
1183+
game_state.audio_manager.play_sfx(SfxPreset::Confirmation1);
11861184
*score += 1;
11871185
score_text.text = format!("Score: {}", score);
11881186
if *score >= win_amount {
@@ -1195,14 +1193,14 @@ fn logic(game_state: &mut GameState) {
11951193
// Crash!
11961194
*game_state.bool_map.get_mut("crashed").unwrap() = true;
11971195
//game_state.add_text_actor("crashed", "You crashed. You fail. :-(");
1198-
game_state.audio_manager.play_sfx(SfxPreset::Jingle3, 1.0);
1196+
game_state.audio_manager.play_sfx(SfxPreset::Jingle3);
11991197
game_state.audio_manager.stop_music();
12001198
}
12011199

12021200
if win {
12031201
game_state
12041202
.audio_manager
1205-
.play_sfx(SfxPreset::Congratulations, 1.0);
1203+
.play_sfx(SfxPreset::Congratulations);
12061204
let mut you_win = game_state.add_text_actor("you win", "You Win!");
12071205
you_win.font_size = 120.0;
12081206
you_win.translation.y = -50.0;

examples/scenarios/road_race.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ fn game_logic(game_state: &mut GameState) {
126126
if *health_amount > 0 {
127127
*health_amount -= 1;
128128
health_message.text = format!("Health: {}", *health_amount);
129-
game_state.audio_manager.play_sfx(SfxPreset::Impact3, 0.5);
129+
game_state.audio_manager.play_sfx(SfxPreset::Impact3);
130130
}
131131
}
132132
if *health_amount == 0 {
133133
let game_over = game_state.add_text_actor("game over", "Game Over");
134134
game_over.font_size = 128.0;
135135
game_state.audio_manager.stop_music();
136-
game_state.audio_manager.play_sfx(SfxPreset::Jingle3, 0.5);
136+
game_state.audio_manager.play_sfx(SfxPreset::Jingle3);
137137
}
138138
}

examples/sfx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ fn logic(game_state: &mut GameState) {
2929
// None of the timers repeat, and they're all set to different times, so when the timer in
3030
// index X goes off, play sound effect in index X
3131
if timer.tick(game_state.delta).just_finished() {
32-
// Play a new sound effect at full volume
32+
// Play a new sound effect
3333
let sfx = SfxPreset::variant_iter().nth(i).unwrap();
34-
game_state.audio_manager.play_sfx(sfx, 1.0);
34+
game_state.audio_manager.play_sfx(sfx);
3535
// Update the text to show which sound effect we are playing
3636
let sfx_label = game_state.text_actors.get_mut("sfx_label").unwrap();
3737
sfx_label.text = format!("{:?}", sfx);

scenarios/road_race.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ Now we need to actually handle the health. At **_the bottom_** of the `game_log
244244
1. If `*health_amount` is greater than `0` (we don't want to try to subtract from an unsigned number without checking first)
245245
1. Subtract `1` from `*health_amount`
246246
1. Set `health_message` to the string "Health: {}", where "{}" is the value of `*health-amount`.
247-
1. Use the `audio_manager` to play `SfxPreset::Impact3` at full volume. The value for volume ranges from `0.0` (silent) to `1.0` (full volume).
247+
1. Use the `audio_manager` to play `SfxPreset::Impact3`
248248
1. Try it! The game should mostly work, just with a sort of odd, frozen ending, with music still playing.
249249

250250
```rust
@@ -258,7 +258,7 @@ for event in game_state.collision_events.drain(..) {
258258
if *health_amount > 0 {
259259
*health_amount -= 1;
260260
health_message.text = format!("Health: {}", *health_amount);
261-
game_state.audio_manager.play_sfx(SfxPreset::Impact3, 1.0);
261+
game_state.audio_manager.play_sfx(SfxPreset::Impact3);
262262
}
263263
}
264264
```
@@ -279,15 +279,15 @@ Finally, at the very end of the `game_logic()` function we can do a bit of clean
279279
1. Create a text actor, and set its text to `"Game Over"`
280280
1. Using the mutable reference from creating the text actor, set its `font_size` to `128.0` (if this crashes on your system, reduce the font size to a smaller number)
281281
1. Use the `audio_manager` to stop the music.
282-
1. Use the `audio_manager` to play `SfxPreset::Jingle3` at full volume (it's a sad sound)
282+
1. Use the `audio_manager` to play `SfxPreset::Jingle3` (it's a sad sound)
283283
1. Try it!
284284

285285
```rust
286286
if *health_amount == 0 {
287287
let game_over = game_state.add_text_actor("game over", "Game Over");
288288
game_over.font_size = 128.0;
289289
game_state.audio_manager.stop_music();
290-
game_state.audio_manager.play_sfx(SfxPreset::Jingle3, 1.0);
290+
game_state.audio_manager.play_sfx(SfxPreset::Jingle3);
291291
}
292292
```
293293

src/audio.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use std::array::IntoIter;
55

66
#[derive(Debug, Default)]
77
pub struct AudioManager {
8-
sfx_queue: Vec<(SfxPreset, f32)>,
8+
sfx_queue: Vec<SfxPreset>,
99
music_queue: Vec<Option<(MusicPreset, f32)>>,
1010
playing: AudioChannel,
1111
}
1212

1313
impl AudioManager {
14-
/// Play a sound, `volume` ranges from `0.0` to `1.0`.
15-
pub fn play_sfx(&mut self, sfx_preset: SfxPreset, volume: f32) {
16-
self.sfx_queue.push((sfx_preset, volume.clamp(0.0, 1.0)));
14+
/// Play a sound
15+
pub fn play_sfx(&mut self, sfx_preset: SfxPreset) {
16+
self.sfx_queue.push(sfx_preset);
1717
}
1818
/// Play looping music. `volume` ranges from `0.0` to `1.0`. Any music already playing will be
1919
/// stopped.
@@ -138,28 +138,21 @@ pub fn queue_managed_audio_system(
138138
audio: Res<Audio>,
139139
mut game_state: ResMut<GameState>,
140140
) {
141-
for (sfx_preset, volume) in game_state.audio_manager.sfx_queue.drain(..) {
142-
let sfx_path = sfx_preset.to_path();
143-
let sfx_handle = asset_server.load(sfx_path);
144-
// To be able to set the volume of a sound effect, we need the channel it is being played
145-
// in. We'll start by naively creating a new channel for every single sound effect. If this
146-
// ends up being a performance or correctness problem, we'll need to circle back and do
147-
// something more sophisticated (like keep a set number of channels around at different
148-
// volumes).
149-
let new_sfx_channel = AudioChannel::new(sfx_path.into());
150-
audio.set_volume_in_channel(volume, &new_sfx_channel);
151-
audio.play_in_channel(sfx_handle, &new_sfx_channel);
141+
for sfx in game_state.audio_manager.sfx_queue.drain(..) {
142+
let sfx_handle = asset_server.load(sfx.to_path());
143+
audio.play(sfx_handle);
152144
}
153-
let mut playing_music = game_state.audio_manager.playing.clone();
145+
let playing = game_state.audio_manager.playing.clone();
146+
let mut new_playing = playing.clone();
154147
for item in game_state.audio_manager.music_queue.drain(..) {
155-
audio.stop_channel(&playing_music);
148+
audio.stop_channel(&playing);
156149
if let Some((music, volume)) = item {
157150
let music_path = music.to_path();
158151
let music_handle = asset_server.load(music_path);
159-
playing_music = AudioChannel::new(music_path.into());
160-
audio.set_volume_in_channel(volume, &playing_music);
161-
audio.play_looped_in_channel(music_handle, &playing_music);
152+
new_playing = AudioChannel::new(music_path.into());
153+
audio.set_volume_in_channel(volume, &new_playing);
154+
audio.play_looped_in_channel(music_handle, &new_playing);
162155
}
163156
}
164-
game_state.audio_manager.playing = playing_music;
157+
game_state.audio_manager.playing = new_playing;
165158
}

0 commit comments

Comments
 (0)