Add bullet/laser sound

Firing the weapon now makes a sound. I've implemented this by spawning
the playback component on the bullet rather than the gun. This seemed
easier than figuring out how to reset a playback component that lives on
the ship entity -- although thats probably better for memory access
patterns.
This commit is contained in:
2025-12-17 14:29:57 -06:00
parent 3963b548b9
commit ae093d2c9c
3 changed files with 11 additions and 2 deletions

View File

@@ -199,6 +199,8 @@ fn input_ship_shoot(
MeshMaterial2d(game_assets.bullet().1),
ship_pos.clone(), // clone ship transform
Lifetime(Timer::from_seconds(BULLET_LIFETIME, TimerMode::Once)),
AudioPlayer::new(game_assets.laser_sound()),
PlaybackSettings::ONCE, // `Lifetime` already despawns the entity, so this doesn't need to
));
}
}

View File

@@ -58,7 +58,7 @@ impl Default for WorldSize {
pub struct GameAssets {
meshes: [Handle<Mesh>; 5],
materials: [Handle<ColorMaterial>; 7],
sounds: [Handle<AudioSource>; 1],
sounds: [Handle<AudioSource>; 2],
}
impl GameAssets {
@@ -101,6 +101,10 @@ impl GameAssets {
pub fn wreck_sound(&self) -> Handle<AudioSource> {
self.sounds[0].clone()
}
pub fn laser_sound(&self) -> Handle<AudioSource> {
self.sounds[1].clone()
}
}
impl FromWorld for GameAssets {
@@ -129,7 +133,10 @@ impl FromWorld for GameAssets {
world_materials.add(BULLET_COLOR),
];
let loader = world.resource_mut::<AssetServer>();
let sounds = [loader.load("explosionCrunch_004.ogg")];
let sounds = [
loader.load("explosionCrunch_004.ogg"),
loader.load("laserSmall_001.ogg")
];
GameAssets {
meshes,
materials,