Skip to content

Commit

Permalink
Move assets to extras, turn extras into plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshiew committed Jan 8, 2025
1 parent 839c38d commit 9120256
Show file tree
Hide file tree
Showing 31 changed files with 63 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/extras/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ publish = false

[dependencies]
ahash = { workspace = true }
bevy = { workspace = true }
serde = { workspace = true, features = ["derive"] }
tracing = { workspace = true }
strum = { workspace = true, features = ["derive"] }

infinigen-common = { path = "../common" }
infinigen-plugins = { path = "../plugins" }

noise = "0.9.0"
splines = "4.4.0"
Expand Down
File renamed without changes.
25 changes: 25 additions & 0 deletions crates/extras/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
//! Default world generators and block definitions.
use std::str::FromStr;

use bevy::prelude::*;
use infinigen_plugins::assets::DefaultBlockTypes;
use infinigen_plugins::world::WorldInitializer;
use worldgen::WorldGenTypes;

pub mod blocks;
pub mod worldgen;

pub struct ExtrasPlugin;

impl Plugin for ExtrasPlugin {
fn build(&self, app: &mut App) {
tracing::info!("Initializing extras plugin");
app.insert_resource(DefaultBlockTypes(crate::blocks::block_types().collect()))
.insert_resource(WorldInitializer(Box::new(
move |world_gen_name: &str, seed, palette| {
let world_gen_type =
WorldGenTypes::from_str(world_gen_name).unwrap_or_else(|_| {
panic!("couldn't parse world gen type from {}", world_gen_name)
});
world_gen_type.as_world_gen(seed, palette)
},
)));
}
}
8 changes: 6 additions & 2 deletions crates/extras/src/worldgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ pub mod mountain_islands;
pub mod single_block;

#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, EnumString)]
pub enum WorldGenTypes {
pub(crate) enum WorldGenTypes {
Flat,
#[default]
MountainIslands,
SingleBlock,
}

impl WorldGenTypes {
pub fn as_world_gen(&self, seed: u32, palette: Palette) -> Arc<dyn WorldGen + Send + Sync> {
pub(crate) fn as_world_gen(
&self,
seed: u32,
palette: Palette,
) -> Arc<dyn WorldGen + Send + Sync> {
match self {
WorldGenTypes::Flat => Arc::new(flat::Flat::from(palette)),
WorldGenTypes::MountainIslands => {
Expand Down
18 changes: 3 additions & 15 deletions crates/infinigen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::str::FromStr;

use bevy::prelude::*;
use infinigen_extras::worldgen::WorldGenTypes;
use infinigen_plugins::assets::AssetSettings;
use infinigen_plugins::camera::setup::CameraSettings;
use infinigen_plugins::scene::{self, SceneSettings};
use infinigen_plugins::world::{self, WorldSettings};
Expand All @@ -23,14 +19,6 @@ impl AppPlugin {
impl Plugin for AppPlugin {
fn build(&self, app: &mut App) {
tracing::info!("Initializing app plugin with config: {:#?}", self.settings);
let world_setting = self.settings.world.clone();
let seed = self.settings.seed.to_owned() as u32;
let world = Box::new(move |palette| {
let world_gen_type = WorldGenTypes::from_str(&world_setting).unwrap_or_else(|_| {
panic!("couldn't parse world gen type from {}", &world_setting)
});
world_gen_type.as_world_gen(seed, palette)
});
app.init_state::<AppState>()
.insert_resource(CameraSettings {
zoom_level: self.settings.zoom_level,
Expand All @@ -47,9 +35,9 @@ impl Plugin for AppPlugin {
vview_distance: self.settings.vview_distance as usize,
zoom_level: self.settings.zoom_level,
})
.insert_resource(WorldSettings { world })
.insert_resource(AssetSettings {
default_block_types: infinigen_extras::blocks::block_types().collect(),
.insert_resource(WorldSettings {
world_gen_name: self.settings.world.clone(),
seed: self.settings.seed as u32,
})
.add_plugins((
assets::AssetsPlugin,
Expand Down
5 changes: 5 additions & 0 deletions crates/infinigen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ fn main() -> ExitCode {
..default()
}),
..default()
})
.set(AssetPlugin {
file_path: "../extras/assets".into(),
..default()
}),
bevy_framepace::FramepacePlugin,
#[cfg(all(feature = "remote", not(target_family = "wasm")))]
Expand All @@ -101,6 +105,7 @@ fn main() -> ExitCode {
RemoteHttpPlugin::default(),
))
.add_plugins((AppPlugin::new(cfg),))
.add_plugins(infinigen_extras::ExtrasPlugin)
.run()
{
AppExit::Success => ExitCode::SUCCESS,
Expand Down
4 changes: 1 addition & 3 deletions crates/plugins/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ mod setup;
pub struct AssetsPlugin;

#[derive(Resource)]
pub struct AssetSettings {
pub default_block_types: Vec<BlockType>,
}
pub struct DefaultBlockTypes(pub Vec<BlockType>);

impl Plugin for AssetsPlugin {
fn build(&self, app: &mut App) {
Expand Down
8 changes: 4 additions & 4 deletions crates/plugins/src/assets/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use strum::IntoEnumIterator;

use crate::assets::blocks::{BlockDefinition, BlockRegistry, MaterialType};
use crate::assets::loading::AssetFolders;
use crate::assets::AssetSettings;
use crate::assets::DefaultBlockTypes;
use crate::AppState;

#[allow(clippy::too_many_arguments)]
Expand All @@ -22,7 +22,7 @@ pub fn setup(
loaded_folders: Res<Assets<LoadedFolder>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut textures: ResMut<Assets<Image>>,
settings: Res<AssetSettings>,
settings: Res<DefaultBlockTypes>,
block_definitions: Res<Assets<BlockDefinition>>,
) {
// block textures
Expand Down Expand Up @@ -68,8 +68,8 @@ pub fn setup(
.collect();
if block_definitions.is_empty() {
tracing::warn!("No block definition files found, falling back to default definitions");
block_definitions = settings
.default_block_types
let DefaultBlockTypes(default_block_types) = &*settings;
block_definitions = default_block_types
.iter()
.map(BlockDefinition::from)
.collect();
Expand Down
17 changes: 15 additions & 2 deletions crates/plugins/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,30 @@ impl Plugin for WorldPlugin {
}
}

pub type WorldInitializerFn =
Box<dyn Fn(&str, u32, Palette) -> Arc<dyn WorldGen + Send + Sync> + Send + Sync>;

#[derive(Resource)]
pub struct WorldInitializer(pub WorldInitializerFn);

#[derive(Resource)]
pub struct WorldSettings {
pub world: Box<dyn Fn(Palette) -> Arc<dyn WorldGen + Send + Sync> + Send + Sync>,
pub world_gen_name: String,
pub seed: u32,
}

fn init_world(
mut next_state: ResMut<NextState<AppState>>,
registry: Res<BlockRegistry>,
world_initializer: Res<WorldInitializer>,
settings: Res<WorldSettings>,
mut world: ResMut<World>,
) {
world.generator = (*settings.world)(registry.definitions.palette());
let WorldInitializer(world_initializer) = &*world_initializer;
world.generator = world_initializer(
&settings.world_gen_name,
settings.seed,
registry.definitions.palette(),
);
next_state.set(AppState::MainGame);
}

0 comments on commit 9120256

Please sign in to comment.