use crate::config::world::GlobalWorldConfig;
use bevy::asset::Handle;
use bevy::prelude::*;
pub fn world_config_plugin(app: &mut App) {
app.init_resource::<WorldConfigResource>()
.add_systems(Startup, start_loading_planets)
.add_systems(Update, update_planets);
}
#[derive(Resource, Default)]
pub struct WorldConfigResource {
handle: Option<Handle<GlobalWorldConfig>>,
pub config: Option<GlobalWorldConfig>,
}
fn start_loading_planets(assets: Res<AssetServer>, mut planets: ResMut<WorldConfigResource>) {
planets.handle = Some(assets.load("config/world.wc.toml"));
}
pub fn update_planets(
mut ev_config: EventReader<AssetEvent<GlobalWorldConfig>>,
assets: ResMut<Assets<GlobalWorldConfig>>,
mut resource: ResMut<WorldConfigResource>,
) {
let Some(handle) = resource.handle.as_ref() else {
return;
};
let waiting_for_asset_id = handle.id();
for ev in ev_config.read() {
match ev {
AssetEvent::Added { id } => {
if *id == waiting_for_asset_id {
info!("world config loaded");
let world_config = assets.get(*id).unwrap();
resource.config = Some(world_config.clone());
}
}
AssetEvent::Modified { id } => {
if *id == waiting_for_asset_id {
info!("world config modified - reloading");
let world_config = assets.get(*id).unwrap();
resource.config = Some(world_config.clone());
}
}
_ => {}
}
}
}