@@ -15,7 +15,8 @@ impl Plugin for PickablesPlugin {
15
15
fn build ( & self , app : & mut bevy:: prelude:: App ) {
16
16
app. observe ( on_new_day)
17
17
. observe ( on_new_chunk)
18
- . add_systems ( Startup , ( initialize, load_textures, startup) . chain ( ) ) ;
18
+ . observe ( on_pickable_collected)
19
+ . add_systems ( Startup , ( load_textures, startup) . chain ( ) ) ;
19
20
}
20
21
}
21
22
@@ -64,25 +65,21 @@ impl Distribution<PickableItemType> for Standard {
64
65
}
65
66
}
66
67
67
- #[ derive( Resource ) ]
68
- pub struct PickableCount ( u8 ) ;
69
-
70
68
#[ derive( Component ) ]
71
69
pub struct Pickable {
72
- pub id : u8 ,
73
70
pub item_type : PickableItemType ,
74
71
pub x : usize ,
75
72
}
76
73
77
74
#[ derive( Component ) ]
78
75
pub struct PlacedPickable {
79
- pub id : u8 ,
80
76
pub entity : Entity ,
81
77
pub item_type : PickableItemType ,
82
78
}
83
79
84
- fn initialize ( mut commands : Commands ) {
85
- commands. insert_resource ( PickableCount ( 0 ) ) ;
80
+ #[ derive( Event ) ]
81
+ pub struct PlacedPickableCollected {
82
+ pub entity : Entity ,
86
83
}
87
84
88
85
fn load_textures (
@@ -95,13 +92,11 @@ fn load_textures(
95
92
commands. insert_resource ( Tiles ( asset_server. load ( "purple-valley-icon-set/icons.png" ) ) ) ;
96
93
}
97
94
98
- fn startup ( mut commands : Commands , game_world : Res < GameWorld > , mut counter : ResMut < PickableCount > ) {
95
+ fn startup ( mut commands : Commands , game_world : Res < GameWorld > ) {
99
96
let mut rng = thread_rng ( ) ;
100
97
let pickables_count = rng. gen_range ( 8 ..64 ) as usize ;
101
98
for _ in 0 ..pickables_count {
102
- counter. 0 += 1 ;
103
99
commands. spawn ( Pickable {
104
- id : counter. 0 ,
105
100
item_type : random ( ) ,
106
101
x : game_world. get_random_x_block ( ) ,
107
102
} ) ;
@@ -130,7 +125,6 @@ fn on_new_chunk(
130
125
for ( entity, item) in items {
131
126
parent. spawn ( (
132
127
PlacedPickable {
133
- id : item. id ,
134
128
entity,
135
129
item_type : item. item_type . clone ( ) ,
136
130
} ,
@@ -157,3 +151,15 @@ fn on_new_chunk(
157
151
}
158
152
} ) ;
159
153
}
154
+
155
+ fn on_pickable_collected (
156
+ trigger : Trigger < PlacedPickableCollected > ,
157
+ query : Query < ( Entity , & Pickable ) > ,
158
+ mut commands : Commands ,
159
+ ) {
160
+ if let Ok ( ( entity, _) ) = query. get ( trigger. event ( ) . entity ) {
161
+ commands. entity ( entity) . despawn ( ) ;
162
+ } else {
163
+ println ! ( "Pickable not found: {:?}" , trigger. event( ) . entity) ;
164
+ }
165
+ }
0 commit comments