Corrosive-Games / bevy-parallax

Parallax background plugin for Bevy
MIT License
112 stars 17 forks source link

Despawn/Remove Functionality #36

Open lanesawyer opened 7 months ago

lanesawyer commented 7 months ago

Hey there, been playing with bevy-parallax today and ran into a problem: I can't manage to remove the parallax!

This may be me being awful at Bevy and not a problem with the library itself, so if that's the case I apologize in advance.

My setup has different game states (Menu, Game), and I only want the parallax drawn when I'm in Game state. I only have one camera for the various states, so I can't simply throw away the camera entirely like some of the examples in this repository do to reset the parallax.

Here's how I'm initializing it:

fn initialize_parallax(
    mut commands: Commands,
    mut create_parallax: EventWriter<CreateParallaxEvent>,
    camera_query: Query<Entity, With<MainCamera>>,
) {
    let camera = camera_query.get_single().expect("Camera not found");
    commands.entity(camera).insert(ParallaxCameraComponent::default());

    create_parallax.send(CreateParallaxEvent {
        // ... the layers_data,
        camera
    });

I tried to remove the ParallaxCameraComponent from the camera when leaving the Game game state, but that isn't working:

fn despawn_parallax(
    mut commands: Commands,
    camera_query: Query<Entity, With<MainCamera>>,
) {
    for entity in &mut camera_query.iter() {
        commands.entity(entity).remove::<ParallaxCameraComponent>();
    }
}

Is this a valid use case for bevy-parallax adding some sort of remove/despawn command added in future versions, or am I just doing it wrong?

evaogbe commented 1 month ago

I'm not sure if this is right, but I got it to work by removing the LayerComponent.

fn cleanup_background(mut commands: Commands, layer_query: Query<Entity, With<LayerComponent>>) {
    for entity in &layer_query {
        commands.entity(entity).despawn_recursive();
    }
}
lanesawyer commented 1 month ago

Sweet, I'll give that a go. Thank you!