Skip to content
Open
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
72 changes: 72 additions & 0 deletions crates/bevy_state/src/state_scoped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,75 @@ pub fn despawn_entities_on_enter_state<S: States>(
}
}
}

#[cfg(test)]
mod tests {
use super::*;

use bevy_app::App;

use crate::{
app::{AppExtStates, StatesPlugin},
prelude::CommandsStatesExt,
};

#[test]
fn despawn_on_exit_from_computed_state() {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, States)]
enum State {
On,
Off,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct ComputedState;
impl bevy_state::state::ComputedStates for ComputedState {
type SourceStates = State;

fn compute(sources: Self::SourceStates) -> Option<Self> {
match sources {
State::On => Some(ComputedState),
State::Off => None,
}
}
}

let mut app = App::new();
app.add_plugins(StatesPlugin);

app.insert_state(State::On);
app.add_computed_state::<ComputedState>();
app.update();

assert_eq!(
app.world()
.resource::<bevy_state::state::State<State>>()
.get(),
&State::On
);
assert_eq!(
app.world()
.resource::<bevy_state::state::State<ComputedState>>()
.get(),
&ComputedState
);

let entity = app.world_mut().spawn(DespawnOnExit(ComputedState)).id();
assert!(app.world().get_entity(entity).is_ok());

app.world_mut().commands().set_state(State::Off);
app.update();

assert_eq!(
app.world()
.resource::<bevy_state::state::State<State>>()
.get(),
&State::Off
);
assert!(app
.world()
.get_resource::<bevy_state::state::State<ComputedState>>()
.is_none());
assert!(app.world().get_entity(entity).is_err());
}
}