11use bevy:: prelude:: * ;
22
3- use crate :: simulation:: {
4- FactorySystems ,
5- item:: { ItemDef , Stack } ,
6- logistics:: { InputInventory , OutputInventory } ,
7- machine:: power:: Powered ,
8- recipe:: RecipeDef ,
3+ use crate :: {
4+ assets:: indexing:: IndexMap ,
5+ simulation:: {
6+ FactorySystems ,
7+ item:: { ItemDef , Stack } ,
8+ logistics:: { InputInventory , OutputInventory } ,
9+ machine:: power:: Powered ,
10+ recipe:: RecipeDef ,
11+ } ,
912} ;
1013
1114use super :: { SelectedRecipe , progress:: on_progress_state_add} ;
@@ -44,6 +47,7 @@ impl ProcessState {
4447fn consume_input (
4548 query : Query < ( & mut ProcessState , & mut InputInventory , & SelectedRecipe ) , With < Powered > > ,
4649 recipes : Res < Assets < RecipeDef > > ,
50+ recipe_index : Res < IndexMap < RecipeDef > > ,
4751) {
4852 for ( mut state, mut inventory, selected_recipe) in query {
4953 if !matches ! ( * state, ProcessState :: InsufficientInput ) {
@@ -54,10 +58,9 @@ fn consume_input(
5458 continue ;
5559 } ;
5660
57- let recipe = recipes
58- . iter ( )
59- . map ( |( _, recipe_def) | recipe_def)
60- . find ( |recipe_def| recipe_def. id == * recipe_id)
61+ let recipe = recipe_index
62+ . get ( recipe_id)
63+ . and_then ( |asset_id| recipes. get ( * asset_id) )
6164 . expect ( "Selected recipe refers to non-existent recipe" ) ;
6265
6366 if inventory. consume_input ( recipe) . is_err ( ) {
@@ -73,7 +76,9 @@ fn consume_input(
7376fn progress_work (
7477 query : Query < ( & mut ProcessState , & SelectedRecipe ) , With < Powered > > ,
7578 recipes : Res < Assets < RecipeDef > > ,
79+ recipe_index : Res < IndexMap < RecipeDef > > ,
7680 items : Res < Assets < ItemDef > > ,
81+ item_index : Res < IndexMap < ItemDef > > ,
7782 time : Res < Time > ,
7883) {
7984 for ( mut state, selected_recipe) in query {
@@ -89,20 +94,18 @@ fn progress_work(
8994 continue ;
9095 } ;
9196
92- let recipe = recipes
93- . iter ( )
94- . map ( |( _, recipe_def) | recipe_def)
95- . find ( |recipe_def| recipe_def. id == * recipe_id)
97+ let recipe = recipe_index
98+ . get ( recipe_id)
99+ . and_then ( |asset_id| recipes. get ( * asset_id) )
96100 . expect ( "Selected recipe refers to non-existent id" ) ;
97101
98102 let output: Vec < Stack > = recipe
99103 . output
100104 . iter ( )
101105 . map ( |( item_id, quantity) | {
102- let item_def = items
103- . iter ( )
104- . map ( |( _, item_def) | item_def)
105- . find ( |item_def| item_def. id == * item_id)
106+ let item_def = item_index
107+ . get ( item_id)
108+ . and_then ( |asset_id| items. get ( * asset_id) )
106109 . expect ( "Recipe refers to non-existent output item" ) ;
107110
108111 Stack {
0 commit comments