Skip to content

Commit cf207d1

Browse files
committed
add game win state
closes #51 closes #52
1 parent 839ea06 commit cf207d1

File tree

12 files changed

+495
-32
lines changed

12 files changed

+495
-32
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ bevy = { version = "0.17.2", default-features = false, features = [
2020
"bevy_state",
2121
"bevy_audio",
2222
"multi_threaded",
23+
"flac",
2324
"png",
2425
"std",
2526
"vorbis",

assets/soundtrack/space.flac

6.29 MB
Binary file not shown.
-9.34 MB
Binary file not shown.

src/audio/mod.rs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub(super) fn plugin(app: &mut App) {
88
app.init_resource::<SfxLibrary>();
99
app.add_observer(on_play_soundtrack_event);
1010
app.add_observer(on_play_sfx_event);
11-
app.add_systems(Update, fade_in);
11+
app.add_systems(Update, (fade_in, fade_out));
1212
}
1313

1414
#[derive(Resource)]
@@ -48,6 +48,7 @@ struct SoundtrackPlayer;
4848
pub struct Soundtracks {
4949
pub main_theme: Handle<AudioSource>,
5050
pub battle_theme: Handle<AudioSource>,
51+
pub end_theme: Handle<AudioSource>,
5152
}
5253

5354
impl FromWorld for Soundtracks {
@@ -56,17 +57,20 @@ impl FromWorld for Soundtracks {
5657
let soundtracks = Soundtracks {
5758
main_theme: asset_server.load("soundtrack/spacetheme.ogg"),
5859
battle_theme: asset_server.load("soundtrack/through-space.ogg"),
60+
end_theme: asset_server.load("soundtrack/space.flac"),
5961
};
6062
Self {
6163
main_theme: soundtracks.main_theme,
6264
battle_theme: soundtracks.battle_theme,
65+
end_theme: soundtracks.end_theme,
6366
}
6467
}
6568
}
6669

6770
pub enum Soundtrack {
68-
MainTheme,
69-
BattleTheme,
71+
Main,
72+
Battle,
73+
End,
7074
}
7175

7276
#[derive(Event)]
@@ -84,29 +88,36 @@ fn on_play_soundtrack_event(
8488
>,
8589
) {
8690
let track_handle = match soundtrack_event.soundtrack {
87-
Soundtrack::MainTheme => soundtracks.main_theme.clone(),
88-
Soundtrack::BattleTheme => soundtracks.battle_theme.clone(),
91+
Soundtrack::Main => soundtracks.main_theme.clone(),
92+
Soundtrack::Battle => soundtracks.battle_theme.clone(),
93+
Soundtrack::End => soundtracks.end_theme.clone(),
8994
};
9095

96+
let mut is_already_playing = false;
9197
for audio_player_entity in audio_player.iter_mut() {
9298
if audio_player_entity.1.0 == track_handle {
93-
return;
99+
is_already_playing = true;
100+
} else {
101+
commands
102+
.entity(audio_player_entity.0)
103+
.insert(FadeOut { duration: 0.5 });
94104
}
95-
commands.entity(audio_player_entity.0).despawn();
96105
}
97106

98-
commands.spawn((
99-
SoundtrackPlayer,
100-
AudioPlayer(track_handle.clone()),
101-
PlaybackSettings {
102-
mode: PlaybackMode::Loop,
103-
volume: Volume::Linear(0.0),
104-
..default()
105-
},
106-
FadeIn { duration: 4.0 },
107-
Transform::default(),
108-
GlobalTransform::default(),
109-
));
107+
if !is_already_playing {
108+
commands.spawn((
109+
SoundtrackPlayer,
110+
AudioPlayer(track_handle.clone()),
111+
PlaybackSettings {
112+
mode: PlaybackMode::Loop,
113+
volume: Volume::Linear(0.0),
114+
..default()
115+
},
116+
FadeIn { duration: 4.0 },
117+
Transform::default(),
118+
GlobalTransform::default(),
119+
));
120+
}
110121
}
111122

112123
fn on_play_sfx_event(sfx_event: On<PlaySfxEvent>, mut commands: Commands, sfx: Res<SfxLibrary>) {
@@ -148,3 +159,25 @@ fn fade_in(
148159
}
149160
}
150161
}
162+
163+
#[derive(Component)]
164+
struct FadeOut {
165+
duration: f32,
166+
}
167+
168+
fn fade_out(
169+
mut commands: Commands,
170+
mut audio_sink: Query<(&FadeOut, &mut AudioSink, Entity)>,
171+
time: Res<Time>,
172+
) {
173+
for (fade_out, mut audio, entity) in audio_sink.iter_mut() {
174+
let current_volume = audio.volume();
175+
audio.set_volume(
176+
current_volume.fade_towards(Volume::Linear(0.0), time.delta_secs() / fade_out.duration),
177+
);
178+
println!("fading out: {:?}", audio.volume().to_linear());
179+
if audio.volume().to_linear() <= 0.15 {
180+
commands.entity(entity).despawn();
181+
}
182+
}
183+
}

0 commit comments

Comments
 (0)