Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// This example shows off a number of the features that micro_musicbox provides:
// - Basic music player
// - Channel based audio volume mixing w/ master volume
// - Cross fade audio
use std::time::Duration;
use bevy::ecs::schedule::ShouldRun;
use bevy::prelude::*;
use micro_musicbox::prelude::AudioSource;
use micro_musicbox::prelude::*;
use micro_musicbox::CombinedAudioPlugins;
use crate::utilities::{AppState, DetailsMarker, TextMarker};
mod utilities;
pub fn main() {
App::new()
.add_plugin(utilities::SetupPlugin) // Loads resources
.add_plugins(DefaultPlugins)
.add_plugins(CombinedAudioPlugins::<AssetServer>::new())
.add_system_set(SystemSet::on_enter(AppState::Running).with_system(setup_audio).with_system(set_instructions))
.add_system_set(SystemSet::on_update(AppState::Running).with_run_criteria(has_music_state).with_system(cross_fade_tracks))
.run();
}
/// A resource that we'll use to keep track of which track is currently the active one, for fading
/// in and out
pub struct MusicState {
pub playing_first: bool,
}
pub fn has_music_state(state: Option<Res<MusicState>>) -> ShouldRun {
state.is_some().into()
}
pub fn set_instructions(
mut instructions: Query<&mut Text, (With<TextMarker>, Without<DetailsMarker>)>,
mut details: Query<&mut Text, (With<DetailsMarker>, Without<TextMarker>)>,
) {
for mut text in &mut instructions {
text.sections[0].value = String::from("Press Right Shift To Change Tracks")
}
for mut text in &mut details {
text.sections[0].value = String::from("Press 1-9 to change volume (10% - 90%)")
}
}
pub fn setup_audio(mut commands: Commands, mut musicbox: MusicBox<AssetServer>) {
musicbox.play_music("The-White-Kitty.mp3");
commands.insert_resource(MusicState {
playing_first: true,
});
}
// pub fn
pub fn cross_fade_tracks(
input: Res<Input<KeyCode>>,
mut music_box: MusicBox<AssetServer>,
mut music_state: ResMut<MusicState>,
) {
if input.just_released(KeyCode::RShift) {
if music_state.playing_first {
music_box.cross_fade_music(
"The-Great-Madeja.mp3",
AudioTween::new(Duration::from_millis(500), AudioEasing::InOutPowf(0.6)),
);
} else {
music_box.cross_fade_music(
"The-White-Kitty.mp3",
AudioTween::new(Duration::from_millis(500), AudioEasing::InOutPowf(0.6)),
);
}
music_state.playing_first = !music_state.playing_first;
}
if let Some(value) = map_key_values(&input) {
//
// You can set the volume of a channel with a convenience method
//
music_box.set_music_volume(value as f32 / 10.0);
}
if input.just_released(KeyCode::Return) {
//
// You can also get a mutable ref for the settings, in case you
// need to do any maths with them. If you don't need `MusicBox`
// in a system, then you can directly access the settings with
// `Res<AudioSettings>`, or the ResMut equivalent
//
let mut settings = music_box.settings_mut();
if settings.master_volume < 1.0 {
settings.master_volume = 1.0;
} else {
settings.master_volume = 0.5;
}
}
}
fn map_key_values(input: &Res<Input<KeyCode>>) -> Option<usize> {
if input.just_released(KeyCode::Key1) {
Some(1)
} else if input.just_released(KeyCode::Key2) {
Some(2)
} else if input.just_released(KeyCode::Key3) {
Some(3)
} else if input.just_released(KeyCode::Key4) {
Some(4)
} else if input.just_released(KeyCode::Key5) {
Some(5)
} else if input.just_released(KeyCode::Key6) {
Some(6)
} else if input.just_released(KeyCode::Key7) {
Some(7)
} else if input.just_released(KeyCode::Key8) {
Some(8)
} else if input.just_released(KeyCode::Key9) {
Some(9)
} else {
None
}
}