Skip to content

Commit

Permalink
feat(example): improve drag and drop example with cleanup (#292)
Browse files Browse the repository at this point in the history
feat(example): update drag and drop example with plugin

modularize spin into two systems: update and cleanup
  • Loading branch information
TheFedaikin authored Feb 23, 2024
1 parent 1471a9f commit a318858
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions examples/drag_and_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ fn main() {
app.add_plugins((
DefaultPlugins.set(low_latency_window_plugin()),
DefaultPickingPlugins,
SpinPlugin,
))
.add_systems(Startup, setup)
.add_systems(Update, spin);
.add_systems(Startup, setup);
#[cfg(feature = "backend_egui")]
app.add_plugins(bevy_egui::EguiPlugin);
app.run();
Expand Down Expand Up @@ -50,6 +50,15 @@ fn setup(
}
}

pub struct SpinPlugin;

impl Plugin for SpinPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, spin)
.add_systems(Update, spin_cleanup);
}
}

#[derive(Component)]
struct Spin(f32);

Expand All @@ -60,3 +69,12 @@ fn spin(mut square: Query<(&mut Spin, &mut Transform)>) {
spin.0 += delta;
}
}

fn spin_cleanup(mut square: Query<(Entity, &Spin, &mut Transform)>, mut commands: Commands) {
for (entity, spin, mut transform) in square.iter_mut() {
if spin.0.abs().le(&0.001) {
transform.rotation = Quat::default(); // <- reset the rotation to zero when it's visually neglible
commands.entity(entity).remove::<Spin>(); // <- remove the component so it's stopped updating
}
}
}

0 comments on commit a318858

Please sign in to comment.