From 865070ed90aed07ecc3e76221dbf6657caf5ebff Mon Sep 17 00:00:00 2001 From: Franklin Date: Sun, 12 Nov 2023 16:38:23 -0400 Subject: [PATCH] Fixed guns reloading all animation players when reload anim is triggered on the same gun --- src/comps/core/items/guns/glock17.rs | 37 ++++++ src/comps/core/items/guns/mod.rs | 1 + src/comps/core/items/item.rs | 3 +- src/comps/core/mod.rs | 1 + .../core/spawners/guns/glock17_spawner.rs | 18 +++ .../core}/spawners/guns/m4a1_spawner.rs | 2 +- src/comps/core/spawners/guns/mod.rs | 2 + src/{setup => comps/core}/spawners/item.rs | 5 +- src/{setup => comps/core}/spawners/mod.rs | 3 +- src/{setup => comps/core}/spawners/player.rs | 0 src/comps/core/spawners/spawn.rs | 15 +++ .../core}/spawners/spawn_point.rs | 2 +- src/logic/core/guns/firearm.rs | 6 + src/logic/core/player/hands.rs | 109 ++++++++++-------- src/main.rs | 3 +- src/scenes/scene1/init.rs | 13 ++- src/scenes/scene1/spawn_points.rs | 18 ++- src/setup/mod.rs | 2 - src/setup/spawn.rs | 16 --- src/setup/spawners/guns/mod.rs | 1 - src/utils/hierarchy.rs | 17 +++ src/utils/mod.rs | 1 + 22 files changed, 190 insertions(+), 85 deletions(-) create mode 100644 src/comps/core/items/guns/glock17.rs create mode 100644 src/comps/core/spawners/guns/glock17_spawner.rs rename src/{setup => comps/core}/spawners/guns/m4a1_spawner.rs (72%) create mode 100644 src/comps/core/spawners/guns/mod.rs rename src/{setup => comps/core}/spawners/item.rs (85%) rename src/{setup => comps/core}/spawners/mod.rs (64%) rename src/{setup => comps/core}/spawners/player.rs (100%) create mode 100644 src/comps/core/spawners/spawn.rs rename src/{setup => comps/core}/spawners/spawn_point.rs (85%) delete mode 100644 src/setup/spawn.rs delete mode 100644 src/setup/spawners/guns/mod.rs create mode 100644 src/utils/hierarchy.rs diff --git a/src/comps/core/items/guns/glock17.rs b/src/comps/core/items/guns/glock17.rs new file mode 100644 index 0000000..b0e34a4 --- /dev/null +++ b/src/comps/core/items/guns/glock17.rs @@ -0,0 +1,37 @@ +use bevy::prelude::*; + +use crate::{comps::core::{items::item::{Item, ItemType}, grid::UGrid, markers::holdable::HoldableObjectType}, logic::core::guns::firearm::Firearm}; + +#[derive(Component)] +pub struct Glock17GunItem; + +impl Item for Glock17GunItem { + fn get_type(&self) -> ItemType { + ItemType::Holdable(HoldableObjectType::Firearm(Firearm::Glock17)) + } + + fn asset_path(&self) -> &str { + "weapons/glock_17_pistol.glb" + } + + #[doc = " Optional Stackable. If value is Some(x) x is the max quantity per stack"] + fn stackable(&self) -> Option { + None + } + + fn inventory_size(&self) -> UGrid { + UGrid { width: 2, height: 1 } + } + + fn inventory_rotatable(&self) -> bool { + true + } + + fn inventory_title(&self) -> String { + String::from("Glock17 Firearm") + } + + fn inventory_description(&self) -> String { + String::from("Glock17 Firearm chambered in 9x19mm.") + } +} \ No newline at end of file diff --git a/src/comps/core/items/guns/mod.rs b/src/comps/core/items/guns/mod.rs index 1291f9b..c743ae6 100644 --- a/src/comps/core/items/guns/mod.rs +++ b/src/comps/core/items/guns/mod.rs @@ -1 +1,2 @@ pub mod m4a1; +pub mod glock17; \ No newline at end of file diff --git a/src/comps/core/items/item.rs b/src/comps/core/items/item.rs index 3b64ce6..5f4d4e1 100644 --- a/src/comps/core/items/item.rs +++ b/src/comps/core/items/item.rs @@ -67,6 +67,7 @@ pub trait Item { )), )) .id(); + let firearm_size = firearm.get_size(); commands .spawn(( firearm.holdable_object_data(), @@ -80,7 +81,7 @@ pub trait Item { ..Default::default() }, RigidBody::Dynamic, - Collider::cuboid(5.0, 5.0, 5.0), + Collider::cuboid(firearm_size.x, firearm_size.y, firearm_size.z), Interactable::Item, )) .push_children(&[firearm_asset_entity]); diff --git a/src/comps/core/mod.rs b/src/comps/core/mod.rs index 07d3e29..53963b0 100644 --- a/src/comps/core/mod.rs +++ b/src/comps/core/mod.rs @@ -4,3 +4,4 @@ pub mod grid; pub mod inventory; pub mod items; pub mod markers; +pub mod spawners; \ No newline at end of file diff --git a/src/comps/core/spawners/guns/glock17_spawner.rs b/src/comps/core/spawners/guns/glock17_spawner.rs new file mode 100644 index 0000000..28fe788 --- /dev/null +++ b/src/comps/core/spawners/guns/glock17_spawner.rs @@ -0,0 +1,18 @@ +use bevy::prelude::*; + +use crate::comps::core::{items::{guns::glock17::Glock17GunItem, item::Item}, spawners::item::ItemSpawnPoint}; + +#[derive(Component)] +pub struct Glock17SpawnPoint { + pub transform: Transform, +} + +impl ItemSpawnPoint for Glock17SpawnPoint { + fn get_transform(&self) -> Transform { + self.transform + } + + fn get_item(&self) -> Box { + Box::new(Glock17GunItem) + } +} \ No newline at end of file diff --git a/src/setup/spawners/guns/m4a1_spawner.rs b/src/comps/core/spawners/guns/m4a1_spawner.rs similarity index 72% rename from src/setup/spawners/guns/m4a1_spawner.rs rename to src/comps/core/spawners/guns/m4a1_spawner.rs index 59043dd..152522b 100644 --- a/src/setup/spawners/guns/m4a1_spawner.rs +++ b/src/comps/core/spawners/guns/m4a1_spawner.rs @@ -1,6 +1,6 @@ use bevy::prelude::*; -use crate::{setup::spawners::item::ItemSpawnPoint, comps::core::items::{guns::m4a1::M4a1GunItem, item::Item}}; +use crate::comps::core::{items::{guns::m4a1::M4a1GunItem, item::Item}, spawners::item::ItemSpawnPoint}; #[derive(Component)] pub struct M4a1SpawnPoint { diff --git a/src/comps/core/spawners/guns/mod.rs b/src/comps/core/spawners/guns/mod.rs new file mode 100644 index 0000000..32ee70e --- /dev/null +++ b/src/comps/core/spawners/guns/mod.rs @@ -0,0 +1,2 @@ +pub mod m4a1_spawner; +pub mod glock17_spawner; \ No newline at end of file diff --git a/src/setup/spawners/item.rs b/src/comps/core/spawners/item.rs similarity index 85% rename from src/setup/spawners/item.rs rename to src/comps/core/spawners/item.rs index 70fc4a9..3e0219a 100644 --- a/src/setup/spawners/item.rs +++ b/src/comps/core/spawners/item.rs @@ -1,6 +1,6 @@ use bevy::{prelude::*, gltf::Gltf}; -use crate::{setup::{load_state::GameLoadState, assets::GltfAssets, spawners::guns::m4a1_spawner::M4a1SpawnPoint}, comps::core::items::item::Item}; +use crate::{setup::{load_state::GameLoadState, assets::GltfAssets}, comps::core::{items::item::Item, spawners::guns::{m4a1_spawner::M4a1SpawnPoint, glock17_spawner::Glock17SpawnPoint}}}; #[bevy_trait_query::queryable] pub trait ItemSpawnPoint { @@ -32,6 +32,7 @@ impl Plugin for ItemSpawnPointPlugin { fn build(&self, app: &mut App) { use bevy_trait_query::RegisterExt; app - .register_component_as::(); + .register_component_as::() + .register_component_as::(); } } \ No newline at end of file diff --git a/src/setup/spawners/mod.rs b/src/comps/core/spawners/mod.rs similarity index 64% rename from src/setup/spawners/mod.rs rename to src/comps/core/spawners/mod.rs index 8518499..03190df 100644 --- a/src/setup/spawners/mod.rs +++ b/src/comps/core/spawners/mod.rs @@ -1,4 +1,5 @@ pub mod item; pub mod player; pub mod spawn_point; -pub mod guns; \ No newline at end of file +pub mod guns; +pub mod spawn; \ No newline at end of file diff --git a/src/setup/spawners/player.rs b/src/comps/core/spawners/player.rs similarity index 100% rename from src/setup/spawners/player.rs rename to src/comps/core/spawners/player.rs diff --git a/src/comps/core/spawners/spawn.rs b/src/comps/core/spawners/spawn.rs new file mode 100644 index 0000000..e945ad1 --- /dev/null +++ b/src/comps/core/spawners/spawn.rs @@ -0,0 +1,15 @@ +use bevy::prelude::*; + +use super::{player::player_spawner, item::{item_spawner, ItemSpawnPointPlugin}, spawn_point::SpawnPointPlugin}; + + +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), + ); + } +} \ No newline at end of file diff --git a/src/setup/spawners/spawn_point.rs b/src/comps/core/spawners/spawn_point.rs similarity index 85% rename from src/setup/spawners/spawn_point.rs rename to src/comps/core/spawners/spawn_point.rs index 1ab0082..9519105 100644 --- a/src/setup/spawners/spawn_point.rs +++ b/src/comps/core/spawners/spawn_point.rs @@ -1,6 +1,6 @@ use bevy::prelude::*; -use crate::setup::spawners::player::PlayerSpawnPoint; +use crate::comps::core::spawners::player::PlayerSpawnPoint; #[bevy_trait_query::queryable] diff --git a/src/logic/core/guns/firearm.rs b/src/logic/core/guns/firearm.rs index 43682e0..a57182c 100644 --- a/src/logic/core/guns/firearm.rs +++ b/src/logic/core/guns/firearm.rs @@ -111,4 +111,10 @@ impl Firearm { }, } } + pub fn get_size(&self) -> Vec3 { + match self { + Firearm::M4A1 => Vec3 { x: 0.3, y: 0.7, z: 2.5 }, + Firearm::Glock17 => Vec3::ZERO, + } + } } diff --git a/src/logic/core/player/hands.rs b/src/logic/core/player/hands.rs index 6b63b5f..4933f8a 100644 --- a/src/logic/core/player/hands.rs +++ b/src/logic/core/player/hands.rs @@ -1,4 +1,4 @@ -use bevy::prelude::*; +use bevy::{prelude::*, ecs::system::SystemParam}; use bevy_rapier3d::prelude::*; use crate::{ @@ -19,34 +19,40 @@ use crate::{ load_state::GameLoadState, }, ui::game::{hud::hud::HudState, settings::SettingsScreenUIConfiguration}, - utils::rad_deg::radians_from_degrees, + utils::{rad_deg::radians_from_degrees, self}, }; -pub fn capture_hand_usage( - mut commands: Commands, - mouse_buttons: Res>, - keyboard_input: Res>, - mut hand_query: Query<&mut Transform, With>, - time: Res