From ac119eb60a9f92478e4a82c604550a1afa87bf34 Mon Sep 17 00:00:00 2001 From: Franklin Date: Sat, 11 Nov 2023 19:45:32 -0400 Subject: [PATCH] Interactable system completed --- src/comps/core/any_inventory.rs | 4 ++-- src/comps/core/grid.rs | 7 +++++++ src/comps/core/inventory_item.rs | 1 - src/comps/core/item.rs | 2 +- src/comps/core/markers/interactable.rs | 17 +++++++++++++++- src/comps/core/markers/player.rs | 1 + src/logic/core/player/hands.rs | 28 +++++++++++++++++++++++--- src/scenes/scene1/init.rs | 4 ++-- src/scenes/scene1/obstacles.rs | 21 +++++++++++++++++++ src/scenes/scene1/spawn_points.rs | 4 ++-- src/ui/game/inventory/menu.rs | 1 - 11 files changed, 77 insertions(+), 13 deletions(-) diff --git a/src/comps/core/any_inventory.rs b/src/comps/core/any_inventory.rs index 921daf4..14cd7f2 100644 --- a/src/comps/core/any_inventory.rs +++ b/src/comps/core/any_inventory.rs @@ -1,8 +1,8 @@ -use bevy::{prelude::*, ecs::component::SparseStorage}; +use bevy::ecs::component::SparseStorage; use super::{item::Item, inventory_item::InventoryItem, grid::UGrid}; -#[derive(Component)] + pub struct AnyInventory { size: UGrid, items: Vec diff --git a/src/comps/core/grid.rs b/src/comps/core/grid.rs index a48fbf8..0ba804e 100644 --- a/src/comps/core/grid.rs +++ b/src/comps/core/grid.rs @@ -5,6 +5,13 @@ pub struct UGrid { pub height: u32, } +impl UGrid { + /// Creates a ugrid with width = size and height = size + pub fn new_square(size: u32) -> Self { + Self { width: size, height: size } + } +} + impl std::ops::Add for UGrid { type Output = UGrid; diff --git a/src/comps/core/inventory_item.rs b/src/comps/core/inventory_item.rs index c7ddcce..1df0bec 100644 --- a/src/comps/core/inventory_item.rs +++ b/src/comps/core/inventory_item.rs @@ -3,7 +3,6 @@ use bevy::ecs::component::SparseStorage; use super::{item::Item, grid::UGrid}; -//#[derive()] pub struct InventoryItem { item: Box>, /// Coordinates that this InventoryItem occupies inside an AnyInventory diff --git a/src/comps/core/item.rs b/src/comps/core/item.rs index 6a2996b..ccbcce4 100644 --- a/src/comps/core/item.rs +++ b/src/comps/core/item.rs @@ -2,7 +2,7 @@ use bevy::prelude::*; use super::grid::UGrid; - +#[allow(unused)] pub enum ItemType { Holdable, Equippable, diff --git a/src/comps/core/markers/interactable.rs b/src/comps/core/markers/interactable.rs index 41d28e0..78017cb 100644 --- a/src/comps/core/markers/interactable.rs +++ b/src/comps/core/markers/interactable.rs @@ -1,10 +1,25 @@ +use std::fmt::Display; + use bevy::prelude::Component; +use crate::comps::core::any_inventory::AnyInventory; + #[allow(unused)] #[derive(Component)] pub enum Interactable { Holdable, - Lootable, + Lootable(AnyInventory), + Item, +} + +impl Display for Interactable { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Interactable::Holdable => write!(f, "Holdable"), + Interactable::Lootable(_) => write!(f, "Lootable"), + Interactable::Item => write!(f, "Item"), + } + } } \ No newline at end of file diff --git a/src/comps/core/markers/player.rs b/src/comps/core/markers/player.rs index d44c8f7..bd57abd 100644 --- a/src/comps/core/markers/player.rs +++ b/src/comps/core/markers/player.rs @@ -5,6 +5,7 @@ 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 6778b7e..f030e32 100644 --- a/src/logic/core/player/hands.rs +++ b/src/logic/core/player/hands.rs @@ -1,7 +1,8 @@ use bevy::prelude::*; +use bevy_rapier3d::prelude::*; use crate::{ - comps::core::markers::{firearm::{FirearmData, MagazineData}, holdable::InPlayerHands, player::{PlayerHand, Player}}, + 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, }; @@ -165,7 +166,28 @@ pub fn capture_hand_usage( /// 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)>, + interactables_query: Query<(Entity, &Interactable)>, + keyboard_input: Res>, + rapier_context: Res, ) { - + 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"); + } + } + } + } + } } \ No newline at end of file diff --git a/src/scenes/scene1/init.rs b/src/scenes/scene1/init.rs index 3f20660..8fc7a63 100644 --- a/src/scenes/scene1/init.rs +++ b/src/scenes/scene1/init.rs @@ -6,7 +6,7 @@ use crate::{ camera_player_sync::{ follow_cursor_with_camera, update_camera_vertical_position, MouseMovementSettings, }, - hands::capture_hand_usage, + hands::{capture_hand_usage, interact_action}, player_vertical_sync::sync_player_y_state, player_values_state::PlayerValuesState, }, guns::despawn_shots::{despawn_muzzle_flashes, despawn_stray_bullets}}, setup::{assets::load_all_assets, load_state::GameLoadState, spawn::add_all_spawners, animations::{load_animations, AllFirearmAnimations}, equipment::{EquipmentChangeEvent, change_equipment}, spawners::player::player_spawner}, }; @@ -35,7 +35,7 @@ pub fn load_scene(application: &mut App) { application.add_systems(Update, set_skybox_if_loaded); application.add_systems(Update, update_camera_vertical_position); - application.add_systems(Update, capture_hand_usage); + application.add_systems(Update, (capture_hand_usage, interact_action)); application.add_systems(Update, change_equipment.before(player_spawner)); application.add_systems(Update, (despawn_muzzle_flashes, despawn_stray_bullets)); diff --git a/src/scenes/scene1/obstacles.rs b/src/scenes/scene1/obstacles.rs index a36ca29..670465f 100644 --- a/src/scenes/scene1/obstacles.rs +++ b/src/scenes/scene1/obstacles.rs @@ -1,6 +1,8 @@ use bevy::prelude::*; use bevy_rapier3d::prelude::*; +use crate::comps::core::{any_inventory::AnyInventory, markers::interactable::Interactable, grid::UGrid}; + pub fn spawn_obstacles( mut commands: Commands, mut meshes: ResMut>, @@ -11,6 +13,7 @@ pub fn spawn_obstacles( let box_2_mesh = shape::Box::new(3.0, 7.0, 3.0).into(); let box_3_mesh = shape::Box::new(3.0, 7.0, 3.0).into(); let box_4_mesh = shape::Box::new(3.0, 7.0, 3.0).into(); + let box_5_mesh = shape::Box::new(2.0, 2.0, 2.0).into(); commands .spawn((Collider::from_bevy_mesh(&box_1_mesh, &Default::default()).unwrap(), Name::new("Obstacle 1"))) .insert(RigidBody::Fixed) @@ -66,4 +69,22 @@ pub fn spawn_obstacles( transform: Transform::from_xyz(15.0, 3.5, 15.0), ..default() }); + + // CRATE + commands + .spawn((Collider::from_bevy_mesh(&box_5_mesh, &Default::default()).unwrap(), Name::new("Crate"))) + .insert(RigidBody::Fixed) + .insert(PbrBundle { + mesh: meshes.add(box_5_mesh.into()), + material: materials.add(StandardMaterial { + base_color: Color::BLUE, + perceptual_roughness: 0.1, + ..default() + }), + transform: Transform::from_xyz(20.0, 2.0, 20.0), + ..default() + }) + .insert( + Interactable::Lootable(AnyInventory::new(UGrid::new_square(10))) + ); } diff --git a/src/scenes/scene1/spawn_points.rs b/src/scenes/scene1/spawn_points.rs index fb3a88e..7078fe5 100644 --- a/src/scenes/scene1/spawn_points.rs +++ b/src/scenes/scene1/spawn_points.rs @@ -1,8 +1,8 @@ use bevy::prelude::*; -use crate::{setup::{spawn::SpawnPoint, equipment::Equipment}, comps::core::markers::player::{Player, PlayerData}}; +use crate::{setup::spawn::SpawnPoint, comps::core::markers::player::{Player, PlayerData}}; pub fn set_spawn_points(mut commands: Commands) { - commands.spawn(SpawnPoint { at: Transform::from_xyz(3.0, 5.0, 2.0), what: Player(PlayerData { equipment: Equipment::Nothing }) }); + commands.spawn(SpawnPoint { at: Transform::from_xyz(3.0, 5.0, 2.0), what: Player(PlayerData::default()) }); } \ No newline at end of file diff --git a/src/ui/game/inventory/menu.rs b/src/ui/game/inventory/menu.rs index ee7b767..dd64d77 100644 --- a/src/ui/game/inventory/menu.rs +++ b/src/ui/game/inventory/menu.rs @@ -6,7 +6,6 @@ use bevy::prelude::*; #[allow(unused)] pub fn setup_inventory_screen( mut commands: Commands, - ) { } \ No newline at end of file