From 7685caf99ffd856772e7f4de4a97524f16633890 Mon Sep 17 00:00:00 2001 From: Franklin Date: Sat, 11 Nov 2023 21:26:49 -0400 Subject: [PATCH] Interactable clue UI and trigger plus interaction on key press F --- src/comps/core/markers/player.rs | 1 - src/logic/core/player/hands.rs | 41 +++++++++++++--------- src/ui/game/hud/hud.rs | 8 +++++ src/ui/game/hud/interact_clue.rs | 59 ++++++++++++++++++++++++++++++++ src/ui/game/hud/mod.rs | 3 ++ src/ui/game/hud/plugin.rs | 17 +++++++++ src/ui/game/mod.rs | 3 +- src/ui/game/plugin.rs | 3 +- 8 files changed, 115 insertions(+), 20 deletions(-) create mode 100644 src/ui/game/hud/hud.rs create mode 100644 src/ui/game/hud/interact_clue.rs create mode 100644 src/ui/game/hud/mod.rs create mode 100644 src/ui/game/hud/plugin.rs diff --git a/src/comps/core/markers/player.rs b/src/comps/core/markers/player.rs index bd57abd..d44c8f7 100644 --- a/src/comps/core/markers/player.rs +++ b/src/comps/core/markers/player.rs @@ -5,7 +5,6 @@ use crate::setup::equipment::Equipment; #[derive(Clone, Default, Reflect)] pub struct PlayerData { pub equipment: Equipment, - pub is_looking_at_interactable: bool, } #[derive(Component, Reflect)] pub struct Player(pub PlayerData); diff --git a/src/logic/core/player/hands.rs b/src/logic/core/player/hands.rs index f030e32..f51127e 100644 --- a/src/logic/core/player/hands.rs +++ b/src/logic/core/player/hands.rs @@ -3,7 +3,7 @@ use bevy_rapier3d::prelude::*; use crate::{ comps::core::markers::{firearm::{FirearmData, MagazineData}, holdable::InPlayerHands, player::{PlayerHand, Player}, camera::MainCamera, interactable::Interactable}, - logic::core::guns::{player_firing::PlayerFiringInfo, shoot::shoot_bullet}, utils::rad_deg::radians_from_degrees, setup::{animations::AllFirearmAnimations, load_state::GameLoadState, equipment::{EquipmentChangeEvent, Equipment}}, ui::game::settings::SettingsScreenUIConfiguration, + logic::core::guns::{player_firing::PlayerFiringInfo, shoot::shoot_bullet}, utils::rad_deg::radians_from_degrees, setup::{animations::AllFirearmAnimations, load_state::GameLoadState, equipment::{EquipmentChangeEvent, Equipment}}, ui::game::{settings::SettingsScreenUIConfiguration, hud::hud::HudState}, }; pub fn capture_hand_usage( @@ -162,32 +162,39 @@ pub fn capture_hand_usage( } -#[allow(unused)] /// Method that is run when player hits interact button. /// Should raycast where the player is looking and scan for interactable pub fn interact_action( - mut commands: Commands, - mut player_query: Query<(&GlobalTransform, &Transform), (With)>, + //mut commands: Commands, + player_query: Query<&Transform, (With, Without)>, + camera_query: Query<&GlobalTransform, (With, Without)>, interactables_query: Query<(Entity, &Interactable)>, keyboard_input: Res>, rapier_context: Res, + mut hud_state: ResMut, ) { - for (global_transform, transform) in player_query.iter_mut() { - let ray_pos = global_transform.translation(); - let ray_dir = transform.forward() * 2.0; // TODO: Move this into global Resource state - let max_toi = 4.0; - let solid = true; - - if let Some((entity, toi)) = rapier_context.cast_ray( - ray_pos, ray_dir, max_toi, solid, QueryFilter::only_fixed() - ) { - for (interactable_entity, interactable) in interactables_query.iter() { - if interactable_entity == entity { - if keyboard_input.just_pressed(KeyCode::F) { // TODO: Move this key to Controls state global - println!("Interacted with interactable"); + for transform in player_query.iter() { + for global_transform in camera_query.iter() { + let ray_pos = global_transform.translation(); + let ray_dir = transform.forward() * 2.0; // TODO: Move this into global Resource state + let max_toi = 4.0; + let solid = true; + + if let Some((entity, _toi)) = rapier_context.cast_ray( + ray_pos, ray_dir, max_toi, solid, QueryFilter::only_fixed() + ) { + for (interactable_entity, _interactable) in interactables_query.iter() { + if interactable_entity == entity { + hud_state.interaction_clue_shown = true; + if keyboard_input.just_pressed(KeyCode::F) { // TODO: Move this key to Controls state global + println!("Interacted with interactable"); + + } } + return; } } } } + hud_state.interaction_clue_shown = false; } \ No newline at end of file diff --git a/src/ui/game/hud/hud.rs b/src/ui/game/hud/hud.rs new file mode 100644 index 0000000..bebbab7 --- /dev/null +++ b/src/ui/game/hud/hud.rs @@ -0,0 +1,8 @@ +use bevy::prelude::*; + +#[derive(Resource, Reflect, Default)] +#[reflect(Resource)] +pub struct HudState { + pub interaction_clue_shown: bool, + +} \ No newline at end of file diff --git a/src/ui/game/hud/interact_clue.rs b/src/ui/game/hud/interact_clue.rs new file mode 100644 index 0000000..fc244a6 --- /dev/null +++ b/src/ui/game/hud/interact_clue.rs @@ -0,0 +1,59 @@ +use bevy::{prelude::*, ui::FocusPolicy}; + +use crate::ui::game::settings::SettingsScreenUIConfiguration; + +use super::hud::HudState; + +#[derive(Component)] +pub struct InteractClueHudMarker; + +pub fn setup_interact_clue(mut commands: Commands) { + commands.spawn(( + NodeBundle { + style: Style { + display: Display::Flex, + width: Val::Percent(100.0), + height: Val::Percent(100.0), + justify_content: JustifyContent::Center, + align_items: AlignItems::Center, + ..Default::default() + }, + visibility: Visibility::Hidden, + background_color: BackgroundColor(Color::NONE), + focus_policy: FocusPolicy::Pass, + ..Default::default() + }, Name::new("Interaction Clue Background"), + InteractClueHudMarker) + ).with_children(|parent| { + parent.spawn((TextBundle { + text: Text::from_section("Loot", TextStyle { + font_size: 12.0, color: Color::WHITE, ..Default::default() + }).with_alignment(TextAlignment::Center), + ..Default::default() + }, Name::new("Interaction Clue text"), + )); + }); +} + +pub fn update_interact_clue( + //mut commands: Commands, + interact_clue: Res, + settings_screen_config: Res, + mut query: Query<&mut Visibility, With> +) { + if !settings_screen_config.is_changed() && !interact_clue.is_changed() { + return; + } + for mut vis in query.iter_mut() { + if settings_screen_config.settings_menu_shown { + *vis = Visibility::Hidden; + return; + } + if interact_clue.interaction_clue_shown { + *vis = Visibility::Visible; + } else { + *vis = Visibility::Hidden; + } + } + +} \ No newline at end of file diff --git a/src/ui/game/hud/mod.rs b/src/ui/game/hud/mod.rs new file mode 100644 index 0000000..7bdffc1 --- /dev/null +++ b/src/ui/game/hud/mod.rs @@ -0,0 +1,3 @@ +pub mod interact_clue; +pub mod plugin; +pub mod hud; \ No newline at end of file diff --git a/src/ui/game/hud/plugin.rs b/src/ui/game/hud/plugin.rs new file mode 100644 index 0000000..8fab4b2 --- /dev/null +++ b/src/ui/game/hud/plugin.rs @@ -0,0 +1,17 @@ +use bevy::prelude::*; + +use crate::ui::game::hud::hud::HudState; + +use super::interact_clue::{setup_interact_clue, update_interact_clue}; + + +pub struct HudOverlayPlugin; + +impl Plugin for HudOverlayPlugin { + fn build(&self, app: &mut App) { + app.insert_resource(HudState::default()); + app.register_type::(); + app.add_systems(Startup, setup_interact_clue); + app.add_systems(Update, update_interact_clue); + } +} \ No newline at end of file diff --git a/src/ui/game/mod.rs b/src/ui/game/mod.rs index 2cebe68..346d6ab 100644 --- a/src/ui/game/mod.rs +++ b/src/ui/game/mod.rs @@ -3,4 +3,5 @@ pub mod plugin; pub mod fps_counter; pub mod settings; pub mod settings_screen; -pub mod inventory; \ No newline at end of file +pub mod inventory; +pub mod hud; \ No newline at end of file diff --git a/src/ui/game/plugin.rs b/src/ui/game/plugin.rs index 006ac6b..9b6869b 100644 --- a/src/ui/game/plugin.rs +++ b/src/ui/game/plugin.rs @@ -2,12 +2,13 @@ use bevy::prelude::*; use crate::ui::game::settings::GameConfiguration; -use super::{fps_counter, settings_screen, settings::SettingsScreenUIConfiguration, inventory}; +use super::{fps_counter, settings_screen, settings::SettingsScreenUIConfiguration, inventory, hud}; pub struct MainGameUIPlugin; impl Plugin for MainGameUIPlugin { fn build(&self, app: &mut App) { + app.add_plugins(hud::plugin::HudOverlayPlugin); app.insert_resource(GameConfiguration::default()); app.insert_resource(SettingsScreenUIConfiguration::default()); app.add_systems(Startup, (