Skip to content

Commit db0746f

Browse files
authored
Merge pull request #456 from Freyja-moth/cleanup
Cleanup
2 parents 26f4395 + 37e0a11 commit db0746f

17 files changed

Lines changed: 317 additions & 343 deletions

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ mod third_party;
1818
mod ui_camera;
1919

2020
use asset_processing::default_image_sampler_descriptor;
21+
use bevy::app::HierarchyPropagatePlugin;
2122
use bevy::gltf::GltfPlugin;
2223
use bevy::gltf::convert_coordinates::GltfConvertCoordinates;
24+
use bevy::light::NotShadowCaster;
2325
use bevy::log::LogPlugin;
2426
use bevy::log::tracing_subscriber::field::MakeExt;
2527
use bevy::pbr::DefaultOpaqueRendererMethod;
@@ -112,6 +114,7 @@ fn main() -> AppExit {
112114
SeedlingPlugin::default(),
113115
#[cfg(feature = "web")]
114116
SeedlingPlugin::new_web_audio(),
117+
HierarchyPropagatePlugin::<NotShadowCaster>::new(Update),
115118
));
116119

117120
app.insert_resource(GlobalAmbientLight::NONE);
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use bevy::prelude::*;
2-
1+
use bevy::{app::Propagate, light::NotShadowCaster, prelude::*};
32
use bevy_trenchbroom::prelude::*;
43

5-
use crate::props::effects::disable_shadow_casting;
6-
74
pub(super) fn plugin(app: &mut App) {
85
app.add_observer(setup_light_window_brush_entity);
96
}
@@ -15,13 +12,13 @@ fn setup_light_window_brush_entity(add: On<Add, LightWindow>, mut commands: Comm
1512
let entity = add.entity;
1613
commands
1714
.entity(entity)
15+
.insert((NotShadowCaster, Propagate(NotShadowCaster)))
1816
// Using `children!` here would run into https://github.com/Noxmore/bevy_trenchbroom/issues/95
1917
.with_child(SpotLight {
2018
color: Color::srgb_u8(239, 173, 144),
2119
intensity: 200_000.0,
2220
radius: 0.1,
2321
shadows_enabled: true,
2422
..default()
25-
})
26-
.queue(disable_shadow_casting);
23+
});
2724
}

src/props/effects.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,5 @@
11
//! Utility functions for adding special effects to props.
22
3-
use bevy::{light::NotShadowCaster, prelude::*, scene::SceneInstanceReady};
4-
5-
use std::iter;
3+
use bevy::app::App;
64

75
pub(super) fn plugin(_app: &mut App) {}
8-
9-
pub(crate) fn disable_shadow_casting_on_instance_ready(
10-
ready: On<SceneInstanceReady>,
11-
mut commands: Commands,
12-
) {
13-
commands.entity(ready.entity).queue(disable_shadow_casting);
14-
}
15-
16-
pub(crate) fn disable_shadow_casting(entity_world: EntityWorldMut) {
17-
let entity = entity_world.id();
18-
entity_world
19-
.into_world_mut()
20-
.run_system_cached_with(disable_shadow_casting_system, entity)
21-
.unwrap();
22-
}
23-
24-
fn disable_shadow_casting_system(
25-
In(entity): In<Entity>,
26-
children: Query<&Children>,
27-
is_mesh: Query<&Mesh3d>,
28-
mut commands: Commands,
29-
) {
30-
for child in iter::once(entity).chain(children.iter_descendants(entity)) {
31-
if is_mesh.get(child).is_ok() {
32-
commands.entity(child).insert(NotShadowCaster);
33-
}
34-
}
35-
}

src/props/generic.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
1-
use crate::{
2-
asset_tracking::LoadResource, third_party::bevy_trenchbroom::GetTrenchbroomModelPath as _,
3-
};
1+
use crate::{asset_tracking::LoadResource, third_party::bevy_trenchbroom::GetTrenchbroomModelPath};
42

53
use super::setup::*;
4+
use avian3d::prelude::{ColliderConstructor, RigidBody};
65
use bevy::prelude::*;
76
use bevy_trenchbroom::prelude::*;
87

98
pub(super) fn plugin(app: &mut App) {
10-
app.add_observer(setup_static_prop_with_convex_hull::<Grate>)
11-
.add_observer(setup_static_prop_with_convex_decomposition::<Table>)
12-
.add_observer(setup_static_prop_with_convex_hull::<Bookshelf>)
13-
.add_observer(setup_static_prop_with_convex_hull::<Generator2>)
14-
.add_observer(setup_static_prop_with_convex_hull::<BarrelLargeClosed>)
15-
.add_observer(setup_static_prop_with_convex_hull::<Barrel01>)
16-
.add_observer(setup_static_prop_with_convex_hull::<CrateSquare>)
17-
.add_observer(setup_static_prop_with_convex_hull::<FenceBarsDecorativeSingle>)
18-
.add_observer(setup_static_prop_with_convex_hull::<DoorStainedGlass>);
19-
20-
app.add_observer(setup_dynamic_prop_with_convex_hull::<PackageMedium>)
21-
.add_observer(setup_dynamic_prop_with_convex_hull::<PackageSmall>);
22-
23-
app.add_observer(setup_nonphysical_prop::<IvyPart8>)
24-
.add_observer(setup_nonphysical_prop::<SmallDoorSign1>);
25-
269
app.load_asset::<Gltf>(PackageMedium::model_path())
2710
.load_asset::<Gltf>(PackageSmall::model_path())
2811
.load_asset::<Gltf>(Grate::model_path())
@@ -44,67 +27,78 @@ pub(super) fn plugin(app: &mut App) {
4427
base(Transform, Visibility),
4528
model("models/darkmod/containers/package_medium.gltf")
4629
)]
30+
#[component(on_add = setup_prop::<PackageMedium>(RigidBody::Dynamic, ColliderConstructor::ConvexHullFromMesh))]
4731
pub(crate) struct PackageMedium;
4832

