From ca0987bbe3a8640c5ab0251b835cbd929ca19f20 Mon Sep 17 00:00:00 2001 From: Franklin Blanco Date: Thu, 16 Nov 2023 13:02:46 -0800 Subject: [PATCH] Fixed bevy_blender plugin not adding some components when something is spawned before it --- .../core/markers/proxy/physics/rapier.rs | 16 ++++--------- src/comps/core/markers/proxy/plugin.rs | 9 +++---- src/comps/core/spawners/spawn.rs | 2 ++ src/main.rs | 2 +- src/setup/assets.rs | 5 +--- src/setup/load_state.rs | 24 ++++++++++++++++++- 6 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/comps/core/markers/proxy/physics/rapier.rs b/src/comps/core/markers/proxy/physics/rapier.rs index ae5d49c..b0b31e4 100644 --- a/src/comps/core/markers/proxy/physics/rapier.rs +++ b/src/comps/core/markers/proxy/physics/rapier.rs @@ -1,6 +1,6 @@ use bevy::prelude::*; // use bevy::render::primitives::Aabb; -use bevy_rapier3d::geometry::Collider as RapierCollider; +use bevy_rapier3d::geometry::{Collider as RapierCollider, Sensor}; use bevy_rapier3d::prelude::{ActiveCollisionTypes, ActiveEvents, ComputedColliderShape}; use super::utils::*; @@ -82,18 +82,12 @@ pub fn physics_replace_proxies( .unwrap(); commands .entity(entity) - .insert(rapier_collider) - // FIXME: this is just for demo purposes !!! - .insert( - ActiveCollisionTypes::default() - | ActiveCollisionTypes::KINEMATIC_STATIC - | ActiveCollisionTypes::STATIC_STATIC - | ActiveCollisionTypes::DYNAMIC_STATIC, - ) - .insert(ActiveEvents::COLLISION_EVENTS); + .insert(rapier_collider); + // FIXME: this is just for demo purposes !!!; + //.insert(ActiveEvents::COLLISION_EVENTS); // .insert(ActiveEvents::COLLISION_EVENTS) // break; - // RapierCollider::convex_hull(points) + } } } diff --git a/src/comps/core/markers/proxy/plugin.rs b/src/comps/core/markers/proxy/plugin.rs index 98d2006..e16132f 100644 --- a/src/comps/core/markers/proxy/plugin.rs +++ b/src/comps/core/markers/proxy/plugin.rs @@ -1,5 +1,7 @@ use bevy::app::{Plugin, Update}; +use crate::setup::load_state::update_game_load_state; + use super::{character::{player_hitbox::PlayerHitBox, player_character::PlayerCharacter, player_eye::PlayerEye, in_player_hands_parent::InPlayerHandsParent}, physics::{rapier::{AutoAABBCollider, physics_replace_proxies}, self}}; @@ -15,9 +17,8 @@ impl Plugin for ProxyComponentsPlugin { app.register_type::(); // Physics - app - .register_type::() - .register_type::() - .add_systems(Update, physics_replace_proxies); + app.register_type::(); + app.register_type::(); + app.add_systems(Update, (physics_replace_proxies, update_game_load_state)); } } \ No newline at end of file diff --git a/src/comps/core/spawners/spawn.rs b/src/comps/core/spawners/spawn.rs index dcc009b..24f03ed 100644 --- a/src/comps/core/spawners/spawn.rs +++ b/src/comps/core/spawners/spawn.rs @@ -1,5 +1,7 @@ use bevy::prelude::*; +use crate::comps::core::markers::proxy::physics::rapier::physics_replace_proxies; + use super::{ item::{item_spawner, ItemSpawnPointPlugin}, player::player_spawner, diff --git a/src/main.rs b/src/main.rs index 7a0ff3d..7d9be37 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,7 @@ fn setup_plugins(application: &mut App) { .add_plugins(proxy::plugin::ProxyComponentsPlugin) .add_plugins(MainGameUIPlugin) .add_plugins(MainEditorUiPlugin) - .add_plugins(RapierDebugRenderPlugin::default()); + ;//.add_plugins(RapierDebugRenderPlugin::default()); } fn load(application: &mut App) { diff --git a/src/setup/assets.rs b/src/setup/assets.rs index 930509e..4727322 100644 --- a/src/setup/assets.rs +++ b/src/setup/assets.rs @@ -30,7 +30,7 @@ pub struct GltfAsset { pub fn load_all_assets( mut commands: Commands, asset_server: Res, - mut game_load_state: ResMut, + //mut game_load_state: ResMut, ) { let skybox_handle: Handle = asset_server.load(CUBEMAPS[0].0); commands.insert_resource(Cubemap { @@ -52,9 +52,6 @@ pub fn load_all_assets( commands.insert_resource(GltfAssets { assets: Vec::from([m4a1_gltf_asset, glock17_gltf_asset, main_character_asset]), }); - game_load_state.assets_loaded = true; - // This works becaue this system is called on startup, so commands will finish running when game starts the update phase. - // Meaning assets will be finished loading as soon as the first update system runs. } impl GltfAssetType { diff --git a/src/setup/load_state.rs b/src/setup/load_state.rs index 5c88ebf..8042fc9 100644 --- a/src/setup/load_state.rs +++ b/src/setup/load_state.rs @@ -1,9 +1,12 @@ -use bevy::prelude::*; +use bevy::{prelude::*, gltf::Gltf}; + +use super::assets::GltfAssets; #[derive(Resource, Default, Reflect)] #[reflect(Resource)] pub struct GameLoadState { pub assets_loaded: bool, + pub loaded_assets: Vec>, pub animations_loaded: bool, pub player_loaded: bool, } @@ -13,3 +16,22 @@ impl GameLoadState { self.assets_loaded && self.animations_loaded } } + + +pub fn update_game_load_state( + mut game_state: ResMut, + mut asset_event_reader: EventReader>, + gltf_assets: Res, +) { + if let Some(asset_event) = asset_event_reader.read().next() { + match asset_event { + AssetEvent::Added { id } => { + game_state.loaded_assets.push(*id); + if gltf_assets.assets.len() == game_state.loaded_assets.len() { + game_state.assets_loaded = true; + } + } + _ => () + } + } +} \ No newline at end of file