From cdf1d573cf7068dd7adbecf43a4585d9ee6aa096 Mon Sep 17 00:00:00 2001 From: Franklin Date: Sun, 12 Nov 2023 00:43:54 -0400 Subject: [PATCH] Work on interaction system, re enabled weapon switching and noticed that Meshes are down to the bone, meaning creating a hitreg system will be relatively easy later on. Focus on the inventory definition, honestly I would just go for static primary, secondary, body armor and helmet inventory spaces. Later, add backpacks with the tetris system you just created. For the moment make everything pickupable --- src/comps/core/any_inventory.rs | 3 ++- src/comps/core/events/loot_container.rs | 6 ++++++ src/comps/core/events/mod.rs | 1 + src/comps/core/inventory_item.rs | 7 +++++-- src/comps/core/item.rs | 2 +- src/comps/core/item_inventory.rs | 12 ++++++++++++ src/comps/core/markers/interactable.rs | 1 - src/comps/core/mod.rs | 5 ++++- src/comps/core/player_inventory.rs | 16 ++++++++++++++++ src/logic/core/player/hands.rs | 23 ++++++++++++++--------- src/ui/game/inventory/menu.rs | 17 ++++++++++++++--- src/ui/game/inventory/mod.rs | 3 ++- src/ui/game/inventory/plugin.rs | 16 ++++++++++++++++ src/ui/game/plugin.rs | 1 + 14 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 src/comps/core/events/loot_container.rs create mode 100644 src/comps/core/events/mod.rs create mode 100644 src/comps/core/item_inventory.rs create mode 100644 src/comps/core/player_inventory.rs create mode 100644 src/ui/game/inventory/plugin.rs diff --git a/src/comps/core/any_inventory.rs b/src/comps/core/any_inventory.rs index 14cd7f2..40310a7 100644 --- a/src/comps/core/any_inventory.rs +++ b/src/comps/core/any_inventory.rs @@ -2,7 +2,8 @@ use bevy::ecs::component::SparseStorage; use super::{item::Item, inventory_item::InventoryItem, grid::UGrid}; - +#[allow(unused)] +#[derive(Clone)] pub struct AnyInventory { size: UGrid, items: Vec diff --git a/src/comps/core/events/loot_container.rs b/src/comps/core/events/loot_container.rs new file mode 100644 index 0000000..a909c91 --- /dev/null +++ b/src/comps/core/events/loot_container.rs @@ -0,0 +1,6 @@ +use bevy::prelude::*; + +use crate::comps::core::any_inventory::AnyInventory; + +#[derive(Event)] +pub struct LootContainerEvent(pub AnyInventory); \ No newline at end of file diff --git a/src/comps/core/events/mod.rs b/src/comps/core/events/mod.rs new file mode 100644 index 0000000..d332458 --- /dev/null +++ b/src/comps/core/events/mod.rs @@ -0,0 +1 @@ +pub mod loot_container; \ No newline at end of file diff --git a/src/comps/core/inventory_item.rs b/src/comps/core/inventory_item.rs index 1df0bec..4243945 100644 --- a/src/comps/core/inventory_item.rs +++ b/src/comps/core/inventory_item.rs @@ -1,10 +1,13 @@ +use std::sync::Arc; + use bevy::ecs::component::SparseStorage; use super::{item::Item, grid::UGrid}; +#[derive(Clone)] pub struct InventoryItem { - item: Box>, + item: Arc>, /// Coordinates that this InventoryItem occupies inside an AnyInventory occupied_spots: Vec, rotated: Option, @@ -24,7 +27,7 @@ impl InventoryItem { ) -> Self { let size = item.inventory_size(); let rotated = item.inventory_rotatable().then(|| false); - Self { item: Box::new(item), occupied_spots, rotated } + Self { item: Arc::new(item), occupied_spots, rotated } } /// Returns true if there is overlap between both pub fn is_in_range(&self, spots_to_occupy: &Vec) -> bool { diff --git a/src/comps/core/item.rs b/src/comps/core/item.rs index ccbcce4..2267fbf 100644 --- a/src/comps/core/item.rs +++ b/src/comps/core/item.rs @@ -9,7 +9,7 @@ pub enum ItemType { Consumable, } -pub trait Item: Component { +pub trait Item: Component { fn get_type(&self) -> ItemType; fn asset_path(&self) -> &str; /// Optional Stackable. If value is Some(x) x is the max quantity per stack diff --git a/src/comps/core/item_inventory.rs b/src/comps/core/item_inventory.rs new file mode 100644 index 0000000..a74f26a --- /dev/null +++ b/src/comps/core/item_inventory.rs @@ -0,0 +1,12 @@ +use std::sync::Arc; + +use bevy::{prelude::*, ecs::component::SparseStorage}; + +use super::item::Item; + +/// # ItemInventory +/// Specifically made to hold single items such as Guns, Pieces of armor. +#[derive(Component, Clone, Default)] +pub struct ItemInventory { + pub item: Option>>, +} \ No newline at end of file diff --git a/src/comps/core/markers/interactable.rs b/src/comps/core/markers/interactable.rs index 78017cb..dbeeecf 100644 --- a/src/comps/core/markers/interactable.rs +++ b/src/comps/core/markers/interactable.rs @@ -5,7 +5,6 @@ use bevy::prelude::Component; use crate::comps::core::any_inventory::AnyInventory; - #[allow(unused)] #[derive(Component)] pub enum Interactable { diff --git a/src/comps/core/mod.rs b/src/comps/core/mod.rs index 35791bb..ece68b7 100644 --- a/src/comps/core/mod.rs +++ b/src/comps/core/mod.rs @@ -3,4 +3,7 @@ pub mod markers; pub mod any_inventory; pub mod item; pub mod grid; -pub mod inventory_item; \ No newline at end of file +pub mod inventory_item; +pub mod events; +pub mod item_inventory; +pub mod player_inventory; \ No newline at end of file diff --git a/src/comps/core/player_inventory.rs b/src/comps/core/player_inventory.rs new file mode 100644 index 0000000..4695bcf --- /dev/null +++ b/src/comps/core/player_inventory.rs @@ -0,0 +1,16 @@ +use bevy::prelude::*; + +use super::{item_inventory::ItemInventory, any_inventory::AnyInventory, grid::UGrid}; + +#[derive(Component)] +pub struct PlayerInventory { + pub primary: ItemInventory, + pub secondary: ItemInventory, + pub backpack: AnyInventory, +} + +impl Default for PlayerInventory { + fn default() -> Self { + Self { primary: Default::default(), secondary: Default::default(), backpack: AnyInventory::new(UGrid::new_square(10)) } + } +} \ No newline at end of file diff --git a/src/logic/core/player/hands.rs b/src/logic/core/player/hands.rs index f51127e..1d99ace 100644 --- a/src/logic/core/player/hands.rs +++ b/src/logic/core/player/hands.rs @@ -2,8 +2,8 @@ use bevy::prelude::*; use bevy_rapier3d::prelude::*; use crate::{ - comps::core::markers::{firearm::{FirearmData, MagazineData}, holdable::InPlayerHands, player::{PlayerHand, Player}, camera::MainCamera, interactable::Interactable}, - logic::core::guns::{player_firing::PlayerFiringInfo, shoot::shoot_bullet}, utils::rad_deg::radians_from_degrees, setup::{animations::AllFirearmAnimations, load_state::GameLoadState, equipment::{EquipmentChangeEvent, Equipment}}, ui::game::{settings::SettingsScreenUIConfiguration, hud::hud::HudState}, + comps::core::{markers::{firearm::{FirearmData, MagazineData}, holdable::InPlayerHands, player::{PlayerHand, Player}, camera::MainCamera, interactable::Interactable}, events::loot_container::LootContainerEvent}, + logic::core::guns::{player_firing::PlayerFiringInfo, shoot::shoot_bullet, firearm::Firearm}, utils::rad_deg::radians_from_degrees, setup::{animations::AllFirearmAnimations, load_state::GameLoadState, equipment::{EquipmentChangeEvent, Equipment}}, ui::game::{settings::SettingsScreenUIConfiguration, hud::hud::HudState}, }; pub fn capture_hand_usage( @@ -42,15 +42,15 @@ pub fn capture_hand_usage( // Equipping gun - /*if !settings_screen_config.settings_menu_shown { + if !settings_screen_config.settings_menu_shown { if keyboard_input.just_pressed(KeyCode::Key1) { - equipment_change_event_writer.send(EquipmentChangeEvent(Equipment { primary_firearm: Some(Firearm::M4A1) } )); + equipment_change_event_writer.send(EquipmentChangeEvent(Equipment::Firearm(Firearm::M4A1))); } else if keyboard_input.just_pressed(KeyCode::Key2) { - equipment_change_event_writer.send(EquipmentChangeEvent(Equipment { primary_firearm: Some(Firearm::Glock17) } )); + equipment_change_event_writer.send(EquipmentChangeEvent(Equipment::Firearm(Firearm::Glock17) )); } else if keyboard_input.just_pressed(KeyCode::Key3) { - equipment_change_event_writer.send(EquipmentChangeEvent(Equipment { primary_firearm: None } )); + equipment_change_event_writer.send(EquipmentChangeEvent(Equipment::Nothing )); } - }*/ + } // Firearm stuff if let Equipment::Firearm(player_firearm) = player_query.single().0.equipment.clone() { @@ -172,6 +172,7 @@ pub fn interact_action( keyboard_input: Res>, rapier_context: Res, mut hud_state: ResMut, + mut loot_container_event_writer: EventWriter, ) { for transform in player_query.iter() { for global_transform in camera_query.iter() { @@ -183,12 +184,16 @@ pub fn interact_action( if let Some((entity, _toi)) = rapier_context.cast_ray( ray_pos, ray_dir, max_toi, solid, QueryFilter::only_fixed() ) { - for (interactable_entity, _interactable) in interactables_query.iter() { + for (interactable_entity, interactable) in interactables_query.iter() { if interactable_entity == entity { hud_state.interaction_clue_shown = true; if keyboard_input.just_pressed(KeyCode::F) { // TODO: Move this key to Controls state global println!("Interacted with interactable"); - + match interactable { + Interactable::Holdable => todo!(), + Interactable::Lootable(any_inventory) => loot_container_event_writer.send(LootContainerEvent(any_inventory.clone())), + Interactable::Item => todo!(), + } } } return; diff --git a/src/ui/game/inventory/menu.rs b/src/ui/game/inventory/menu.rs index dd64d77..27af43b 100644 --- a/src/ui/game/inventory/menu.rs +++ b/src/ui/game/inventory/menu.rs @@ -1,11 +1,22 @@ use bevy::prelude::*; -//use crate::comps::core::any_inventory::AnyInventory; +use crate::comps::core::events::loot_container::LootContainerEvent; -#[allow(unused)] +/// # Inventory Screen +/// Should contain player inventory and if player is looting something as well pub fn setup_inventory_screen( mut commands: Commands, -) { +) { + +} + +pub fn update_inventory_screen( + mut commands: Commands, + mut event_reader: EventReader, +) { + for loot_container_event in event_reader.read() { + //let a = loot_container_event.0.clone(); + } } \ No newline at end of file diff --git a/src/ui/game/inventory/mod.rs b/src/ui/game/inventory/mod.rs index 915a9fa..d379867 100644 --- a/src/ui/game/inventory/mod.rs +++ b/src/ui/game/inventory/mod.rs @@ -1 +1,2 @@ -pub mod menu; \ No newline at end of file +pub mod menu; +pub mod plugin; \ No newline at end of file diff --git a/src/ui/game/inventory/plugin.rs b/src/ui/game/inventory/plugin.rs new file mode 100644 index 0000000..6d50301 --- /dev/null +++ b/src/ui/game/inventory/plugin.rs @@ -0,0 +1,16 @@ +use bevy::prelude::*; + +use crate::comps::core::events::loot_container::LootContainerEvent; + +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_systems(Startup, setup_inventory_screen); + app.add_systems(Update, update_inventory_screen); + } +} \ No newline at end of file diff --git a/src/ui/game/plugin.rs b/src/ui/game/plugin.rs index 9b6869b..130d69a 100644 --- a/src/ui/game/plugin.rs +++ b/src/ui/game/plugin.rs @@ -9,6 +9,7 @@ pub struct MainGameUIPlugin; impl Plugin for MainGameUIPlugin { fn build(&self, app: &mut App) { app.add_plugins(hud::plugin::HudOverlayPlugin); + app.add_plugins(inventory::plugin::InventoryMenuPlugin); app.insert_resource(GameConfiguration::default()); app.insert_resource(SettingsScreenUIConfiguration::default()); app.add_systems(Startup, (