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;
/// # 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<Box<dyn Item>>,
}

View File

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

View File

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

View File

@ -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<dyn Item>),
}
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"),
}
}
}

View File

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

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},
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<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 despawn_shots;
pub mod equip_firearm;
pub mod firearm;
pub mod player_firing;
pub mod shoot;

View File

@ -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<Input<KeyCode>>,
rapier_context: Res<RapierContext>,
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 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()));
}
}
}

View File

@ -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::<LootContainerEvent>();
app.add_event::<PickupItemEvent>();
app.add_systems(Startup, setup_inventory_screen);
app.add_systems(Update, update_inventory_screen);
}