generated from 2ndbeam/bevy-template
refactor!: Split everything into submodules
- Bump version to 0.2.0 - Split every module to systems, observers, plugins, etc - Renamed Crate to Container - Removed Wall component - Removed try_insert_item system - Removed inventory::ui module
This commit is contained in:
parent
3094a8af13
commit
7c386d4128
20 changed files with 830 additions and 772 deletions
|
|
@ -0,0 +1,99 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::{
|
||||
inventory::ActiveInventory,
|
||||
player::Player,
|
||||
ui::UiRoot,
|
||||
};
|
||||
|
||||
use super::{*, bundles::*, observers::*};
|
||||
|
||||
const UI_SLOT_ASSET_PATH: &'static str = "sprites/ui/inventory_slot.png";
|
||||
const TEMP_ITEM_PATH: &'static str = "sprites/items/choco_bar.png";
|
||||
|
||||
pub fn setup_ui_inventory(
|
||||
mut commands: Commands,
|
||||
asset_server: Res<AssetServer>,
|
||||
inventory_query: Query<(Option<&ChildOf>, Entity, &Inventory, Option<&Children>)>,
|
||||
player_query: Query<(), With<Player>>,
|
||||
active_inventory_query: Query<Entity, With<ActiveInventory>>,
|
||||
item_query: Query<&Item>,
|
||||
root_query: Query<Entity, With<UiRoot>>,
|
||||
) {
|
||||
let Ok(root) = root_query.single() else {
|
||||
error!("Query contains more than one UiRoot");
|
||||
return;
|
||||
};
|
||||
let ui_slot_image: Handle<Image> = asset_server.load(UI_SLOT_ASSET_PATH);
|
||||
let temp_item_image: Handle<Image> = asset_server.load(TEMP_ITEM_PATH);
|
||||
let (mut player_inventory_ids, mut active_inventory_ids) = (Vec::new(), Vec::new());
|
||||
for (inventory_parent, inventory_entity, inventory, children) in inventory_query.iter().sort::<Entity>() {
|
||||
let is_player = match inventory_parent {
|
||||
Some(parent) => {
|
||||
if player_query.get(parent.0)
|
||||
.is_ok() { true }
|
||||
else {
|
||||
if active_inventory_query.get(inventory_entity)
|
||||
.is_ok() { false }
|
||||
else { continue; }
|
||||
}
|
||||
},
|
||||
None => {
|
||||
if active_inventory_query.get(inventory_entity)
|
||||
.is_ok() { false }
|
||||
else { continue; }
|
||||
},
|
||||
};
|
||||
let items = match children {
|
||||
Some(children) => {
|
||||
children.iter().filter_map(|item_entity| {
|
||||
match item_query.get(item_entity) {
|
||||
Ok(item) => Some((item, item_entity)),
|
||||
Err(_) => None,
|
||||
}
|
||||
}).collect::<Vec<(&Item, Entity)>>()
|
||||
}
|
||||
None => Vec::new(),
|
||||
};
|
||||
let inventory_id = commands.spawn(ui_inventory_bundle(inventory, inventory_entity))
|
||||
.with_children(|commands| {
|
||||
for x in 0..inventory.size.x { for y in 0..inventory.size.y {
|
||||
let mut slot_commands = commands.spawn(inventory_slot_bundle(x, y, ui_slot_image.clone()));
|
||||
slot_commands.observe(on_slot_over)
|
||||
.observe(on_slot_out)
|
||||
.observe(on_item_drag_drop);
|
||||
if let Some((item, entity)) = items.iter()
|
||||
.find(|(i, _)| i.position.unwrap_or_default() == UVec2::new(x, y)) {
|
||||
slot_commands.with_children(|commands| {
|
||||
commands.spawn(ui_item_bundle(item, *entity, temp_item_image.clone()))
|
||||
.observe(on_item_over)
|
||||
.observe(on_item_out)
|
||||
.observe(on_item_drag_start)
|
||||
.observe(on_item_drag)
|
||||
.observe(on_item_drag_end);
|
||||
});
|
||||
}
|
||||
} }
|
||||
}).id();
|
||||
if is_player {
|
||||
player_inventory_ids.push(inventory_id);
|
||||
} else {
|
||||
active_inventory_ids.push(inventory_id);
|
||||
}
|
||||
}
|
||||
let player_inventory_manager = commands.spawn(ui_manager_bundle(player_inventory_ids, true)).id();
|
||||
let active_inventory_manager = commands.spawn(ui_manager_bundle(active_inventory_ids, false)).id();
|
||||
for entity in active_inventory_query {
|
||||
commands.entity(entity).remove::<ActiveInventory>();
|
||||
}
|
||||
commands.entity(root).add_children(&[player_inventory_manager, active_inventory_manager]);
|
||||
}
|
||||
|
||||
pub fn clear_ui_inventory(
|
||||
mut commands: Commands,
|
||||
inventory_query: Query<Entity, With<UiInventoryManager>>,
|
||||
) {
|
||||
for entity in inventory_query {
|
||||
commands.entity(entity).despawn();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue