brkrs 0.0.1

Breakout/Arkanoid-style game built in Rust using the Bevy engine, with physics powered by bevy_rapier3d
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
//! Audio system module for the brick-breaker game.
//!
//! This module provides an event-driven audio system that plays sounds in response
//! to game events such as brick collisions, wall bounces, and level transitions.
//!
//! # Architecture
//!
//! The audio system uses Bevy's observer pattern to react to game events:
//!
//! - [`AudioPlugin`] registers all audio resources and observers
//! - [`AudioConfig`] stores user-adjustable volume and mute settings
//! - [`AudioAssets`] holds loaded audio asset handles keyed by [`SoundType`]
//! - [`ActiveSounds`] tracks concurrent playback to limit simultaneous sounds
//!
//! # Sound Types
//!
//! The system supports 8 distinct sound effects:
//!
//! - `BrickDestroy` - Standard brick destruction
//! - `MultiHitImpact` - Multi-hit brick damage (indices 10-13)
//! - `WallBounce` - Ball bounces off wall
//! - `PaddleHit` - Ball bounces off paddle
//! - `PaddleWallHit` - Paddle collides with wall
//! - `PaddleBrickHit` - Paddle collides with brick
//! - `LevelStart` - Level begins
//! - `LevelComplete` - Level completed
//!
//! # Graceful Degradation
//!
//! The system handles missing audio assets gracefully by logging warnings
//! without crashing, allowing development and testing without audio files.
//!
//! # Example
//!
//! ```ignore
//! // Register the audio plugin in your app
//! app.add_plugins(AudioPlugin);
//!
//! // Audio will automatically play when game events occur
//! ```

use bevy::prelude::*;
use ron::de::from_str;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[cfg(target_arch = "wasm32")]
use web_sys;

/// Maximum number of concurrent sounds of the same type.
const MAX_CONCURRENT_SOUNDS: u8 = 4;

/// Identifies the category of sound effect for mapping and concurrent tracking.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SoundType {
    /// Standard brick destruction sound.
    BrickDestroy,
    /// Multi-hit brick damage sound (indices 10-13).
    MultiHitImpact,
    /// Ball bounces off wall boundary.
    WallBounce,
    /// Ball bounces off paddle.
    PaddleHit,
    /// Paddle collides with wall boundary.
    PaddleWallHit,
    /// Paddle collides with brick.
    PaddleBrickHit,
    /// Level begins.
    LevelStart,
    /// Level completed.
    LevelComplete,
}

/// User-adjustable audio settings, persisted across sessions.
///
/// # Fields
///
/// - `master_volume` - Global volume multiplier (0.0 to 1.0)
/// - `muted` - Whether audio is muted
#[derive(Resource, Debug, Clone, Serialize, Deserialize)]
pub struct AudioConfig {
    /// Global volume multiplier (0.0 to 1.0).
    #[serde(default = "default_volume")]
    pub master_volume: f32,
    /// Whether audio is muted.
    #[serde(default)]
    pub muted: bool,
}

fn default_volume() -> f32 {
    1.0
}

impl Default for AudioConfig {
    fn default() -> Self {
        Self {
            master_volume: 1.0,
            muted: false,
        }
    }
}

impl AudioConfig {
    /// Create a new AudioConfig with the given volume and mute state.
    pub fn new(master_volume: f32, muted: bool) -> Self {
        Self {
            master_volume: master_volume.clamp(0.0, 1.0),
            muted,
        }
    }

    /// Set the master volume (clamped to 0.0-1.0).
    pub fn set_volume(&mut self, volume: f32) {
        self.master_volume = volume.clamp(0.0, 1.0);
    }

    /// Toggle mute state and return the new state.
    pub fn toggle_mute(&mut self) -> bool {
        self.muted = !self.muted;
        self.muted
    }

    /// Check if the audio config is valid (volume in range).
    pub fn is_valid(&self) -> bool {
        (0.0..=1.0).contains(&self.master_volume)
    }
}

/// Loaded sound asset handles, keyed by SoundType.
#[derive(Resource, Debug, Default)]
pub struct AudioAssets {
    /// Map of sound types to their loaded audio handles.
    pub sounds: HashMap<SoundType, Handle<AudioSource>>,
}

impl AudioAssets {
    /// Get the audio handle for a specific sound type.
    pub fn get(&self, sound_type: SoundType) -> Option<&Handle<AudioSource>> {
        self.sounds.get(&sound_type)
    }
}

