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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//! `pyri_state` is a `bevy_state` alternative offering flexible change detection & scheduling.
//!
//! **NOTE:** This crate is incompatible with the `bevy/bevy_state` feature, so make sure it's
//! disabled.
//!
//! # Overview
//!
//! 1. The current state is a [`Resource`](bevy_ecs::resource::Resource) or
//! [`Component`](bevy_ecs::component::Component) that implements [`State`](state::State).
//! 2. The [next state](next_state) is stored in a
//! [`NextStateBuffer`](next_state::buffer::NextStateBuffer) resource by default.
//! 3. A state flush is triggered by the [`TriggerStateFlush`](next_state::TriggerStateFlush)
//! resource and handled in the [`StateFlush`](schedule::StateFlush) schedule.
//! 4. State flush hooks are organized into [`ResolveStateSystems`](schedule::ResolveStateSystems)
//! system sets.
//! 5. Tools are provided for state [setup], [access], [pattern-matching](pattern),
//! [debugging](debug), and [more](extra).
//!
//! # Getting started
//!
//! Import the [prelude] to bring traits and common types into scope:
//!
//! ```
//! use pyri_state::prelude::*;
//! ```
//!
//! Define your own [`State`](state::State) type using the
//! [derive macro](pyri_state_derive::State):
//!
//! ```
//! # use pyri_state::prelude::*;
//! #
//! #[derive(State, Clone, PartialEq, Eq, Default)]
//! struct Level(pub usize);
//! ```
//!
//! Add [`StatePlugin`](setup::StatePlugin) and initialize your state type:
//!
//! ```
//! # use bevy::prelude::*;
//! # use pyri_state::prelude::*;
//! #
//! # #[derive(State, Clone, PartialEq, Eq, Default)]
//! # struct Level(pub usize);
//! #
//! # fn plugin(app: &mut App) {
//! app.add_plugins(StatePlugin).init_state::<Level>();
//! # }
//! ```
//!
//! Add update systems with [`StatePattern::on_update`](pattern::StatePattern::on_update):
//!
//! ```
//! # use bevy::prelude::*;
//! # use pyri_state::prelude::*;
//! #
//! # #[derive(State, Clone, PartialEq, Eq, Default)]
//! # struct Level(pub usize);
//! #
//! # fn update_level_timer() {}
//! # fn update_boss_health_bar() {}
//! # fn spawn_enemy_waves() {}
//! #
//! # fn plugin(app: &mut App) {
//! app.add_systems(Update, (
//! Level::ANY.on_update(update_level_timer),
//! Level(10).on_update(update_boss_health_bar),
//! state!(Level(4..=6)).on_update(spawn_enemy_waves),
//! ));
//! # }
//! ```
//!
//! Add flush hooks with other [`StatePattern`](pattern::StatePattern) methods:
//!
//! ```
//! # use bevy::prelude::*;
//! # use pyri_state::prelude::*;
//! #
//! # #[derive(State, Clone, PartialEq, Eq, Default)]
//! # struct Level(pub usize);
//! #
//! # fn despawn_old_level() {}
//! # fn spawn_new_level() {}
//! # fn play_boss_music() {}
//! # fn save_checkpoint() {}
//! # fn spawn_tutorial_popup() {}
//! #
//! # fn plugin(app: &mut App) {
//! app.add_systems(StateFlush, (
//! // Short-hand for `on_exit` followed by `on_enter`.
//! Level::ANY.on_edge(despawn_old_level, spawn_new_level),
//! Level(10).on_enter(play_boss_music),
//! state!(Level(4 | 7 | 10)).on_enter(save_checkpoint),
//! Level::with(|x| x.0 < 4).on_enter(spawn_tutorial_popup),
//! ));
//! # }
//! ```
// Support configuring Bevy lints within code.
extern crate alloc;
// Allow macros to refer to this crate as `pyri_state` internally.
extern crate self as pyri_state;
/// Re-exported traits and common types.
///
/// Import the prelude to get started:
///
/// ```
/// use pyri_state::prelude::*;
/// ```