From beed11d94573481fa4ea001f147563add7ba6e0a Mon Sep 17 00:00:00 2001 From: Franklin Date: Fri, 17 Nov 2023 11:08:54 -0400 Subject: [PATCH] Fixed pickup and inv system --- .../proxy/character/in_player_hands_parent.rs | 12 +++- .../markers/proxy/character/player_eye.rs | 44 ++++++++++++- src/comps/core/markers/proxy/plugin.rs | 4 +- src/comps/core/spawners/player.rs | 46 +------------- src/comps/core/spawners/spawn.rs | 4 +- src/logic/core/player/hands.rs | 63 ++++++++++--------- src/logic/core/player/inventory.rs | 5 +- 7 files changed, 96 insertions(+), 82 deletions(-) diff --git a/src/comps/core/markers/proxy/character/in_player_hands_parent.rs b/src/comps/core/markers/proxy/character/in_player_hands_parent.rs index 418532d..11d316f 100644 --- a/src/comps/core/markers/proxy/character/in_player_hands_parent.rs +++ b/src/comps/core/markers/proxy/character/in_player_hands_parent.rs @@ -1,6 +1,14 @@ -use bevy::{ecs::{component::Component, reflect::ReflectComponent}, reflect::Reflect}; +use bevy::prelude::*; #[derive(Component, Reflect, Default, Debug)] #[reflect(Component)] -pub struct InPlayerHandsParent; \ No newline at end of file +pub struct InPlayerHandsParent; + +#[allow(unused)] +pub fn insert_components_into_player_hand( + mut commands: Commands, + +) { + +} \ No newline at end of file diff --git a/src/comps/core/markers/proxy/character/player_eye.rs b/src/comps/core/markers/proxy/character/player_eye.rs index a643d08..7ffca80 100644 --- a/src/comps/core/markers/proxy/character/player_eye.rs +++ b/src/comps/core/markers/proxy/character/player_eye.rs @@ -1,6 +1,46 @@ -use bevy::{ecs::{component::Component, reflect::ReflectComponent}, reflect::Reflect}; +use bevy::prelude::*; +use bevy_rapier3d::prelude::*; + +use crate::{comps::core::markers::{proxy::physics::rapier::LinkToPlayer, camera::MainCamera}, logic::core::player::player_values_state::PlayerValuesState}; #[derive(Component, Reflect, Default, Debug)] #[reflect(Component)] -pub struct PlayerEye; \ No newline at end of file +pub struct PlayerEye; + +pub fn insert_components_into_spawned_player( + mut commands: Commands, + eye_query: Query>, + player_collider_query: Query, With, Added)>, + player_values_state: Res, +) { + for eye in eye_query.iter() { + // Spawn camera + let camera = commands + .spawn(MainCamera) + .insert(Camera3dBundle { + transform: Transform::from_xyz(0.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y), + ..Default::default() + }) + //s.insert(Skybox(skybox_handle.clone())) + .insert(VisibilityBundle { + visibility: Visibility::Inherited, + ..Default::default() + }) + //.push_children(&[player_hand]) + .id(); + commands.entity(eye).add_child(camera); + } + for entity in player_collider_query.iter() { + commands.entity(entity) + .insert(Restitution::coefficient(0.0)) + .insert(Friction { + coefficient: 0.0, + combine_rule: CoefficientCombineRule::Multiply, + }) + .insert(ColliderMassProperties::Mass( + player_values_state.player_initial_weight, + )) + ; + } +} \ No newline at end of file diff --git a/src/comps/core/markers/proxy/plugin.rs b/src/comps/core/markers/proxy/plugin.rs index f0a8cc6..e6b5803 100644 --- a/src/comps/core/markers/proxy/plugin.rs +++ b/src/comps/core/markers/proxy/plugin.rs @@ -2,7 +2,7 @@ use bevy::app::{Plugin, Update}; use crate::setup::load_state::update_game_load_state; -use super::{character::{player_hitbox::PlayerHitBox, player_character::PlayerCharacter, player_eye::PlayerEye, in_player_hands_parent::InPlayerHandsParent}, physics::{rapier::{AutoAABBCollider, physics_replace_proxies}, self}}; +use super::{character::{player_hitbox::PlayerHitBox, player_character::PlayerCharacter, player_eye::{PlayerEye, insert_components_into_spawned_player}, in_player_hands_parent::{InPlayerHandsParent, insert_components_into_player_hand}}, physics::{rapier::{AutoAABBCollider, physics_replace_proxies}, self}}; @@ -25,5 +25,7 @@ impl Plugin for ProxyComponentsPlugin { app.register_type::(); app.register_type::(); app.add_systems(Update, (physics_replace_proxies, update_game_load_state)); + app.add_systems(Update, insert_components_into_spawned_player); + app.add_systems(Update, insert_components_into_player_hand); } } \ No newline at end of file diff --git a/src/comps/core/spawners/player.rs b/src/comps/core/spawners/player.rs index cd8a165..15bc4d1 100644 --- a/src/comps/core/spawners/player.rs +++ b/src/comps/core/spawners/player.rs @@ -4,10 +4,7 @@ use bevy_rapier3d::prelude::*; use crate::{ comps::core::{ inventory::player_inventory::PlayerInventory, - markers::{ - camera::MainCamera, - player::{Player, PlayerData}, proxy::{character::player_eye::PlayerEye, physics::rapier::{LinkToPlayer, self}}, - }, + markers::player::{Player, PlayerData}, }, logic::core::{ guns::player_firing::PlayerFiringInfo, @@ -52,7 +49,7 @@ pub fn player_spawner( //let plyer_transform = Transform::from_xyz(25.0, 0.0, 25.0).with_scale(Vec3 { x: 3.0, y: 3.0, z: 3.0 }); let player_scene = commands.spawn( ( - SceneBundle { + SceneBundle { //TODO: Iterate through assets with asset type scene: loaded_gltf_assets.get(assets.assets[2].asset.clone()).unwrap().scenes[0].clone(), transform: Transform::from_translation(Vec3::ZERO).with_scale(Vec3 { x: 3.0, y: 3.0, z: 3.0 }), visibility: Visibility::Inherited, @@ -117,43 +114,4 @@ pub fn player_spawner( )); commands.entity(player_spawn_point_entity).despawn(); } -} - - -// Camera -pub fn insert_components_into_spawned_player( - mut commands: Commands, - eye_query: Query>, - player_collider_query: Query, With, Added)>, - player_values_state: Res, -) { - for eye in eye_query.iter() { - // Spawn camera - let camera = commands - .spawn(MainCamera) - .insert(Camera3dBundle { - transform: Transform::from_xyz(0.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y), - ..Default::default() - }) - //s.insert(Skybox(skybox_handle.clone())) - .insert(VisibilityBundle { - visibility: Visibility::Inherited, - ..Default::default() - }) - //.push_children(&[player_hand]) - .id(); - commands.entity(eye).add_child(camera); - } - for entity in player_collider_query.iter() { - commands.entity(entity) - .insert(Restitution::coefficient(0.0)) - .insert(Friction { - coefficient: 0.0, - combine_rule: CoefficientCombineRule::Multiply, - }) - .insert(ColliderMassProperties::Mass( - player_values_state.player_initial_weight, - )) - ; - } } \ No newline at end of file diff --git a/src/comps/core/spawners/spawn.rs b/src/comps/core/spawners/spawn.rs index 03b6ece..dcc009b 100644 --- a/src/comps/core/spawners/spawn.rs +++ b/src/comps/core/spawners/spawn.rs @@ -2,7 +2,7 @@ use bevy::prelude::*; use super::{ item::{item_spawner, ItemSpawnPointPlugin}, - player::{player_spawner, insert_components_into_spawned_player}, + player::player_spawner, spawn_point::SpawnPointPlugin, }; @@ -10,6 +10,6 @@ pub struct SpawnerPlugin; impl Plugin for SpawnerPlugin { fn build(&self, app: &mut App) { app.add_plugins((SpawnPointPlugin, ItemSpawnPointPlugin)); - app.add_systems(Update, (player_spawner, item_spawner, insert_components_into_spawned_player.after(player_spawner))); + app.add_systems(Update, (player_spawner, item_spawner)); } } diff --git a/src/logic/core/player/hands.rs b/src/logic/core/player/hands.rs index 459cb07..dd97b17 100644 --- a/src/logic/core/player/hands.rs +++ b/src/logic/core/player/hands.rs @@ -10,7 +10,7 @@ use crate::{ firearm::{FirearmData, MagazineData}, holdable::InPlayerHands, interactable::Interactable, - player::Player, proxy::character::in_player_hands_parent::InPlayerHandsParent, + player::Player, proxy::{character::in_player_hands_parent::InPlayerHandsParent, physics::rapier::LinkToPlayer}, }, }, logic::core::guns::{player_firing::PlayerFiringInfo, shoot::shoot_bullet}, @@ -295,7 +295,8 @@ pub fn capture_hand_usage( /// Should raycast where the player is looking and scan for interactable pub fn interact_action( //mut commands: Commands, - player_query: Query<(Entity, &Transform), (With, Without)>, + player_query: Query, Without)>, + player_collider_query: Query, With, Without)>, camera_query: Query<&GlobalTransform, (With, Without)>, interactables_query: Query<(Entity, &Interactable)>, keyboard_input: Res>, @@ -304,42 +305,44 @@ pub fn interact_action( mut pickup_item_event_writer: EventWriter, game_ui_state: Res, ) { - for (player_entity, transform) in player_query.iter() { + for player_entity 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 ray_dir = global_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::default().exclude_collider(player_entity), - ) { - for (interactable_entity, interactable) in interactables_query.iter() { - if interactable_entity == entity { - hud_state.interaction_clue_shown = true; - hud_state.interaction_clue_text = interactable.to_string(); - if keyboard_input.just_pressed(KeyCode::F) && !game_ui_state.any_window() { - // TODO: Move this key to Controls state global - match interactable { - Interactable::Holdable => todo!(), - /*Interactable::Lootable(any_inventory) => { - loot_container_event_writer - .send(LootContainerEvent(any_inventory.clone())) - }*/ - Interactable::Item(item) => { - pickup_item_event_writer.send(PickupItemEvent { - entity: interactable_entity, - item: item.clone(), - player: player_entity, - }); + for player_collider_entity in player_collider_query.iter() { + if let Some((entity, _)) = rapier_context.cast_ray( + ray_pos, + ray_dir, + max_toi, + solid, + QueryFilter::default().exclude_collider(player_collider_entity), + ) { + for (interactable_entity, interactable) in interactables_query.iter() { + if interactable_entity == entity { + hud_state.interaction_clue_shown = true; + hud_state.interaction_clue_text = interactable.to_string(); + if keyboard_input.just_pressed(KeyCode::F) && !game_ui_state.any_window() { + // TODO: Move this key to Controls state global + match interactable { + Interactable::Holdable => todo!(), + /*Interactable::Lootable(any_inventory) => { + loot_container_event_writer + .send(LootContainerEvent(any_inventory.clone())) + }*/ + Interactable::Item(item) => { + pickup_item_event_writer.send(PickupItemEvent { + entity: interactable_entity, + item: item.clone(), + player: player_entity, + }); + } } } + return; } - return; } } } diff --git a/src/logic/core/player/inventory.rs b/src/logic/core/player/inventory.rs index dcf2860..3d25022 100644 --- a/src/logic/core/player/inventory.rs +++ b/src/logic/core/player/inventory.rs @@ -18,7 +18,9 @@ pub fn update_player_inventory_system( loaded_gltf_assets: Res>, ) { for event in pickup_item_events.read() { + println!("Pickup Item Event received"); for (player_entity, mut player_inventory, player_transform) in player_query.iter_mut() { + println!("Player found"); if player_entity == event.player { // Get item type and where it should go commands @@ -28,7 +30,8 @@ pub fn update_player_inventory_system( player_inventory::drop_slot_in_game_world(&mut commands, player_transform, &mut inventory_changed_events, &mut player_inventory, &assets_gltf, &loaded_gltf_assets, event.item.get_item_slot()); player_inventory.pickup_item(event.item.as_ref(), event.item.get_item_slot()); - inventory_changed_events.send(PlayerInventoryChangedEvent { item: Some(event.item.clone()), slot_type: event.item.get_item_slot() }) + inventory_changed_events.send(PlayerInventoryChangedEvent { item: Some(event.item.clone()), slot_type: event.item.get_item_slot() }); + println!("Inventory Changed Event sent"); } } }