@@ -9,9 +9,14 @@ pub mod spotlight;
9
9
pub mod sprite;
10
10
pub mod wave;
11
11
12
+ use std:: borrow:: Cow ;
13
+
12
14
use avian2d:: prelude:: * ;
15
+ use bevy:: ecs:: system:: EntityCommand ;
13
16
use bevy:: prelude:: * ;
17
+ use pyri_state:: prelude:: * ;
14
18
19
+ use crate :: screen:: Screen ;
15
20
use crate :: util:: prelude:: * ;
16
21
17
22
pub ( super ) fn plugin ( app : & mut App ) {
@@ -42,23 +47,16 @@ impl Configure for GameRoot {
42
47
fn configure ( app : & mut App ) {
43
48
app. register_type :: < Self > ( ) ;
44
49
app. init_resource :: < Self > ( ) ;
50
+ app. add_systems ( StateFlush , Screen :: ANY . on_exit ( clear_game_root) ) ;
45
51
}
46
52
}
47
53
48
54
impl FromWorld for GameRoot {
49
55
fn from_world ( world : & mut World ) -> Self {
50
- let players = world
51
- . spawn ( ( Name :: new ( "Players" ) , SpatialBundle :: default ( ) ) )
52
- . id ( ) ;
53
- let enemies = world
54
- . spawn ( ( Name :: new ( "Enemies" ) , SpatialBundle :: default ( ) ) )
55
- . id ( ) ;
56
- let projectiles = world
57
- . spawn ( ( Name :: new ( "Projectiles" ) , SpatialBundle :: default ( ) ) )
58
- . id ( ) ;
59
- let vfx = world
60
- . spawn ( ( Name :: new ( "Vfx" ) , SpatialBundle :: default ( ) ) )
61
- . id ( ) ;
56
+ let players = world. spawn_with ( root ( "Players" ) ) . id ( ) ;
57
+ let enemies = world. spawn_with ( root ( "Enemies" ) ) . id ( ) ;
58
+ let projectiles = world. spawn_with ( root ( "Projectiles" ) ) . id ( ) ;
59
+ let vfx = world. spawn_with ( root ( "Vfx" ) ) . id ( ) ;
62
60
63
61
Self {
64
62
players,
@@ -69,12 +67,18 @@ impl FromWorld for GameRoot {
69
67
}
70
68
}
71
69
72
- impl GameRoot {
73
- pub fn despawn_descendants ( & self , commands : & mut Commands ) {
74
- commands. entity ( self . players ) . despawn_descendants ( ) ;
75
- commands. entity ( self . enemies ) . despawn_descendants ( ) ;
76
- commands. entity ( self . projectiles ) . despawn_descendants ( ) ;
77
- commands. entity ( self . vfx ) . despawn_descendants ( ) ;
70
+ fn clear_game_root ( mut commands : Commands , game_root : Res < GameRoot > ) {
71
+ commands. entity ( game_root. players ) . despawn_descendants ( ) ;
72
+ commands. entity ( game_root. enemies ) . despawn_descendants ( ) ;
73
+ commands. entity ( game_root. projectiles ) . despawn_descendants ( ) ;
74
+ commands. entity ( game_root. vfx ) . despawn_descendants ( ) ;
75
+ }
76
+
77
+ fn root ( name : impl Into < Cow < ' static , str > > ) -> impl EntityCommand < World > {
78
+ let name = name. into ( ) ;
79
+
80
+ move |mut entity : EntityWorldMut | {
81
+ entity. insert ( ( Name :: new ( name) , SpatialBundle :: default ( ) ) ) ;
78
82
}
79
83
}
80
84
0 commit comments