feat: ecs audio system

This commit is contained in:
2025-08-15 20:28:47 -05:00
parent 342f378860
commit fa12611c69
6 changed files with 81 additions and 3 deletions

54
src/systems/audio.rs Normal file
View File

@@ -0,0 +1,54 @@
//! Audio system for handling sound playback in the Pac-Man game.
//!
//! This module provides an ECS-based audio system that integrates with SDL2_mixer
//! for playing sound effects. The system uses NonSendMut resources to handle SDL2's
//! main-thread requirements while maintaining Bevy ECS compatibility.
use bevy_ecs::{
event::{Event, EventReader, EventWriter},
system::{NonSendMut, ResMut},
};
use crate::{audio::Audio, error::GameError, systems::components::AudioState};
/// Events for triggering audio playback
#[derive(Event, Debug, Clone, Copy, PartialEq, Eq)]
pub enum AudioEvent {
/// Play the "eat" sound when Pac-Man consumes a pellet
PlayEat,
}
/// Non-send resource wrapper for SDL2 audio system
///
/// This wrapper is needed because SDL2 audio components are not Send,
/// but Bevy ECS requires Send for regular resources. Using NonSendMut
/// allows us to use SDL2 audio on the main thread while integrating
/// with the ECS system.
pub struct AudioResource(pub Audio);
/// System that processes audio events and plays sounds
pub fn audio_system(
mut audio: NonSendMut<AudioResource>,
mut audio_state: ResMut<AudioState>,
mut audio_events: EventReader<AudioEvent>,
_errors: EventWriter<GameError>,
) {
// Set mute state if it has changed
if audio.0.is_muted() != audio_state.muted {
audio.0.set_mute(audio_state.muted);
}
// Process audio events
for event in audio_events.read() {
match event {
AudioEvent::PlayEat => {
if !audio.0.is_disabled() && !audio_state.muted {
audio.0.eat();
// Update the sound index for cycling through sounds
audio_state.sound_index = (audio_state.sound_index + 1) % 4;
// 4 eat sounds available
}
}
}
}
}

View File

@@ -107,3 +107,12 @@ pub struct DeltaTime(pub f32);
#[derive(Resource, Default)]
pub struct RenderDirty(pub bool);
/// Resource for tracking audio state
#[derive(Resource, Debug, Clone, Default)]
pub struct AudioState {
/// Whether audio is currently muted
pub muted: bool,
/// Current sound index for cycling through eat sounds
pub sound_index: usize,
}

View File

@@ -2,7 +2,10 @@ use bevy_ecs::{event::EventReader, prelude::*, query::With, system::Query};
use crate::{
events::GameEvent,
systems::components::{EntityType, ItemCollider, PacmanCollider, ScoreResource},
systems::{
audio::AudioEvent,
components::{EntityType, ItemCollider, PacmanCollider, ScoreResource},
},
};
pub fn item_system(
@@ -11,6 +14,7 @@ pub fn item_system(
mut score: ResMut<ScoreResource>,
pacman_query: Query<Entity, With<PacmanCollider>>,
item_query: Query<(Entity, &EntityType), With<ItemCollider>>,
mut events: EventWriter<AudioEvent>,
) {
for event in collision_events.read() {
if let GameEvent::Collision(entity1, entity2) = event {
@@ -37,6 +41,8 @@ pub fn item_system(
// Remove the collected item
commands.entity(item_ent).despawn();
events.write(AudioEvent::PlayEat);
}
}
}

View File

@@ -3,6 +3,7 @@
//! This module contains all the ECS-related logic, including components, systems,
//! and resources.
pub mod audio;
pub mod blinking;
pub mod collision;
pub mod components;

View File

@@ -7,7 +7,7 @@ use std::time::Duration;
use thousands::Separable;
/// The maximum number of systems that can be profiled. Must not be exceeded, or it will panic.
const MAX_SYSTEMS: usize = 11;
const MAX_SYSTEMS: usize = 12;
/// The number of durations to keep in the circular buffer.
const TIMING_WINDOW_SIZE: usize = 30;