feat!: Item processing

- Added try_insert_item system
- Added rotated field on Item
- Implemented UI item drawing with scale and rotation

BREAKING CHANGE: Item.swap_size renamed to Item.rotate
    Item.clone_swapped renamed to Item.clone_rotated
This commit is contained in:
Alexey 2026-03-11 14:55:27 +03:00
commit c46fa75e54
3 changed files with 114 additions and 15 deletions

View file

@ -1,7 +1,7 @@
use bevy::prelude::*;
use leafwing_input_manager::prelude::*;
use crate::{GameState, InputAction as Action, inventory::{ActiveInventory, Inventory}};
use crate::{GameState, InputAction as Action, inventory::{ActiveInventory, Inventory, item::Item}};
#[derive(Component)]
pub struct Player {
@ -18,7 +18,7 @@ fn player_bundle(asset_server: &Res<AssetServer>) -> impl Bundle {
Sprite::from_image(image),
Transform::from_xyz(0f32, 0f32, 1f32),
Action::default_input_map(),
Inventory::new(UVec2::new(12, 8)),
Inventory::new(UVec2::new(4, 4)),
ActiveInventory,
)
}
@ -27,7 +27,37 @@ pub fn setup_player(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(player_bundle(&asset_server));
}
pub fn try_insert_item(
mut commands: Commands,
item_query: Query<&Item>,
inventory_query: Query<(Entity, &Inventory, Option<&Children>)>,
) {
let mut item = Item::new(UVec2::new(1, 1));
for (entity, inventory, children) in inventory_query {
let children = match children {
Some(children) => &children[..],
None => &[],
};
match inventory.find_free_space(item_query, children, item.size) {
Some((position, should_rotate)) => {
if should_rotate {
item.rotate();
}
item.position = Some(position);
info!("Spawning item {item:?}");
commands.entity(entity).with_child(item);
},
None => {
warn!("Inventory does not have space for {}", item.size);
},
}
// only first inventory
break;
}
}
pub fn handle_input(
mut commands: Commands,
time: Res<Time>,
state: Res<State<GameState>>,
mut next_state: ResMut<NextState<GameState>>,
@ -49,6 +79,10 @@ pub fn handle_input(
if direction != 0f32 {
sprite.flip_x = direction < 0f32;
}
if action_state.just_pressed(&Action::Interact) {
commands.run_system_cached(try_insert_item);
}
},
GameState::Inventory => {
let (_, mut action_state, _, _) = player;