amethyst / evoli

An ecosystem-simulation game made with Amethyst
https://community.amethyst.rs/t/evoli-introduction/770
Other
217 stars 33 forks source link

Music and infrastructure for day-night cycle #102

Open chrispetkau opened 5 years ago

chrispetkau commented 5 years ago

https://github.com/amethyst/evoli/issues/76

Added events for GoodMorning and GoodNight, and hooked them up to hotkeys LAlt-M and LAlt-N respectively. MusicSystem responds to DayNightCycleEvents by playing the appropriate music.

chrispetkau commented 5 years ago

I encountered several misleading things during this task: 1) DjSystem doesn't do what you want it to do. 2) The Music resource (in resources/audio.rs) only existed to try to make the DjSystem do what you want it to do. 3) audio::output::init_output() adds an AudioSink as a Resource, but AudioSinks are not re-usable: you need to create a new one every time you need to create a new sound. The engine doing the wrong thing here really sent me off in the wrong direction. 4) There is no #audio channel in the Development section of the Amethyst Discord. Red flag.

Once I came to all these conclusions, the code more or less fell into place. I wrote a MusicSystem which adds audio::output::Output as a Resource and then creates AudioSinks as necessary to effect playback.

And this bit of code is why I persist with Rust despite the ongoing borrow-checker battles and 5-minute compiles:

self.audio_sink = self
    .audio_source
    .as_ref()
    .and_then(|audio_source| audio_source_storage.get(audio_source))
    .and_then(|audio_source| {
        let mut audio_sink = AudioSink::new(&audio_output);
        audio_sink.set_volume(0.25);
        audio_sink.append(audio_source).ok().and(Some(audio_sink))
    });

Terse, all errors considered and handled, not an if or extraneous indentation in sight! :D