/// Tracks concurrent playback count per sound type.
///
/// Enforces a maximum of 4 concurrent sounds per type to prevent audio distortion.
#[derive(Resource, Debug, Default)]
pub struct ActiveSounds {
    /// Active instances per sound type.
    counts: HashMap<SoundType, u8>,
}

impl ActiveSounds {
    /// Increment the count for a sound type. Returns true if under limit.
    pub fn try_increment(&mut self, sound_type: SoundType) -> bool {
        let count = self.counts.entry(sound_type).or_insert(0);
        if *count < MAX_CONCURRENT_SOUNDS {
            *count += 1;
            true
        } else {
            false
        }
    }

    /// Decrement the count for a sound type.
    pub fn decrement(&mut self, sound_type: SoundType) {
        if let Some(count) = self.counts.get_mut(&sound_type) {
            *count = count.saturating_sub(1);
        }
    }

    /// Get the current count for a sound type.
    pub fn count(&self, sound_type: SoundType) -> u8 {
        *self.counts.get(&sound_type).unwrap_or(&0)
    }
}

/// Tracks active audio entity instances so we can decrement counts when playback ends.
#[derive(Resource, Debug, Default)]
pub struct ActiveAudioInstances {
    /// Map from spawned audio entity -> SoundType
    pub instances: HashMap<Entity, SoundType>,
}

/// Audio manifest for deserializing the audio configuration file.
#[derive(Debug, Deserialize)]
struct AudioManifest {
    sounds: HashMap<SoundType, String>,
}

/// Audio plugin that registers all audio resources and systems.
pub struct AudioPlugin;

impl Plugin for AudioPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<AudioAssets>()
            .init_resource::<ActiveSounds>()
            .init_resource::<ActiveAudioInstances>()
            .add_systems(Startup, (load_audio_config, load_audio_assets).chain())
            .add_systems(Update, save_audio_config_on_change)
            .add_systems(Update, cleanup_finished_sounds)
            .add_observer(on_multi_hit_brick_sound)
            .add_observer(on_brick_destroyed_sound)
            .add_observer(on_ball_wall_hit_sound)
            .add_observer(on_paddle_ball_hit_sound)
            .add_observer(on_paddle_wall_hit_sound)
            .add_observer(on_paddle_brick_hit_sound)
            .add_observer(on_level_started_sound)
            .add_observer(on_level_complete_sound);
    }
}

/// Decrement counts for audio entities that have finished playback.
fn cleanup_finished_sounds(
    mut removed: RemovedComponents<AudioPlayer>,
    mut active_instances: ResMut<ActiveAudioInstances>,
    mut active_sounds: ResMut<ActiveSounds>,
) {
    for removed_entity in removed.read() {
        if let Some(sound_type) = active_instances.instances.remove(&removed_entity) {
            active_sounds.decrement(sound_type);
            debug!(target: "audio", ?sound_type, entity = ?removed_entity, "Audio instance finished, decremented count");
        }
    }
}

/// Path to the audio config file.
const AUDIO_CONFIG_PATH: &str = "config/audio.ron";

