~starkingdoms/starkingdoms

ref: 103444e57478b2f702c26a6dfb9a62b34a60e5fe starkingdoms/crates/unified/src/server/world_config.rs -rw-r--r-- 1.8 KiB
103444e5 — core chore(wasm)!: fix compilations on webassembly 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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());
                }
            },
            _ => {}
        }
    }
}