Skip to content

Commit 5527285

Browse files
authored
refactor: use handle for selected recipe over string (#84)
1 parent 1dbc961 commit 5527285

File tree

4 files changed

+37
-38
lines changed

4 files changed

+37
-38
lines changed

src/gameplay/hud/tome/tab_inspect.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,15 @@ fn on_recipe_select(
7878
click: On<Pointer<Click>>,
7979
mut commands: Commands,
8080
inspected: Res<Inspected>,
81-
recipes: Res<Assets<RecipeDef>>,
82-
query: Query<&Recipe>,
81+
recipe_badges: Query<&Recipe>,
8382
) {
84-
let Some(recipe_id) = query
85-
.get(click.entity)
86-
.ok()
87-
.and_then(|r| recipes.get(r.0))
88-
.map(|r| r.id.clone())
89-
else {
83+
let Ok(recipe) = recipe_badges.get(click.entity) else {
9084
return;
9185
};
9286

9387
commands.trigger(SelectRecipe {
9488
entity: inspected.0,
95-
recipe_id,
89+
recipe: recipe.0,
9690
});
9791
}
9892

src/gameplay/recipe/process.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
use bevy::prelude::*;
22

3-
use crate::{
4-
assets::indexing::IndexMap,
5-
gameplay::{
6-
FactorySystems,
7-
item::stack::Stack,
8-
recipe::{Input, Output, assets::RecipeDef, select::SelectedRecipe},
9-
storage::Storage,
10-
},
3+
use crate::gameplay::{
4+
FactorySystems,
5+
item::stack::Stack,
6+
recipe::{Input, Output, assets::RecipeDef, select::SelectedRecipe},
7+
storage::Storage,
118
};
129

1310
use super::progress::on_progress_state_add;
@@ -45,7 +42,6 @@ fn consume_input(
4542
query: Query<(&mut ProcessState, &Storage, &SelectedRecipe)>,
4643
mut input_query: Query<(&mut Stack, &Input)>,
4744
recipes: Res<Assets<RecipeDef>>,
48-
recipe_index: Res<IndexMap<RecipeDef>>,
4945
) {
5046
for (mut state, storage, selected_recipe) in query {
5147
if !matches!(*state, ProcessState::InsufficientInput) {
@@ -66,10 +62,9 @@ fn consume_input(
6662
}
6763
}
6864

69-
let recipe = recipe_index
70-
.get(selected_recipe.0.as_str())
71-
.and_then(|asset_id| recipes.get(*asset_id))
72-
.expect("Selected recipe refers to non-existent recipe");
65+
let Some(recipe) = recipes.get(&selected_recipe.0) else {
66+
continue;
67+
};
7368

7469
let timer = Timer::new(recipe.duration, TimerMode::Once);
7570

src/gameplay/recipe/select.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,28 @@ pub fn plugin(app: &mut App) {
1919
#[derive(Component, Reflect, Default, Deref, DerefMut)]
2020
#[reflect(Component)]
2121
#[require(ProcessState)]
22-
pub struct SelectedRecipe(pub String);
22+
pub struct SelectedRecipe(pub Handle<RecipeDef>);
2323

2424
#[derive(EntityEvent, Reflect)]
2525
pub struct SelectRecipe {
2626
pub entity: Entity,
27-
pub recipe_id: String,
27+
pub recipe: AssetId<RecipeDef>,
2828
}
2929

3030
#[derive(Message, Reflect)]
3131
pub struct RecipeChanged(pub Entity);
3232

3333
fn on_select_recipe(
3434
select_recipe: On<SelectRecipe>,
35-
recipes: Res<Assets<RecipeDef>>,
36-
recipe_index: Res<IndexMap<RecipeDef>>,
35+
mut recipes: ResMut<Assets<RecipeDef>>,
3736
mut items: ResMut<Assets<ItemDef>>,
3837
item_index: Res<IndexMap<ItemDef>>,
3938
mut commands: Commands,
4039
mut recipe_changes: MessageWriter<RecipeChanged>,
4140
) {
42-
let recipe_def = recipe_index
43-
.get(&select_recipe.recipe_id)
44-
.and_then(|asset_id| recipes.get(*asset_id))
45-
.expect("Attempted to select invalid recipe");
41+
let Some(recipe_def) = recipes.get(select_recipe.recipe) else {
42+
return;
43+
};
4644

4745
commands
4846
.entity(select_recipe.entity)
@@ -82,9 +80,13 @@ fn on_select_recipe(
8280
));
8381
}
8482

83+
let Some(handle) = recipes.get_strong_handle(select_recipe.recipe) else {
84+
return;
85+
};
86+
8587
commands
8688
.entity(select_recipe.entity)
87-
.insert(SelectedRecipe(select_recipe.recipe_id.to_owned()));
89+
.insert(SelectedRecipe(handle));
8890

8991
recipe_changes.write(RecipeChanged(select_recipe.entity));
9092
}

src/gameplay/structure/default_recipe.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use bevy::prelude::*;
22

3-
use crate::gameplay::{
4-
hud::tome::tab_inspect::Inspect,
5-
recipe::select::SelectRecipe,
6-
structure::{Structure, assets::StructureDef, interactable::Interact},
7-
world::construction::StructureConstructed,
3+
use crate::{
4+
assets::indexing::IndexMap,
5+
gameplay::{
6+
hud::tome::tab_inspect::Inspect,
7+
recipe::{assets::RecipeDef, select::SelectRecipe},
8+
structure::{Structure, assets::StructureDef, interactable::Interact},
9+
world::construction::StructureConstructed,
10+
},
811
};
912

1013
pub fn plugin(app: &mut App) {
@@ -19,6 +22,7 @@ fn apply_default_recipe(
1922
mut commands: Commands,
2023
structure_query: Query<&Structure>,
2124
structure_defs: Res<Assets<StructureDef>>,
25+
recipe_index: Res<IndexMap<RecipeDef>>,
2226
) {
2327
for StructureConstructed(entity) in structures_constructed.read() {
2428
let Ok(Structure(handle)) = structure_query.get(*entity) else {
@@ -29,10 +33,14 @@ fn apply_default_recipe(
2933
continue;
3034
};
3135

32-
if let Some(recipe) = &structure_def.default_recipe {
36+
if let Some(recipe) = structure_def
37+
.default_recipe
38+
.as_ref()
39+
.and_then(|id| recipe_index.get(id))
40+
{
3341
commands.trigger(SelectRecipe {
3442
entity: *entity,
35-
recipe_id: recipe.to_owned(),
43+
recipe: *recipe,
3644
});
3745
}
3846

0 commit comments

Comments
 (0)