Skip to content
This repository has been archived by the owner on Oct 25, 2021. It is now read-only.

Music and infrastructure for day-night cycle #102

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions resources/assets/ambient_night.ogg
Git LFS file not shown
9 changes: 9 additions & 0 deletions resources/input.ron
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,14 @@
"CameraMoveBackward": [
[Key(LShift), Key(Down)]
],
"TogglePauseMenu": [
[Key(Escape)]
],
"GoodMorning": [
[Key(LAlt), Key(M)]
],
"GoodNight": [
[Key(LAlt), Key(N)]
],
},
)
2 changes: 1 addition & 1 deletion resources/prefabs/ui/controls.ron
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Container(
id: "done button",
x: 0.0,
y: 50.0,
z: 10.0,
z: 15.0,
width: 100.0,
height: 50.0,
anchor: BottomMiddle,
Expand Down
4 changes: 2 additions & 2 deletions resources/prefabs/ui/controls_row.ron
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ Label(
id: "controls_row",
anchor: TopMiddle,
width: 800.0,
height: 100.0,
height: 70.0,
z: 10.0,
),
text: (
text: "CONTROLS ROW PLACEHOLDER",
font: File("assets/fonts/OpenSans-Regular.ttf", ("TTF", ())),
font_size: 30.0,
font_size: 25.0,
color: (0.0, 0.0, 0.0, 1.0),
)
)
16 changes: 10 additions & 6 deletions src/components/combat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use amethyst::{
//use amethyst_inspector::Inspect;
use log::error;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use std::{collections::HashMap, ops::DerefMut, time::Duration};

#[derive(Default, Debug, Clone, Deserialize, Serialize, PrefabData)]
#[prefab(Component)]
Expand Down Expand Up @@ -211,10 +210,15 @@ pub struct Factions(HashMap<String, Entity>);
// factions as prey that are on top of their definition. For example, 'Plants' cannot define 'Herbivores' as their prey
// because 'Herbivores' is defined after 'Plants'.
pub fn load_factions(world: &mut World) {
let prefab_handle = world.exec(|loader: PrefabLoader<'_, FactionPrefabData>| {
loader.load("prefabs/factions.ron", RonFormat, ())
});

world
.res
.entry::<ProgressCounter>()
.or_insert(ProgressCounter::default());
let prefab_handle = world.exec(
|(loader, mut progress): (PrefabLoader<FactionPrefabData>, Write<ProgressCounter>)| {
loader.load("prefabs/factions.ron", RonFormat, progress.deref_mut())
},
);
world.create_entity().with(prefab_handle.clone()).build();
}

Expand Down
5 changes: 5 additions & 0 deletions src/events/day_night_cycle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[derive(Debug, Copy, Clone)]
pub enum DayNightCycleEvent {
GoodMorning,
GoodNight,
}
6 changes: 6 additions & 0 deletions src/events/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// event producers and consumers will both depend on the same event enumeration
// there may be multiple producers; there may be multiple consumers
// that is, there is no clear owner
// thus, events are defined in this directory independent of their producers and consumers

pub mod day_night_cycle;
28 changes: 13 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#[macro_use]
extern crate log;

