Fixed pickup and inv system

This commit is contained in:
Franklin 2023-11-17 11:08:54 -04:00
parent 7569b40fd7
commit beed11d945
7 changed files with 96 additions and 82 deletions

View File

@ -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;
#[allow(unused)]
pub fn insert_components_into_player_hand(
mut commands: Commands,
) {
}

View File

@ -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;
pub fn insert_components_into_spawned_player(
mut commands: Commands,
eye_query: Query<Entity, Added<PlayerEye>>,
player_collider_query: Query<Entity, (With<LinkToPlayer>, With<Collider>, Added<Collider>)>,
player_values_state: Res<PlayerValuesState>,
) {
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,
))
;
}
}

View File

@ -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::<physics::rapier::RigidBodyBlender>();
app.register_type::<physics::rapier::LinkToPlayer>();
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);
}
}

View File

@ -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,
@ -118,42 +115,3 @@ 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<Entity, Added<PlayerEye>>,
player_collider_query: Query<Entity, (With<LinkToPlayer>, With<Collider>, Added<Collider>)>,
player_values_state: Res<PlayerValuesState>,
) {
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,
))
;
}
}

View File

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

View File

@ -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<Player>, Without<MainCamera>)>,
player_query: Query<Entity, (With<Player>, Without<MainCamera>)>,
player_collider_query: Query<Entity, (With<LinkToPlayer>, With<Collider>, Without<MainCamera>)>,
camera_query: Query<&GlobalTransform, (With<MainCamera>, Without<Player>)>,
interactables_query: Query<(Entity, &Interactable)>,
keyboard_input: Res<Input<KeyCode>>,
@ -304,42 +305,44 @@ pub fn interact_action(
mut pickup_item_event_writer: EventWriter<PickupItemEvent>,
game_ui_state: Res<GameUiState>,
) {
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;
}
}
}

View File

@ -18,7 +18,9 @@ pub fn update_player_inventory_system(
loaded_gltf_assets: Res<Assets<Gltf>>,
) {
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");
}
}
}