Skip to content

Commit 6b64812

Browse files
authored
refactor: deprecate inventory in recipe input/output (#19)
1 parent b2d1537 commit 6b64812

8 files changed

Lines changed: 195 additions & 173 deletions

File tree

src/simulation/item/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ pub use self::{
1212
};
1313

1414
pub fn plugin(app: &mut App) {
15+
app.register_type::<Item>();
16+
app.register_type::<Quantity>();
17+
app.register_type::<Full>();
18+
19+
app.add_systems(Update, mark_full);
20+
1521
app.add_plugins((assets::plugin, compendium::plugin));
1622

1723
app.register_type::<PlayerInventory>()
@@ -22,6 +28,34 @@ pub fn plugin(app: &mut App) {
2228
#[reflect(Component)]
2329
pub struct Item(pub Handle<ItemDef>);
2430

31+
#[derive(Component, Reflect)]
32+
#[reflect(Component)]
33+
pub struct Quantity(pub u32);
34+
35+
#[derive(Component, Reflect)]
36+
#[reflect(Component)]
37+
#[component(storage = "SparseSet")]
38+
pub struct Full;
39+
40+
fn mark_full(
41+
query: Query<(Entity, &Item, &Quantity)>,
42+
item_defs: Res<Assets<ItemDef>>,
43+
mut commands: Commands,
44+
) {
45+
for (entity, item, quantity) in query {
46+
let stack_size = item_defs
47+
.get(&item.0)
48+
.map(|def| def.stack_size)
49+
.unwrap_or(1);
50+
51+
if quantity.0 >= stack_size {
52+
commands.entity(entity).insert(Full);
53+
} else {
54+
commands.entity(entity).remove::<Full>();
55+
}
56+
}
57+
}
58+
2559
#[derive(Component, Reflect, Deref, DerefMut)]
2660
#[reflect(Component)]
2761
pub struct PlayerInventory(Inventory);

src/simulation/logistics/conveyor_belt.rs

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ use bevy::{
77
use bevy_aseprite_ultra::prelude::*;
88

99
use 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
}

src/simulation/logistics/io.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/simulation/logistics/mod.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,12 @@ use bevy::prelude::*;
33
mod assets;
44
mod conveyor_belt;
55
mod conveyor_hole;
6-
mod io;
76

87
pub use self::{
98
assets::LogisticAssets,
109
conveyor_hole::{ConveyorHole, ConveyorHoleOf},
11-
io::{InputInventory, OutputInventory},
1210
};
1311

1412
pub fn plugin(app: &mut App) {
15-
app.add_plugins((
16-
assets::plugin,
17-
conveyor_belt::plugin,
18-
conveyor_hole::plugin,
19-
io::plugin,
20-
));
13+
app.add_plugins((assets::plugin, conveyor_belt::plugin, conveyor_hole::plugin));
2114
}

src/simulation/machine/build.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use crate::{
55
assets::manifest::{Id, Manifest},
66
simulation::{
77
FactorySystems,
8-
item::Inventory,
9-
logistics::{ConveyorHoleOf, InputInventory, OutputInventory},
8+
logistics::ConveyorHoleOf,
109
machine::{
1110
Machine, Structure,
1211
assets::{StructureAssets, StructureTemplate},
@@ -159,12 +158,6 @@ fn spawn_structures(
159158
entity.despawn();
160159
};
161160
}
162-
"merger" => {
163-
entity.insert((
164-
InputInventory(Inventory::sized(10)),
165-
OutputInventory(Inventory::sized(10)),
166-
));
167-
}
168161
_ => {}
169162
};
170163
}

src/simulation/recipe/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,40 @@ pub use self::{
1212
};
1313

1414
pub fn plugin(app: &mut App) {
15+
app.register_type::<Inputs>();
16+
app.register_type::<InputOf>();
17+
app.register_type::<Outputs>();
18+
app.register_type::<OutputOf>();
19+
app.register_type::<RequiredQuantity>();
20+
1521
app.add_plugins((
1622
assets::plugin,
1723
process::plugin,
1824
progress::plugin,
1925
select::plugin,
2026
));
2127
}
28+
29+
#[derive(Component, Reflect, Default)]
30+
#[reflect(Component)]
31+
#[relationship_target(relationship = InputOf, linked_spawn)]
32+
pub struct Inputs(Vec<Entity>);
33+
34+
#[derive(Component, Reflect)]
35+
#[reflect(Component)]
36+
#[relationship(relationship_target = Inputs)]
37+
pub struct InputOf(pub Entity);
38+
39+
#[derive(Component, Reflect, Default)]
40+
#[reflect(Component)]
41+
#[relationship_target(relationship = OutputOf, linked_spawn)]
42+
pub struct Outputs(Vec<Entity>);
43+
44+
#[derive(Component, Reflect)]
45+
#[reflect(Component)]
46+
#[relationship(relationship_target = Outputs)]
47+
pub struct OutputOf(pub Entity);
48+
49+
#[derive(Component, Reflect)]
50+
#[reflect(Component)]
51+
pub struct RequiredQuantity(pub u32);

0 commit comments

Comments
 (0)