oxygengine_visual_novel/
system.rs

1use crate::{resource::VnStoryManager, vn_story_asset_protocol::VnStoryAsset};
2use core::{
3    app::AppLifeCycle,
4    assets::{asset::AssetId, database::AssetsDatabase},
5    ecs::Universe,
6};
7use std::collections::HashMap;
8
9#[derive(Default)]
10pub struct VnStorySystemCache {
11    story_table: HashMap<AssetId, String>,
12}
13
14pub type VnStorySystemResources<'a> = (
15    &'a AppLifeCycle,
16    &'a AssetsDatabase,
17    &'a mut VnStoryManager,
18    &'a mut VnStorySystemCache,
19);
20
21pub fn vn_story_system(universe: &mut Universe) {
22    let (lifecycle, assets, mut manager, mut cache) =
23        universe.query_resources::<VnStorySystemResources>();
24
25    for id in assets.lately_loaded_protocol("vn-story") {
26        let id = *id;
27        let asset = assets
28            .asset_by_id(id)
29            .expect("trying to use not loaded visual novel story asset");
30        let path = asset.path().to_owned();
31        let asset = asset
32            .get::<VnStoryAsset>()
33            .expect("trying to use non visual novel story asset");
34        let story = asset.get().clone();
35        manager.register_story(&path, story);
36        cache.story_table.insert(id, path);
37    }
38    for id in assets.lately_unloaded_protocol("vn-story") {
39        if let Some(path) = cache.story_table.remove(id) {
40            manager.unregister_story(&path);
41        }
42    }
43
44    manager.process(lifecycle.delta_time_seconds());
45}