1
1
use std:: marker:: PhantomData ;
2
2
3
3
use bevy:: prelude:: * ;
4
+ use pyri_state:: prelude:: * ;
4
5
5
6
use crate :: core:: camera:: CameraRoot ;
7
+ use crate :: core:: pause:: Pause ;
6
8
use crate :: core:: PostTransformSet ;
7
9
use crate :: core:: UpdateSet ;
8
10
use crate :: game:: combat:: hit:: OnHit ;
@@ -20,11 +22,11 @@ pub struct DespawnOnHit;
20
22
impl Configure for DespawnOnHit {
21
23
fn configure ( app : & mut App ) {
22
24
app. register_type :: < Self > ( ) ;
23
- app. observe ( despawn_on_hit ) ;
25
+ app. observe ( apply_despawn_on_hit ) ;
24
26
}
25
27
}
26
28
27
- fn despawn_on_hit (
29
+ fn apply_despawn_on_hit (
28
30
trigger : Trigger < OnHit > ,
29
31
mut despawn : ResMut < LateDespawn > ,
30
32
despawn_query : Query < ( ) , With < DespawnOnHit > > ,
@@ -80,12 +82,14 @@ impl Configure for DespawnOnBeat {
80
82
app. register_type :: < Self > ( ) ;
81
83
app. add_systems (
82
84
Update ,
83
- despawn_on_beat. in_set ( UpdateSet :: Update ) . run_if ( on_beat ( 1 ) ) ,
85
+ apply_despawn_on_beat
86
+ . in_set ( UpdateSet :: Update )
87
+ . run_if ( on_beat ( 1 ) ) ,
84
88
) ;
85
89
}
86
90
}
87
91
88
- fn despawn_on_beat (
92
+ fn apply_despawn_on_beat (
89
93
mut despawn : ResMut < LateDespawn > ,
90
94
mut despawn_query : Query < ( Entity , & mut DespawnOnBeat ) > ,
91
95
) {
@@ -104,17 +108,44 @@ pub struct DespawnOnTimer(pub Timer);
104
108
impl Configure for DespawnOnTimer {
105
109
fn configure ( app : & mut App ) {
106
110
app. register_type :: < Self > ( ) ;
107
- app. add_systems ( Update , despawn_on_timer. in_set ( UpdateSet :: Update ) ) ;
111
+ app. add_systems (
112
+ StateFlush ,
113
+ Pause . on_edge ( unpause_despawn_on_timer, pause_despawn_on_timer) ,
114
+ ) ;
115
+ app. add_systems (
116
+ Update ,
117
+ (
118
+ tick_despawn_on_timer. in_set ( UpdateSet :: TickTimers ) ,
119
+ apply_despawn_on_timer. in_set ( UpdateSet :: Update ) ,
120
+ ) ,
121
+ ) ;
108
122
}
109
123
}
110
124
111
- fn despawn_on_timer (
112
- time : Res < Time > ,
125
+ fn unpause_despawn_on_timer ( mut timer_query : Query < & mut DespawnOnTimer > ) {
126
+ for mut timer in & mut timer_query {
127
+ timer. 0 . unpause ( ) ;
128
+ }
129
+ }
130
+
131
+ fn pause_despawn_on_timer ( mut timer_query : Query < & mut DespawnOnTimer > ) {
132
+ for mut timer in & mut timer_query {
133
+ timer. 0 . pause ( ) ;
134
+ }
135
+ }
136
+
137
+ fn tick_despawn_on_timer ( time : Res < Time > , mut timer_query : Query < & mut DespawnOnTimer > ) {
138
+ for mut timer in & mut timer_query {
139
+ timer. 0 . tick ( time. delta ( ) ) ;
140
+ }
141
+ }
142
+
143
+ fn apply_despawn_on_timer (
113
144
mut despawn : ResMut < LateDespawn > ,
114
- mut despawn_query : Query < ( Entity , & mut DespawnOnTimer ) > ,
145
+ timer_query : Query < ( Entity , & DespawnOnTimer ) > ,
115
146
) {
116
- for ( entity, mut timer) in & mut despawn_query {
117
- if timer. 0 . tick ( time . delta ( ) ) . finished ( ) {
147
+ for ( entity, timer) in & timer_query {
148
+ if timer. 0 . finished ( ) {
118
149
despawn. recursive ( entity) ;
119
150
}
120
151
}
@@ -131,7 +162,17 @@ pub struct RemoveOnTimer<C: Component + TypePath> {
131
162
impl < C : Component + TypePath > Configure for RemoveOnTimer < C > {
132
163
fn configure ( app : & mut App ) {
133
164
app. register_type :: < Self > ( ) ;
134
- app. add_systems ( Update , remove_on_timer :: < C > . in_set ( UpdateSet :: SyncLate ) ) ;
165
+ app. add_systems (
166
+ StateFlush ,
167
+ Pause . on_edge ( unpause_remove_on_timer :: < C > , pause_remove_on_timer :: < C > ) ,
168
+ ) ;
169
+ app. add_systems (
170
+ Update ,
171
+ (
172
+ tick_remove_on_timer :: < C > . in_set ( UpdateSet :: TickTimers ) ,
173
+ apply_remove_on_timer :: < C > . in_set ( UpdateSet :: SyncLate ) ,
174
+ ) ,
175
+ ) ;
135
176
}
136
177
}
137
178
@@ -149,13 +190,33 @@ impl<C: Component + TypePath> RemoveOnTimer<C> {
149
190
}
150
191
}
151
192
152
- fn remove_on_timer < C : Component + TypePath > (
153
- mut commands : Commands ,
154
- mut remove_query : Query < ( Entity , & mut RemoveOnTimer < C > ) > ,
193
+ fn tick_remove_on_timer < C : Component + TypePath > (
155
194
time : Res < Time > ,
195
+ mut timer_query : Query < & mut RemoveOnTimer < C > > ,
156
196
) {
157
- for ( entity, mut remove) in & mut remove_query {
158
- if remove. timer . tick ( time. delta ( ) ) . finished ( ) {
197
+ for mut timer in & mut timer_query {
198
+ timer. timer . tick ( time. delta ( ) ) ;
199
+ }
200
+ }
201
+
202
+ fn unpause_remove_on_timer < C : Component + TypePath > ( mut timer_query : Query < & mut RemoveOnTimer < C > > ) {
203
+ for mut timer in & mut timer_query {
204
+ timer. timer . unpause ( ) ;
205
+ }
206
+ }
207
+
208
+ fn pause_remove_on_timer < C : Component + TypePath > ( mut timer_query : Query < & mut RemoveOnTimer < C > > ) {
209
+ for mut timer in & mut timer_query {
210
+ timer. timer . pause ( ) ;
211
+ }
212
+ }
213
+
214
+ fn apply_remove_on_timer < C : Component + TypePath > (
215
+ mut commands : Commands ,
216
+ timer_query : Query < ( Entity , & RemoveOnTimer < C > ) > ,
217
+ ) {
218
+ for ( entity, timer) in & timer_query {
219
+ if timer. timer . finished ( ) {
159
220
commands. entity ( entity) . remove :: < ( C , RemoveOnTimer < C > ) > ( ) ;
160
221
}
161
222
}
@@ -174,7 +235,7 @@ impl<C: Component + TypePath> Configure for RemoveOnBeat<C> {
174
235
app. register_type :: < Self > ( ) ;
175
236
app. add_systems (
176
237
Update ,
177
- remove_on_beat :: < C >
238
+ apply_remove_on_beat :: < C >
178
239
. in_set ( UpdateSet :: SyncLate )
179
240
. run_if ( on_beat ( 1 ) ) ,
180
241
) ;
@@ -194,7 +255,7 @@ impl<C: Component + TypePath> RemoveOnBeat<C> {
194
255
}
195
256
}
196
257
197
- fn remove_on_beat < C : Component + TypePath > (
258
+ fn apply_remove_on_beat < C : Component + TypePath > (
198
259
mut commands : Commands ,
199
260
mut remove_query : Query < ( Entity , & mut RemoveOnBeat < C > ) > ,
200
261
) {
0 commit comments