mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-07 07:15:45 -06:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c8f389b163 |
BIN
assets/game/sound/pacman_death.wav
Normal file
BIN
assets/game/sound/pacman_death.wav
Normal file
Binary file not shown.
@@ -19,6 +19,8 @@ pub enum Asset {
|
|||||||
AtlasImage,
|
AtlasImage,
|
||||||
/// Terminal Vector font for text rendering (TerminalVector.ttf)
|
/// Terminal Vector font for text rendering (TerminalVector.ttf)
|
||||||
Font,
|
Font,
|
||||||
|
/// Sound effect for Pac-Man's death
|
||||||
|
DeathSound,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Asset {
|
impl Asset {
|
||||||
@@ -37,6 +39,7 @@ impl Asset {
|
|||||||
Wav4 => "sound/waka/4.ogg",
|
Wav4 => "sound/waka/4.ogg",
|
||||||
AtlasImage => "atlas.png",
|
AtlasImage => "atlas.png",
|
||||||
Font => "TerminalVector.ttf",
|
Font => "TerminalVector.ttf",
|
||||||
|
DeathSound => "sound/pacman_death.wav",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
45
src/audio.rs
45
src/audio.rs
@@ -16,6 +16,7 @@ const SOUND_ASSETS: [Asset; 4] = [Asset::Wav1, Asset::Wav2, Asset::Wav3, Asset::
|
|||||||
pub struct Audio {
|
pub struct Audio {
|
||||||
_mixer_context: Option<mixer::Sdl2MixerContext>,
|
_mixer_context: Option<mixer::Sdl2MixerContext>,
|
||||||
sounds: Vec<Chunk>,
|
sounds: Vec<Chunk>,
|
||||||
|
death_sound: Option<Chunk>,
|
||||||
next_sound_index: usize,
|
next_sound_index: usize,
|
||||||
muted: bool,
|
muted: bool,
|
||||||
disabled: bool,
|
disabled: bool,
|
||||||
@@ -44,6 +45,7 @@ impl Audio {
|
|||||||
return Self {
|
return Self {
|
||||||
_mixer_context: None,
|
_mixer_context: None,
|
||||||
sounds: Vec::new(),
|
sounds: Vec::new(),
|
||||||
|
death_sound: None,
|
||||||
next_sound_index: 0,
|
next_sound_index: 0,
|
||||||
muted: false,
|
muted: false,
|
||||||
disabled: true,
|
disabled: true,
|
||||||
@@ -65,6 +67,7 @@ impl Audio {
|
|||||||
return Self {
|
return Self {
|
||||||
_mixer_context: None,
|
_mixer_context: None,
|
||||||
sounds: Vec::new(),
|
sounds: Vec::new(),
|
||||||
|
death_sound: None,
|
||||||
next_sound_index: 0,
|
next_sound_index: 0,
|
||||||
muted: false,
|
muted: false,
|
||||||
disabled: true,
|
disabled: true,
|
||||||
@@ -93,12 +96,33 @@ impl Audio {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let death_sound = match get_asset_bytes(Asset::DeathSound) {
|
||||||
|
Ok(data) => match RWops::from_bytes(&data) {
|
||||||
|
Ok(rwops) => match rwops.load_wav() {
|
||||||
|
Ok(chunk) => Some(chunk),
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("Failed to load death sound from asset API: {}", e);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("Failed to create RWops for death sound: {}", e);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("Failed to load death sound asset: {}", e);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// If no sounds loaded successfully, disable audio
|
// If no sounds loaded successfully, disable audio
|
||||||
if sounds.is_empty() {
|
if sounds.is_empty() && death_sound.is_none() {
|
||||||
tracing::warn!("No sounds loaded successfully. Audio will be disabled.");
|
tracing::warn!("No sounds loaded successfully. Audio will be disabled.");
|
||||||
return Self {
|
return Self {
|
||||||
_mixer_context: Some(mixer_context),
|
_mixer_context: Some(mixer_context),
|
||||||
sounds: Vec::new(),
|
sounds: Vec::new(),
|
||||||
|
death_sound: None,
|
||||||
next_sound_index: 0,
|
next_sound_index: 0,
|
||||||
muted: false,
|
muted: false,
|
||||||
disabled: true,
|
disabled: true,
|
||||||
@@ -108,6 +132,7 @@ impl Audio {
|
|||||||
Audio {
|
Audio {
|
||||||
_mixer_context: Some(mixer_context),
|
_mixer_context: Some(mixer_context),
|
||||||
sounds,
|
sounds,
|
||||||
|
death_sound,
|
||||||
next_sound_index: 0,
|
next_sound_index: 0,
|
||||||
muted: false,
|
muted: false,
|
||||||
disabled: false,
|
disabled: false,
|
||||||
@@ -138,6 +163,24 @@ impl Audio {
|
|||||||
self.next_sound_index = (self.next_sound_index + 1) % self.sounds.len();
|
self.next_sound_index = (self.next_sound_index + 1) % self.sounds.len();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Plays the death sound effect.
|
||||||
|
pub fn death(&mut self) {
|
||||||
|
if self.disabled || self.muted {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(chunk) = &self.death_sound {
|
||||||
|
mixer::Channel::all().play(chunk, 0).ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Halts all currently playing audio channels.
|
||||||
|
pub fn stop_all(&mut self) {
|
||||||
|
if !self.disabled {
|
||||||
|
mixer::Channel::all().halt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Instantly mutes or unmutes all audio channels by adjusting their volume.
|
/// Instantly mutes or unmutes all audio channels by adjusting their volume.
|
||||||
///
|
///
|
||||||
/// Sets all 4 mixer channels to zero volume when muting, or restores them to
|
/// Sets all 4 mixer channels to zero volume when muting, or restores them to
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ pub fn get_asset_bytes(asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> {
|
|||||||
Asset::Wav4 => Ok(Cow::Borrowed(include_bytes!("../../assets/game/sound/waka/4.ogg"))),
|
Asset::Wav4 => Ok(Cow::Borrowed(include_bytes!("../../assets/game/sound/waka/4.ogg"))),
|
||||||
Asset::AtlasImage => Ok(Cow::Borrowed(include_bytes!("../../assets/game/atlas.png"))),
|
Asset::AtlasImage => Ok(Cow::Borrowed(include_bytes!("../../assets/game/atlas.png"))),
|
||||||
Asset::Font => Ok(Cow::Borrowed(include_bytes!("../../assets/game/TerminalVector.ttf"))),
|
Asset::Font => Ok(Cow::Borrowed(include_bytes!("../../assets/game/TerminalVector.ttf"))),
|
||||||
|
Asset::DeathSound => Ok(Cow::Borrowed(include_bytes!("../../assets/game/sound/pacman_death.wav"))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ pub struct AudioState {
|
|||||||
pub enum AudioEvent {
|
pub enum AudioEvent {
|
||||||
/// Play the "eat" sound when Pac-Man consumes a pellet
|
/// Play the "eat" sound when Pac-Man consumes a pellet
|
||||||
PlayEat,
|
PlayEat,
|
||||||
|
/// Play the death sound
|
||||||
|
PlayDeath,
|
||||||
|
/// Stop all currently playing sounds
|
||||||
|
StopAll,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Non-send resource wrapper for SDL2 audio system
|
/// Non-send resource wrapper for SDL2 audio system
|
||||||
@@ -59,6 +63,16 @@ pub fn audio_system(
|
|||||||
// 4 eat sounds available
|
// 4 eat sounds available
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
AudioEvent::PlayDeath => {
|
||||||
|
if !audio.0.is_disabled() && !audio_state.muted {
|
||||||
|
audio.0.death();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AudioEvent::StopAll => {
|
||||||
|
if !audio.0.is_disabled() {
|
||||||
|
audio.0.stop_all();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user