Skip to content

Commit

Permalink
✨ audio
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienjuif committed Dec 3, 2023
1 parent 9260e93 commit 71d6bff
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 5 deletions.
5 changes: 5 additions & 0 deletions assets/credits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Credits

## Audio

- [explosion.ogg](https://freesound.org/people/Werra/sounds/244394/) - edited
Binary file added assets/sounds/explosion.ogg
Binary file not shown.
48 changes: 48 additions & 0 deletions src/audio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use bevy::prelude::*;
use bevy_cameraman::Cameraman;

const DEBUG: bool = false;

pub struct AudioPlugin;

impl Plugin for AudioPlugin {
fn build(&self, app: &mut App) {
app.add_systems(PostStartup, setup);
}
}

fn setup(mut commands: Commands, query_camera: Query<Entity, With<Cameraman>>) {
let gap = 200.;
let listener = SpatialListener::new(gap);

for entity in &query_camera {
let mut cmd = commands.entity(entity);
let cmd = cmd.insert((SpatialBundle::default(), listener.clone()));

if DEBUG {
cmd.with_children(|parent| {
// left ear
parent.spawn(SpriteBundle {
sprite: Sprite {
color: Color::RED,
custom_size: Some(Vec2::splat(20.0)),
..default()
},
transform: Transform::from_xyz(-gap / 2.0, 0.0, 0.0),
..default()
});

// right ear
parent.spawn(SpriteBundle {
sprite: Sprite {
color: Color::GREEN,
custom_size: Some(Vec2::splat(20.0)),
..default()
},
transform: Transform::from_xyz(gap / 2.0, 0.0, 0.0),
..default()
});
});
}
}
}
26 changes: 21 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod audio;
mod castles;
mod common;
mod health;
Expand All @@ -7,6 +8,7 @@ mod player;
mod racks;
mod teams;

use audio::AudioPlugin;
use bevy::{
log::{Level, LogPlugin},
prelude::*,
Expand All @@ -24,16 +26,29 @@ use racks::RacksPlugin;
use teams::TeamsPlugin;
use xxhash_rust::xxh3::xxh3_64;

const AUDIO_SCALE: f32 = 1. / 100.0;

fn main() {
let mut app = App::new();
let seed = b"13U2x";

app.add_plugins((
DefaultPlugins.set(LogPlugin {
level: Level::TRACE,
filter: "wgpu=error,bevy_render=warn,bevy_app=warn,bevy_ecs=warn,naga=warn,gilrs=warn,game::health=info,game::racks=info"
.to_string(),
}),
DefaultPlugins
.set(LogPlugin {
level: Level::TRACE,
filter: [
"wgpu=error",
"bevy_render=warn,bevy_app=warn,bevy_ecs=warn",
"naga=warn",
"gilrs=warn",
"game::health=info,game::racks=info",
]
.join(","),
})
.set(bevy::audio::AudioPlugin {
spatial_scale: bevy::audio::SpatialScale::new_2d(AUDIO_SCALE),
..default()
}),
RngPlugin::new().with_rng_seed(xxh3_64(seed)),
PhysicsPlugin,
TeamsPlugin,
Expand All @@ -42,6 +57,7 @@ fn main() {
CastlesPlugin,
HealthPlugin,
LocalPlayerPlugin,
AudioPlugin,
))
// --- camera ---
.add_plugins((
Expand Down
9 changes: 9 additions & 0 deletions src/minions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ struct ExplosionBundle {
sensor: Sensor,
collider: Collider,
timer_destroyable: TimeDestroyable,
audio: AudioBundle,
}

impl ExplosionBundle {
pub fn new(
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<ColorMaterial>>,
asset_server: &Res<AssetServer>,
mut translation: Vec3,
team: Team,
) -> Self {
Expand All @@ -138,6 +140,10 @@ impl ExplosionBundle {
timer_destroyable: TimeDestroyable {
timer: Timer::from_seconds(0.2, bevy::time::TimerMode::Once),
},
audio: AudioBundle {
source: asset_server.load("sounds/explosion.ogg"),
settings: PlaybackSettings::ONCE.with_spatial(true),
},
}
}
}
Expand Down Expand Up @@ -239,6 +245,7 @@ fn check_collisions_minions(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
asset_server: Res<AssetServer>,
mut collision_events: EventReader<CollisionEvent>,
// queries
mut query_minions: Query<(&Transform, &Team, &mut Minion), With<Minion>>,
Expand Down Expand Up @@ -268,6 +275,7 @@ fn check_collisions_minions(
commands.spawn(ExplosionBundle::new(
&mut meshes,
&mut materials,
&asset_server,
transform_a.translation,
team_a.clone(),
));
Expand Down Expand Up @@ -305,6 +313,7 @@ fn check_collisions_minions(
commands.spawn(ExplosionBundle::new(
&mut meshes,
&mut materials,
&asset_server,
minion_transform.translation,
minion_team.clone(),
));
Expand Down

0 comments on commit 71d6bff

Please sign in to comment.