You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
martian-chess/examples/hello-fmod.rs

95 lines
3.0 KiB
Rust

//! This example demonstrates how to use the FmodPlugin to play a sound.
//! Make sure to follow the instructions in the README.md to set up the demo project.
use bevy::input::keyboard::KeyboardInput;
use bevy::input::ButtonState;
use bevy::prelude::*;
use bevy_fmod::prelude::AudioSource;
use bevy_fmod::prelude::*;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
FmodPlugin {
audio_banks_paths: &[
"./assets/audio/Martian Chess/Build/Desktop/Master.bank",
"./assets/audio/Martian Chess/Build/Desktop/Master.strings.bank",
"./assets/audio/Martian Chess/Build/Desktop/Music.bank",
],
},
))
.add_systems(Startup, startup)
.add_systems(PostStartup, play_music)
.add_systems(Update, set_music)
.run();
}
#[derive(Component)]
struct MyMusicPlayer;
fn startup(mut commands: Commands, studio: Res<FmodStudio>) {
let event_description = studio.0.get_event("event:/Martian Chess").unwrap();
studio
.0
.get_bank_list(10)
.expect("List banks")
.iter()
.for_each(|bank| {
info!("Bank: {:?}", bank.get_path().expect("Get bank path"));
bank.get_event_list(100)
.iter()
.flat_map(|events| events.iter())
.for_each(|event| {
info!("Event path: {:?}", event.get_path());
info!(
"Event parameter description {:?}",
event.get_parameter_description_by_name("Game State")
);
});
});
commands
.spawn(MyMusicPlayer)
.insert(AudioSource::new(event_description));
}
fn play_music(mut audio_sources: Query<&AudioSource, With<MyMusicPlayer>>) {
if let Ok(audio_source) = audio_sources.get_single_mut() {
audio_source.play();
}
}
fn set_music(
mut events: EventReader<KeyboardInput>,
mut audio_sources: Query<&AudioSource, With<MyMusicPlayer>>,
) {
events.iter().for_each(
|KeyboardInput {
key_code, state, ..
}| {
if let Ok(audio_source) = audio_sources.get_single_mut() {
let target = match state {
ButtonState::Pressed => match key_code {
Some(KeyCode::Key1) => 0.0,
Some(KeyCode::Key2) => 1.0,
Some(KeyCode::Key3) => 2.0,
Some(KeyCode::Key4) => 3.0,
Some(KeyCode::Key5) => 4.0,
Some(KeyCode::Key6) => 5.0,
_ => -1.0,
},
_ => -1.0,
};
if target >= 0.0 {
audio_source
.event_instance
.set_parameter_by_name("Game State", target, true)
.expect("Set audio parameter");
}
}
},
);
}