/// Load audio configuration from disk or use defaults.
fn load_audio_config(mut commands: Commands) {
    #[cfg(not(target_arch = "wasm32"))]
    let config = {
        match std::fs::read_to_string(AUDIO_CONFIG_PATH) {
            Ok(content) => match ron::de::from_str::<AudioConfig>(&content) {
                Ok(mut loaded) => {
                    // Ensure volume is in valid range
                    loaded.master_volume = loaded.master_volume.clamp(0.0, 1.0);
                    info!(
                        target: "audio",
                        volume = loaded.master_volume,
                        muted = loaded.muted,
                        "Loaded audio config"
                    );
                    loaded
                }
                Err(e) => {
                    warn!(
                        target: "audio",
                        error = %e,
                        "Failed to parse audio config, using defaults"
                    );
                    AudioConfig::default()
                }
            },
            Err(_) => {
                info!(
                    target: "audio",
                    "Audio config not found, using defaults"
                );
                AudioConfig::default()
            }
        }
    };

    #[cfg(target_arch = "wasm32")]
    let config = {
        // On WASM, try to load from `localStorage` under the key `brkrs_audio`.
        // We store the serialized RON string in localStorage to keep parity
        // with the native `config/audio.ron` format.
        let storage_key = "brkrs_audio";
        if let Some(window) = web_sys::window() {
            if let Ok(Some(storage)) = window.local_storage() {
                if let Ok(Some(item)) = storage.get_item(storage_key) {
                    match ron::de::from_str::<AudioConfig>(&item) {
                        Ok(mut loaded) => {
                            loaded.master_volume = loaded.master_volume.clamp(0.0, 1.0);
                            info!(
                                target: "audio",
                                volume = loaded.master_volume,
                                muted = loaded.muted,
                                "Loaded audio config from localStorage"
                            );
                            loaded
                        }
                        Err(e) => {
                            warn!(
                                target: "audio",
                                error = %e,
                                "Failed to parse audio config from localStorage, using defaults"
                            );
                            AudioConfig::default()
                        }
                    }
                } else {
                    info!(target: "audio", "No audio config in localStorage, using defaults");
                    AudioConfig::default()
                }
            } else {
                warn!(target: "audio", "localStorage unavailable, using defaults");
                AudioConfig::default()
            }
        } else {
            warn!(target: "audio", "window object unavailable (WASM), using defaults");
            AudioConfig::default()
        }
    };

    commands.insert_resource(config);
}

/// Save audio configuration when it changes.
fn save_audio_config_on_change(config: Res<AudioConfig>) {
    if !config.is_changed() {
        return;
    }

    #[cfg(not(target_arch = "wasm32"))]
    {
        // Ensure config directory exists
        if let Some(parent) = std::path::Path::new(AUDIO_CONFIG_PATH).parent() {
            if let Err(e) = std::fs::create_dir_all(parent) {
                warn!(
                    target: "audio",
                    error = %e,
                    "Failed to create config directory"
                );
                return;
            }
        }

        // Serialize and save
        let content = match ron::ser::to_string_pretty(&*config, ron::ser::PrettyConfig::default())
        {
            Ok(s) => s,
            Err(e) => {
                warn!(
                    target: "audio",
                    error = %e,
                    "Failed to serialize audio config"
                );
                return;
            }
        };

        if let Err(e) = std::fs::write(AUDIO_CONFIG_PATH, content) {
            warn!(
                target: "audio",
                error = %e,
                "Failed to save audio config"
            );
        } else {
            debug!(
                target: "audio",
                volume = config.master_volume,
                muted = config.muted,
                "Saved audio config"
            );
        }
    }

    // On WASM, localStorage saving would go here
    #[cfg(target_arch = "wasm32")]
    {
        // Serialize to RON and store in localStorage under `brkrs_audio`.
        let storage_key = "brkrs_audio";
        match ron::ser::to_string_pretty(&*config, ron::ser::PrettyConfig::default()) {
            Ok(serialized) => {
                if let Some(window) = web_sys::window() {
                    match window.local_storage() {
                        Ok(Some(storage)) => {
                            if let Err(e) = storage.set_item(storage_key, &serialized) {
                                warn!(
                                    target: "audio",
                                    error = %e,
                                    "Failed to save audio config to localStorage"
                                );
                            } else {
                                debug!(
                                    target: "audio",
                                    volume = config.master_volume,
                                    muted = config.muted,
                                    "Saved audio config to localStorage"
                                );
                            }
                        }
                        _ => warn!(target: "audio", "localStorage unavailable, config not saved"),
                    }
                } else {
                    warn!(target: "audio", "window object unavailable, config not saved");
                }
            }
            Err(e) => warn!(
                target: "audio",
                error = %e,
                "Failed to serialize audio config for localStorage"
            ),
        }
    }
}