4933
#[point_class(
5034
base(Transform, Visibility),
5135
model("models/darkmod/containers/package_small.gltf")
5236
)]
37+
#[component(on_add = setup_prop::<PackageSmall>(RigidBody::Dynamic, ColliderConstructor::ConvexHullFromMesh))]
5338
pub(crate) struct PackageSmall;
5439

5540
// generic static props
5641
#[point_class(
5742
base(Transform, Visibility),
5843
model("models/darkmod/fireplace/grate.gltf")
5944
)]
45+
#[component(on_add = setup_prop::<Grate>(RigidBody::Static, ColliderConstructor::ConvexHullFromMesh))]
6046
pub(crate) struct Grate;
6147

6248
#[point_class(
6349
base(Transform, Visibility),
6450
model("models/darkmod/furniture/tables/rtable1.gltf")
6551
)]
52+
#[component(on_add = setup_prop::<Table>(RigidBody::Static, ColliderConstructor::ConvexDecompositionFromMesh))]
6653
pub(crate) struct Table;
6754

6855
#[point_class(
6956
base(Transform, Visibility),
7057
model("models/darkmod/furniture/shelves/bookshelf02.gltf")
7158
)]
59+
#[component(on_add = setup_prop::<Bookshelf>(RigidBody::Static, ColliderConstructor::ConvexHullFromMesh))]
7260
pub(crate) struct Bookshelf;
7361

7462
#[point_class(
7563
base(Transform, Visibility),
7664
model("models/darkmod/mechanical/generator2/generator2.gltf")
7765
)]
66+
#[component(on_add = setup_prop::<Generator2>(RigidBody::Static, ColliderConstructor::ConvexHullFromMesh))]
7867
pub(crate) struct Generator2;
7968

8069
#[point_class(
8170
base(Transform, Visibility),
8271
model("models/darkmod/containers/barrel_large_closed.gltf")
8372
)]
73+
#[component(on_add = setup_prop::<BarrelLargeClosed>(RigidBody::Static, ColliderConstructor::ConvexHullFromMesh))]
8474
pub(crate) struct BarrelLargeClosed;
8575

8676
#[point_class(
8777
base(Transform, Visibility),
8878
model("models/darkmod/containers/barrel01.gltf")
8979
)]
80+
#[component(on_add = setup_prop::<Barrel01>(RigidBody::Static, ColliderConstructor::ConvexHullFromMesh))]
9081
pub(crate) struct Barrel01;
9182

9283
#[point_class(
9384
base(Transform, Visibility),
9485
model("models/darkmod/containers/crate_square.gltf")
9586
)]
87+
#[component(on_add = setup_prop::<CrateSquare>(RigidBody::Static, ColliderConstructor::ConvexHullFromMesh))]
9688
pub(crate) struct CrateSquare;
9789

9890
#[point_class(
9991
base(Transform, Visibility),
10092
model("models/darkmod/architecture/fencing/fence_bars_decorative01_single.gltf")
10193
)]
94+
#[component(on_add = setup_prop::<FenceBarsDecorativeSingle>(RigidBody::Static, ColliderConstructor::ConvexHullFromMesh))]
10295
pub(crate) struct FenceBarsDecorativeSingle;
10396

10497
#[point_class(
10598
base(Transform, Visibility),
10699
model("models/darkmod/architecture/doors/door_stained_glass_118x52.gltf")
107100
)]
101+
#[component(on_add = setup_prop::<DoorStainedGlass>(RigidBody::Static, ColliderConstructor::ConvexHullFromMesh))]
108102
pub(crate) struct DoorStainedGlass;
109103

110104
// Generic non-physical props
@@ -113,10 +107,12 @@ pub(crate) struct DoorStainedGlass;
113107
base(Transform, Visibility),
114108
model("models/darkmod/nature/ivy_part08.gltf")
115109
)]
110+
#[component(on_add = setup_nonphysical_prop::<IvyPart8>)]
116111
pub(crate) struct IvyPart8;
117112

118113
#[point_class(
119114
base(Transform, Visibility),
120115
model("models/darkmod/decorative/signs/small_door_sign1.gltf")
121116
)]
117+
#[component(on_add = setup_nonphysical_prop::<SmallDoorSign1>)]
122118
pub(crate) struct SmallDoorSign1;

