Skip to content

Commit

Permalink
Use a SystemSet for system ordering (#5)
Browse files Browse the repository at this point in the history
I ran into an issue when trying to add the transform propagation systems
to a schedule more than once. The app would panic because there was more
than one instance of `propagate_transforms::<P>` and
`recenter_transform_on_grid::<P>` in the system graph, so Bevy didn't
know how to actually resolve the dependencies. By using a `SystemSet`,
we can avoid this issue nicely.

Unfortunately, I couldn't come up with a good name for the set, so I
decided to stick the definition inside the `build()` method itself. That
way at least the poor naming won't be exposed 😛. It still feels a little
icky to me though.

---------

Co-authored-by: Aevyrie <aevyrie@gmail.com>
Co-authored-by: Pyxrs <simplycmd00@gmail.com>
  • Loading branch information
3 people authored Nov 10, 2023
1 parent 5dacfd8 commit 0907ec1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
run: sudo apt-get update -yq
- name: Install dependencies
run: sudo apt-get install -yq --no-install-recommends libudev-dev libasound2-dev libxcb-composite0-dev

format:
runs-on: ubuntu-latest
needs: [setup]
Expand All @@ -30,7 +29,7 @@ jobs:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2.7.0
- run: cargo check --all-features --all-targets
- run: cargo check --all-features

check-no-defaults:
runs-on: ubuntu-latest
Expand All @@ -39,7 +38,7 @@ jobs:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2.7.0
- run: cargo check --no-default-features --all-targets
- run: cargo check --no-default-features

clippy:
runs-on: ubuntu-latest
Expand All @@ -49,7 +48,7 @@ jobs:
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2.7.0
- run: rustup component add clippy
- run: cargo clippy --all-features --all-targets -- -D warnings
- run: cargo clippy --all-features -- -D warnings

doc:
runs-on: ubuntu-latest
Expand All @@ -69,4 +68,4 @@ jobs:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2.7.0
- run: cargo test --all-features --all-targets
- run: cargo test --all-features
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "big_space"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
description = "A floating origin plugin for bevy"
license = "MIT OR Apache-2.0"
Expand All @@ -11,7 +11,7 @@ documentation = "https://docs.rs/crate/big_space/latest"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = { version = "0.11", default_features = false }
bevy = { version = "0.12", default_features = false }

[dev-dependencies]
bevy = { version = "0.12", default-features = false, features = [
Expand All @@ -25,6 +25,7 @@ bevy = { version = "0.12", default-features = false, features = [
bevy_framepace = { version = "0.14", default-features = false }

[features]
default = ["debug", "camera"]
default = ["debug", "camera", "bevy_render"]
debug = ["bevy/bevy_gizmos"]
camera = ["bevy/bevy_render"]
bevy_render = ["bevy/bevy_render"]
camera = ["bevy_render"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ I intend to track the `main` branch of Bevy. PRs supporting this are welcome!

| bevy | big_space |
| ---- | --------- |
| 0.12 | 0.4 |
| 0.11 | 0.3 |
| 0.10 | 0.2 |
| 0.9 | 0.1 |
Expand Down
2 changes: 1 addition & 1 deletion src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub fn default_camera_inputs(
keyboard
.pressed(KeyCode::ShiftLeft)
.then(|| cam.boost = true);
if let Some(total_mouse_motion) = mouse_move.iter().map(|e| e.delta).reduce(|sum, i| sum + i) {
if let Some(total_mouse_motion) = mouse_move.read().map(|e| e.delta).reduce(|sum, i| sum + i) {
cam.pitch += total_mouse_motion.y as f64 * -0.1;
cam.yaw += total_mouse_motion.x as f64 * -0.1;
}
Expand Down
3 changes: 1 addition & 2 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ impl<P: GridPrecision> Plugin for FloatingOriginDebugPlugin<P> {
}
}

/// Update the rendered debug bounds to only highlight occupied [`GridCell`]s. [`DebugBounds`] are
/// spawned or hidden as needed.
/// Update the rendered debug bounds to only highlight occupied [`GridCell`]s.
pub fn update_debug_bounds<P: GridPrecision>(
mut gizmos: Gizmos,
settings: Res<FloatingOriginSettings>,
Expand Down
44 changes: 22 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ impl<P: GridPrecision> FloatingOriginPlugin<P> {

impl<P: GridPrecision + Reflect + FromReflect + TypePath> Plugin for FloatingOriginPlugin<P> {
fn build(&self, app: &mut App) {
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
struct RootGlobalTransformUpdates;

app.insert_resource(FloatingOriginSettings::new(
self.grid_edge_length,
self.switching_threshold,
Expand All @@ -144,28 +147,20 @@ impl<P: GridPrecision + Reflect + FromReflect + TypePath> Plugin for FloatingOri
.add_systems(
PostStartup,
(
recenter_transform_on_grid::<P>,
sync_simple_transforms::<P>
.after(recenter_transform_on_grid::<P>)
.before(propagate_transforms::<P>),
update_global_from_grid::<P>
.after(recenter_transform_on_grid::<P>)
.before(propagate_transforms::<P>),
propagate_transforms::<P>,
recenter_transform_on_grid::<P>.before(RootGlobalTransformUpdates),
(sync_simple_transforms::<P>, update_global_from_grid::<P>)
.in_set(RootGlobalTransformUpdates),
propagate_transforms::<P>.after(RootGlobalTransformUpdates),
)
.in_set(TransformSystem::TransformPropagate),
)
.add_systems(
PostUpdate,
(
recenter_transform_on_grid::<P>,
sync_simple_transforms::<P>
.after(recenter_transform_on_grid::<P>)
.before(propagate_transforms::<P>),
update_global_from_grid::<P>
.after(recenter_transform_on_grid::<P>)
.before(propagate_transforms::<P>),
propagate_transforms::<P>,
recenter_transform_on_grid::<P>.before(RootGlobalTransformUpdates),
(sync_simple_transforms::<P>, update_global_from_grid::<P>)
.in_set(RootGlobalTransformUpdates),
propagate_transforms::<P>.after(RootGlobalTransformUpdates),
)
.in_set(TransformSystem::TransformPropagate),
);
Expand Down Expand Up @@ -272,9 +267,14 @@ impl FloatingOriginSettings {
#[derive(Bundle, Default)]
pub struct FloatingSpatialBundle<P: GridPrecision> {
/// The visibility of the entity.
#[cfg(feature = "bevy_render")]
pub visibility: Visibility,
/// The computed visibility of the entity.
pub computed: ComputedVisibility,
/// The inherited visibility of the entity.
#[cfg(feature = "bevy_render")]
pub inherited: InheritedVisibility,
/// The view visibility of the entity.
#[cfg(feature = "bevy_render")]
pub view: ViewVisibility,
/// The transform of the entity.
pub transform: Transform,
/// The global transform of the entity.
Expand All @@ -296,7 +296,7 @@ pub fn recenter_transform_on_grid<P: GridPrecision>(
) {
query
.par_iter_mut()
.for_each_mut(|(mut grid_pos, mut transform)| {
.for_each(|(mut grid_pos, mut transform)| {
if transform.as_ref().translation.abs().max_element()
> settings.maximum_distance_from_origin
{
Expand Down Expand Up @@ -326,14 +326,14 @@ pub fn update_global_from_grid<P: GridPrecision>(
let mut all_entities = entities.p1();
all_entities
.par_iter_mut()
.for_each_mut(|(local, global, entity_cell)| {
.for_each(|(local, global, entity_cell)| {
update_global_from_cell_local(&settings, entity_cell, &origin_cell, local, global);
});
} else {
let mut moved_cell_entities = entities.p0();
moved_cell_entities
.par_iter_mut()
.for_each_mut(|(local, global, entity_cell)| {
.for_each(|(local, global, entity_cell)| {
update_global_from_cell_local(&settings, entity_cell, &origin_cell, local, global);
});
}
Expand Down Expand Up @@ -368,7 +368,7 @@ pub fn sync_simple_transforms<P: GridPrecision>(
) {
query
.par_iter_mut()
.for_each_mut(|(transform, mut global_transform)| {
.for_each(|(transform, mut global_transform)| {
*global_transform = GlobalTransform::from(*transform);
});
}

0 comments on commit 0907ec1

Please sign in to comment.