/// Load audio assets from the manifest file.
/// If an `AssetServer` resource is not available (e.g., in minimal test setups),
/// gracefully skip loading and leave `AudioAssets` empty.
fn load_audio_assets(
    asset_server: Option<Res<AssetServer>>,
    mut audio_assets: ResMut<AudioAssets>,
) {
    // If there's no AssetServer available, skip loading (graceful degradation).
    let asset_server = match asset_server {
        Some(s) => s,
        None => {
            warn!(target: "audio", "AssetServer missing; skipping audio asset loading");
            return;
        }
    };
    // Try to read the manifest file
    #[cfg(not(target_arch = "wasm32"))]
    let manifest_content = std::fs::read_to_string("assets/audio/manifest.ron");
    #[cfg(target_arch = "wasm32")]
    // Use an absolute path based on the crate root so moving this file doesn't
    // break the include. `env!("CARGO_MANIFEST_DIR")` is evaluated at compile
    // time and `concat!` produces a literal suitable for `include_str!`.
    let manifest_content: Result<&str, &str> = Ok(include_str!(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/assets/audio/manifest.ron"
    )));

    match manifest_content {
        Ok(content) => {
            #[cfg(not(target_arch = "wasm32"))]
            let content_ref = content.as_str();
            #[cfg(target_arch = "wasm32")]
            let content_ref = content;

            match from_str::<AudioManifest>(content_ref) {
                Ok(manifest) => {
                    for (sound_type, file_name) in manifest.sounds {
                        let path = format!("audio/{}", file_name);
                        let handle: Handle<AudioSource> = asset_server.load(&path);
                        audio_assets.sounds.insert(sound_type, handle);
                        debug!(
                            target: "audio",
                            ?sound_type,
                            %path,
                            "Loaded audio asset"
                        );
                    }
                    info!(
                        target: "audio",
                        count = audio_assets.sounds.len(),
                        "Audio assets loaded from manifest"
                    );
                }
                Err(e) => {
                    warn!(
                        target: "audio",
                        error = %e,
                        "Failed to parse audio manifest, audio will be disabled"
                    );
                }
            }
        }
        Err(_e) => {
            warn!(
                target: "audio",
                "Audio manifest not found, audio will be disabled"
            );
        }
    }
}

/// Play a sound of the given type, respecting volume, mute, and concurrent limits.
fn play_sound(
    sound_type: SoundType,
    config: &AudioConfig,
    assets: &AudioAssets,
    active_sounds: &mut ActiveSounds,
    active_instances: &mut ActiveAudioInstances,
    commands: &mut Commands,
) {
    // Check if muted
    if config.muted {
        return;
    }

    // Check if volume is effectively zero
    if config.master_volume <= 0.0 {
        return;
    }

    // Check concurrent limit
    if !active_sounds.try_increment(sound_type) {
        debug!(
            target: "audio",
            ?sound_type,
            "Dropped sound: concurrent limit reached"
        );
        return;
    }

    // Get the audio handle
    let Some(handle) = assets.get(sound_type) else {
        warn!(
            target: "audio",
            ?sound_type,
            "Audio asset missing"
        );
        active_sounds.decrement(sound_type);
        return;
    };

    // Spawn the audio player and record the spawned entity so we can
    // decrement the concurrent-count when playback finishes (entity despawn).
    let entity = commands
        .spawn((
            AudioPlayer::new(handle.clone()),
            PlaybackSettings {
                mode: bevy::audio::PlaybackMode::Despawn,
                volume: bevy::audio::Volume::Linear(config.master_volume),
                ..default()
            },
        ))
        .id();

    // Register the spawned entity so we can detect when it is removed
    // (playback finished or entity despawned) and decrement the count.
    active_instances.instances.insert(entity, sound_type);

    debug!(
        target: "audio",
        ?sound_type,
        volume = config.master_volume,
        "Playing sound"
    );
}

// =============================================================================
// Event definitions
// =============================================================================

/// Emitted when a destructible brick is removed from the game.
/// Used by audio system to play brick destruction sound.
#[derive(Event, Debug, Clone)]
pub struct BrickDestroyed {
    /// The entity that was destroyed.
    pub entity: Entity,
    /// The brick type that was destroyed.
    pub brick_type: u8,
}

/// Emitted when the ball bounces off a wall boundary.
/// Used by audio system to play wall bounce sound.
#[derive(Event, Debug, Clone)]
pub struct BallWallHit {
    /// The ball entity that hit the wall.
    pub entity: Entity,
    /// The collision impulse.
    pub impulse: Vec3,
}

/// Emitted when a level has finished loading and is ready for play.
/// Used by audio system to play level start sound.
#[derive(Event, Debug, Clone)]
pub struct LevelStarted {
    /// Index of the level that started.
    pub level_index: u32,
}

/// Emitted when a level has been completed (all destructible bricks destroyed).
/// Used by audio system to play level complete sound.
#[derive(Event, Debug, Clone)]
pub struct LevelCompleted {
    /// Index of the level that was completed.
    pub level_index: u32,
}

// =============================================================================
// Audio observers
// =============================================================================

