Interactable clue UI and trigger plus interaction on key press F

This commit is contained in:
Franklin 2023-11-11 21:26:49 -04:00
parent ac119eb60a
commit 7685caf99f
8 changed files with 115 additions and 20 deletions

View File

@ -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);

View File

@ -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<Player>)>,
//mut commands: Commands,
player_query: Query<&Transform, (With<Player>, Without<MainCamera>)>,
camera_query: Query<&GlobalTransform, (With<MainCamera>, Without<Player>)>,
interactables_query: Query<(Entity, &Interactable)>,
keyboard_input: Res<Input<KeyCode>>,
rapier_context: Res<RapierContext>,
mut hud_state: ResMut<HudState>,
) {
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;
}

8
src/ui/game/hud/hud.rs Normal file
View File

@ -0,0 +1,8 @@
use bevy::prelude::*;
#[derive(Resource, Reflect, Default)]
#[reflect(Resource)]
pub struct HudState {
pub interaction_clue_shown: bool,
}

View File

@ -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<HudState>,
settings_screen_config: Res<SettingsScreenUIConfiguration>,
mut query: Query<&mut Visibility, With<InteractClueHudMarker>>
) {
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;
}
}
}

3
src/ui/game/hud/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod interact_clue;
pub mod plugin;
pub mod hud;

17
src/ui/game/hud/plugin.rs Normal file
View File

@ -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::<HudState>();
app.add_systems(Startup, setup_interact_clue);
app.add_systems(Update, update_interact_clue);
}
}

View File

@ -3,4 +3,5 @@ pub mod plugin;
pub mod fps_counter;
pub mod settings;
pub mod settings_screen;
pub mod inventory;
pub mod inventory;
pub mod hud;

View File

@ -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, (