From cb99170d6e410bdfb6ede4f6817e19881f2b0bd3 Mon Sep 17 00:00:00 2001 From: LorrensP-2158466 Date: Sun, 6 Apr 2025 20:23:51 +0200 Subject: [PATCH] drop --- src/interaction/objects.rs | 60 ++++++++++++++++++++------------------ src/interaction/ui.rs | 5 ++-- src/player/toolbar.rs | 6 ++++ 3 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/interaction/objects.rs b/src/interaction/objects.rs index 10d78c0..00bb787 100644 --- a/src/interaction/objects.rs +++ b/src/interaction/objects.rs @@ -13,14 +13,15 @@ pub fn plugin(_app: &mut App) {} pub fn handle_pick_up( mut commands: Commands, // current action - mut action: Query<(&PlayerAction, &mut Item), With>, + mut player_query: Query<(&PlayerAction, &Transform, &mut Item), With>, mut vis_query: Query<&mut Visibility>, children: Query<&mut Children>, + mut item_transform: Query<&mut Transform, (With, Without)>, // current interactable mut interaction_opportunity: ResMut, ) { - let (action, mut item) = single_mut!(action); + let (action, transform, mut item) = single_mut!(player_query); if *action == PlayerAction::PickUp { // take out the interaction, because we are picking it up let Some(target) = interaction_opportunity.0.take() else { @@ -44,36 +45,37 @@ pub fn handle_pick_up( commands.entity(collider).remove::(); } } + // for simplicities sake, set the transform of the item that is dropped to that of the player + let mut item_transform = item_transform.get_mut(dropped).unwrap(); + *item_transform = *transform; } } } -pub fn handle_drop(// mut commands: Commands, - // // current action - // mut action: Query<(&PlayerAction, &Transform, &mut Item), With>, - // mut vis_query: Query<&mut Visibility>, - // mut item_transform: Query<&mut Transform, With>, - // children: Query<&mut Children>, +pub fn handle_drop( + mut commands: Commands, + // current action + mut player_query: Query<(&PlayerAction, &Transform, &mut Item), With>, + mut vis_query: Query<&mut Visibility>, + mut item_transform: Query<&mut Transform, (With, Without)>, + children: Query<&mut Children>, ) { - // let (action, transform, mut item) = single_mut!(action); - // if *action == PlayerAction::Drop { - // if let Ok(mut vis) = vis_query.get_mut(target) { - // *vis = Visibility::Hidden; - // } - // if let Ok(colliders) = children.get(target) { - // for &collider in colliders { - // commands.entity(collider).insert(ColliderDisabled); - // } - // } - // if let Some(dropped) = replaced { - // if let Ok(mut vis) = vis_query.get_mut(dropped) { - // *vis = Visibility::Visible; - // } - // if let Ok(colliders) = children.get(dropped) { - // for &collider in colliders { - // commands.entity(collider).remove::(); - // } - // } - // } - // } + let (action, transform, mut item) = single_mut!(player_query); + if *action == PlayerAction::Drop { + let Some(item) = item.take() else { + return; + }; + if let Ok(mut vis) = vis_query.get_mut(item) { + *vis = Visibility::Visible; + } + if let Ok(colliders) = children.get(item) { + for &collider in colliders { + commands.entity(collider).remove::(); + } + } + + // for simplicities sake, set the transform of the item that is dropped to that of the player + let mut item_transform = item_transform.get_mut(item).unwrap(); + *item_transform = *transform; + } } diff --git a/src/interaction/ui.rs b/src/interaction/ui.rs index e2ca24f..dbf1731 100644 --- a/src/interaction/ui.rs +++ b/src/interaction/ui.rs @@ -1,7 +1,6 @@ use crate::GameState; -use crate::player::toolbar::Item; -use crate::player::{Player, PlayerAction}; -use crate::util::{single, single_mut}; +use crate::player::Player; +use crate::util::single; use bevy::{prelude::*, window::PrimaryWindow}; use bevy_egui::{EguiContexts, EguiPlugin, egui}; use bevy_rapier3d::prelude::*; diff --git a/src/player/toolbar.rs b/src/player/toolbar.rs index 78baad4..2934a14 100644 --- a/src/player/toolbar.rs +++ b/src/player/toolbar.rs @@ -9,6 +9,12 @@ use super::Player; #[derive(Component, Default, Debug)] pub struct Item(Option); +impl Item { + pub fn take(&mut self) -> Option { + self.0.take() + } +} + #[derive(Component, Default, Debug)] pub struct ItemIcon(Handle);