/// Observer for multi-hit brick impact sound.
fn on_multi_hit_brick_sound(
    trigger: On<crate::systems::multi_hit::MultiHitBrickHit>,
    config: Res<AudioConfig>,
    assets: Res<AudioAssets>,
    mut active_sounds: ResMut<ActiveSounds>,
    mut active_instances: ResMut<ActiveAudioInstances>,
    mut commands: Commands,
) {
    let event = trigger.event();
    debug!(
        target: "audio",
        entity = ?event.entity,
        previous_type = event.previous_type,
        new_type = event.new_type,
        "Multi-hit brick impact"
    );
    play_sound(
        SoundType::MultiHitImpact,
        &config,
        &assets,
        &mut active_sounds,
        &mut active_instances,
        &mut commands,
    );
}

/// Observer for brick destruction sound.
fn on_brick_destroyed_sound(
    trigger: On<BrickDestroyed>,
    config: Res<AudioConfig>,
    assets: Res<AudioAssets>,
    mut active_sounds: ResMut<ActiveSounds>,
    mut active_instances: ResMut<ActiveAudioInstances>,
    mut commands: Commands,
) {
    let event = trigger.event();
    // Don't play destruction sound for multi-hit bricks (they use MultiHitImpact)
    if crate::level_format::is_multi_hit_brick(event.brick_type) {
        return;
    }
    debug!(
        target: "audio",
        entity = ?event.entity,
        brick_type = event.brick_type,
        "Brick destroyed"
    );
    play_sound(
        SoundType::BrickDestroy,
        &config,
        &assets,
        &mut active_sounds,
        &mut active_instances,
        &mut commands,
    );
}

/// Observer for ball wall hit sound.
fn on_ball_wall_hit_sound(
    trigger: On<BallWallHit>,
    config: Res<AudioConfig>,
    assets: Res<AudioAssets>,
    mut active_sounds: ResMut<ActiveSounds>,
    mut active_instances: ResMut<ActiveAudioInstances>,
    mut commands: Commands,
) {
    let event = trigger.event();
    debug!(
        target: "audio",
        entity = ?event.entity,
        impulse = ?event.impulse,
        "Ball wall hit"
    );
    play_sound(
        SoundType::WallBounce,
        &config,
        &assets,
        &mut active_sounds,
        &mut active_instances,
        &mut commands,
    );
}

/// Observer for paddle-ball hit sound (ball bounces off paddle).
fn on_paddle_ball_hit_sound(
    trigger: On<crate::BallHit>,
    config: Res<AudioConfig>,
    assets: Res<AudioAssets>,
    mut active_sounds: ResMut<ActiveSounds>,
    mut active_instances: ResMut<ActiveAudioInstances>,
    mut commands: Commands,
) {
    let event = trigger.event();
    debug!(
        target: "audio",
        ball = ?event.ball,
        impulse = ?event.impulse,
        "Paddle-ball hit"
    );
    play_sound(
        SoundType::PaddleHit,
        &config,
        &assets,
        &mut active_sounds,
        &mut active_instances,
        &mut commands,
    );
}

/// Observer for paddle-wall hit sound.
fn on_paddle_wall_hit_sound(
    trigger: On<crate::WallHit>,
    config: Res<AudioConfig>,
    assets: Res<AudioAssets>,
    mut active_sounds: ResMut<ActiveSounds>,
    mut active_instances: ResMut<ActiveAudioInstances>,
    mut commands: Commands,
) {
    let event = trigger.event();
    debug!(
        target: "audio",
        impulse = ?event.impulse,
        "Paddle-wall hit"
    );
    play_sound(
        SoundType::PaddleWallHit,
        &config,
        &assets,
        &mut active_sounds,
        &mut active_instances,
        &mut commands,
    );
}

/// Observer for paddle-brick hit sound.
fn on_paddle_brick_hit_sound(
    trigger: On<crate::BrickHit>,
    config: Res<AudioConfig>,
    assets: Res<AudioAssets>,
    mut active_sounds: ResMut<ActiveSounds>,
    mut active_instances: ResMut<ActiveAudioInstances>,
    mut commands: Commands,
) {
    let event = trigger.event();
    debug!(
        target: "audio",
        impulse = ?event.impulse,
        "Paddle-brick hit"
    );
    play_sound(
        SoundType::PaddleBrickHit,
        &config,
        &assets,
        &mut active_sounds,
        &mut active_instances,
        &mut commands,
    );
}