src/props/setup.rs

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,49 @@
55
use crate::third_party::avian3d::CollisionLayer;
66
use crate::third_party::bevy_trenchbroom::LoadTrenchbroomModel as _;
77
use avian3d::prelude::*;
8+
use bevy::asset::io::embedded::GetAssetServer;
9+
use bevy::ecs::lifecycle::HookContext;
10+
use bevy::ecs::world::DeferredWorld;
811
use bevy::prelude::*;
912

1013
use bevy_trenchbroom::class::QuakeClass;
14+
use bevy_trenchbroom::util::IsSceneWorld as _;
1115

1216
pub(super) fn plugin(_app: &mut App) {}
1317

14-
pub(crate) fn setup_static_prop_with_convex_hull<T: QuakeClass>(
15-
add: On<Add, T>,
16-
asset_server: Res<AssetServer>,
17-
mut commands: Commands,
18-
) {
19-
let bundle = static_bundle::<T>(&asset_server, ColliderConstructor::ConvexHullFromMesh);
20-
commands.entity(add.entity).insert(bundle);
18+
pub(crate) fn setup_prop<T: QuakeClass>(
19+
rigid_body: RigidBody,
20+
collider: ColliderConstructor,
21+
) -> impl FnOnce(DeferredWorld, HookContext) {
22+
move |mut world, ctx| {
23+
if world.is_scene_world() {
24+
return;
25+
}
26+
world.commands().queue(move |world: &mut World| {
27+
let asset_server = world.get_asset_server().clone();
28+
world.entity_mut(ctx.entity).insert(quake_bundle::<T>(
29+
asset_server,
30+
rigid_body,
31+
collider,
32+
));
33+
});
34+
}
2135
}
2236

23-
pub(crate) fn setup_nonphysical_prop<T: QuakeClass>(
24-
add: On<Add, T>,
25-
asset_server: Res<AssetServer>,
26-
mut commands: Commands,
27-
) {
28-
let model = asset_server.load_trenchbroom_model::<T>();
29-
commands.entity(add.entity).insert(SceneRoot(model));
30-
}
31-
32-
pub(crate) fn setup_static_prop_with_convex_decomposition<T: QuakeClass>(
33-
add: On<Add, T>,
34-
asset_server: Res<AssetServer>,
35-
mut commands: Commands,
36-
) {
37-
let bundle = static_bundle::<T>(
38-
&asset_server,
39-
ColliderConstructor::ConvexDecompositionFromMesh,
40-
);
41-
commands.entity(add.entity).insert(bundle);
37+
pub(crate) fn setup_nonphysical_prop<T: QuakeClass>(mut world: DeferredWorld, ctx: HookContext) {
38+
if world.is_scene_world() {
39+
return;
40+
}
41+
world.commands().queue(move |world: &mut World| {
42+
let asset_server = world.get_asset_server().clone();
43+
let model = asset_server.load_trenchbroom_model::<T>();
44+
world.entity_mut(ctx.entity).insert(SceneRoot(model));
45+
});
4246
}
4347

44-
pub(crate) fn setup_dynamic_prop_with_convex_hull<T: QuakeClass>(
45-
add: On<Add, T>,
46-
asset_server: Res<AssetServer>,
47-
mut commands: Commands,
48-
) {
49-
let bundle = dynamic_bundle::<T>(&asset_server, ColliderConstructor::ConvexHullFromMesh);
50-
commands.entity(add.entity).insert(bundle);
51-
}
52-
53-
pub(crate) fn dynamic_bundle<T: QuakeClass>(
54-
asset_server: &AssetServer,
48+
pub(crate) fn quake_bundle<T: QuakeClass>(
49+
asset_server: AssetServer,
50+
rigid_body: RigidBody,
5551
constructor: ColliderConstructor,
5652
) -> impl Bundle {
5753
let model = asset_server.load_trenchbroom_model::<T>();
@@ -60,22 +56,7 @@ pub(crate) fn dynamic_bundle<T: QuakeClass>(
6056
.with_default_layers(CollisionLayers::new(CollisionLayer::Prop, LayerMask::ALL))
6157
// About the density of oak wood (600-800 kg/m^3)
6258
.with_default_density(800.0),
63-
RigidBody::Dynamic,
64-
SceneRoot(model),
65-
)
66-
}
67-
68-
pub(crate) fn static_bundle<T: QuakeClass>(
69-
asset_server: &AssetServer,
70-
constructor: ColliderConstructor,
71-
) -> impl Bundle {
72-
let model = asset_server.load_trenchbroom_model::<T>();
73-
(
74-
ColliderConstructorHierarchy::new(constructor).with_default_layers(CollisionLayers::new(
75-
CollisionLayer::Default,
76-
LayerMask::ALL,
77-
)),
78-
RigidBody::Static,
59+
rigid_body,
7960
SceneRoot(model),
8061
)
8162
}

0 commit comments

Comments
 (0)