Compiles and doesn't crash on startup, changed some Boxes to Arcs and added a PickupItemEvent

This commit is contained in:
Franklin 2023-11-12 18:51:35 -04:00
parent aa7eb521b8
commit 45f412c7c0
10 changed files with 37 additions and 31 deletions

View File

@ -1,10 +1,8 @@
use bevy::prelude::*;
use crate::comps::core::items::item::Item; use crate::comps::core::items::item::Item;
/// # ItemInventory /// # ItemInventory
/// Specifically made to hold single items such as Guns, Pieces of armor. /// Specifically made to hold single items such as Guns, Pieces of armor.
#[derive(Component, Clone, Default)] #[derive(Default)]
pub struct ItemInventory<'a> { pub struct ItemInventory {
pub item: Option<&'a dyn Item>, pub item: Option<Box<dyn Item>>,
} }

View File

@ -3,13 +3,13 @@ use bevy::prelude::*;
use super::item_inventory::ItemInventory; use super::item_inventory::ItemInventory;
#[derive(Component)] #[derive(Component)]
pub struct PlayerInventory<'a> { pub struct PlayerInventory {
pub primary: ItemInventory<'a>, pub primary: ItemInventory,
pub secondary: ItemInventory<'a>, pub secondary: ItemInventory,
//pub backpack: AnyInventory, //pub backpack: AnyInventory,
} }
impl<'a> Default for PlayerInventory<'a> { impl Default for PlayerInventory {
fn default() -> Self { fn default() -> Self {
Self { Self {
primary: Default::default(), primary: Default::default(),
@ -17,4 +17,4 @@ impl<'a> Default for PlayerInventory<'a> {
//backpack: AnyInventory::new(UGrid::new_square(10)), //backpack: AnyInventory::new(UGrid::new_square(10)),
} }
} }
} }

View File

@ -17,7 +17,7 @@ pub enum ItemType {
} }
#[bevy_trait_query::queryable] #[bevy_trait_query::queryable]
pub trait Item { pub trait Item: Sync + Send {
fn get_type(&self) -> ItemType; fn get_type(&self) -> ItemType;
fn asset_path(&self) -> &str; fn asset_path(&self) -> &str;
/// Optional Stackable. If value is Some(x) x is the max quantity per stack /// 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.y,
firearm_size.z, firearm_size.z,
), ),
Interactable::Item, Interactable::Item(firearm.get_item()),
)) ))
.push_children(&[firearm_asset_entity]); .push_children(&[firearm_asset_entity]);
} }

View File

@ -1,7 +1,9 @@
use std::fmt::Display; use std::{fmt::Display, sync::Arc};
use bevy::prelude::Component; use bevy::prelude::Component;
use crate::comps::core::items::item::Item;
//use crate::comps::core::inventory::any_inventory::AnyInventory; //use crate::comps::core::inventory::any_inventory::AnyInventory;
#[allow(unused)] #[allow(unused)]
@ -9,7 +11,7 @@ use bevy::prelude::Component;
pub enum Interactable { pub enum Interactable {
Holdable, Holdable,
//Lootable(AnyInventory), //Lootable(AnyInventory),
Item, Item(Arc<dyn Item>),
} }
impl Display for Interactable { impl Display for Interactable {
@ -17,7 +19,7 @@ impl Display for Interactable {
match self { match self {
Interactable::Holdable => write!(f, "Holdable"), Interactable::Holdable => write!(f, "Holdable"),
//Interactable::Lootable(_) => write!(f, "Lootable"), //Interactable::Lootable(_) => write!(f, "Lootable"),
Interactable::Item => write!(f, "Item"), Interactable::Item(_) => write!(f, "Item"),
} }
} }
} }

View File

