forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_scoped.rs
More file actions
137 lines (127 loc) · 3.86 KB
/
state_scoped.rs
File metadata and controls
137 lines (127 loc) · 3.86 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! Shows how to spawn entities that are automatically despawned either when
//! entering or exiting specific game states.
//!
//! This pattern is useful for managing menus, levels, or other state-specific
//! content that should only exist during certain states.
//!
//! If the entity was already despawned then no error will be logged. This means
//! that you don't have to worry about duplicate [`DespawnOnExit`] and
//! [`DespawnOnEnter`] components deep in your hierarchy.
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.init_state::<GameState>()
.add_systems(Startup, setup_camera)
.add_systems(OnEnter(GameState::A), on_a_enter)
.add_systems(OnEnter(GameState::B), on_b_enter)
.add_systems(OnExit(GameState::A), on_a_exit)
.add_systems(OnExit(GameState::B), on_b_exit)
.add_systems(Update, toggle)
.insert_resource(TickTock(Timer::from_seconds(1.0, TimerMode::Repeating)))
.run();
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)]
enum GameState {
#[default]
A,
B,
}
#[derive(Resource)]
struct TickTock(Timer);
fn on_a_enter(mut commands: Commands) {
info!("on_a_enter");
commands.spawn((
DespawnOnExit(GameState::A),
Text::new("Game is in state 'A'"),
TextFont {
font_size: 33.0,
..default()
},
TextColor(Color::srgb(0.5, 0.5, 1.0)),
Node {
position_type: PositionType::Absolute,
top: px(0),
left: px(0),
..default()
},
(children![DespawnOnExit(GameState::A)]),
));
}
fn on_a_exit(mut commands: Commands) {
info!("on_a_exit");
commands.spawn((
DespawnOnEnter(GameState::A),
Text::new("Game state 'A' will be back in 1 second"),
TextFont {
font_size: 33.0,
..default()
},
TextColor(Color::srgb(0.5, 0.5, 1.0)),
Node {
position_type: PositionType::Absolute,
top: px(0),
left: px(500),
..default()
},
// You can apply this even when the parent has a state scoped component.
// It is unnecessary but in complex hierarchies it saves you from having to
// mentally track which components are found at the top level.
(children![DespawnOnEnter(GameState::A)]),
));
}
fn on_b_enter(mut commands: Commands) {
info!("on_b_enter");
commands.spawn((
DespawnOnExit(GameState::B),
Text::new("Game is in state 'B'"),
TextFont {
font_size: 33.0,
..default()
},
TextColor(Color::srgb(0.5, 0.5, 1.0)),
Node {
position_type: PositionType::Absolute,
top: px(50),
left: px(0),
..default()
},
(children![DespawnOnExit(GameState::B)]),
));
}
fn on_b_exit(mut commands: Commands) {
info!("on_b_exit");
commands.spawn((
DespawnOnEnter(GameState::B),
Text::new("Game state 'B' will be back in 1 second"),
TextFont {
font_size: 33.0,
..default()
},
TextColor(Color::srgb(0.5, 0.5, 1.0)),
Node {
position_type: PositionType::Absolute,
top: px(50),
left: px(500),
..default()
},
(children![DespawnOnEnter(GameState::B)]),
));
}
fn setup_camera(mut commands: Commands) {
commands.spawn(Camera3d::default());
}
fn toggle(
time: Res<Time>,
mut timer: ResMut<TickTock>,
state: Res<State<GameState>>,
mut next_state: ResMut<NextState<GameState>>,
) {
if !timer.0.tick(time.delta()).is_finished() {
return;
}
*next_state = match state.get() {
GameState::A => NextState::Pending(GameState::B),
GameState::B => NextState::Pending(GameState::A),
}
}