use amethyst::assets::PrefabLoaderSystem;
use amethyst::{
assets::Processor,
audio::{AudioBundle, DjSystem},
core::frame_limiter::FrameRateLimitStrategy,
core::transform::TransformBundle,
assets::{PrefabLoaderSystem, Processor},
audio::AudioBundle,
core::{frame_limiter::FrameRateLimitStrategy, transform::TransformBundle},
gltf::GltfSceneLoaderSystem,
input::{InputBundle, StringBindings},
prelude::*,
Expand All @@ -20,16 +18,20 @@ use amethyst::{
};

mod components;
mod events;
mod render_graph;
mod resources;
mod states;
mod systems;
mod utils;

use crate::components::{combat, creatures};
use crate::render_graph::RenderGraph;
use crate::resources::audio::Music;
use crate::states::loading::LoadingState;
use crate::{
components::{combat, creatures},
events::day_night_cycle,
render_graph::RenderGraph,
states::loading::LoadingState,
systems::music::MusicSystem,
};

fn main() -> amethyst::Result<()> {
amethyst::start_logger(Default::default());
Expand Down Expand Up @@ -67,11 +69,6 @@ fn main() -> amethyst::Result<()> {
"",
&[],
)
.with(
DjSystem::new(|music: &mut Music| music.music.next()),
"dj",
&[],
)
.with_bundle(TransformBundle::new())?
.with_bundle(AudioBundle::default())?
.with_bundle(WindowBundle::from_config(display_config))?
Expand All @@ -93,7 +90,8 @@ fn main() -> amethyst::Result<()> {
)
.with_thread_local(RenderingSystem::<DefaultBackend, _>::new(
RenderGraph::default(),
));
))
.with(MusicSystem::default(), "music_system", &[]);

// Set up the core application.
let mut game: Application<GameData> =
Expand Down
40 changes: 0 additions & 40 deletions src/resources/audio.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/resources/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod audio;
pub mod debug;
pub mod prefabs;
pub mod world_bounds;
Expand Down
66 changes: 40 additions & 26 deletions src/resources/prefabs.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::collections::HashMap;
use std::fs::read_dir;
use std::{collections::HashMap, fs::read_dir, ops::DerefMut};

use crate::components::creatures::CreaturePrefabData;
use amethyst::{
assets::{AssetStorage, Handle, Prefab, PrefabLoader, ProgressCounter, RonFormat},
ecs::World,
ecs::{World, Write},
ui::{UiLoader, UiPrefab},
utils::application_root_dir,
};
Expand Down Expand Up @@ -73,29 +72,41 @@ fn make_name(subdirectory: &str, entry: &std::fs::DirEntry) -> String {
// These prefabs are then stored in a resource of type CreaturePrefabs that is used by the spawner system.
// At initialization time, we put temporary keys for the prefabs since they're not loaded yet.
// When their loading is finished, we read the name of the entity inside to change the keys. This is done in the update_prefabs function.
pub fn initialize_prefabs(world: &mut World) -> ProgressCounter {
let mut progress_counter = ProgressCounter::new();
pub fn initialize_prefabs(world: &mut World) {
world
.res
.entry::<ProgressCounter>()
.or_insert(ProgressCounter::default());

// load ui prefabs
{
let mut ui_prefab_registry = UiPrefabRegistry::default();
world
.res
.entry::<UiPrefabRegistry>()
.or_insert(UiPrefabRegistry::default());
let prefab_dir_path = application_root_dir()
.unwrap()
.into_os_string()
.into_string()
.unwrap()
+ "/resources/prefabs/ui";
let prefab_iter = read_dir(prefab_dir_path).unwrap();
ui_prefab_registry.prefabs = prefab_iter
.map(|prefab_dir_entry| {
world.exec(|loader: UiLoader<'_>| {
loader.load(
make_name("prefabs/ui/", &prefab_dir_entry.unwrap()),
&mut progress_counter,
)
})
})
.collect::<Vec<Handle<UiPrefab>>>();
world.add_resource(ui_prefab_registry);
world.exec(
|(mut ui_prefab_registry, loader, mut progress): (
Write<UiPrefabRegistry>,
UiLoader,
Write<ProgressCounter>,
)| {
ui_prefab_registry.deref_mut().prefabs = prefab_iter
.map(|prefab_dir_entry| {
loader.load(
make_name("prefabs/ui/", &prefab_dir_entry.unwrap()),
progress.deref_mut(),
)
})
.collect::<Vec<Handle<UiPrefab>>>();
},
);
}

// load creature prefabs
Expand All @@ -109,13 +120,18 @@ pub fn initialize_prefabs(world: &mut World) -> ProgressCounter {
+ "/resources/prefabs/creatures";
let prefab_iter = read_dir(prefab_dir_path).unwrap();
prefab_iter.map(|prefab_dir_entry| {
world.exec(|loader: PrefabLoader<'_, CreaturePrefabData>| {
loader.load(
make_name("prefabs/creatures/", &prefab_dir_entry.unwrap()),
RonFormat,
&mut progress_counter,
)
})
world.exec(
|(loader, mut progress): (
PrefabLoader<'_, CreaturePrefabData>,
Write<ProgressCounter>,
)| {
loader.load(
make_name("prefabs/creatures/", &prefab_dir_entry.unwrap()),
RonFormat,
progress.deref_mut(),
)
},
)
})
};

Expand All @@ -125,8 +141,6 @@ pub fn initialize_prefabs(world: &mut World) -> ProgressCounter {
}
world.add_resource(creature_prefabs);
}

