This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.rs
81 lines (66 loc) · 2.31 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use bevy::prelude::*;
pub struct PropagatingStatesHierarchyPlugin;
impl Plugin for PropagatingStatesHierarchyPlugin {
fn build(&self, app: &mut App) {
app.add_state::<MajorState>();
app.add_state::<MinorState>();
app.add_systems(OnEnter(MajorState::B), update_state(MinorState::C));
app.add_systems(OnExit(MajorState::B), update_state(MinorState::None));
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, States)]
pub enum MajorState {
#[default]
A,
B,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, States)]
pub enum MinorState {
#[default]
None,
C,
D,
}
pub fn update_state<T: States>(state: T) -> impl Fn(ResMut<NextState<T>>) {
move |mut next_state: ResMut<NextState<T>>| {
next_state.set(state.clone());
}
}
#[cfg(test)]
mod tests {
use bevy::prelude::*;
use crate::{update_state, PropagatingStatesHierarchyPlugin, MajorState, MinorState};
#[test]
fn default_state() {
let mut app = App::new();
app.add_plugins(MinimalPlugins);
app.add_plugins(PropagatingStatesHierarchyPlugin);
assert_eq!(*app.world.resource::<State<MajorState>>(), MajorState::A);
assert_eq!(*app.world.resource::<State<MinorState>>(), MinorState::None);
}
#[test]
fn change_major_state_to_b() {
let mut app = App::new();
app.add_plugins(MinimalPlugins);
app.add_plugins(PropagatingStatesHierarchyPlugin);
app.add_systems(Update, update_state(MajorState::B).run_if(run_once()));
for _ in 0..3 {
app.update();
}
assert_eq!(*app.world.resource::<State<MajorState>>(), MajorState::B);
assert_eq!(*app.world.resource::<State<MinorState>>(), MinorState::C);
}
#[test]
fn change_major_state_to_b_then_a() {
let mut app = App::new();
app.add_plugins(MinimalPlugins);
app.add_plugins(PropagatingStatesHierarchyPlugin);
app.add_systems(Update, update_state(MajorState::B).run_if(run_once()));
app.add_systems(OnEnter(MajorState::B), update_state(MajorState::A));
for _ in 0..4 {
app.update();
}
assert_eq!(*app.world.resource::<State<MajorState>>(), MajorState::A);
assert_eq!(*app.world.resource::<State<MinorState>>(), MinorState::None);
}
}