From 45f412c7c00cc080f1428902c9cdf9a352c4be40 Mon Sep 17 00:00:00 2001 From: Franklin Date: Sun, 12 Nov 2023 18:51:35 -0400 Subject: [PATCH] Compiles and doesn't crash on startup, changed some Boxes to Arcs and added a PickupItemEvent --- src/comps/core/inventory/item_inventory.rs | 8 +++----- src/comps/core/inventory/player_inventory.rs | 10 +++++----- src/comps/core/items/item.rs | 4 ++-- src/comps/core/markers/interactable.rs | 8 +++++--- src/comps/core/spawners/player.rs | 5 +++-- src/logic/core/guns/equip_firearm.rs | 4 ---- src/logic/core/guns/firearm.rs | 12 ++++++++++-- src/logic/core/guns/mod.rs | 1 - src/logic/core/player/hands.rs | 12 ++++++------ src/ui/game/inventory/plugin.rs | 4 +++- 10 files changed, 37 insertions(+), 31 deletions(-) delete mode 100644 src/logic/core/guns/equip_firearm.rs diff --git a/src/comps/core/inventory/item_inventory.rs b/src/comps/core/inventory/item_inventory.rs index a631c8c..b79c7b5 100644 --- a/src/comps/core/inventory/item_inventory.rs +++ b/src/comps/core/inventory/item_inventory.rs @@ -1,10 +1,8 @@ -use bevy::prelude::*; - use crate::comps::core::items::item::Item; /// # ItemInventory /// Specifically made to hold single items such as Guns, Pieces of armor. -#[derive(Component, Clone, Default)] -pub struct ItemInventory<'a> { - pub item: Option<&'a dyn Item>, +#[derive(Default)] +pub struct ItemInventory { + pub item: Option>, } diff --git a/src/comps/core/inventory/player_inventory.rs b/src/comps/core/inventory/player_inventory.rs index 59787b7..dd4cef0 100644 --- a/src/comps/core/inventory/player_inventory.rs +++ b/src/comps/core/inventory/player_inventory.rs @@ -3,13 +3,13 @@ use bevy::prelude::*; use super::item_inventory::ItemInventory; #[derive(Component)] -pub struct PlayerInventory<'a> { - pub primary: ItemInventory<'a>, - pub secondary: ItemInventory<'a>, +pub struct PlayerInventory { + pub primary: ItemInventory, + pub secondary: ItemInventory, //pub backpack: AnyInventory, } -impl<'a> Default for PlayerInventory<'a> { +impl Default for PlayerInventory { fn default() -> Self { Self { primary: Default::default(), @@ -17,4 +17,4 @@ impl<'a> Default for PlayerInventory<'a> { //backpack: AnyInventory::new(UGrid::new_square(10)), } } -} +} \ No newline at end of file diff --git a/src/comps/core/items/item.rs b/src/comps/core/items/item.rs index f75597d..2349ee4 100644 --- a/src/comps/core/items/item.rs +++ b/src/comps/core/items/item.rs @@ -17,7 +17,7 @@ pub enum ItemType { } #[bevy_trait_query::queryable] -pub trait Item { +pub trait Item: Sync + Send { fn get_type(&self) -> ItemType; fn asset_path(&self) -> &str; /// Optional Stackable. If value is Some(x) x is the max quantity per stack @@ -86,7 +86,7 @@ pub trait Item { firearm_size.y, firearm_size.z, ), - Interactable::Item, + Interactable::Item(firearm.get_item()), )) .push_children(&[firearm_asset_entity]); } diff --git a/src/comps/core/markers/interactable.rs b/src/comps/core/markers/interactable.rs index 111ee83..7789dc0 100644 --- a/src/comps/core/markers/interactable.rs +++ b/src/comps/core/markers/interactable.rs @@ -1,7 +1,9 @@ -use std::fmt::Display; +use std::{fmt::Display, sync::Arc}; use bevy::prelude::Component; +use crate::comps::core::items::item::Item; + //use crate::comps::core::inventory::any_inventory::AnyInventory; #[allow(unused)] @@ -9,7 +11,7 @@ use bevy::prelude::Component; pub enum Interactable { Holdable, //Lootable(AnyInventory), - Item, + Item(Arc), } impl Display for Interactable { @@ -17,7 +19,7 @@ impl Display for Interactable { match self { Interactable::Holdable => write!(f, "Holdable"), //Interactable::Lootable(_) => write!(f, "Lootable"), - Interactable::Item => write!(f, "Item"), + Interactable::Item(_) => write!(f, "Item"), } } } diff --git a/src/comps/core/spawners/player.rs b/src/comps/core/spawners/player.rs index 11877bb..7414dc1 100644 --- a/src/comps/core/spawners/player.rs +++ b/src/comps/core/spawners/player.rs @@ -2,10 +2,10 @@ use bevy::prelude::*; use bevy_rapier3d::prelude::*; use crate::{ - comps::core::markers::{ + comps::core::{markers::{ camera::MainCamera, player::{Player, PlayerData, PlayerHand}, - }, + }, inventory::player_inventory::PlayerInventory}, logic::core::{ guns::player_firing::PlayerFiringInfo, player::{ @@ -114,6 +114,7 @@ pub fn player_spawner( }) // Data .insert(PlayerFiringInfo::default()) + .insert(PlayerInventory::default()) .push_children(&[camera]); game_load_state.player_loaded = true; diff --git a/src/logic/core/guns/equip_firearm.rs b/src/logic/core/guns/equip_firearm.rs deleted file mode 100644 index 57a0d15..0000000 --- a/src/logic/core/guns/equip_firearm.rs +++ /dev/null @@ -1,4 +0,0 @@ -use bevy::prelude::*; - -#[derive(Event)] -pub struct SpawnFirearmEvent(); diff --git a/src/logic/core/guns/firearm.rs b/src/logic/core/guns/firearm.rs index 2c7f051..a676929 100644 --- a/src/logic/core/guns/firearm.rs +++ b/src/logic/core/guns/firearm.rs @@ -1,7 +1,9 @@ -use crate::comps::core::markers::{ +use std::sync::Arc; + +use crate::comps::core::{markers::{ firearm::{FirearmData, FirearmType, FiringPoint}, holdable::HoldableObjectData, -}; +}, items::{item::Item, guns::{m4a1::M4a1GunItem, glock17::Glock17GunItem}}}; use bevy::prelude::*; use super::{caliber::Caliber, spray_pattern::FirearmSprayPattern}; @@ -123,4 +125,10 @@ impl Firearm { Firearm::Glock17 => Vec3::ZERO, } } + pub fn get_item(&self) -> Arc { + match self { + Firearm::M4A1 => Arc::new(M4a1GunItem), + Firearm::Glock17 => Arc::new(Glock17GunItem), + } + } } diff --git a/src/logic/core/guns/mod.rs b/src/logic/core/guns/mod.rs index a88d06d..1a6a129 100644 --- a/src/logic/core/guns/mod.rs +++ b/src/logic/core/guns/mod.rs @@ -1,6 +1,5 @@ pub mod caliber; pub mod despawn_shots; -pub mod equip_firearm; pub mod firearm; pub mod player_firing; pub mod shoot; diff --git a/src/logic/core/player/hands.rs b/src/logic/core/player/hands.rs index 4b9ecb0..9fbe1cf 100644 --- a/src/logic/core/player/hands.rs +++ b/src/logic/core/player/hands.rs @@ -2,13 +2,13 @@ use bevy::{ecs::system::SystemParam, prelude::*}; use bevy_rapier3d::prelude::*; use crate::{ - comps::core::markers::{ + comps::core::{markers::{ camera::MainCamera, firearm::{FirearmData, MagazineData}, holdable::InPlayerHands, interactable::Interactable, player::{Player, PlayerHand}, - }, + }, events::pickup_item::PickupItemEvent}, logic::core::guns::{firearm::Firearm, player_firing::PlayerFiringInfo, shoot::shoot_bullet}, setup::{ animations::AllFirearmAnimations, @@ -259,7 +259,7 @@ pub fn interact_action( keyboard_input: Res>, rapier_context: Res, mut hud_state: ResMut, - //mut loot_container_event_writer: EventWriter, + mut pickup_item_event_writer: EventWriter, ) { for (player_entity, transform) in player_query.iter() { for global_transform in camera_query.iter() { @@ -292,9 +292,9 @@ pub fn interact_action( loot_container_event_writer .send(LootContainerEvent(any_inventory.clone())) }*/ - Interactable::Item => { - //loot_container_event_writer - // .send() + Interactable::Item(item) => { + + pickup_item_event_writer.send(PickupItemEvent(item.clone())); } } } diff --git a/src/ui/game/inventory/plugin.rs b/src/ui/game/inventory/plugin.rs index dae2602..cc7bca0 100644 --- a/src/ui/game/inventory/plugin.rs +++ b/src/ui/game/inventory/plugin.rs @@ -2,13 +2,15 @@ use bevy::prelude::*; //use crate::comps::core::events::loot_container::LootContainerEvent; +use crate::comps::core::events::pickup_item::PickupItemEvent; + use super::menu::{setup_inventory_screen, update_inventory_screen}; pub struct InventoryMenuPlugin; impl Plugin for InventoryMenuPlugin { fn build(&self, app: &mut App) { - //app.add_event::(); + app.add_event::(); app.add_systems(Startup, setup_inventory_screen); app.add_systems(Update, update_inventory_screen); }