diff --git a/crates/blenvy/src/blueprints/animation.rs b/crates/blenvy/src/blueprints/animation.rs index c5c9eaf..2e1d2e4 100644 --- a/crates/blenvy/src/blueprints/animation.rs +++ b/crates/blenvy/src/blueprints/animation.rs @@ -178,7 +178,8 @@ pub fn trigger_instance_animation_markers_events( __animation_graphs: Res>, mut _animation_marker_events: EventWriter, ) { - for (__entity, __markers, player_link, animations, __animation_infos) in animation_infos.iter() { + for (__entity, __markers, player_link, animations, __animation_infos) in animation_infos.iter() + { //let (animation_player, animation_transitions) = animation_players.get(player_link.0).unwrap(); //let foo = animation_transitions.get_main_animation().unwrap(); diff --git a/crates/blenvy/src/blueprints/assets.rs b/crates/blenvy/src/blueprints/assets.rs index 8685cc8..7f5ec3f 100644 --- a/crates/blenvy/src/blueprints/assets.rs +++ b/crates/blenvy/src/blueprints/assets.rs @@ -77,16 +77,15 @@ impl Default for BlueprintAssetsLoadState { // for preloading asset files #[derive(serde::Deserialize, bevy::asset::Asset, bevy::reflect::TypePath, Debug)] -pub(crate) struct File{ +pub(crate) struct File { pub(crate) path: String, } #[derive(serde::Deserialize, bevy::asset::Asset, bevy::reflect::TypePath, Debug)] -pub(crate) struct BlueprintPreloadAssets{ - pub(crate) assets: Vec<(String, File)> +pub(crate) struct BlueprintPreloadAssets { + pub(crate) assets: Vec<(String, File)>, } - #[derive(Component)] pub(crate) struct BlueprintMetaHandle(pub Handle); @@ -94,6 +93,5 @@ pub(crate) struct BlueprintMetaHandle(pub Handle); #[derive(Component)] pub(crate) struct BlueprintMetaLoaded; - #[derive(Component)] -pub(crate) struct BlueprintMetaLoading; \ No newline at end of file +pub(crate) struct BlueprintMetaLoading; diff --git a/crates/blenvy/src/blueprints/materials.rs b/crates/blenvy/src/blueprints/materials.rs index 4c5008e..b2950c9 100644 --- a/crates/blenvy/src/blueprints/materials.rs +++ b/crates/blenvy/src/blueprints/materials.rs @@ -58,9 +58,11 @@ pub(crate) fn inject_materials( } else { let model_handle: Handle = asset_server.load(material_info.path.clone()); // FIXME: kinda weird now let Some(mat_gltf) = assets_gltf.get(model_handle.id()) else { - warn!("materials file {} should have been preloaded skipping",material_info.path); + warn!( + "materials file {} should have been preloaded skipping", + material_info.path + ); continue; - }; /*let mat_gltf = assets_gltf.get(model_handle.id()).unwrap_or_else(|| { panic!( @@ -93,18 +95,14 @@ pub(crate) fn inject_materials( material_info.name, material_info.path.clone() ); - + commands.entity(*child).insert(material.clone()); } } - } } } - commands.entity(entity).insert(MaterialProcessed); - - } } diff --git a/crates/blenvy/src/blueprints/mod.rs b/crates/blenvy/src/blueprints/mod.rs index c4a697a..56f4190 100644 --- a/crates/blenvy/src/blueprints/mod.rs +++ b/crates/blenvy/src/blueprints/mod.rs @@ -110,8 +110,7 @@ impl Plugin for BlueprintsPlugin { .register_type::>() .register_type::() .register_type::>>() - .add_plugins(RonAssetPlugin::::new(&["meta.ron"]),) - + .add_plugins(RonAssetPlugin::::new(&["meta.ron"])) .configure_sets( Update, (GltfBlueprintsSet::Spawn, GltfBlueprintsSet::AfterSpawn) diff --git a/crates/blenvy/src/blueprints/spawn_from_blueprints.rs b/crates/blenvy/src/blueprints/spawn_from_blueprints.rs index 46e9e6e..5d92496 100644 --- a/crates/blenvy/src/blueprints/spawn_from_blueprints.rs +++ b/crates/blenvy/src/blueprints/spawn_from_blueprints.rs @@ -1,17 +1,15 @@ use std::path::Path; -use bevy::{ - gltf::Gltf, - prelude::*, - scene::SceneInstance, - utils::hashbrown::HashMap, -}; +use bevy::{gltf::Gltf, prelude::*, scene::SceneInstance, utils::hashbrown::HashMap}; use crate::{ - AnimationInfos, AssetLoadTracker, AssetToBlueprintInstancesMapper, BlueprintAnimationInfosLink, BlueprintAnimationPlayerLink, BlueprintAnimations, BlueprintAssetsLoadState, BlueprintAssetsLoaded, BlueprintAssetsNotLoaded, BlueprintMetaLoaded, BlueprintMetaLoading, BlueprintPreloadAssets, InstanceAnimationInfosLink, InstanceAnimationPlayerLink, InstanceAnimations, WatchingForChanges + AnimationInfos, AssetLoadTracker, AssetToBlueprintInstancesMapper, BlueprintAnimationInfosLink, + BlueprintAnimationPlayerLink, BlueprintAnimations, BlueprintAssetsLoadState, + BlueprintAssetsLoaded, BlueprintAssetsNotLoaded, BlueprintMetaLoaded, BlueprintMetaLoading, + BlueprintPreloadAssets, InstanceAnimationInfosLink, InstanceAnimationPlayerLink, + InstanceAnimations, WatchingForChanges, }; - /// this is a flag component for our levels/game world #[derive(Component)] pub struct GameWorldTag; @@ -67,8 +65,6 @@ pub struct HideUntilReady; /// Companion to the `HideUntilReady` component: this stores the visibility of the entity before the blueprint was inserted into it pub(crate) struct OriginalVisibility(Visibility); - - #[derive(Component, Reflect, Default, Debug)] #[reflect(Component)] /// marker component, gets added to all children of a currently spawning blueprint instance, can be usefull to avoid manipulating still in progress entities @@ -117,25 +113,45 @@ Overview of the Blueprint Spawning process */ pub(super) fn blueprints_prepare_metadata_file_for_spawn( - blueprint_instances_to_spawn: Query<( - Entity, - &BlueprintInfo, - Option<&Name>, - Option<&Parent>, - Option<&HideUntilReady>, - Option<&Visibility>, - Option<&AddToGameWorld>, - - ), (Without, Without, Without)>, + blueprint_instances_to_spawn: Query< + ( + Entity, + &BlueprintInfo, + Option<&Name>, + Option<&Parent>, + Option<&HideUntilReady>, + Option<&Visibility>, + Option<&AddToGameWorld>, + ), + ( + Without, + Without, + Without, + ), + >, mut game_world: Query>, asset_server: Res, mut commands: Commands, ) { - for (entity, blueprint_info, entity_name, original_parent, hide_until_ready, original_visibility, add_to_world) in blueprint_instances_to_spawn.iter() { + for ( + entity, + blueprint_info, + entity_name, + original_parent, + hide_until_ready, + original_visibility, + add_to_world, + ) in blueprint_instances_to_spawn.iter() + { // get path to assets / metadata file - info!("Step 1: spawn request detected: loading metadata file for {:?}", blueprint_info); + info!( + "Step 1: spawn request detected: loading metadata file for {:?}", + blueprint_info + ); let blueprint_path = blueprint_info.path.clone(); - let metadata_path = blueprint_path.replace(".glb", ".meta.ron").replace(".gltf", ".meta.ron"); // FIXME: horrible + let metadata_path = blueprint_path + .replace(".glb", ".meta.ron") + .replace(".gltf", ".meta.ron"); // FIXME: horrible let mut asset_infos: Vec = vec![]; //let foo_handle:Handle = asset_server.load(metadata_path); let untyped_handle = asset_server.load_untyped(metadata_path.clone()); @@ -157,11 +173,11 @@ pub(super) fn blueprints_prepare_metadata_file_for_spawn( ..Default::default() }, BlueprintMetaLoading, - BlueprintSpawning + BlueprintSpawning, )); // if the entity has no name, add one based on the blueprint's - if entity_name.is_none(){ + if entity_name.is_none() { commands .entity(entity) .insert(bevy::prelude::Name::from(blueprint_info.name.clone())); @@ -170,14 +186,14 @@ pub(super) fn blueprints_prepare_metadata_file_for_spawn( if original_parent.is_none() { // only allow hiding until ready when the entity does not have a parent (?) if hide_until_ready.is_some() { - // if there is already a set visibility, save it for later if let Some(original_visibility) = original_visibility { - commands.entity(entity).insert(OriginalVisibility(*original_visibility)); + commands + .entity(entity) + .insert(OriginalVisibility(*original_visibility)); } // & now hide the instance until it is ready - commands.entity(entity) - .insert(Visibility::Hidden); + commands.entity(entity).insert(Visibility::Hidden); } // only allow automatically adding a newly spawned blueprint instance to the "world", if the entity does not have a parent @@ -200,9 +216,7 @@ pub(crate) fn blueprints_check_assets_metadata_files_loading( asset_server: Res, mut commands: Commands, ) { - for (entity, _blueprint_info, mut assets_to_load) in blueprint_assets_to_load.iter_mut() { - let mut all_loaded = true; let mut loaded_amount = 0; let total = assets_to_load.asset_infos.len(); @@ -221,10 +235,12 @@ pub(crate) fn blueprints_check_assets_metadata_files_loading( all_loaded = false; } if all_loaded { - commands.entity(entity).insert(BlueprintMetaHandle(asset_server.load(tracker.path.clone()))).remove::(); + commands + .entity(entity) + .insert(BlueprintMetaHandle(asset_server.load(tracker.path.clone()))) + .remove::(); break; } - } let progress: f32 = loaded_amount as f32 / total as f32; assets_to_load.progress = progress; @@ -232,9 +248,11 @@ pub(crate) fn blueprints_check_assets_metadata_files_loading( } } - pub(super) fn blueprints_prepare_spawn( - blueprint_instances_to_spawn: Query<(Entity, &BlueprintInfo, &BlueprintMetaHandle), Added>, + blueprint_instances_to_spawn: Query< + (Entity, &BlueprintInfo, &BlueprintMetaHandle), + Added, + >, mut commands: Commands, asset_server: Res, // for hot reload @@ -243,12 +261,11 @@ pub(super) fn blueprints_prepare_spawn( // for debug // all_names: Query<&Name> blueprint_metas: Res>, - ) { for (entity, blueprint_info, blueprint_meta_handle) in blueprint_instances_to_spawn.iter() { info!( "Step 2: metadata loaded: loading assets for {:?}", - blueprint_info, + blueprint_info, ); // we add the asset of the blueprint itself // TODO: add detection of already loaded data @@ -270,56 +287,55 @@ pub(super) fn blueprints_prepare_spawn( // and we also add all its assets /* prefetch attempt */ if let Some(blenvy_metadata) = blueprint_metas.get(&blueprint_meta_handle.0) { - for asset in blenvy_metadata.assets.iter() { - let asset_path = asset.1.path.clone(); - let asset_name = asset.0.clone(); + for asset in blenvy_metadata.assets.iter() { + let asset_path = asset.1.path.clone(); + let asset_name = asset.0.clone(); - let untyped_handle = asset_server.load_untyped(&asset_path); - let asset_id = untyped_handle.id(); - let loaded = asset_server.is_loaded_with_dependencies(asset_id); - if !loaded { - asset_infos.push(AssetLoadTracker { - name: asset_name.clone(), - path: asset_path.clone(), - id: asset_id, - loaded: false, - handle: untyped_handle.clone(), - }); - } - - // FIXME: dang, too early, asset server has not yet started loading yet - // let path_id = asset_server.get_path_id(&asset.path).expect("we should have alread checked for this asset"); - let path_id = asset_path.clone(); - - // Only do this if hot reload is enabled - if watching_for_changes.0 { - if !assets_to_blueprint_instances - .untyped_id_to_blueprint_entity_ids - .contains_key(&path_id) - { - assets_to_blueprint_instances - .untyped_id_to_blueprint_entity_ids - .insert(path_id.clone(), vec![]); + let untyped_handle = asset_server.load_untyped(&asset_path); + let asset_id = untyped_handle.id(); + let loaded = asset_server.is_loaded_with_dependencies(asset_id); + if !loaded { + asset_infos.push(AssetLoadTracker { + name: asset_name.clone(), + path: asset_path.clone(), + id: asset_id, + loaded: false, + handle: untyped_handle.clone(), + }); } - // only insert if not already present in mapping - if !assets_to_blueprint_instances.untyped_id_to_blueprint_entity_ids - [&path_id] - .contains(&entity) - { - // println!("adding mapping between {} and entity {:?}", path_id, all_names.get(entity)); - assets_to_blueprint_instances + // FIXME: dang, too early, asset server has not yet started loading yet + // let path_id = asset_server.get_path_id(&asset.path).expect("we should have alread checked for this asset"); + let path_id = asset_path.clone(); + + // Only do this if hot reload is enabled + if watching_for_changes.0 { + if !assets_to_blueprint_instances .untyped_id_to_blueprint_entity_ids - .get_mut(&path_id) - .unwrap() - .push(entity); + .contains_key(&path_id) + { + assets_to_blueprint_instances + .untyped_id_to_blueprint_entity_ids + .insert(path_id.clone(), vec![]); + } + + // only insert if not already present in mapping + if !assets_to_blueprint_instances.untyped_id_to_blueprint_entity_ids[&path_id] + .contains(&entity) + { + // println!("adding mapping between {} and entity {:?}", path_id, all_names.get(entity)); + assets_to_blueprint_instances + .untyped_id_to_blueprint_entity_ids + .get_mut(&path_id) + .unwrap() + .push(entity); + } } } + } else { + warn!("no asset metadata found for {}, please make sure to generate them using the Blender add-on, or preload your assets manually", blueprint_info.path); } - }else { - warn!("no asset metadata found for {}, please make sure to generate them using the Blender add-on, or preload your assets manually", blueprint_info.path); - } - + // Only do this if hot reload is enabled // TODO: should this be added to the list of "all assets" on the blender side instead if watching_for_changes.0 { @@ -361,7 +377,8 @@ pub(super) fn blueprints_prepare_spawn( commands.entity(entity).insert(BlueprintAssetsLoaded); } - commands.entity(entity) + commands + .entity(entity) .insert(BlueprintMetaLoaded) .remove::() .remove::(); @@ -423,12 +440,7 @@ pub(crate) fn blueprints_check_assets_loading( pub(crate) fn blueprints_assets_loaded( spawn_placeholders: Query< - ( - Entity, - &BlueprintInfo, - Option<&Transform>, - Option<&Name>, - ), + (Entity, &BlueprintInfo, Option<&Transform>, Option<&Name>), ( Added, Without, @@ -442,13 +454,7 @@ pub(crate) fn blueprints_assets_loaded( mut commands: Commands, ) { - for ( - entity, - blueprint_info, - transform, - name, - ) in spawn_placeholders.iter() - { + for (entity, blueprint_info, transform, name) in spawn_placeholders.iter() { /*info!( "BLUEPRINT: all assets loaded, attempting to spawn blueprint SCENE {:?} for entity {:?}, id: {:}, parent:{:?}", blueprint_info.name, name, entity, original_parent @@ -522,8 +528,6 @@ pub(crate) fn blueprints_assets_loaded( graph, }, )); - - } } @@ -875,8 +879,9 @@ pub(crate) fn blueprints_finalize_instances( } } - - commands.entity(entity).remove::(); + commands + .entity(entity) + .remove::(); for child in all_children.iter_descendants(entity) { commands.entity(child).remove::(); } @@ -884,7 +889,7 @@ pub(crate) fn blueprints_finalize_instances( if hide_until_ready.is_some() { if let Some(original_visibility) = original_visibility { commands.entity(entity).insert(original_visibility.0); - }else { + } else { commands.entity(entity).insert(Visibility::Inherited); } } diff --git a/crates/blenvy/src/components/ronstring_to_reflect_component.rs b/crates/blenvy/src/components/ronstring_to_reflect_component.rs index 209330a..e772d89 100644 --- a/crates/blenvy/src/components/ronstring_to_reflect_component.rs +++ b/crates/blenvy/src/components/ronstring_to_reflect_component.rs @@ -70,13 +70,13 @@ fn components_string_to_components( .expect("deserialzer should have been generated from string"); let reflect_deserializer = ReflectDeserializer::new(type_registry); /*let component = reflect_deserializer - .deserialize(&mut deserializer) - .unwrap_or_else(|_| { - panic!( - "failed to deserialize component {} with value: {:?}", - name, value - ) - });*/ + .deserialize(&mut deserializer) + .unwrap_or_else(|_| { + panic!( + "failed to deserialize component {} with value: {:?}", + name, value + ) + });*/ let Ok(component) = reflect_deserializer.deserialize(&mut deserializer) else { warn!( "failed to deserialize component {} with value: {:?}", diff --git a/crates/blenvy/src/lib.rs b/crates/blenvy/src/lib.rs index fef8dd3..ac4ccf9 100644 --- a/crates/blenvy/src/lib.rs +++ b/crates/blenvy/src/lib.rs @@ -45,7 +45,7 @@ pub struct BlenvyPlugin { // for save & load pub save_component_filter: SceneFilter, pub save_resource_filter: SceneFilter, - pub save_path: PathBuf + pub save_path: PathBuf, } impl Default for BlenvyPlugin { @@ -58,7 +58,7 @@ impl Default for BlenvyPlugin { save_component_filter: SceneFilter::default(), save_resource_filter: SceneFilter::default(), - save_path: PathBuf::from("blenvy_saves") // TODO: use https://docs.rs/dirs/latest/dirs/ to default to the correct user directory + save_path: PathBuf::from("blenvy_saves"), // TODO: use https://docs.rs/dirs/latest/dirs/ to default to the correct user directory } } } @@ -71,7 +71,7 @@ impl Plugin for BlenvyPlugin { ExportRegistryPlugin::default(), BlueprintsPlugin::default(), #[cfg(not(target_arch = "wasm32"))] // save & load is only for non wasm platforms - SaveLoadPlugin::default() + SaveLoadPlugin::default(), )) .insert_resource(BlenvyConfig { export_registry: self.export_registry, @@ -85,7 +85,7 @@ impl Plugin for BlenvyPlugin { save_component_filter: self.save_component_filter.clone(), save_resource_filter: self.save_resource_filter.clone(), - save_path: self.save_path.clone() + save_path: self.save_path.clone(), }); } } diff --git a/crates/blenvy/src/registry/mod.rs b/crates/blenvy/src/registry/mod.rs index 0c924e6..64f4f3a 100644 --- a/crates/blenvy/src/registry/mod.rs +++ b/crates/blenvy/src/registry/mod.rs @@ -35,7 +35,8 @@ fn export_registry(blenvy_config: Res) -> bool { impl Plugin for ExportRegistryPlugin { fn build(&self, app: &mut App) { - app.register_asset_root().add_systems(Startup, export_types.run_if(export_registry)); + app.register_asset_root() + .add_systems(Startup, export_types.run_if(export_registry)); } } diff --git a/crates/blenvy/src/save_load/common.rs b/crates/blenvy/src/save_load/common.rs index af13f4b..9f4205c 100644 --- a/crates/blenvy/src/save_load/common.rs +++ b/crates/blenvy/src/save_load/common.rs @@ -5,34 +5,44 @@ use crate::{BlueprintInfo, GameWorldTag, HideUntilReady, SpawnBlueprint}; use super::BlueprintWorld; pub(crate) fn spawn_from_blueprintworld( - added_blueprint_worlds: Query<(Entity, &BlueprintWorld), Added >, + added_blueprint_worlds: Query<(Entity, &BlueprintWorld), Added>, mut commands: Commands, -){ - for (__entity, blueprint_world) in added_blueprint_worlds.iter(){ +) { + for (__entity, blueprint_world) in added_blueprint_worlds.iter() { println!("added blueprintWorld {:?}", blueprint_world); // here we spawn the static part our game world/level, which is also a blueprint ! - let __static_world = commands.spawn(( - BlueprintInfo::from_path(blueprint_world.path.as_str()), // all we need is a Blueprint info... - SpawnBlueprint, // and spawnblueprint to tell blenvy to spawn the blueprint now - HideUntilReady, // only reveal the level once it is ready - GameWorldTag, - )).id(); + let __static_world = commands + .spawn(( + BlueprintInfo::from_path(blueprint_world.path.as_str()), // all we need is a Blueprint info... + SpawnBlueprint, // and spawnblueprint to tell blenvy to spawn the blueprint now + HideUntilReady, // only reveal the level once it is ready + GameWorldTag, + )) + .id(); // here we spawn the dynamic entities part of our game world/level, which is also a blueprint ! - let __dynamic_world = commands.spawn(( - BlueprintInfo::from_path(blueprint_world.path.replace(".glb", "_dynamic.glb").replace(".gltf", "_dynamic.gltf").as_str()), // all we need is a Blueprint info... - SpawnBlueprint, // and spawnblueprint to tell blenvy to spawn the blueprint now - HideUntilReady, // only reveal the level once it is ready - GameWorldTag - )).id(); + let __dynamic_world = commands + .spawn(( + BlueprintInfo::from_path( + blueprint_world + .path + .replace(".glb", "_dynamic.glb") + .replace(".gltf", "_dynamic.gltf") + .as_str(), + ), // all we need is a Blueprint info... + SpawnBlueprint, // and spawnblueprint to tell blenvy to spawn the blueprint now + HideUntilReady, // only reveal the level once it is ready + GameWorldTag, + )) + .id(); // commands.entity(entity).add_child(static_world); // commands.entity(entity).add_child(dynamic_world); - } + } } -/* +/* pub(crate) fn inject_dynamic_into_children( added_dynamic: Query >, all_children: Query<&Children>, @@ -43,4 +53,4 @@ pub(crate) fn inject_dynamic_into_children( commands.entity(child).insert(Dynamic); } } -}*/ \ No newline at end of file +}*/ diff --git a/crates/blenvy/src/save_load/loading.rs b/crates/blenvy/src/save_load/loading.rs index 39bd181..cd0da69 100644 --- a/crates/blenvy/src/save_load/loading.rs +++ b/crates/blenvy/src/save_load/loading.rs @@ -2,7 +2,6 @@ use bevy::prelude::*; use crate::{BlueprintInfo, DynamicEntitiesRoot, GameWorldTag, HideUntilReady, SpawnBlueprint}; - #[derive(Event)] pub struct LoadingRequest { pub path: String, @@ -17,23 +16,22 @@ pub struct LoadingRequested { pub path: String, } - /* - Loading - load request recieved - pause things ? - unload everything - load static data using blueprintInfo - - load dynamic data from save file + - load dynamic data from save file -General: +General: * wrap loading a bevy scene as a blueprint ? * meh, has no assets & co, different logic ? */ pub fn process_load_requests( mut load_requests: EventReader, - mut commands: Commands + mut commands: Commands, ) { let mut save_path: String = "".into(); for load_request in load_requests.read() { @@ -47,14 +45,13 @@ pub fn process_load_requests( } pub fn should_load(loading_requests: Option>) -> bool { - return resource_exists::(loading_requests) + return resource_exists::(loading_requests); } // TODO: replace with generic despawner ? pub(crate) fn prepare_loading( - mut commands: Commands, + mut commands: Commands, gameworlds: Query>, - ) { for e in gameworlds.iter() { info!("--loading: despawn old world/level"); @@ -62,28 +59,25 @@ pub(crate) fn prepare_loading( } } - - - pub(crate) fn load_game( mut commands: Commands, asset_server: Res, load_request: Res, ) { info!("--loading: load dynamic data"); - + //let save_path = Path::new(load_request.path.clone().as_str()); info!("LOADING FROM {:?}", load_request.path.clone()); /*let world_root = commands - .spawn(( - bevy::prelude::Name::from("world"), - GameWorldTag, - TransformBundle::default(), - InheritedVisibility::default(), - )) - .id();*/ + .spawn(( + bevy::prelude::Name::from("world"), + GameWorldTag, + TransformBundle::default(), + InheritedVisibility::default(), + )) + .id();*/ // and we fill it with dynamic data // let input = std::fs::read(&path)?; @@ -95,16 +89,18 @@ pub(crate) fn load_game( }, bevy::prelude::Name::from("World_dynamic"), DynamicEntitiesRoot, - GameWorldTag + GameWorldTag, )) .id(); - let _static_data = commands.spawn(( - BlueprintInfo::from_path("levels/World.glb"), // all we need is a Blueprint info... - SpawnBlueprint, - HideUntilReady, - GameWorldTag, - )).id(); + let _static_data = commands + .spawn(( + BlueprintInfo::from_path("levels/World.glb"), // all we need is a Blueprint info... + SpawnBlueprint, + HideUntilReady, + GameWorldTag, + )) + .id(); //commands.entity(world_root).add_child(static_data); //commands.entity(world_root).add_child(dynamic_data); @@ -113,4 +109,4 @@ pub(crate) fn load_game( info!("--loading: loaded dynamic data"); commands.remove_resource::(); -} \ No newline at end of file +} diff --git a/crates/blenvy/src/save_load/mod.rs b/crates/blenvy/src/save_load/mod.rs index 4475a10..521740f 100644 --- a/crates/blenvy/src/save_load/mod.rs +++ b/crates/blenvy/src/save_load/mod.rs @@ -21,7 +21,6 @@ pub(crate) struct RootEntity; /// internal helper component to store parents before resetting them pub(crate) struct OriginalParent(pub(crate) Entity); - /// Marker component to Flag the root entity of all static entities (immutables) #[derive(Component, Reflect, Debug, Default)] #[reflect(Component)] @@ -32,7 +31,6 @@ pub struct StaticEntitiesRoot; #[reflect(Component)] pub struct DynamicEntitiesRoot; - #[derive(Resource, Clone, Debug, Default, Reflect)] #[reflect(Resource)] pub struct StaticEntitiesBlueprintInfo { @@ -40,9 +38,8 @@ pub struct StaticEntitiesBlueprintInfo { pub path: String, } - #[derive(Component, Debug)] -pub struct BlueprintWorld{ +pub struct BlueprintWorld { pub path: String, } impl BlueprintWorld { @@ -55,8 +52,6 @@ impl BlueprintWorld { } } - - #[derive(Debug, Clone, Default)] /// Plugin for saving & loading pub struct SaveLoadPlugin {} @@ -65,13 +60,10 @@ impl Plugin for SaveLoadPlugin { fn build(&self, app: &mut App) { app.register_type::() .register_type::() - .add_event::() .add_event::() - // common - .add_systems(Update, (spawn_from_blueprintworld, )) // inject_dynamic_into_children - + .add_systems(Update, (spawn_from_blueprintworld,)) // inject_dynamic_into_children // saving .add_systems(Update, process_save_requests) .add_systems( @@ -79,9 +71,7 @@ impl Plugin for SaveLoadPlugin { (prepare_save_game, apply_deferred, save_game, cleanup_save) .chain() .run_if(should_save), - ) - .add_event::() .add_event::() //loading @@ -91,9 +81,8 @@ impl Plugin for SaveLoadPlugin { (prepare_loading, apply_deferred, load_game) .chain() .run_if(should_load), - //.run_if(not(resource_exists::)) - // .in_set(LoadingSet::Load), - ) - ; + //.run_if(not(resource_exists::)) + // .in_set(LoadingSet::Load), + ); } } diff --git a/crates/blenvy/src/save_load/saving.rs b/crates/blenvy/src/save_load/saving.rs index 54da31b..da183d7 100644 --- a/crates/blenvy/src/save_load/saving.rs +++ b/crates/blenvy/src/save_load/saving.rs @@ -2,8 +2,8 @@ use std::fs::File; use std::io::Write; use std::path::Path; -use bevy::{prelude::*, tasks::IoTaskPool}; use bevy::prelude::World; +use bevy::{prelude::*, tasks::IoTaskPool}; use crate::{BlenvyConfig, BlueprintInfo, Dynamic, FromBlueprint, RootEntity, SpawnBlueprint}; @@ -16,17 +16,15 @@ pub struct SavingRequest { #[derive(Event)] pub struct SaveFinished; // TODO: merge the the events above - /// resource that keeps track of the current save request #[derive(Resource, Default)] pub struct SavingRequested { pub path: String, } - pub fn process_save_requests( mut saving_requests: EventReader, - mut commands: Commands + mut commands: Commands, ) { let mut save_path: String = "".into(); for saving_request in saving_requests.read() { @@ -39,12 +37,10 @@ pub fn process_save_requests( } } - pub fn should_save(saving_requests: Option>) -> bool { - return resource_exists::(saving_requests) + return resource_exists::(saving_requests); } - // any child of dynamic/ saveable entities that is not saveable itself should be removed from the list of children pub(crate) fn prepare_save_game( saveables: Query, With)>, @@ -54,7 +50,8 @@ pub(crate) fn prepare_save_game( mut commands: Commands, ) { - for entity in saveables.iter() { // FIXME : not sure about this one + for entity in saveables.iter() { + // FIXME : not sure about this one commands.entity(entity).insert(SpawnBlueprint); } @@ -83,8 +80,6 @@ pub(crate) fn prepare_save_game( }*/ } - - pub(crate) fn save_game(world: &mut World) { info!("saving"); @@ -125,7 +120,6 @@ pub(crate) fn save_game(world: &mut World) { .allow::() .allow::() .allow::() - /*.deny::() .deny::() .deny::>() @@ -135,11 +129,12 @@ pub(crate) fn save_game(world: &mut World) { // for root entities, it is the same EXCEPT we make sure parents are not included let filter_root = filter.clone().deny::(); - let filter_resources = config.clone() + let filter_resources = config + .clone() .save_resource_filter .deny::>() .clone(); - //.allow::(); + //.allow::(); // for default stuff let scene_builder = DynamicSceneBuilder::from_world(world) @@ -190,7 +185,6 @@ pub(crate) fn save_game(world: &mut World) { }) .detach(); - let static_world_path = "levels/world.glb"; let fake_foo = format!("(dynamic: {bla}, static: {static_world_path})"); let real_save_path = format!("{bla}.save.ron"); @@ -217,5 +211,4 @@ pub(crate) fn cleanup_save( saving_finished.send(SaveFinished); commands.remove_resource::(); - -} \ No newline at end of file +} diff --git a/examples/animation/src/main.rs b/examples/animation/src/main.rs index f1f55ec..2f63ce0 100644 --- a/examples/animation/src/main.rs +++ b/examples/animation/src/main.rs @@ -2,9 +2,8 @@ use std::time::Duration; use bevy::prelude::*; use blenvy::{ - BlenvyPlugin, BlueprintAnimationPlayerLink, - BlueprintAnimations, BlueprintInfo, GameWorldTag, HideUntilReady, - SpawnBlueprint, + BlenvyPlugin, BlueprintAnimationPlayerLink, BlueprintAnimations, BlueprintInfo, GameWorldTag, + HideUntilReady, SpawnBlueprint, }; mod component_examples; diff --git a/examples/blueprints/src/main.rs b/examples/blueprints/src/main.rs index faf04f6..1080c0e 100644 --- a/examples/blueprints/src/main.rs +++ b/examples/blueprints/src/main.rs @@ -1,6 +1,7 @@ use bevy::prelude::*; use blenvy::{ - AddToGameWorld, BlenvyPlugin, BluePrintBundle, BlueprintInfo, Dynamic, GameWorldTag, HideUntilReady, SpawnBlueprint + AddToGameWorld, BlenvyPlugin, BluePrintBundle, BlueprintInfo, Dynamic, GameWorldTag, + HideUntilReady, SpawnBlueprint, }; use rand::Rng; @@ -32,12 +33,8 @@ fn setup_game(mut commands: Commands) { } // you can also spawn blueprint instances at runtime -pub fn spawn_blueprint_instance( - keycode: Res>, - mut commands: Commands, -) { +pub fn spawn_blueprint_instance(keycode: Res>, mut commands: Commands) { if keycode.just_pressed(KeyCode::KeyS) { - let mut rng = rand::thread_rng(); let range = 5.5; let x: f32 = rng.gen_range(-range..range); diff --git a/examples/save_load/src/main.rs b/examples/save_load/src/main.rs index 63de626..6160d83 100644 --- a/examples/save_load/src/main.rs +++ b/examples/save_load/src/main.rs @@ -1,15 +1,18 @@ use std::any::TypeId; use bevy::{prelude::*, utils::hashbrown::HashSet}; -use blenvy::{AddToGameWorld, BlenvyPlugin, BlueprintInfo, BlueprintWorld, Dynamic, GameWorldTag, HideUntilReady, LoadingRequest, SavingRequest, SpawnBlueprint}; +use blenvy::{ + AddToGameWorld, BlenvyPlugin, BlueprintInfo, BlueprintWorld, Dynamic, GameWorldTag, + HideUntilReady, LoadingRequest, SavingRequest, SpawnBlueprint, +}; use rand::Rng; // mod game; // use game::*; mod component_examples; -use component_examples::*; use bevy_inspector_egui::quick::WorldInspectorPlugin; +use component_examples::*; fn main() { App::new() @@ -39,19 +42,16 @@ fn main() { // GamePlugin, // specific to our game ComponentsExamplesPlugin, // Showcases different type of components /structs )) - .add_systems(Startup, setup_game) - .add_systems(Update, (spawn_blueprint_instance, move_movers, save_game, load_game)) - + .add_systems( + Update, + (spawn_blueprint_instance, move_movers, save_game, load_game), + ) .run(); } // this is how you setup & spawn a level from a blueprint -fn setup_game( - mut commands: Commands, -) { - - +fn setup_game(mut commands: Commands) { // would be nice: contains both static & dynamic stuff commands.spawn(( BlueprintWorld::from_path("levels/World.glb"), // will take care of loading both static & dynamic data @@ -59,10 +59,7 @@ fn setup_game( } // you can also spawn blueprint instances at runtime -fn spawn_blueprint_instance( - keycode: Res>, - mut commands: Commands, -) { +fn spawn_blueprint_instance(keycode: Res>, mut commands: Commands) { if keycode.just_pressed(KeyCode::KeyT) { // random position let mut rng = rand::thread_rng(); @@ -73,32 +70,26 @@ fn spawn_blueprint_instance( // random name let name_index: u64 = rng.gen(); - commands - .spawn(( - BlueprintInfo::from_path("blueprints/test.glb"), - SpawnBlueprint, - Dynamic, - bevy::prelude::Name::from(format!("test{}", name_index)), - HideUntilReady, - AddToGameWorld, - TransformBundle::from_transform(Transform::from_xyz(x, 2.0, y)), - )); + commands.spawn(( + BlueprintInfo::from_path("blueprints/test.glb"), + SpawnBlueprint, + Dynamic, + bevy::prelude::Name::from(format!("test{}", name_index)), + HideUntilReady, + AddToGameWorld, + TransformBundle::from_transform(Transform::from_xyz(x, 2.0, y)), + )); } } -fn move_movers( - mut movers: Query<(&mut Transform), With> -) { - for mut transform in movers.iter_mut(){ +fn move_movers(mut movers: Query<(&mut Transform), With>) { + for mut transform in movers.iter_mut() { // println!("moving dynamic entity"); transform.translation.x += 0.005; } } -fn save_game( - keycode: Res>, - mut save_requests: EventWriter, -) { +fn save_game(keycode: Res>, mut save_requests: EventWriter) { if keycode.just_pressed(KeyCode::KeyS) { save_requests.send(SavingRequest { path: "scenes/save.scn.ron".into(), @@ -106,11 +97,7 @@ fn save_game( } } - -fn load_game( - keycode: Res>, - mut load_requests: EventWriter, -) { +fn load_game(keycode: Res>, mut load_requests: EventWriter) { if keycode.just_pressed(KeyCode::KeyL) { load_requests.send(LoadingRequest { path: "scenes/save.scn.ron".into(), diff --git a/testing/bevy_example/src/game/in_game.rs b/testing/bevy_example/src/game/in_game.rs index d5c80c6..3c69d34 100644 --- a/testing/bevy_example/src/game/in_game.rs +++ b/testing/bevy_example/src/game/in_game.rs @@ -1,8 +1,8 @@ use crate::{GameState, InAppRunning}; use bevy::prelude::*; use blenvy::{ - AddToGameWorld, BluePrintBundle, BlueprintInfo, Dynamic, GameWorldTag, - HideUntilReady, SpawnBlueprint, + AddToGameWorld, BluePrintBundle, BlueprintInfo, Dynamic, GameWorldTag, HideUntilReady, + SpawnBlueprint, }; //use bevy_rapier3d::prelude::Velocity;