Skip to content

Instantly share code, notes, and snippets.

@ndarilek
Created February 28, 2019 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndarilek/fb6123c85956c2a458e1e2a057c11afe to your computer and use it in GitHub Desktop.
Save ndarilek/fb6123c85956c2a458e1e2a057c11afe to your computer and use it in GitHub Desktop.
```
pub struct AudioEmitter {
context: Context,
sources: HashMap<String, Arc<Mutex<StaticSource>>>,
}
impl AudioEmitter {
pub fn new(context: Context) -> Self {
AudioEmitter {
context,
sources: HashMap::new(),
}
}
...
}
impl MainState {
...
fn init_player(&self, world: &mut World, width: f32, height: f32) -> amethyst::Result<()> {
let mut start_transform = Transform::default();
start_transform.set_xyz(width / 2., height / 2., 0.);
let cockpit = {
let sounds = world.read_resource::<Sounds>();
sounds.cockpit.clone()
};
let mut emitter = AudioEmitter::new(&world.read_resource());
emitter.insert_and_play("cockpit", cockpit)?;
emitter.set_gain("cockpit", 0.5)?;
emitter.set_loop("cockpit", true)?;
let fire = {
let sounds = world.read_resource::<Sounds>();
sounds.fire.clone()
};
emitter.insert("fire", fire);
world
.create_entity()
.with(Player)
.with(start_transform)
.with(AudioListener)
.with(emitter)
.build();
Ok(())
}
}
impl SimpleState for MainState {
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
let StateData { world, .. } = data;
...
self.init_player(world, width, height)
.expect("Failed to initialize player");
}
...
}
impl<'s> System<'s> for AudioSystem {
type SystemData = (
Entities<'s>,
Read<'s, AssetStorage<Audio>>,
ReadStorage<'s, Transform>,
ReadExpect<'s, Context>,
ReadStorage<'s, AudioListener>,
WriteStorage<'s, AudioEmitter>,
);
...
fn setup(&mut self, res: &mut Resources) {
Self::SystemData::setup(res);
let alto = Alto::load_default().unwrap();
let device = alto.open(None).unwrap();
let context = device.new_context(None).unwrap();
res.insert(context);
}
}
}
impl<'a, 'b> bundle::SystemBundle<'a, 'b> for AudioBundle {
fn build(self, builder: &mut DispatcherBuilder<'a, 'b>) -> amethyst::Result<()> {
builder.add(AudioSystem::default(), "audio_system", &[]);
builder.add(Processor::<Audio>::new(), "audio_processor", &[]);
Ok(())
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment