First sound! Added ship explosion sound effect

It's a simple one-shot sound clip that gets dispatched as one more part
of the ship impact routine.

The GameAssets struct has been updated to have an array of handles for
audio assets (just the one, for now).

This sound file, and the next several, are all from Kenney
(www.kenney.nl).
This commit is contained in:
2025-12-17 10:46:24 -06:00
parent a48dfc1d65
commit 09ff4dc6ca
5 changed files with 23 additions and 4 deletions

2
Cargo.lock generated
View File

@@ -242,7 +242,7 @@ dependencies = [
[[package]]
name = "asteroids"
version = "0.6.0-dev1"
version = "0.6.0-dev2"
dependencies = [
"bevy",
"bevy-inspector-egui",

View File

@@ -1,6 +1,6 @@
[package]
name = "asteroids"
version = "0.6.0-dev1"
version = "0.6.0-dev2"
edition = "2024"
license = "AGPL-3.0-only"

Binary file not shown.

View File

@@ -3,6 +3,7 @@
//! Asteroids, the player's ship, and such.
use bevy::{
audio::{AudioPlayer, PlaybackSettings},
camera::visibility::Visibility,
ecs::{
component::Component,
@@ -232,5 +233,11 @@ pub fn ship_impact_listener(
// STEP 4: Respawn player (teleport them to the origin)
player.0.translation = Vec3::ZERO;
player.1.0 = Vec2::ZERO;
// STEP 5: Play crash sound
commands.spawn((
AudioPlayer::new(game_assets.wreck_sound()),
PlaybackSettings::ONCE,
));
}
}

View File

@@ -1,7 +1,8 @@
//! All the resources for the game
use bevy::{
asset::{Assets, Handle},
asset::{AssetServer, Assets, Handle},
audio::AudioSource,
ecs::{
resource::Resource,
world::{FromWorld, World},
@@ -57,6 +58,7 @@ impl Default for WorldSize {
pub struct GameAssets {
meshes: [Handle<Mesh>; 5],
materials: [Handle<ColorMaterial>; 7],
sounds: [Handle<AudioSource>; 1],
}
impl GameAssets {
@@ -95,6 +97,10 @@ impl GameAssets {
pub fn bullet(&self) -> (Handle<Mesh>, Handle<ColorMaterial>) {
(self.meshes[4].clone(), self.materials[6].clone())
}
pub fn wreck_sound(&self) -> Handle<AudioSource> {
self.sounds[0].clone()
}
}
impl FromWorld for GameAssets {
@@ -122,6 +128,12 @@ impl FromWorld for GameAssets {
world_materials.add(ASTEROID_SMALL_COLOR),
world_materials.add(BULLET_COLOR),
];
GameAssets { meshes, materials }
let loader = world.resource_mut::<AssetServer>();
let sounds = [loader.load("explosionCrunch_004.ogg")];
GameAssets {
meshes,
materials,
sounds,
}
}
}