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

Simplify command application #239

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/audio/sound_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ struct PlaySoundEffect {

impl Command for PlaySoundEffect {
fn apply(self, world: &mut World) {
// If you need more complex behavior, use `world.run_system_once_with`,
// as demonstrated with `PlaySoundtrack`.
world.resource_scope(|world, mut sound_effects: Mut<SoundEffectHandles>| {
sound_effects.play(self.name, world, self.settings);
});
Expand Down
52 changes: 31 additions & 21 deletions src/audio/soundtrack.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use bevy::{audio::PlaybackMode, ecs::world::Command, prelude::*};
use bevy::{
audio::PlaybackMode,
ecs::{system::RunSystemOnce as _, world::Command},
prelude::*,
};

use crate::assets::SoundtrackHandles;

Expand All @@ -22,29 +26,35 @@ impl Command for PlaySoundtrack {
/// This command will despawn the current soundtrack, then spawn a new one
/// if necessary.
fn apply(self, world: &mut World) {
let mut soundtrack_query = world.query_filtered::<Entity, With<IsSoundtrack>>();
let soundtracks: Vec<_> = soundtrack_query.iter(world).collect();
for entity in soundtracks {
world.entity_mut(entity).despawn_recursive();
}
world.run_system_once_with(self, apply_play_soundtrack);
}
}

let soundtrack_key = match &self {
PlaySoundtrack::Key(key) => key.clone(),
PlaySoundtrack::Disable => return,
};
fn apply_play_soundtrack(
In(play_soundtrack): In<PlaySoundtrack>,
mut commands: Commands,
soundtrack_query: Query<Entity, With<IsSoundtrack>>,
soundtrack_handles: Res<SoundtrackHandles>,
) {
for entity in soundtrack_query.iter() {
commands.entity(entity).despawn_recursive();
}

let soundtrack_handles = world.resource::<SoundtrackHandles>();
world.spawn((
AudioSourceBundle {
source: soundtrack_handles[&soundtrack_key].clone_weak(),
settings: PlaybackSettings {
mode: PlaybackMode::Loop,
..default()
},
let soundtrack_key = match play_soundtrack {
PlaySoundtrack::Key(key) => key,
PlaySoundtrack::Disable => return,
};

commands.spawn((
AudioSourceBundle {
source: soundtrack_handles[&soundtrack_key].clone_weak(),
settings: PlaybackSettings {
mode: PlaybackMode::Loop,
..default()
},
IsSoundtrack,
));
}
},
IsSoundtrack,
));
}

/// An extension trait with convenience methods for soundtrack commands.
Expand Down
8 changes: 4 additions & 4 deletions src/theme/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ pub struct InteractionPalette {
pub struct OnPress(#[reflect(ignore)] pub SystemId);

fn apply_on_press(
interactions: Query<(&Interaction, &OnPress), Changed<Interaction>>,
interaction_query: Query<(&Interaction, &OnPress), Changed<Interaction>>,
mut commands: Commands,
) {
for (interaction, &OnPress(system_id)) in &interactions {
for (interaction, &OnPress(system_id)) in &interaction_query {
if matches!(interaction, Interaction::Pressed) {
commands.run_system(system_id);
}
Expand All @@ -64,10 +64,10 @@ fn apply_interaction_palette(
}

fn trigger_interaction_sfx(
interactions: Query<&Interaction, Changed<Interaction>>,
interaction_query: Query<&Interaction, Changed<Interaction>>,
mut commands: Commands,
) {
for interaction in &interactions {
for interaction in &interaction_query {
match interaction {
Interaction::Hovered => {
commands.play_sound_effect(SoundEffectHandles::KEY_BUTTON_HOVER)
Expand Down