Skip to content

Commit a07c8a4

Browse files
authored
refactor: recipe module code touch ups (#89)
1 parent cf2d960 commit a07c8a4

File tree

3 files changed

+48
-61
lines changed

3 files changed

+48
-61
lines changed

src/gameplay/recipe/mod.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,3 @@ pub fn plugin(app: &mut App) {
1313
select::plugin,
1414
));
1515
}
16-
17-
#[derive(Component, Reflect, Debug)]
18-
#[reflect(Component)]
19-
pub struct Input {
20-
pub quantity: u32,
21-
}
22-
23-
#[derive(Component, Reflect, Debug)]
24-
#[reflect(Component)]
25-
pub struct Output {
26-
pub quantity: u32,
27-
}

src/gameplay/recipe/process.rs

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use crate::gameplay::{
66
recipe::{assets::Recipe, select::SelectedRecipe},
77
};
88

9-
use super::progress::on_progress_state_add;
10-
119
pub fn plugin(app: &mut App) {
1210
app.add_systems(
1311
FixedUpdate,
@@ -19,24 +17,13 @@ pub fn plugin(app: &mut App) {
1917

2018
#[derive(Component, Reflect, Default)]
2119
#[reflect(Component)]
22-
#[component(on_add = on_progress_state_add)]
2320
pub enum ProcessState {
2421
#[default]
2522
InsufficientInput,
2623
Working(Timer),
2724
Completed,
2825
}
2926

30-
impl ProcessState {
31-
pub fn progress(&self) -> f32 {
32-
match self {
33-
Self::InsufficientInput => 0.0,
34-
Self::Working(timer) => timer.fraction(),
35-
Self::Completed => 1.0,
36-
}
37-
}
38-
}
39-
4027
fn consume_input(
4128
query: Query<(&mut ProcessState, &mut Inventory, &SelectedRecipe)>,
4229
recipes: Res<Assets<Recipe>>,
@@ -50,25 +37,11 @@ fn consume_input(
5037
continue;
5138
};
5239

53-
if !recipe.input.iter().all(|(item_id, required_amount)| {
54-
inventory
55-
.items
56-
.get(item_id)
57-
.is_some_and(|quantity| quantity >= required_amount)
58-
}) {
40+
if !can_afford_recipe(recipe, &inventory) {
5941
continue;
6042
}
6143

62-
for (item_id, required_amount) in recipe.input.iter() {
63-
inventory
64-
.items
65-
.entry(*item_id)
66-
.and_modify(|quantity| *quantity -= required_amount);
67-
}
68-
69-
let Some(recipe) = recipes.get(&selected_recipe.0) else {
70-
continue;
71-
};
44+
consume_recipe_input(recipe, &mut inventory);
7245

7346
let timer = Timer::new(recipe.duration, TimerMode::Once);
7447

@@ -103,14 +76,36 @@ fn produce_output(
10376
continue;
10477
};
10578

106-
for (item_id, amount) in recipe.output.iter() {
107-
inventory
108-
.items
109-
.entry(*item_id)
110-
.and_modify(|quantity| *quantity += amount)
111-
.or_insert(*amount);
112-
}
79+
produce_recipe_output(recipe, &mut inventory);
11380

11481
*state = ProcessState::InsufficientInput;
11582
}
11683
}
84+
85+
fn can_afford_recipe(recipe: &Recipe, inventory: &Inventory) -> bool {
86+
recipe.input.iter().all(|(item_id, required_amount)| {
87+
inventory
88+
.items
89+
.get(item_id)
90+
.is_some_and(|quantity| quantity >= required_amount)
91+
})
92+
}
93+
94+
fn consume_recipe_input(recipe: &Recipe, inventory: &mut Inventory) {
95+
for (item_id, required_amount) in recipe.input.iter() {
96+
inventory
97+
.items
98+
.entry(*item_id)
99+
.and_modify(|quantity| *quantity -= required_amount);
100+
}
101+
}
102+
103+
fn produce_recipe_output(recipe: &Recipe, inventory: &mut Inventory) {
104+
for (item_id, amount) in recipe.output.iter() {
105+
inventory
106+
.items
107+
.entry(*item_id)
108+
.and_modify(|quantity| *quantity += amount)
109+
.or_insert(*amount);
110+
}
111+
}

src/gameplay/recipe/progress.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
1-
use bevy::{
2-
ecs::{lifecycle::HookContext, world::DeferredWorld},
3-
prelude::*,
4-
sprite::Anchor,
5-
};
1+
use bevy::{prelude::*, sprite::Anchor};
62

73
use super::process::ProcessState;
84

95
pub fn plugin(app: &mut App) {
6+
app.add_observer(on_proess_state_add);
7+
108
app.add_systems(Update, update_progress_bars);
119
}
1210

1311
#[derive(Component, Reflect)]
1412
#[reflect(Component)]
1513
#[relationship_target(relationship = ProgressBarFillOf, linked_spawn)]
16-
pub struct ProgressBarFill(Entity);
14+
struct ProgressBarFill(Entity);
1715

1816
#[derive(Component, Reflect)]
1917
#[reflect(Component)]
2018
#[relationship(relationship_target = ProgressBarFill)]
21-
pub struct ProgressBarFillOf(Entity);
19+
struct ProgressBarFillOf(Entity);
2220

23-
pub fn on_progress_state_add(mut world: DeferredWorld, HookContext { entity, .. }: HookContext) {
24-
world.commands().spawn((
21+
fn on_proess_state_add(add: On<Add, ProcessState>, mut commands: Commands) {
22+
commands.spawn((
2523
Name::new("Progress Bar"),
26-
ChildOf(entity),
24+
ChildOf(add.entity),
2725
Transform::from_xyz(0.0, 48.0, 100.0),
2826
Sprite::from_color(Color::BLACK, Vec2::new(64.0, 16.0)),
2927
children![(
3028
Name::new("Progress Bar Fill"),
31-
ProgressBarFillOf(entity),
29+
ProgressBarFillOf(add.entity),
3230
Transform::from_xyz(-32.0, 0.0, 1.0),
3331
Sprite {
3432
color: Color::linear_rgb(0.0, 0.8, 0.1),
@@ -41,8 +39,8 @@ pub fn on_progress_state_add(mut world: DeferredWorld, HookContext { entity, ..
4139
}
4240

4341
fn update_progress_bars(
44-
work_states: Query<&ProcessState>,
4542
progress_bars: Query<(&mut Sprite, &ProgressBarFillOf)>,
43+
work_states: Query<&ProcessState>,
4644
) {
4745
for (mut sprite, progress_bar_of) in progress_bars {
4846
let Ok(state) = work_states.get(progress_bar_of.0) else {
@@ -53,6 +51,12 @@ fn update_progress_bars(
5351
continue;
5452
};
5553

56-
sprite.custom_size = Some(Vec2::new(rect.width() * state.progress(), rect.height()));
54+
let progress = match state {
55+
ProcessState::InsufficientInput => 0.0,
56+
ProcessState::Working(timer) => timer.fraction(),
57+
ProcessState::Completed => 1.0,
58+
};
59+
60+
sprite.custom_size = Some(Vec2::new(rect.width() * progress, rect.height()));
5761
}
5862
}

0 commit comments

Comments
 (0)