nightshade 0.13.3

A cross-platform data-oriented game engine.
Documentation
//! Audio component definitions.

/// Audio playback component.
///
/// Attach to an entity to play audio. For spatial audio, also requires
/// a transform component for 3D positioning relative to the [`AudioListener`].
#[derive(Debug, Clone, Default)]
pub struct AudioSource {
    /// Name of the audio asset to play.
    pub audio_ref: Option<String>,
    /// Playback volume (0.0 = silent, 1.0 = full volume).
    pub volume: f32,
    /// Whether to loop the audio continuously.
    pub looping: bool,
    /// Whether the audio is currently playing.
    pub playing: bool,
    /// Whether to apply 3D spatial positioning based on transform.
    pub spatial: bool,
    /// Whether to apply reverb effect.
    pub reverb: bool,
}

impl AudioSource {
    /// Creates an audio source referencing the given asset name.
    pub fn new(audio_ref: impl Into<String>) -> Self {
        Self {
            audio_ref: Some(audio_ref.into()),
            volume: 1.0,
            looping: false,
            playing: false,
            spatial: false,
            reverb: false,
        }
    }

    /// Sets the playback volume.
    pub fn with_volume(mut self, volume: f32) -> Self {
        self.volume = volume;
        self
    }

    /// Sets whether the audio should loop.
    pub fn with_looping(mut self, looping: bool) -> Self {
        self.looping = looping;
        self
    }

    /// Enables or disables spatial audio.
    pub fn with_spatial(mut self, spatial: bool) -> Self {
        self.spatial = spatial;
        self
    }

    /// Enables or disables reverb effect.
    pub fn with_reverb(mut self, reverb: bool) -> Self {
        self.reverb = reverb;
        self
    }

    /// Marks the audio to start playing immediately.
    pub fn playing(mut self) -> Self {
        self.playing = true;
        self
    }
}

/// Marker component for the spatial audio listener.
///
/// Attach to an entity (typically the camera) to define the position and
/// orientation from which spatial audio is heard. Only one listener should
/// be active at a time.
#[derive(Debug, Clone, Copy, Default)]
pub struct AudioListener;