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
use crate::{resource::VnStoryManager, vn_story_asset_protocol::VnStoryAsset};
use core::{
    app::AppLifeCycle,
    assets::{asset::AssetId, database::AssetsDatabase},
    ecs::Universe,
};
use std::collections::HashMap;

#[derive(Default)]
pub struct VnStorySystemCache {
    story_table: HashMap<AssetId, String>,
}

pub type VnStorySystemResources<'a> = (
    &'a AppLifeCycle,
    &'a AssetsDatabase,
    &'a mut VnStoryManager,
    &'a mut VnStorySystemCache,
);

pub fn vn_story_system(universe: &mut Universe) {
    let (lifecycle, assets, mut manager, mut cache) =
        universe.query_resources::<VnStorySystemResources>();

    for id in assets.lately_loaded_protocol("vn-story") {
        let id = *id;
        let asset = assets
            .asset_by_id(id)
            .expect("trying to use not loaded visual novel story asset");
        let path = asset.path().to_owned();
        let asset = asset
            .get::<VnStoryAsset>()
            .expect("trying to use non visual novel story asset");
        let story = asset.get().clone();
        manager.register_story(&path, story);
        cache.story_table.insert(id, path);
    }
    for id in assets.lately_unloaded_protocol("vn-story") {
        if let Some(path) = cache.story_table.remove(id) {
            manager.unregister_story(&path);
        }
    }

    manager.process(lifecycle.delta_time_seconds());
}