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
//! Changes the current background music.

use amethyst::{
    assets::{AssetStorage, Loader},
    audio::Source,
    ecs::{World, WorldExt},
};

use crate::audio::{AudioFileFormat, Music};

use super::{BoxedGameEvent, ExecutionConditions, GameEvent};

#[derive(Clone)]
pub struct BgmChangeEvent {
    filename: String,
    format: AudioFileFormat,
}

impl BgmChangeEvent {
    pub fn new(filename: impl Into<String>, format: AudioFileFormat) -> BgmChangeEvent {
        BgmChangeEvent {
            filename: filename.into(),
            format,
        }
    }
}

impl GameEvent for BgmChangeEvent {
    fn boxed_clone(&self) -> BoxedGameEvent {
        Box::new(self.clone())
    }

    fn get_execution_conditions(&self) -> ExecutionConditions {
        ExecutionConditions {
            requires_disabled_input: false,
        }
    }

    fn start(&mut self, _world: &mut World) { }

    fn tick(&mut self, world: &mut World, _disabled_inputs: bool) {
        let mut music = world.write_resource::<Music>();
        let filename = format!("bgm/{}", self.filename);
        let format = self.format.clone();
        let loader = world.read_resource::<Loader>();
        let storage = world.read_resource::<AssetStorage<Source>>();

        music.play_bgm(filename, format, &loader, &storage);
    }

    fn is_complete(&self, _world: &mut World) -> bool {
        true
    }
}