progress_counter
}

// Once the prefabs are loaded, this function is called to update the ekeys in the CreaturePrefabs struct.
Expand Down
2 changes: 1 addition & 1 deletion src/states/controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl ControlsState {
bindings
};
let y = -150.; // start below our title label
let y_step = -40.;
let y_step = -30.;
(
&world.entities(),
&mut world.write_storage::<UiTransform>(),
Expand Down
44 changes: 17 additions & 27 deletions src/states/loading.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,48 @@
use crate::{
components::combat::load_factions,
resources::{
audio::initialise_audio,
prefabs::{initialize_prefabs, update_prefabs},
world_bounds::WorldBounds,
},
states::{main_game::MainGameState, menu::MenuState},
};
use std::env;

use crate::components::combat::load_factions;
use amethyst::{
assets::ProgressCounter,
prelude::*,
renderer::debug_drawing::{DebugLines, DebugLinesParams},
};
use std::env;

const SKIP_MENU_ARG: &str = "no_menu";

pub struct LoadingState {
prefab_loading_progress: Option<ProgressCounter>,
}

impl Default for LoadingState {
fn default() -> Self {
LoadingState {
prefab_loading_progress: None,
}
}
}
#[derive(Default)]
pub struct LoadingState {}

impl SimpleState for LoadingState {
fn on_start(&mut self, mut data: StateData<GameData>) {
data.world
.res
.entry::<ProgressCounter>()
.or_insert(ProgressCounter::default());
load_factions(data.world);
self.prefab_loading_progress = Some(initialize_prefabs(&mut data.world));
initialise_audio(data.world);
initialize_prefabs(&mut data.world);
data.world
.add_resource(DebugLinesParams { line_width: 1.0 });

data.world.add_resource(DebugLines::new());
data.world
.add_resource(WorldBounds::new(-10.0, 10.0, -10.0, 10.0));
}

fn update(&mut self, data: &mut StateData<GameData>) -> SimpleTrans {
data.data.update(&data.world);
if let Some(ref counter) = self.prefab_loading_progress.as_ref() {
if counter.is_complete() {
self.prefab_loading_progress = None;
update_prefabs(&mut data.world);
if env::args().any(|arg| arg == SKIP_MENU_ARG) {
return Trans::Switch(Box::new(MainGameState::new(data.world)));
} else {
return Trans::Switch(Box::new(MenuState::default()));
}
if data.world.read_resource::<ProgressCounter>().is_complete() {
info!("loading complete");
update_prefabs(&mut data.world);
// TODO how to reset the ProgressCounter? data.world.write_resource::<Option<ProgressCounter>>().as_mut() = Some(ProgressCounter::new());
if env::args().any(|arg| arg == SKIP_MENU_ARG) {
return Trans::Switch(Box::new(MainGameState::new(data.world)));
} else {
return Trans::Switch(Box::new(MenuState::default()));
}
}

Expand Down
Loading