/// Observer for level started sound.
fn on_level_started_sound(
    trigger: On<LevelStarted>,
    config: Res<AudioConfig>,
    assets: Res<AudioAssets>,
    mut active_sounds: ResMut<ActiveSounds>,
    mut active_instances: ResMut<ActiveAudioInstances>,
    mut commands: Commands,
) {
    let event = trigger.event();
    debug!(
        target: "audio",
        level_index = event.level_index,
        "Level started"
    );
    play_sound(
        SoundType::LevelStart,
        &config,
        &assets,
        &mut active_sounds,
        &mut active_instances,
        &mut commands,
    );
}

/// Observer for level complete sound.
fn on_level_complete_sound(
    trigger: On<LevelCompleted>,
    config: Res<AudioConfig>,
    assets: Res<AudioAssets>,
    mut active_sounds: ResMut<ActiveSounds>,
    mut active_instances: ResMut<ActiveAudioInstances>,
    mut commands: Commands,
) {
    let event = trigger.event();
    debug!(
        target: "audio",
        level_index = event.level_index,
        "Level completed"
    );
    play_sound(
        SoundType::LevelComplete,
        &config,
        &assets,
        &mut active_sounds,
        &mut active_instances,
        &mut commands,
    );
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn audio_config_default_values() {
        let config = AudioConfig::default();
        assert_eq!(config.master_volume, 1.0);
        assert!(!config.muted);
        assert!(config.is_valid());
    }

    #[test]
    fn audio_config_new_clamps_volume() {
        let config = AudioConfig::new(1.5, false);
        assert_eq!(config.master_volume, 1.0);

        let config = AudioConfig::new(-0.5, false);
        assert_eq!(config.master_volume, 0.0);
    }

    #[test]
    fn audio_config_set_volume_clamps() {
        let mut config = AudioConfig::default();
        config.set_volume(2.0);
        assert_eq!(config.master_volume, 1.0);

        config.set_volume(-1.0);
        assert_eq!(config.master_volume, 0.0);

        config.set_volume(0.5);
        assert_eq!(config.master_volume, 0.5);
    }

    #[test]
    fn audio_config_toggle_mute() {
        let mut config = AudioConfig::default();
        assert!(!config.muted);

        let muted = config.toggle_mute();
        assert!(muted);
        assert!(config.muted);

        let muted = config.toggle_mute();
        assert!(!muted);
        assert!(!config.muted);
    }

    #[test]
    fn active_sounds_respects_limit() {
        let mut active = ActiveSounds::default();

        // First 4 should succeed
        for _ in 0..4 {
            assert!(active.try_increment(SoundType::BrickDestroy));
        }

        // 5th should fail
        assert!(!active.try_increment(SoundType::BrickDestroy));
        assert_eq!(active.count(SoundType::BrickDestroy), 4);

        // Decrement and try again
        active.decrement(SoundType::BrickDestroy);
        assert_eq!(active.count(SoundType::BrickDestroy), 3);
        assert!(active.try_increment(SoundType::BrickDestroy));
    }

    #[test]
    fn active_sounds_tracks_types_independently() {
        let mut active = ActiveSounds::default();

        // Fill up one type
        for _ in 0..4 {
            assert!(active.try_increment(SoundType::BrickDestroy));
        }

        // Other type should still work
        assert!(active.try_increment(SoundType::WallBounce));
        assert_eq!(active.count(SoundType::WallBounce), 1);
    }

    #[test]
    fn brick_destroyed_event_fields() {
        let event = BrickDestroyed {
            entity: Entity::PLACEHOLDER,
            brick_type: 20,
        };
        assert_eq!(event.brick_type, 20);
    }

    #[test]
    fn ball_wall_hit_event_fields() {
        let event = BallWallHit {
            entity: Entity::PLACEHOLDER,
            impulse: Vec3::new(1.0, 0.0, 0.0),
        };
        assert_eq!(event.impulse.x, 1.0);
    }

    #[test]
    fn level_started_event_fields() {
        let event = LevelStarted { level_index: 5 };
        assert_eq!(event.level_index, 5);
    }

    #[test]
    fn level_completed_event_fields() {
        let event = LevelCompleted { level_index: 3 };
        assert_eq!(event.level_index, 3);
    }
}