diff --git a/.gitignore b/.gitignore index cb7a9e1..a2a03da 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ # IntelliJ folder .idea + +*.iml diff --git a/resources/assets/ambient.ogg b/resources/assets/day_ambient.ogg similarity index 100% rename from resources/assets/ambient.ogg rename to resources/assets/day_ambient.ogg diff --git a/resources/assets/night_ambient.ogg b/resources/assets/night_ambient.ogg new file mode 100644 index 0000000..fafadf6 Binary files /dev/null and b/resources/assets/night_ambient.ogg differ diff --git a/src/main.rs b/src/main.rs index cf284b3..6d23658 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,18 +3,7 @@ extern crate amethyst_derive; use amethyst; use amethyst::assets::PrefabLoaderSystem; -use amethyst::{ - audio::AudioBundle, - core::frame_limiter::FrameRateLimitStrategy, - core::transform::{Transform, TransformBundle}, - core::Named, - ecs::*, - input::InputBundle, - prelude::*, - renderer::*, - ui::{DrawUi, NoCustomUi, UiBundle, UiTransform}, - utils::application_root_dir, -}; +use amethyst::{audio::AudioBundle, core::frame_limiter::FrameRateLimitStrategy, core::transform::{Transform, TransformBundle}, core::Named, ecs::*, input::InputBundle, prelude::*, renderer::*, ui::{DrawUi, NoCustomUi, UiBundle, UiTransform}, utils::application_root_dir, LoggerConfig}; mod components; mod resources; @@ -28,6 +17,7 @@ use crate::components::creatures::{self, IntelligenceTag, Movement, Wander}; use crate::components::digestion::{Digestion, Fullness, Nutrition}; use crate::resources::audio::Music; use crate::states::{loading::LoadingState, CustomStateEvent, CustomStateEventReader}; +use crate::systems::day_night_cycle::DayNightCycle; amethyst_inspector::inspector![ Named, @@ -49,7 +39,12 @@ amethyst_inspector::inspector![ ]; fn main() -> amethyst::Result<()> { - amethyst::start_logger(Default::default()); + amethyst::start_logger(LoggerConfig { + stdout: amethyst::StdoutLog::Colored, + level_filter: amethyst::LogLevelFilter::Off, + log_file: None, + allow_env_override: true, + }); let resources = application_root_dir().clone() + "/resources"; let display_config_path = resources.clone() + "/display_config.ron"; @@ -90,7 +85,7 @@ fn main() -> amethyst::Result<()> { &[], ) .with_bundle(TransformBundle::new())? - .with_bundle(AudioBundle::new(|music: &mut Music| music.music.next()))? + .with_bundle(AudioBundle::new(|music: &mut Music| music.get_current()))? .with( amethyst_inspector::InspectorHierarchy::default(), "inspector_hierarchy", @@ -104,7 +99,12 @@ fn main() -> amethyst::Result<()> { &["imgui_begin"], ) .with_bundle(RenderBundle::new(pipe, Some(display_config)))? - .with_bundle(UiBundle::::new())?; + .with_bundle(UiBundle::::new())? + .with( + DayNightCycle::new(), + "day_night_cycle", + &[] + ); // Set up the core application with a custom state event that allows us to access input events // in the game states. The `CustomStateEventReader` is automatically derived based on `CustomStateEvent`. diff --git a/src/resources/audio.rs b/src/resources/audio.rs index be1ca66..6cf4143 100644 --- a/src/resources/audio.rs +++ b/src/resources/audio.rs @@ -6,11 +6,30 @@ use amethyst::{ use std::iter::Cycle; use std::vec::IntoIter; +use std::collections::HashMap; -const BACKGROUND_MUSIC: &'static [&'static str] = &["assets/ambient.ogg"]; +const DAY_BACKGROUND_MUSIC: &'static str = "assets/day_ambient.ogg"; +const NIGHT_BACKGROUND_MUSIC: &'static str = "assets/night_ambient.ogg"; +#[derive(Default)] pub struct Music { - pub music: Cycle>, + + pub musics: HashMap, + pub current_music: Option, +} + +impl Music { + pub fn get_current(& self) -> Option{ + match &self.current_music { + None => {None}, + Some(cur) => {self.musics.get(cur).cloned()}, + } + + } + + pub fn set_current(&mut self, new_music_name: Option){ + self.current_music = new_music_name; + } } fn load_audio_track(loader: &Loader, world: &World, file: &str) -> SourceHandle { @@ -25,14 +44,13 @@ pub fn initialise_audio(world: &mut World) { let mut sink = world.write_resource::(); sink.set_volume(0.25); - let music = BACKGROUND_MUSIC - .iter() - .map(|file| load_audio_track(&loader, &world, &file)) - .collect::>() - .into_iter() - .cycle(); - Music { music } + + let mut musics = HashMap::new(); + musics.insert("day".into(), load_audio_track(&loader, &world, DAY_BACKGROUND_MUSIC)); + musics.insert("night".into(), load_audio_track(&loader, &world, NIGHT_BACKGROUND_MUSIC)); + + Music { musics: musics, current_music: Some("day".into()), } }; // Add sounds to the world diff --git a/src/systems/day_night_cycle.rs b/src/systems/day_night_cycle.rs new file mode 100644 index 0000000..ecdbe3c --- /dev/null +++ b/src/systems/day_night_cycle.rs @@ -0,0 +1,64 @@ +use amethyst::{ + core::{Time}, + ecs::*, + audio::{SourceHandle}, +}; + +use crate::Music; + +pub struct DayNightCycle{ + pub cycle_time: f32, + pub elapsed_cycle_time: f32, + pub current_cycle: CycleState, + pub cycle_data: CycleData, +} + +impl DayNightCycle { + pub fn new() -> Self { + DayNightCycle{ + cycle_time: 6.0, // FIXME 1 second for testing + current_cycle : CycleState::Day, + elapsed_cycle_time: 0.0, + cycle_data: CycleData { + day_music_name: "day".into(), + night_music_name: "night".into(), + } + } + } +} + +#[derive(Copy,Clone,Debug)] +pub enum CycleState{ + Day, + Night, +} + +pub struct CycleData{ + pub day_music_name: String, + pub night_music_name: String, +} + +impl<'s> System<'s> for DayNightCycle{ + type SystemData = ( Read<'s, Time>, Write<'s, Music> ); + + + fn run(&mut self, (time, mut music): Self::SystemData) { + self.elapsed_cycle_time += time.delta_real_seconds(); + if self.elapsed_cycle_time >= self.cycle_time { + dbg!(&self.current_cycle); + match self.current_cycle { + CycleState::Day => { + self.current_cycle = CycleState::Night; + music.set_current(Some(self.cycle_data.night_music_name.clone())); + + }, + CycleState::Night => { + self.current_cycle = CycleState::Day; + music.set_current(Some(self.cycle_data.day_music_name.clone())); + }, + } + self.elapsed_cycle_time = 0.0; + } + } + +} \ No newline at end of file diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 86b3356..fac1375 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -2,6 +2,7 @@ pub mod behaviors; pub mod camera_movement; pub mod collision; pub mod combat; +pub mod day_night_cycle; pub mod debug; pub mod digestion; pub mod health;