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
use amethyst::{
assets::AssetStorage,
audio::{AudioSink, output::Output, Source},
ecs::{Read, ReadExpect, System, Write, WriteExpect},
};
use crate::{
audio::Music,
config::GameConfig,
};
#[derive(Default)]
pub struct AudioSystem;
impl<'a> System<'a> for AudioSystem {
type SystemData = (
Read<'a, AssetStorage<Source>>,
Option<Write<'a, AudioSink>>,
Read<'a, Output>,
ReadExpect<'a, GameConfig>,
WriteExpect<'a, Music>,
);
fn run(&mut self, (storage, sink, output, game_config, mut music): Self::SystemData) {
if game_config.play_bgm {
if let Some(mut sink) = sink {
match (sink.empty(), music.changed_bgm()) {
(true, _) => {
if let Some(source) = music.next().and_then(|h| storage.get(&h)) {
sink.append(source).unwrap();
}
},
(false, true) => {
sink.stop();
*sink = AudioSink::new(&output);
if let Some(source) = music.next().and_then(|h| storage.get(&h)) {
sink.append(source).unwrap();
}
},
(false, false) => { },
}
}
}
}
}