Skip to content

Commit 684fa06

Browse files
authored
chore: unify inventory to storage component (#76)
1 parent c389933 commit 684fa06

File tree

11 files changed

+87
-92
lines changed

11 files changed

+87
-92
lines changed

src/gameplay/hud/inspect/info.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::{
44
assets::indexing::IndexMap,
55
gameplay::{
66
hud::inspect::{InspectedEntity, InspectionMenuState},
7-
item::inventory::Slots,
87
recipe::{Input, Output, assets::RecipeDef, select::SelectedRecipe},
8+
storage::Storage,
99
},
1010
widgets::{
1111
self,
@@ -28,14 +28,14 @@ pub struct HeldRelic(Entity);
2828
pub fn open_recipe_menu(
2929
mut commands: Commands,
3030
inspected_entity: Res<InspectedEntity>,
31-
structure_query: Query<(&SelectedRecipe, &Slots)>,
31+
structure_query: Query<(&SelectedRecipe, &Storage)>,
3232
input_query: Query<(), With<Input>>,
3333
output_query: Query<(), With<Output>>,
3434
recipes: Res<Assets<RecipeDef>>,
3535
recipe_index: Res<IndexMap<RecipeDef>>,
3636
held_relics: Query<&HeldRelic>,
3737
) {
38-
let Ok((selected_recipe, slots)) = structure_query.get(inspected_entity.0) else {
38+
let Ok((selected_recipe, storage)) = structure_query.get(inspected_entity.0) else {
3939
return;
4040
};
4141

@@ -119,7 +119,7 @@ pub fn open_recipe_menu(
119119
))
120120
.id();
121121

122-
for input in slots.iter().filter(|s| input_query.contains(*s)) {
122+
for input in storage.iter().filter(|s| input_query.contains(*s)) {
123123
let slot = commands
124124
.spawn((ChildOf(input_list_id), widgets::slot::slot_container()))
125125
.id();
@@ -192,7 +192,7 @@ pub fn open_recipe_menu(
192192
))
193193
.id();
194194

195-
for output in slots.iter().filter(|s| output_query.contains(*s)) {
195+
for output in storage.iter().filter(|s| output_query.contains(*s)) {
196196
let slot = commands
197197
.spawn((ChildOf(output_list_id), widgets::slot::slot_container()))
198198
.id();

src/gameplay/hud/inventory/items.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use bevy::{prelude::*, ui::Checked, ui_widgets::RadioButton};
22

33
use crate::gameplay::{
4-
hud::inventory::{InInventory, UIEntry, UIEntryDetails, UIEntryList, UIInventoryTab},
4+
hud::inventory::{UIEntry, UIEntryDetails, UIEntryList, UIInventoryTab},
55
item::{assets::ItemDef, stack::Stack},
6+
storage::StoredBy,
67
};
78

89
pub(super) fn plugin(app: &mut App) {
@@ -31,7 +32,7 @@ fn fill_item_inventory(
3132
q_tab: Single<&UIInventoryTab, Changed<Checked>>,
3233
q_entry_list: Single<Entity, With<UIEntryList>>,
3334
q_entry_details: Single<Entity, With<UIEntryDetails>>,
34-
q_items: Query<&Stack, With<InInventory>>,
35+
q_items: Query<&Stack, With<StoredBy>>,
3536
mut commands: Commands,
3637
) {
3738
commands.entity(*q_entry_list).despawn_children();

src/gameplay/hud/inventory/mod.rs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ use bevy::{
55
ui_widgets::{RadioButton, RadioGroup, ValueChange, observe},
66
};
77

8-
use crate::{
9-
gameplay::item::{assets::ItemDef, stack::Stack},
10-
screens::Screen,
11-
};
12-
138
pub mod items;
149
pub mod people;
1510

@@ -27,8 +22,6 @@ pub(super) fn plugin(app: &mut App) {
2722

2823
app.add_systems(Startup, spawn_ui_root);
2924

30-
app.add_systems(OnEnter(Screen::Gameplay), spawn_inventory);
31-
3225
app.add_systems(
3326
Update,
3427
(
@@ -229,27 +222,3 @@ fn entry_details(asset_server: &AssetServer) -> impl Bundle {
229222
},
230223
)
231224
}
232-
233-
#[derive(Component, Reflect, Debug, Default)]
234-
#[reflect(Component)]
235-
pub struct InInventory;
236-
237-
fn spawn_inventory(
238-
asset_server: Res<AssetServer>,
239-
item_defs: Res<Assets<ItemDef>>,
240-
mut commands: Commands,
241-
) {
242-
let inventory = commands.spawn(Name::new("Inventory")).id();
243-
244-
for (item_id, item_def) in item_defs.iter() {
245-
commands.spawn((
246-
Name::new(item_def.name.clone()),
247-
ChildOf(inventory),
248-
Stack {
249-
item: asset_server.get_id_handle(item_id).unwrap(),
250-
quantity: 0,
251-
},
252-
InInventory,
253-
));
254-
}
255-
}

src/gameplay/item/inventory.rs

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

3-
pub(super) fn plugin(_app: &mut App) {}
3+
use crate::{
4+
gameplay::{
5+
item::{assets::ItemDef, stack::Stack},
6+
storage::StoredBy,
7+
},
8+
screens::Screen,
9+
};
410

5-
#[derive(Component, Reflect, Debug)]
6-
#[reflect(Component)]
7-
#[relationship_target(relationship = SlotOf, linked_spawn)]
8-
pub struct Slots(Vec<Entity>);
11+
pub(super) fn plugin(app: &mut App) {
12+
app.add_systems(OnEnter(Screen::Gameplay), spawn_inventory);
13+
}
914

10-
#[derive(Component, Reflect, Debug)]
11-
#[reflect(Component)]
12-
#[relationship(relationship_target = Slots)]
13-
pub struct SlotOf(pub Entity);
15+
fn spawn_inventory(
16+
asset_server: Res<AssetServer>,
17+
item_defs: Res<Assets<ItemDef>>,
18+
mut commands: Commands,
19+
) {
20+
let inventory = commands.spawn(Name::new("Inventory")).id();
21+
22+
for (item_id, item_def) in item_defs.iter() {
23+
commands.spawn((
24+
Name::new(item_def.name.clone()),
25+
Stack {
26+
item: asset_server.get_id_handle(item_id).unwrap(),
27+
quantity: 0,
28+
},
29+
StoredBy(inventory),
30+
ChildOf(inventory),
31+
));
32+
}
33+
}

src/gameplay/logistics/pathfinding.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ use bevy::{
77

88
use crate::gameplay::{
99
FactorySystems,
10-
item::{
11-
inventory::{SlotOf, Slots},
12-
stack::Stack,
13-
},
10+
item::stack::Stack,
1411
logistics::{
1512
path::Pathable,
1613
porter::{PorterArrival, PorterLost},
1714
},
1815
recipe::{Input, Output, select::RecipeChanged},
16+
storage::{Storage, StoredBy},
1917
world::{
2018
construction::{Constructions, StructureConstructed},
2119
tilemap::coord::Coord,
@@ -41,15 +39,15 @@ pub(super) fn plugin(app: &mut App) {
4139
pub struct WalkPath(pub Vec<Entity>);
4240

4341
fn pathfind(
44-
output_query: Query<(Entity, &Stack, &SlotOf), With<Output>>,
45-
slots_query: Query<&Slots>,
42+
output_query: Query<(Entity, &Stack, &StoredBy), With<Output>>,
43+
storage_query: Query<&Storage>,
4644
input_query: Query<&Stack, With<Input>>,
4745
pathable_query: Query<&Pathable>,
4846
coordinates: Query<&Coord>,
4947
mut commands: Commands,
5048
constructions: Res<Constructions>,
5149
) {
52-
for (entity, stack, SlotOf(start)) in output_query {
50+
for (entity, stack, StoredBy(start)) in output_query {
5351
let mut queue = VecDeque::new();
5452
let mut visited = HashSet::new();
5553
let mut parent = HashMap::new();
@@ -78,10 +76,10 @@ fn pathfind(
7876
visited.insert(*neighbor);
7977
parent.insert(neighbor, current);
8078

81-
let input_entity = slots_query.get(*neighbor).ok().and_then(|slots| {
82-
slots
79+
let input_entity = storage_query.get(*neighbor).ok().and_then(|storage| {
80+
storage
8381
.iter()
84-
.find(|slot| input_query.get(*slot).is_ok_and(|i| i.item == stack.item))
82+
.find(|stored| input_query.get(*stored).is_ok_and(|i| i.item == stack.item))
8583
});
8684

8785
if let Some(goal) = input_entity {

src/gameplay/logistics/porter.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use crate::gameplay::{
55
FactorySystems,
66
item::{
77
assets::{ItemDef, Transport},
8-
inventory::Slots,
98
stack::Stack,
109
},
1110
logistics::pathfinding::{PorterPaths, WalkPath},
1211
recipe::Output,
1312
sprite_sort::{YSortSprite, ZIndexSprite},
13+
storage::Storage,
1414
};
1515

1616
pub(super) fn plugin(app: &mut App) {
@@ -62,22 +62,22 @@ fn spawn_porter(
6262
&Transform,
6363
&mut PorterSpawnTimer,
6464
&mut PorterSpawnOutputIndex,
65-
&Slots,
65+
&Storage,
6666
)>,
6767
mut output_query: Query<(&mut Stack, &mut PorterPaths), With<Output>>,
6868
mut commands: Commands,
6969
item_definitions: Res<Assets<ItemDef>>,
7070
asset_server: Res<AssetServer>,
7171
time: Res<Time>,
7272
) {
73-
for (structure, transform, mut timer, mut index, slots) in structure_query {
73+
for (structure, transform, mut timer, mut index, storage) in structure_query {
7474
if !timer.tick(time.delta()).is_finished() {
7575
continue;
7676
}
7777

78-
let outputs = slots
78+
let outputs = storage
7979
.iter()
80-
.filter(|slot| output_query.contains(*slot))
80+
.filter(|stored| output_query.contains(*stored))
8181
.collect::<Vec<Entity>>();
8282

8383
let Some(output) = outputs.get(index.0) else {

src/gameplay/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub mod item;
77
pub mod logistics;
88
pub mod recipe;
99
pub mod sprite_sort;
10+
pub mod storage;
1011
pub mod structure;
1112
pub mod world;
1213

src/gameplay/recipe/process.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use crate::{
44
assets::indexing::IndexMap,
55
gameplay::{
66
FactorySystems,
7-
item::{inventory::Slots, stack::Stack},
7+
item::stack::Stack,
88
recipe::{Input, Output, assets::RecipeDef, select::SelectedRecipe},
9+
storage::Storage,
910
},
1011
};
1112

@@ -41,26 +42,26 @@ impl ProcessState {
4142
}
4243

4344
fn consume_input(
44-
query: Query<(&mut ProcessState, &Slots, &SelectedRecipe)>,
45+
query: Query<(&mut ProcessState, &Storage, &SelectedRecipe)>,
4546
mut input_query: Query<(&mut Stack, &Input)>,
4647
recipes: Res<Assets<RecipeDef>>,
4748
recipe_index: Res<IndexMap<RecipeDef>>,
4849
) {
49-
for (mut state, slots, selected_recipe) in query {
50+
for (mut state, storage, selected_recipe) in query {
5051
if !matches!(*state, ProcessState::InsufficientInput) {
5152
continue;
5253
}
5354

54-
if !slots
55+
if !storage
5556
.iter()
56-
.filter_map(|slot| input_query.get(slot).ok())
57+
.filter_map(|stored| input_query.get(stored).ok())
5758
.all(|(stack, input)| stack.quantity >= input.quantity)
5859
{
5960
continue;
6061
}
6162

62-
for slot in slots.iter() {
63-
if let Ok((mut stack, input)) = input_query.get_mut(slot) {
63+
for stored in storage.iter() {
64+
if let Ok((mut stack, input)) = input_query.get_mut(stored) {
6465
stack.quantity = stack.quantity.saturating_sub(input.quantity);
6566
}
6667
}
@@ -91,16 +92,16 @@ fn progress_work(query: Query<&mut ProcessState>, time: Res<Time>) {
9192
}
9293

9394
fn produce_output(
94-
query: Query<(&mut ProcessState, &Slots)>,
95+
query: Query<(&mut ProcessState, &Storage)>,
9596
mut output_query: Query<(&mut Stack, &Output)>,
9697
) {
97-
for (mut state, slots) in query {
98+
for (mut state, storage) in query {
9899
if !matches!(*state, ProcessState::Completed) {
99100
continue;
100101
}
101102

102-
for output in slots.iter() {
103-
let Ok((mut stack, output)) = output_query.get_mut(output) else {
103+
for stored in storage.iter() {
104+
let Ok((mut stack, output)) = output_query.get_mut(stored) else {
104105
continue;
105106
};
106107

src/gameplay/recipe/select.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ use super::process::ProcessState;
44
use crate::{
55
assets::indexing::IndexMap,
66
gameplay::{
7-
item::{
8-
assets::ItemDef,
9-
inventory::{SlotOf, Slots},
10-
stack::Stack,
11-
},
7+
item::{assets::ItemDef, stack::Stack},
128
recipe::{Input, Output, assets::RecipeDef},
9+
storage::{Storage, StoredBy},
1310
},
1411
};
1512

@@ -49,7 +46,7 @@ fn on_select_recipe(
4946

5047
commands
5148
.entity(select_recipe.entity)
52-
.despawn_related::<Slots>();
49+
.despawn_related::<Storage>();
5350

5451
for (item_id, quantity) in recipe_def.input.iter() {
5552
let item_handle = item_index
@@ -60,7 +57,7 @@ fn on_select_recipe(
6057
commands.spawn((
6158
Name::new("Input"),
6259
ChildOf(select_recipe.entity),
63-
SlotOf(select_recipe.entity),
60+
StoredBy(select_recipe.entity),
6461
Stack::empty(item_handle),
6562
Input {
6663
quantity: *quantity,
@@ -77,7 +74,7 @@ fn on_select_recipe(
7774
commands.spawn((
7875
Name::new("Output"),
7976
ChildOf(select_recipe.entity),
80-
SlotOf(select_recipe.entity),
77+
StoredBy(select_recipe.entity),
8178
Stack::empty(item_handle),
8279
Output {
8380
quantity: *quantity,

src/gameplay/storage.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use bevy::prelude::*;
2+
3+
#[derive(Component, Reflect, Debug)]
4+
#[reflect(Component)]
5+
#[relationship_target(relationship = StoredBy, linked_spawn)]
6+
pub struct Storage(Vec<Entity>);
7+
8+
#[derive(Component, Reflect, Debug)]
9+
#[reflect(Component)]
10+
#[relationship(relationship_target = Storage)]
11+
pub struct StoredBy(pub Entity);

0 commit comments

Comments
 (0)