@ -2,10 +2,10 @@ use bevy::prelude::*;
use bevy_rapier3d::prelude::*; use bevy_rapier3d::prelude::*;
use crate::{ use crate::{
comps::core::markers::{ comps::core::{markers::{
camera::MainCamera, camera::MainCamera,
player::{Player, PlayerData, PlayerHand}, player::{Player, PlayerData, PlayerHand},
}, }, inventory::player_inventory::PlayerInventory},
logic::core::{ logic::core::{
guns::player_firing::PlayerFiringInfo, guns::player_firing::PlayerFiringInfo,
player::{ player::{
@ -114,6 +114,7 @@ pub fn player_spawner(
}) })
// Data // Data
.insert(PlayerFiringInfo::default()) .insert(PlayerFiringInfo::default())
.insert(PlayerInventory::default())
.push_children(&[camera]); .push_children(&[camera]);
game_load_state.player_loaded = true; game_load_state.player_loaded = true;

View File

@ -1,4 +0,0 @@
use bevy::prelude::*;
#[derive(Event)]
pub struct SpawnFirearmEvent();

View File

@ -1,7 +1,9 @@
use crate::comps::core::markers::{ use std::sync::Arc;
use crate::comps::core::{markers::{
firearm::{FirearmData, FirearmType, FiringPoint}, firearm::{FirearmData, FirearmType, FiringPoint},
holdable::HoldableObjectData, holdable::HoldableObjectData,
}; }, items::{item::Item, guns::{m4a1::M4a1GunItem, glock17::Glock17GunItem}}};
use bevy::prelude::*; use bevy::prelude::*;
use super::{caliber::Caliber, spray_pattern::FirearmSprayPattern}; use super::{caliber::Caliber, spray_pattern::FirearmSprayPattern};
@ -123,4 +125,10 @@ impl Firearm {
Firearm::Glock17 => Vec3::ZERO, Firearm::Glock17 => Vec3::ZERO,
} }
} }
pub fn get_item(&self) -> Arc<dyn Item> {
match self {
Firearm::M4A1 => Arc::new(M4a1GunItem),
Firearm::Glock17 => Arc::new(Glock17GunItem),
}
}
} }

View File

@ -1,6 +1,5 @@
pub mod caliber; pub mod caliber;
pub mod despawn_shots; pub mod despawn_shots;
pub mod equip_firearm;
pub mod firearm; pub mod firearm;
pub mod player_firing; pub mod player_firing;
pub mod shoot; pub mod shoot;

View File

@ -2,13 +2,13 @@ use bevy::{ecs::system::SystemParam, prelude::*};
use bevy_rapier3d::prelude::*; use bevy_rapier3d::prelude::*;
use crate::{ use crate::{
comps::core::markers::{ comps::core::{markers::{
camera::MainCamera, camera::MainCamera,
firearm::{FirearmData, MagazineData}, firearm::{FirearmData, MagazineData},
holdable::InPlayerHands, holdable::InPlayerHands,
interactable::Interactable, interactable::Interactable,
player::{Player, PlayerHand}, player::{Player, PlayerHand},
}, }, events::pickup_item::PickupItemEvent},
logic::core::guns::{firearm::Firearm, player_firing::PlayerFiringInfo, shoot::shoot_bullet}, logic::core::guns::{firearm::Firearm, player_firing::PlayerFiringInfo, shoot::shoot_bullet},
setup::{ setup::{
animations::AllFirearmAnimations, animations::AllFirearmAnimations,
@ -259,7 +259,7 @@ pub fn interact_action(
keyboard_input: Res<Input<KeyCode>>, keyboard_input: Res<Input<KeyCode>>,
rapier_context: Res<RapierContext>, rapier_context: Res<RapierContext>,
mut hud_state: ResMut<HudState>, mut hud_state: ResMut<HudState>,
//mut loot_container_event_writer: EventWriter<LootContainerEvent>, mut pickup_item_event_writer: EventWriter<PickupItemEvent>,
) { ) {
for (player_entity, transform) in player_query.iter() { for (player_entity, transform) in player_query.iter() {
for global_transform in camera_query.iter() { for global_transform in camera_query.iter() {
@ -292,9 +292,9 @@ pub fn interact_action(
loot_container_event_writer loot_container_event_writer
.send(LootContainerEvent(any_inventory.clone())) .send(LootContainerEvent(any_inventory.clone()))
}*/ }*/
Interactable::Item => { Interactable::Item(item) => {
//loot_container_event_writer
// .send() pickup_item_event_writer.send(PickupItemEvent(item.clone()));
} }
} }
} }

View File

@ -2,13 +2,15 @@ use bevy::prelude::*;
//use crate::comps::core::events::loot_container::LootContainerEvent; //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}; use super::menu::{setup_inventory_screen, update_inventory_screen};
pub struct InventoryMenuPlugin; pub struct InventoryMenuPlugin;
impl Plugin for InventoryMenuPlugin { impl Plugin for InventoryMenuPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
//app.add_event::<LootContainerEvent>(); app.add_event::<PickupItemEvent>();
app.add_systems(Startup, setup_inventory_screen); app.add_systems(Startup, setup_inventory_screen);
app.add_systems(Update, update_inventory_screen); app.add_systems(Update, update_inventory_screen);
} }