@@ -7,15 +7,12 @@ use bevy::{
77use bevy_aseprite_ultra:: prelude:: * ;
88
99use crate :: {
10- assets:: indexing:: IndexMap ,
1110 simulation:: {
1211 FactorySystems ,
1312 dismantle:: QueueDismantle ,
14- item:: { Item , ItemAssets , ItemDef , Stack } ,
15- logistics:: {
16- ConveyorHoleOf , LogisticAssets ,
17- io:: { InputInventory , OutputInventory } ,
18- } ,
13+ item:: { Full , Item , ItemAssets , ItemDef , Quantity } ,
14+ logistics:: { ConveyorHoleOf , LogisticAssets } ,
15+ recipe:: { Inputs , Outputs } ,
1916 world:: Terrain ,
2017 } ,
2118 ui:: { Interact , Interactable , YSort } ,
@@ -173,60 +170,66 @@ fn place_items_on_belt(
173170 & mut ConveyorPickupTimer ,
174171 ) > ,
175172 conveyor_holes : Query < & ConveyorHoleOf > ,
176- mut outputs : Query < & mut OutputInventory > ,
173+ outputs_query : Query < & Outputs > ,
174+ output_query : Query < ( & Item , & Quantity ) > ,
177175 mut commands : Commands ,
178176 item_assets : Res < ItemAssets > ,
179- item_index : Res < IndexMap < ItemDef > > ,
180- mut items : ResMut < Assets < ItemDef > > ,
177+ items : Res < Assets < ItemDef > > ,
181178 time : Res < Time > ,
182179) {
183180 for ( entity, transform, belt, length, conveyored_items, mut pickup_timer) in conveyor_belts {
184181 if !pickup_timer. 0 . tick ( time. delta ( ) ) . finished ( ) {
185182 continue ;
186183 }
187184
185+ if ( length. 0 / CONVEYOR_BELT_TRAY_SIZE ) . ceil ( ) as u8 == conveyored_items. len ( ) as u8 {
186+ continue ;
187+ }
188+
188189 let Ok ( pickup_point) = conveyor_holes
189190 . get ( belt. 0 )
190191 . map ( |conveyor_hole_of| conveyor_hole_of. 0 )
191192 else {
192193 continue ;
193194 } ;
194195
195- let Ok ( mut output ) = outputs . get_mut ( pickup_point) else {
196+ let Ok ( outputs ) = outputs_query . get ( pickup_point) else {
196197 continue ;
197198 } ;
198199
199- if ( length. 0 / CONVEYOR_BELT_TRAY_SIZE ) . ceil ( ) as u8 == conveyored_items. len ( ) as u8 {
200+ let Some ( item) = outputs
201+ . iter ( )
202+ . flat_map ( |e| output_query. get ( e) )
203+ . find ( |( _, q) | q. 0 > 0 )
204+ . map ( |( i, _) | i)
205+ else {
200206 continue ;
201- }
207+ } ;
202208
203- if let Ok ( item_id) = output. pop ( ) {
204- // TODO: update inventory so that this becomes a .clone()
205- let item_handle = item_index
206- . get ( & item_id)
207- . and_then ( |asset_id| items. get_strong_handle ( * asset_id) )
208- . expect ( "Inventory contains invalid item id" ) ;
209-
210- commands
211- . spawn ( (
212- Name :: new ( "Item" ) ,
213- Transform :: default ( )
214- . with_translation ( Vec3 :: new ( -length. 0 / 2.0 , 0.0 , 0.0 ) )
215- . with_rotation ( transform. rotation . inverse ( ) ) ,
216- Sprite :: sized ( Vec2 :: splat ( 16.0 ) ) ,
217- AseSlice {
218- aseprite : item_assets. aseprite . clone ( ) ,
219- name : item_id,
220- } ,
221- Item ( item_handle) ,
222- TransportedBy ( entity) ,
223- ChildOf ( entity) ,
224- Interactable ,
225- ) )
226- . observe ( |trigger : Trigger < Interact > , mut commands : Commands | {
227- commands. entity ( trigger. target ( ) ) . despawn ( ) ;
228- } ) ;
229- }
209+ let item_id = items
210+ . get ( & item. 0 )
211+ . map ( |def| def. id . to_owned ( ) )
212+ . unwrap_or ( "unknown" . to_string ( ) ) ;
213+
214+ commands
215+ . spawn ( (
216+ Name :: new ( "Item" ) ,
217+ Transform :: default ( )
218+ . with_translation ( Vec3 :: new ( -length. 0 / 2.0 , 0.0 , 0.0 ) )
219+ . with_rotation ( transform. rotation . inverse ( ) ) ,
220+ Sprite :: sized ( Vec2 :: splat ( 16.0 ) ) ,
221+ AseSlice {
222+ aseprite : item_assets. aseprite . clone ( ) ,
223+ name : item_id,
224+ } ,
225+ Item ( item. 0 . clone ( ) ) ,
226+ TransportedBy ( entity) ,
227+ ChildOf ( entity) ,
228+ Interactable ,
229+ ) )
230+ . observe ( |trigger : Trigger < Interact > , mut commands : Commands | {
231+ commands. entity ( trigger. target ( ) ) . despawn ( ) ;
232+ } ) ;
230233 }
231234}
232235
@@ -255,9 +258,9 @@ fn receive_items_from_belt(
255258 conveyor_belts : Query < & ConveyorBelt > ,
256259 conveyored_items : Query < ( Entity , & Item , & TransportProgress , & TransportedBy ) > ,
257260 conveyor_holes : Query < & ConveyorHoleOf > ,
258- mut inputs : Query < & mut InputInventory > ,
261+ recipe_inputs : Query < & Inputs > ,
262+ mut inputs : Query < ( & Item , & mut Quantity ) , Without < Full > > ,
259263 mut commands : Commands ,
260- items : Res < Assets < ItemDef > > ,
261264) {
262265 for ( entity, item, progress, item_of) in conveyored_items {
263266 if progress. 0 < 1.0 {
@@ -275,22 +278,20 @@ fn receive_items_from_belt(
275278 continue ;
276279 } ;
277280
278- let Ok ( mut input ) = inputs . get_mut ( dropoff_point) else {
281+ let Ok ( input_entities ) = recipe_inputs . get ( dropoff_point) else {
279282 continue ;
280283 } ;
281284
282- let Some ( item_def) = items. get ( & item. 0 ) else {
283- continue ;
284- } ;
285+ for input_entity in input_entities. iter ( ) {
286+ let Ok ( ( recipe_item, mut quantity) ) = inputs. get_mut ( input_entity) else {
287+ continue ;
288+ } ;
285289
286- let mut stack = Stack {
287- item_id : item_def. id . to_owned ( ) ,
288- quantity : 1 ,
289- max_quantity : item_def. stack_size ,
290- } ;
291-
292- if input. add_stack ( & mut stack) . is_ok ( ) {
293- commands. entity ( entity) . despawn ( ) ;
290+ if recipe_item. 0 == item. 0 {
291+ quantity. 0 += 1 ;
292+ commands. entity ( entity) . despawn ( ) ;
293+ break ;
294+ }
294295 }
295296 }
296297}
0 commit comments