Untitled

mail@pastecode.io avatar
unknown
rust
2 months ago
3.0 kB
0
Indexable
Never
pub fn update_inventory(
    mut childbuilder_q: Query<&mut Parent, With<ParentInv>>,
    inventory: Res<Inventory>,
    mut commands: Commands,
    mut event_reader: EventReader<SlotChangedEvent>,
    mut children_e: Local<Vec<Entity>>,
) {
    for _event in event_reader.read() {
        for parent in childbuilder_q.iter_mut() {
            let entity = parent.get();

            for e in children_e.iter() {
                commands.entity(*e).despawn_recursive();
            }
            children_e.clear();

            for item in inventory.items.iter() {
                let print = format!("{}: {}", item.name, item.count);
                let description = item.description.to_string();
                commands.entity(entity).with_children(|parent_spawn| {
                    let children = parent_spawn
                        .spawn(TextBundle {
                            text: Text {
                                sections: vec![TextSection {
                                    value: print,
                                    style: TextStyle {
                                        font_size: 35.0,
                                        ..Default::default()
                                    },
                                }],
                                ..Default::default()
                            },
                            ..Default::default()
                        })
                        .id();
                    let children_2 = parent_spawn
                        .spawn(TextBundle {
                            text: Text {
                                sections: vec![TextSection {
                                    value: description,
                                    style: TextStyle {
                                        font_size: 20.0,
                                        ..Default::default()
                                    },
                                }],
                                ..Default::default()
                            },
                            ..Default::default()
                        })
                        .id();

                    children_e.push(children);
                    children_e.push(children_2);
                });
            }
        }
    }
}

pub fn add_item(
    mut inventory: ResMut<Inventory>,
    mut event_writer: EventWriter<SlotChangedEvent>,
    mut event_reader: EventReader<AddItem>,
) {
    for event in event_reader.read() {
        if inventory.items.contains(&event.item) {
            for item_inv in inventory.items.iter_mut() {
                if item_inv == &event.item {
                    item_inv.count += event.item.count;
                    event_writer.send(SlotChangedEvent);
                }
            }
        } else {
            inventory.items.push(event.item.clone());
            event_writer.send(SlotChangedEvent);
        }
    }
}
Leave a Comment