Interactable system completed

This commit is contained in:
Franklin 2023-11-11 19:45:32 -04:00
parent 3f95e2cd5a
commit ac119eb60a
11 changed files with 77 additions and 13 deletions

View File

@ -1,8 +1,8 @@
use bevy::{prelude::*, ecs::component::SparseStorage}; use bevy::ecs::component::SparseStorage;
use super::{item::Item, inventory_item::InventoryItem, grid::UGrid}; use super::{item::Item, inventory_item::InventoryItem, grid::UGrid};
#[derive(Component)]
pub struct AnyInventory { pub struct AnyInventory {
size: UGrid, size: UGrid,
items: Vec<InventoryItem> items: Vec<InventoryItem>

View File

@ -5,6 +5,13 @@ pub struct UGrid {
pub height: u32, 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 { impl std::ops::Add for UGrid {
type Output = UGrid; type Output = UGrid;

View File

@ -3,7 +3,6 @@ use bevy::ecs::component::SparseStorage;
use super::{item::Item, grid::UGrid}; use super::{item::Item, grid::UGrid};
//#[derive()]
pub struct InventoryItem { pub struct InventoryItem {
item: Box<dyn Item<Storage = SparseStorage>>, item: Box<dyn Item<Storage = SparseStorage>>,
/// Coordinates that this InventoryItem occupies inside an AnyInventory /// Coordinates that this InventoryItem occupies inside an AnyInventory

View File

@ -2,7 +2,7 @@ use bevy::prelude::*;
use super::grid::UGrid; use super::grid::UGrid;
#[allow(unused)]
pub enum ItemType { pub enum ItemType {
Holdable, Holdable,
Equippable, Equippable,

View File

@ -1,10 +1,25 @@
use std::fmt::Display;
use bevy::prelude::Component; use bevy::prelude::Component;
use crate::comps::core::any_inventory::AnyInventory;
#[allow(unused)] #[allow(unused)]
#[derive(Component)] #[derive(Component)]
pub enum Interactable { pub enum Interactable {
Holdable, 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"),
}
}
} }

View File

@ -5,6 +5,7 @@ use crate::setup::equipment::Equipment;
#[derive(Clone, Default, Reflect)] #[derive(Clone, Default, Reflect)]
pub struct PlayerData { pub struct PlayerData {
pub equipment: Equipment, pub equipment: Equipment,
pub is_looking_at_interactable: bool,
} }
#[derive(Component, Reflect)] #[derive(Component, Reflect)]
pub struct Player(pub PlayerData); pub struct Player(pub PlayerData);

View File

@ -1,7 +1,8 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
use crate::{ 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, 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. /// Method that is run when player hits interact button.
/// Should raycast where the player is looking and scan for interactable /// Should raycast where the player is looking and scan for interactable
pub fn interact_action( pub fn interact_action(
mut commands: Commands,
mut player_query: Query<(&GlobalTransform, &Transform), (With<Player>)>,
interactables_query: Query<(Entity, &Interactable)>,
keyboard_input: Res<Input<KeyCode>>,
rapier_context: Res<RapierContext>,
) { ) {
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");
}
}
}
}
}
} }

View File

@ -6,7 +6,7 @@ use crate::{
camera_player_sync::{ camera_player_sync::{
follow_cursor_with_camera, update_camera_vertical_position, MouseMovementSettings, 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, 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}, }, 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, set_skybox_if_loaded);
application.add_systems(Update, update_camera_vertical_position); 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, change_equipment.before(player_spawner));
application.add_systems(Update, (despawn_muzzle_flashes, despawn_stray_bullets)); application.add_systems(Update, (despawn_muzzle_flashes, despawn_stray_bullets));

View File

@ -1,6 +1,8 @@
use bevy::prelude::*; use bevy::prelude::*;
use bevy_rapier3d::prelude::*; use bevy_rapier3d::prelude::*;
use crate::comps::core::{any_inventory::AnyInventory, markers::interactable::Interactable, grid::UGrid};
pub fn spawn_obstacles( pub fn spawn_obstacles(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
@ -11,6 +13,7 @@ pub fn spawn_obstacles(
let box_2_mesh = shape::Box::new(3.0, 7.0, 3.0).into(); 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_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_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 commands
.spawn((Collider::from_bevy_mesh(&box_1_mesh, &Default::default()).unwrap(), Name::new("Obstacle 1"))) .spawn((Collider::from_bevy_mesh(&box_1_mesh, &Default::default()).unwrap(), Name::new("Obstacle 1")))
.insert(RigidBody::Fixed) .insert(RigidBody::Fixed)
@ -66,4 +69,22 @@ pub fn spawn_obstacles(
transform: Transform::from_xyz(15.0, 3.5, 15.0), transform: Transform::from_xyz(15.0, 3.5, 15.0),
..default() ..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)))
);
} }

View File

@ -1,8 +1,8 @@
use bevy::prelude::*; 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) { 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()) });
} }

View File

@ -6,7 +6,6 @@ use bevy::prelude::*;
#[allow(unused)] #[allow(unused)]
pub fn setup_inventory_screen( pub fn setup_inventory_screen(
mut commands: Commands, mut commands: Commands,
) { ) {
} }