Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(example): improve drag and drop example with cleanup #292

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}