Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio / Music #13

Open
4 tasks
nerdachse opened this issue Apr 24, 2024 · 1 comment
Open
4 tasks

Audio / Music #13

nerdachse opened this issue Apr 24, 2024 · 1 comment
Labels
feature Feature request

Comments

@nerdachse
Copy link

nerdachse commented Apr 24, 2024

Hey!

Adding some audio goes a long way for immersion.

There is multiple facets to this.

  • Footsteps
  • Tools / "Abilities" usage
  • Ambient sounds - e.g. birds chirping
  • Background music

Although bevy's audio story isn't great at the moment I've looked into a simple way to incorporate them into riverbed (and other projects)

E.g. for footsteps a simple system could look like this:

pub struct AudioPlugin;

impl Plugin for AudioPlugin {
    fn build(&self, app: &mut bevy::prelude::App) {
        app.add_systems(Update, footsteps);
    }
}

fn footsteps(
    mut footsteps: Local<Option<Entity>>,
    mut last_pos: Local<GlobalTransform>,
    mut cmd: Commands,
    ass: Res<AssetServer>,
    q_realm: Query<&Realm, With<PlayerControlled>>,
    q_player: Query<&GlobalTransform, With<FpsCam>>,
    q_playback: Query<&PlaybackSettings>,
    world: Res<Blocks>,
) {
    let Ok(realm) = q_realm.get_single() else {
        bevy::log::warn!("no realm");
        return;
    };
    let Ok(transform) = q_player.get_single() else {
        bevy::log::warn!("no player camera");
        return;
    };
    let Some(below) = world.raycast(*realm, transform.translation(), transform.down(), 2.0) else {
        bevy::log::debug!("nothing below!");
        return;
    };

    if last_pos.translation() == transform.translation() {
        return;
    }
    *last_pos = transform.clone();

    let entity = *footsteps.get_or_insert(cmd.spawn_empty().id());
    if q_playback.get(entity).is_ok() {
        bevy::log::warn!("Still playing a sound");
        return;
    }

    let Some(source) = (match world.get_block_safe(below.pos) {
        Block::GrassBlock => {
            let mut rng = rand::thread_rng();
            let random_number = rng.gen_range(1..=4);
            Some(format!(
                "sounds/Footsteps Sound FX Pack/OGG/Footsteps_Grass_{:02}.ogg",
                random_number
            ))
        }
        Block::Dirt => {
            let mut rng = rand::thread_rng();
            let random_number = rng.gen_range(1..=4);
            Some(format!(
                "sounds/Footsteps Sound FX Pack/OGG/Footsteps_Dirt_{:02}.ogg",
                random_number
            ))
        }
        Block::Air => None,
        other => {
            bevy::log::warn!("Footsteps for {other:?} not implemented");
            None
        }
    }) else {
        return;
    };

    cmd.entity(entity).insert(AudioBundle {
        source: ass.load(source),
        settings: PlaybackSettings {
            // After the sound has played, this component will be removed
            mode: PlaybackMode::Remove,
            ..default()
        },
    });
}

If you're interested in this, I am open to give back - the main point is just that we have to find assets that we can bundle. I bought some on itch.io for personal use.

Anyway, what do you think?
~ Tom

@Inspirateur
Copy link
Owner

Inspirateur commented Apr 30, 2024

Thanks for this, it's a good start :)

Here are some thoughts:

  • since 2 systems already need the info of which block the player is stepping on (footstep system would make 3) I'll put this info in a component and will compute it at the start of the Update cycle
  • I want this to be asset driven, footsteps sound files will be named after the block they're associated with (we probably can reuse the concept of "block family" from Add more elaborate block breaking #11 here) and these assets will be parsed into a bevy Resource that will know which sound to play based on which block we're on :)
  • I'm not too worried about getting the sounds in this stage of development (I can record placeholder sounds) but if you got MIT licensed assets I'll gladly include them 👍

@Inspirateur Inspirateur added the feature Feature request label May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature request
Projects
None yet
Development

No branches or pull requests

2 participants