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
//! `amethyst` audio ecs components

pub use self::{audio_emitter::AudioEmitter, audio_listener::AudioListener};

use amethyst_assets::PrefabData;
use amethyst_core::{
    ecs::prelude::{Entity, Read, WriteStorage},
    math::Point3,
};
use amethyst_error::Error;

use serde::{Deserialize, Serialize};

use crate::output::Output;

mod audio_emitter;
mod audio_listener;

/// `PrefabData` for loading audio components
///
/// For `AudioListener`, the currently registered `Output` in the `World` will be used.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AudioPrefab {
    emitter: bool,
    /// Left, Right
    listener: Option<(Point3<f32>, Point3<f32>)>,
}

impl<'a> PrefabData<'a> for AudioPrefab {
    type SystemData = (
        WriteStorage<'a, AudioEmitter>,
        WriteStorage<'a, AudioListener>,
        Option<Read<'a, Output>>,
    );
    type Result = ();

    fn add_to_entity(
        &self,
        entity: Entity,
        system_data: &mut Self::SystemData,
        _: &[Entity],
        _: &[Entity],
    ) -> Result<(), Error> {
        if self.emitter {
            system_data.0.insert(entity, AudioEmitter::default())?;
        }
        if let Some((left_ear, right_ear)) = self.listener {
            system_data.1.insert(
                entity,
                AudioListener {
                    left_ear,
                    right_ear,
                },
            )?;
        }
        Ok(())
    }
}