use bevy::asset::Handle;
use bevy::prelude::*;
use bevy_rapier2d::dynamics::AdditionalMassProperties;
use bevy_rapier2d::prelude::Collider;
use bevy_replicon::prelude::Replicated;
use crate::config::planet::{Planet, PlanetBundle, PlanetConfigCollection};
use crate::config::world::{GlobalWorldConfig, WorldConfig};
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 commands: Commands,
mut ev_config: EventReader<AssetEvent<GlobalWorldConfig>>,
mut 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());
}
},
_ => {}
}
}
}