diff --git a/Design.md b/Design.md index 0a7cf20..4d925e6 100644 --- a/Design.md +++ b/Design.md @@ -46,9 +46,11 @@ Multiplayer - [x] Make the player's collider bigger towards the front - [ ] Make the weapon's collider cover the firing point - [ ] If possible, create a new gun pose, like pushed back -- [ ] Bobbing - - [ ] TODO: Gun Bob on run - - [ ] TODO: Gun Bob on walk +- [x] Bobbing + - [x] Gun Bob on run + - [x] Gun Bob on walk + - [ ] Check if Bobbing could be too exagerated + - [ ] Leaning neutralizes Bobbing for some reason - [ ] Reload animation (procedural) - [ ] Real world magazines - [x] Rewriting bullet physics to use raycasts & kinematic rigidbodies (logic controlled) @@ -58,6 +60,18 @@ Multiplayer - [x] Gun colliding with ground and going into low ready - [x] With introduction of floor collision groups now bullets go through floors - [x] Huge bug all attachments get despawned and spawned when gun shoots +- [ ] Bloom on bullets is making them way too bright, bring down the emissive mat color & size of the bullets. +- [ ] Red dot sight needs to be zeroed. (Height over bore) +- [ ] Red dot on sight is too quick on a trackpad. + - [ ] Red dot is automatically going up to max when shift is pressed for the first time. +- [ ] Multiplayer + - [ ] Start work on a multiplayer server +- [ ] Map + - [ ] Start building a map for your hideout +- [ ] Player model hitbox + - [ ] Organs + - [ ] Heart + - [ ] Brain # Design diff --git a/assets/character/main_character.glb b/assets/character/main_character.glb index 6ad061f..5f0bad6 100644 Binary files a/assets/character/main_character.glb and b/assets/character/main_character.glb differ diff --git a/assets/character/other_player_character.glb b/assets/character/other_player_character.glb new file mode 100644 index 0000000..283aac0 Binary files /dev/null and b/assets/character/other_player_character.glb differ diff --git a/src/comps/core/markers/collider_flags.rs b/src/comps/core/markers/collider_flags.rs index e5dec92..c2e61e4 100644 --- a/src/comps/core/markers/collider_flags.rs +++ b/src/comps/core/markers/collider_flags.rs @@ -11,6 +11,7 @@ bitflags! { /// The value `C`, at bit position `2`. const SOLIDS = 0b00000100; const PLAYER = 0b00001000; + const ENEMY = 0b00010000; const FLOOR = 0b00010000; diff --git a/src/comps/core/markers/mod.rs b/src/comps/core/markers/mod.rs index 6720e57..59a22ac 100644 --- a/src/comps/core/markers/mod.rs +++ b/src/comps/core/markers/mod.rs @@ -9,4 +9,5 @@ pub mod settings_screen; pub mod proxy; pub mod inspect_screen; pub mod collider_flags; -pub mod firearm_scene; \ No newline at end of file +pub mod firearm_scene; +pub mod player_health; \ No newline at end of file diff --git a/src/comps/core/markers/player_health.rs b/src/comps/core/markers/player_health.rs new file mode 100644 index 0000000..dd4af37 --- /dev/null +++ b/src/comps/core/markers/player_health.rs @@ -0,0 +1,28 @@ +use super::proxy::other_players::human_body_part::HumanBodyPart; + + +pub const MAX_BLOOD_ML: f32 = 6000.0; +/// Health for a player +pub struct PlayerHealth { + /// Vision state. TODO: Create an enum for Partial blindness, Full blindness, Full blindness. + pub vision: bool, + /// Measured in milliliters + pub current_blood: f32, + /// The average adult has about 4 to 6 liters of blood (9 to 12 US pints) in their body. The average man has more blood than the average woman, and people who weigh more or are taller than others have more blood.[1] This means a person can die from losing 2 1/2 to 4 liters of blood. To compare, this is five to eight times as much blood as people usually give in a blood donation.[2] + /// [Source](https://simple.wikipedia.org/wiki/Exsanguination#:~:text=People%20can%20die%20from%20losing,two%2Dthirds%20of%20their%20blood.&text=The%20average%20adult%20has%20about,than%20others%20have%20more%20blood.) + /// Measured in milliliters + pub max_blood: f32, + pub human_body_parts_state: Vec, +} + +pub struct HumanBodyPartState { + pub body_part: HumanBodyPart, + pub well: bool, +} + + +impl Default for PlayerHealth { + fn default() -> Self { + Self { current_blood: MAX_BLOOD_ML, max_blood: MAX_BLOOD_ML, vision: true, human_body_parts_state: Vec::new() } // TODO: add human body parts here + } +} \ No newline at end of file diff --git a/src/comps/core/markers/proxy/character/player_eye.rs b/src/comps/core/markers/proxy/character/player_eye.rs index b8e9d3f..6c29274 100644 --- a/src/comps/core/markers/proxy/character/player_eye.rs +++ b/src/comps/core/markers/proxy/character/player_eye.rs @@ -1,7 +1,7 @@ use bevy::{prelude::*, core_pipeline::{tonemapping::Tonemapping, bloom::BloomSettings}}; use bevy_rapier3d::prelude::*; -use crate::{comps::core::markers::{proxy::physics::rapier::LinkToPlayer, camera::MainCamera}, logic::core::player::player_values_state::PlayerValuesState}; +use crate::{comps::core::markers::{proxy::physics::rapier::LinkToPlayer, camera::MainCamera, player::Player}, logic::core::player::player_values_state::PlayerValuesState, utils::hierarchy::find_child_in_parent_children}; use super::{third_person_camera::{ThirdPersonCameraProxy, ThirdPersonCamera}, in_player_hands_parent::InPlayerHandsParent}; @@ -12,40 +12,47 @@ pub struct PlayerEye; pub fn insert_components_into_spawned_player( mut commands: Commands, + player_query: Query>, eye_query: Query>, third_person_camera_query: Query>, player_collider_query: Query, With, Added)>, mut hand_query: Query<(Entity, &mut Transform), (With, Without, Without)>, player_values_state: Res, + children: Query<&Children>, ) { for eye in eye_query.iter() { - // Spawn camera - let camera = commands - .spawn(MainCamera) - .insert(Camera3dBundle { - transform: Transform::from_xyz(0.0, 0.0, 0.0), - camera: Camera { - hdr: true, - ..Default::default() - }, - tonemapping: Tonemapping::TonyMcMapface, - ..Default::default() - }) - .insert(BloomSettings { - ..Default::default() - }) - //s.insert(Skybox(skybox_handle.clone())) - .insert(VisibilityBundle { - visibility: Visibility::Inherited, - ..Default::default() - }) - //.push_children(&[player_hand]) - .id(); - for (hand, mut hand_transform) in hand_query.iter_mut() { - hand_transform.rotate_y(180.0f32.to_radians()); - commands.entity(hand).set_parent(camera); + for player in player_query.iter() { + if find_child_in_parent_children(&mut commands, player, eye, &children) { + // Spawn camera + let camera = commands + .spawn(MainCamera) + .insert(Camera3dBundle { + transform: Transform::from_xyz(0.0, 0.0, 0.0), + camera: Camera { + hdr: true, + ..Default::default() + }, + tonemapping: Tonemapping::TonyMcMapface, + ..Default::default() + }) + .insert(BloomSettings { + ..Default::default() + }) + //s.insert(Skybox(skybox_handle.clone())) + .insert(VisibilityBundle { + visibility: Visibility::Inherited, + ..Default::default() + }) + //.push_children(&[player_hand]) + .id(); + for (hand, mut hand_transform) in hand_query.iter_mut() { + hand_transform.rotate_y(180.0f32.to_radians()); + commands.entity(hand).set_parent(camera); + } + commands.entity(eye).add_child(camera); + } } - commands.entity(eye).add_child(camera); + } for third_person_camera in third_person_camera_query.iter() { diff --git a/src/comps/core/markers/proxy/mod.rs b/src/comps/core/markers/proxy/mod.rs index 02da4e3..55c5e34 100644 --- a/src/comps/core/markers/proxy/mod.rs +++ b/src/comps/core/markers/proxy/mod.rs @@ -2,4 +2,5 @@ pub mod character; pub mod plugin; pub mod physics; pub mod bevy; -pub mod weapons; \ No newline at end of file +pub mod weapons; +pub mod other_players; \ No newline at end of file diff --git a/src/comps/core/markers/proxy/other_players/human_body_part.rs b/src/comps/core/markers/proxy/other_players/human_body_part.rs new file mode 100644 index 0000000..1c8baac --- /dev/null +++ b/src/comps/core/markers/proxy/other_players/human_body_part.rs @@ -0,0 +1,38 @@ +use bevy::{ecs::{component::Component, reflect::ReflectComponent}, reflect::Reflect}; + + +#[derive(Component, Reflect, Clone, PartialEq, Default, Debug)] +#[reflect(Component)] +pub enum HumanBodyPart { + Heart, + Brain, + #[default] + Torso, + Legs, + Hands, + Feet, + Eyes, + Head, + Spine, +} + + +impl HumanBodyPart { + /// For the sake of simplicity. + /// A value of true means an instant death if a bullet hits this body part. + /// A value of false means no instant death. + pub fn is_vital(&self) -> bool { + match self { + HumanBodyPart::Heart => true, + HumanBodyPart::Brain => true, + HumanBodyPart::Spine => true, + _ => false + } + } + + /// Take a mutable reference to the player_health + pub fn effect_when_lost() { + + } + +} \ No newline at end of file diff --git a/src/comps/core/markers/proxy/other_players/mod.rs b/src/comps/core/markers/proxy/other_players/mod.rs new file mode 100644 index 0000000..16d7302 --- /dev/null +++ b/src/comps/core/markers/proxy/other_players/mod.rs @@ -0,0 +1 @@ +pub mod human_body_part; \ No newline at end of file diff --git a/src/comps/core/markers/proxy/physics/rapier.rs b/src/comps/core/markers/proxy/physics/rapier.rs index 062d583..5d5d28a 100644 --- a/src/comps/core/markers/proxy/physics/rapier.rs +++ b/src/comps/core/markers/proxy/physics/rapier.rs @@ -62,36 +62,35 @@ pub fn physics_replace_proxies( mut commands: Commands, ) { - for (player_entity, _) in player_query.iter() { for proxy_colider in proxy_colliders.iter_mut() { - let (entity, collider_proxy, name, mut visibility, link_to_player_opt, _) = proxy_colider; + let (entity, collider_proxy, name, mut visibility, _, _) = proxy_colider; // we hide the collider meshes: perhaps they should be removed altogether once processed ? if name.ends_with("_collider") || name.ends_with("_sensor") { *visibility = Visibility::Hidden; } - let entity_to_insert_collider_on = if link_to_player_opt.is_some() { player_entity } else { entity }; + let mut rapier_collider: RapierCollider; match collider_proxy { Collider::Ball(radius) => { println!("proxy: ball"); rapier_collider = RapierCollider::ball(*radius); - commands.entity(entity_to_insert_collider_on) + commands.entity(entity) .insert(rapier_collider) ; } Collider::Cuboid(size) => { println!("proxy: cuboid"); rapier_collider = RapierCollider::cuboid(size.x, size.y, size.z); - commands.entity(entity_to_insert_collider_on) + commands.entity(entity) .insert(rapier_collider) ; } Collider::Capsule(a, b, radius) => { println!("proxy: capsule"); rapier_collider = RapierCollider::capsule(*a, *b, *radius); - commands.entity(entity_to_insert_collider_on) + commands.entity(entity) .insert(rapier_collider) ; } @@ -110,6 +109,7 @@ pub fn physics_replace_proxies( commands.entity(impulse_entity).insert(impulse_to_be_applied.0); } } + //rapier_collider.set_scale(Vec3 { x: 3.0, y: 3.0, z: 3.0 }, 1); commands .entity(entity) @@ -123,6 +123,10 @@ pub fn physics_replace_proxies( } } for (entity, proxy_rigidbody, link_to_player_opt, _) in proxy_rigidbodies.iter() { + let mut entity_to_insert_collider_on = entity ; + for (player_entity, _) in player_query.iter() { + entity_to_insert_collider_on = if link_to_player_opt.is_some() { player_entity } else { entity }; + } let rapier_rigidbody: RapierRigidBody; match proxy_rigidbody { RigidBodyBlender::Fixed => rapier_rigidbody = RapierRigidBody::Fixed, @@ -130,11 +134,10 @@ pub fn physics_replace_proxies( } commands .entity( - if link_to_player_opt.is_some() { player_entity } else { entity } + if link_to_player_opt.is_some() { entity_to_insert_collider_on } else { entity } ) .insert(rapier_rigidbody) ; commands.entity(entity).remove::(); } - } } \ No newline at end of file diff --git a/src/comps/core/markers/proxy/plugin.rs b/src/comps/core/markers/proxy/plugin.rs index 1fdb5ed..6d596ac 100644 --- a/src/comps/core/markers/proxy/plugin.rs +++ b/src/comps/core/markers/proxy/plugin.rs @@ -2,7 +2,7 @@ use bevy::app::{Plugin, Update}; use crate::{setup::load_state::update_game_load_state, comps::core::weapons::{firearm::Firearm, attachments::{weapon_attachment::WeaponAttachment, optic::Optic, stock::Stock, compensator::Compensator, magazine::Magazine, foregrip::ForeGrip}, parts::{charging_handle::ChargingHandle, fire_selector::FireSelector, firing_point::FiringPoint, trigger::Trigger}, slot::{compensator_slot::CompensatorSlot, fore_grip_slot::ForeGripSlot, magazine_slot::MagazineSlot, stock_slot::StockSlot, utility_slot::UtilitySlot, slot::WeaponSlot}}}; -use super::{character::{player_hitbox::PlayerHitBox, player_character::PlayerCharacter, player_eye::{PlayerEye, insert_components_into_spawned_player}, in_player_hands_parent::{InPlayerHandsParent, insert_components_into_player_hand}, third_person_camera::ThirdPersonCameraProxy}, physics::{rapier::{AutoAABBCollider, physics_replace_proxies}, self}, weapons::{firearm::insert_firearm_state_to_firearms, gun_colliders::{GunFirearmCollider, update_gun_collider}, sight_placement::{SightPlacementStart, SightPlacementEnd}, optic_sight_glass::{OpticSightGlass, replace_optic_sight_material, DebugMaterialResource}}}; +use super::{character::{player_hitbox::PlayerHitBox, player_character::PlayerCharacter, player_eye::{PlayerEye, insert_components_into_spawned_player}, in_player_hands_parent::{InPlayerHandsParent, insert_components_into_player_hand}, third_person_camera::ThirdPersonCameraProxy}, physics::{rapier::{AutoAABBCollider, physics_replace_proxies}, self}, weapons::{firearm::insert_firearm_state_to_firearms, gun_colliders::{GunFirearmCollider, update_gun_collider}, sight_placement::{SightPlacementStart, SightPlacementEnd}, optic_sight_glass::{OpticSightGlass, replace_optic_sight_material, DebugMaterialResource}}, other_players::human_body_part::HumanBodyPart}; @@ -20,6 +20,9 @@ impl Plugin for ProxyComponentsPlugin { app.register_type::(); app.register_type::(); + // OtherPlayer + app.register_type::(); + // Firearms app.register_type::(); app.register_type::(); diff --git a/src/comps/core/spawners/dummy.rs b/src/comps/core/spawners/dummy.rs new file mode 100644 index 0000000..da571f6 --- /dev/null +++ b/src/comps/core/spawners/dummy.rs @@ -0,0 +1,71 @@ +use bevy::{prelude::*, gltf::Gltf}; +use bevy_rapier3d::prelude::*; + +use crate::{ + comps::core::markers::collider_flags::ColliderFlags, + setup::{load_state::GameLoadState, assets::GltfAssets}, +}; + +use super::spawn_point::SpawnPoint; + +#[derive(Component)] +pub struct DummySpawnPoint { + pub transform: Transform, +} + +impl SpawnPoint for DummySpawnPoint { + fn get_transform(&self) -> Transform { + self.transform + } +} + +/// System that runs every fame checking if player has been spawned. +/// For player to spawn, everything inside GameLoadState must be loaded Except player. +pub fn dummy_spawner( + mut commands: Commands, + dummy_sp_query: Query<(Entity, &DummySpawnPoint)>, + game_load_state: Res, + assets: Res, + loaded_gltf_assets: Res>, +) { + if !game_load_state.is_everything_except_player_loaded() { + return; + } + + for (dummy_spawn_point_entity, dummy_spawn_point) in dummy_sp_query.iter() { + let dummy_scene = commands.spawn( + ( + SceneBundle { + scene: loaded_gltf_assets.get(assets.assets[3].asset.clone()).unwrap().scenes[0].clone(), + transform: Transform::from_translation(Vec3::ZERO).with_scale(Vec3 { x: 3.0, y: 3.0, z: 3.0 }), + visibility: Visibility::Inherited, + ..Default::default() + }, + Name::new("Dummy Scene Bundle") + ) + ).id(); + + // Spawn dummy + commands + .spawn(TransformBundle { + local: dummy_spawn_point.get_transform(), + ..Default::default() + }) + .insert( + LockedAxes::ROTATION_LOCKED_Z + | LockedAxes::ROTATION_LOCKED_X + | LockedAxes::ROTATION_LOCKED_Y, + ) + .insert(VisibilityBundle { + visibility: Visibility::Visible, + ..Default::default() + }) + // Data + .insert(CollisionGroups::new(Group::from_bits_retain(ColliderFlags::ENEMY.bits()), Group::from_bits_retain((ColliderFlags::SOLIDS | ColliderFlags::PLAYER | ColliderFlags::BULLETS | ColliderFlags::FLOOR).bits()))) + .insert(Name::new("Dummy")) + .add_child(dummy_scene); + + commands.entity(dummy_spawn_point_entity).despawn(); + println!("Spawned dummy"); + } +} \ No newline at end of file diff --git a/src/comps/core/spawners/mod.rs b/src/comps/core/spawners/mod.rs index a4ff6eb..b26f1d0 100644 --- a/src/comps/core/spawners/mod.rs +++ b/src/comps/core/spawners/mod.rs @@ -3,3 +3,4 @@ pub mod item; pub mod player; pub mod spawn; pub mod spawn_point; +pub mod dummy; \ No newline at end of file diff --git a/src/comps/core/spawners/spawn.rs b/src/comps/core/spawners/spawn.rs index dcc009b..67f4fa8 100644 --- a/src/comps/core/spawners/spawn.rs +++ b/src/comps/core/spawners/spawn.rs @@ -3,13 +3,13 @@ use bevy::prelude::*; use super::{ item::{item_spawner, ItemSpawnPointPlugin}, player::player_spawner, - spawn_point::SpawnPointPlugin, + spawn_point::SpawnPointPlugin, dummy::dummy_spawner, }; 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)); + app.add_systems(Update, (player_spawner, item_spawner, dummy_spawner)); } } diff --git a/src/comps/core/spawners/spawn_point.rs b/src/comps/core/spawners/spawn_point.rs index e765c93..b66f919 100644 --- a/src/comps/core/spawners/spawn_point.rs +++ b/src/comps/core/spawners/spawn_point.rs @@ -1,6 +1,6 @@ use bevy::prelude::*; -use crate::comps::core::spawners::player::PlayerSpawnPoint; +use crate::comps::core::spawners::{player::PlayerSpawnPoint, dummy::DummySpawnPoint}; #[bevy_trait_query::queryable] pub trait SpawnPoint { @@ -12,5 +12,6 @@ impl Plugin for SpawnPointPlugin { fn build(&self, app: &mut App) { use bevy_trait_query::RegisterExt; app.register_component_as::(); + app.register_component_as::(); } } diff --git a/src/comps/core/weapons/attachments/attachment.rs b/src/comps/core/weapons/attachments/attachment.rs index e8eb545..d9c3623 100644 --- a/src/comps/core/weapons/attachments/attachment.rs +++ b/src/comps/core/weapons/attachments/attachment.rs @@ -8,6 +8,9 @@ pub trait Attachment { /// Starts at 1 fn current_attachment_index(&self) -> u32; fn all_attachments() -> Vec where Self: Sized; + /// Horizontal recoil % to remove. Additive + fn horizontal_recoil_modifier(&self) -> f32; + fn vertical_recoil_modifier(&self) -> f32; } /// Used to switch attachments. diff --git a/src/comps/core/weapons/attachments/compensator.rs b/src/comps/core/weapons/attachments/compensator.rs index afbcec2..a819f7b 100644 --- a/src/comps/core/weapons/attachments/compensator.rs +++ b/src/comps/core/weapons/attachments/compensator.rs @@ -20,4 +20,14 @@ impl Attachment for Compensator { fn all_attachments() -> Vec where Self: Sized { Vec::from([Self::FirstCompensator]) } + + fn horizontal_recoil_modifier(&self) -> f32 { + 0.12 + } + + fn vertical_recoil_modifier(&self) -> f32 { + 0.12 + } + + } \ No newline at end of file diff --git a/src/comps/core/weapons/attachments/foregrip.rs b/src/comps/core/weapons/attachments/foregrip.rs index bc5ce20..c6574dc 100644 --- a/src/comps/core/weapons/attachments/foregrip.rs +++ b/src/comps/core/weapons/attachments/foregrip.rs @@ -20,4 +20,12 @@ impl Attachment for ForeGrip { fn all_attachments() -> Vec where Self: Sized { Vec::from([Self::Pk5]) } + + fn horizontal_recoil_modifier(&self) -> f32 { + 0.06 + } + + fn vertical_recoil_modifier(&self) -> f32 { + 0.06 + } } \ No newline at end of file diff --git a/src/comps/core/weapons/attachments/magazine.rs b/src/comps/core/weapons/attachments/magazine.rs index d289ed2..39f1124 100644 --- a/src/comps/core/weapons/attachments/magazine.rs +++ b/src/comps/core/weapons/attachments/magazine.rs @@ -33,4 +33,12 @@ impl Attachment for Magazine { fn all_attachments() -> Vec where Self: Sized { Vec::from([Self::Ak105]) } + + fn horizontal_recoil_modifier(&self) -> f32 { + 0.0 + } + + fn vertical_recoil_modifier(&self) -> f32 { + 0.0 + } } \ No newline at end of file diff --git a/src/comps/core/weapons/attachments/optic.rs b/src/comps/core/weapons/attachments/optic.rs index 2a159be..a8d5f3c 100644 --- a/src/comps/core/weapons/attachments/optic.rs +++ b/src/comps/core/weapons/attachments/optic.rs @@ -64,5 +64,13 @@ impl Attachment for Optic { fn all_attachments() -> Vec where Self: Sized { Vec::from([Self::AimpointT1]) } + + fn horizontal_recoil_modifier(&self) -> f32 { + 0.0 + } + + fn vertical_recoil_modifier(&self) -> f32 { + 0.0 + } } diff --git a/src/comps/core/weapons/attachments/silencer.rs b/src/comps/core/weapons/attachments/silencer.rs index 914fefd..c393e7a 100644 --- a/src/comps/core/weapons/attachments/silencer.rs +++ b/src/comps/core/weapons/attachments/silencer.rs @@ -20,4 +20,12 @@ impl Attachment for Silencer { fn all_attachments() -> Vec where Self: Sized { Vec::from([Self::FirstSilencer]) } + + fn horizontal_recoil_modifier(&self) -> f32 { + 0.10 + } + + fn vertical_recoil_modifier(&self) -> f32 { + 0.10 + } } \ No newline at end of file diff --git a/src/comps/core/weapons/attachments/stock.rs b/src/comps/core/weapons/attachments/stock.rs index dbea646..0c945cf 100644 --- a/src/comps/core/weapons/attachments/stock.rs +++ b/src/comps/core/weapons/attachments/stock.rs @@ -20,4 +20,12 @@ impl Attachment for Stock { fn all_attachments() -> Vec where Self: Sized { Vec::from([Self::MagpullTan]) } + + fn horizontal_recoil_modifier(&self) -> f32 { + 0.23 + } + + fn vertical_recoil_modifier(&self) -> f32 { + 0.23 + } } \ No newline at end of file diff --git a/src/comps/core/weapons/attachments/weapon_attachment.rs b/src/comps/core/weapons/attachments/weapon_attachment.rs index ca332a8..8a78e98 100644 --- a/src/comps/core/weapons/attachments/weapon_attachment.rs +++ b/src/comps/core/weapons/attachments/weapon_attachment.rs @@ -23,20 +23,20 @@ impl Default for WeaponAttachment { impl WeaponAttachment { pub fn vertical_recoil_modifier(&self) -> f32 { match self { - WeaponAttachment::Compensator(_) => 0.0, - WeaponAttachment::Magazine(_) => 0.0, - WeaponAttachment::Stock(_) => 0.0, - WeaponAttachment::ForeGrip(_) => 0.0, - WeaponAttachment::Optic(_) => 0.0, + WeaponAttachment::Compensator(comp) => comp.vertical_recoil_modifier(), + WeaponAttachment::Magazine(mag) => mag.vertical_recoil_modifier(), + WeaponAttachment::Stock(stock) => stock.vertical_recoil_modifier(), + WeaponAttachment::ForeGrip(foregrip) => foregrip.vertical_recoil_modifier(), + WeaponAttachment::Optic(optic) => optic.vertical_recoil_modifier(), } } pub fn horizontal_recoil_modifier(&self) -> f32 { match self { - WeaponAttachment::Compensator(_) => 0.0, - WeaponAttachment::Magazine(_) => 0.0, - WeaponAttachment::Stock(_) => 0.0, - WeaponAttachment::ForeGrip(_) => 0.0, - WeaponAttachment::Optic(_) => 0.0, + WeaponAttachment::Compensator(comp) => comp.horizontal_recoil_modifier(), + WeaponAttachment::Magazine(mag) => mag.horizontal_recoil_modifier(), + WeaponAttachment::Stock(stock) => stock.horizontal_recoil_modifier(), + WeaponAttachment::ForeGrip(foregrip) => foregrip.horizontal_recoil_modifier(), + WeaponAttachment::Optic(optic) => optic.horizontal_recoil_modifier(), } } pub fn asset_path<'a>(&self) -> &'a str { diff --git a/src/comps/core/weapons/caliber.rs b/src/comps/core/weapons/caliber.rs index 734e506..ab3a671 100644 --- a/src/comps/core/weapons/caliber.rs +++ b/src/comps/core/weapons/caliber.rs @@ -1,12 +1,17 @@ use bevy::reflect::Reflect; /// Unit = m/s -pub const MIN_LETHAL_BULLET_VELOCITY: f32 = 60.0; +/// When a bullet reaches this speed (213 fps == 64.92), it is no longer able to penetrate human bone. +/// +/// Quote: `The speed at which a projectile must travel to penetrate skin is 163 fps and to break bone is 213 fps, both of which are quite low, so other factors are more important in producing damage. (Belkin, 1978)` +/// +/// [Source](https://webpath.med.utah.edu/TUTORIAL/GUNS/GUNBLST.html#:~:text=The%20speed%20at%20which%20a,more%20important%20in%20producing%20damage.) +pub const MIN_LETHAL_BULLET_VELOCITY: f32 = 64.92; -#[allow(unused)] #[derive(Clone, Reflect, Default)] pub enum Caliber { /// 5.56x45mm + /// Reference [Wikipedia](https://en.wikipedia.org/wiki/5.56%C3%9745mm_NATO) NATO556, /// 9x19mm Parabellum (NATO) /// Reference: [Wikipedia](https://en.wikipedia.org/wiki/9%C3%9719mm_Parabellum) @@ -36,30 +41,19 @@ impl Caliber { Caliber::RU545 => 0.00343, } } - pub fn linear_damping(&self) -> f32 { - match self { - Caliber::NATO556 => 1.0, - Caliber::NATOParabellum9mm => 2.0, - Caliber::RU545 => 1.5, - } - } - pub fn angular_damping(&self) -> f32 { - match self { - Caliber::NATO556 => 1.0, - Caliber::NATOParabellum9mm => 1.0, - Caliber::RU545 => 1.0, - } - } + /// Unit = meters + /// Real life value (BULLET DIAMETER/2) pub fn size(&self) -> f32 { match self { Caliber::NATO556 => 0.07, Caliber::NATOParabellum9mm => 0.05, - Caliber::RU545 => 0.06 + Caliber::RU545 => 0.0056/2.0 } } /// Unit = m/s - /// Real life value + /// Real life value (Muzzle velocity) + /// (TODO: Factor in the barrel that it is being shot from) pub fn muzzle_velocity(&self) -> f32 { match self { Caliber::NATO556 => 991.0, diff --git a/src/comps/core/weapons/firearm_state.rs b/src/comps/core/weapons/firearm_state.rs index 86115ad..daeb336 100644 --- a/src/comps/core/weapons/firearm_state.rs +++ b/src/comps/core/weapons/firearm_state.rs @@ -55,4 +55,23 @@ impl FirearmState { None => None, } } + + pub fn get_horizontal_recoil_modifier(&self) -> f32 { + let mut total = 0.0; + self.attachment_slots.iter().for_each(|attachment| { + if let Some(attachment) = &attachment.attachment { + total += attachment.horizontal_recoil_modifier() + } + }); + 1.0 - total + } + pub fn get_vertical_recoil_modifier(&self) -> f32 { + let mut total = 0.0; + self.attachment_slots.iter().for_each(|attachment| { + if let Some(attachment) = &attachment.attachment { + total += attachment.vertical_recoil_modifier() + } + }); + 1.0 - total + } } \ No newline at end of file diff --git a/src/logic/core/collisions.rs b/src/logic/core/collisions.rs index 85659a4..d2fccb0 100644 --- a/src/logic/core/collisions.rs +++ b/src/logic/core/collisions.rs @@ -9,17 +9,15 @@ use bevy::{prelude::*, ecs::system::SystemParam}; use bevy_rapier3d::{pipeline::CollisionEvent, rapier::geometry::CollisionEventFlags, geometry::Sensor}; -use crate::comps::core::{markers::{bullet::BulletMarker, proxy::weapons::gun_colliders::GunFirearmCollider}, events::{bullet_collision::BulletCollisionEvent, equipped_gun_collision::EquippedGunCollisionEvent, collision_event_type::CollisionEventType}}; +use crate::comps::core::{markers::{bullet::BulletMarker, proxy::weapons::gun_colliders::GunFirearmCollider}, events::{equipped_gun_collision::EquippedGunCollisionEvent, collision_event_type::CollisionEventType}}; #[derive(SystemParam)] pub struct CollisionHandlerQueryParams<'w, 's> { - bullets: Query<'w, 's, (Entity, &'static Transform), With>, firearms: Query<'w, 's, Entity, (With, With, Without)>, } #[derive(SystemParam)] pub struct CollisionHandlerEventParams<'w> { - bullet_collision_events: EventWriter<'w, BulletCollisionEvent>, equipped_gun_collision_events: EventWriter<'w, EquippedGunCollisionEvent>, } @@ -62,25 +60,5 @@ pub fn collision_handler( } continue; // Continue, as we already handled sensor collisions } - // ###### - // Bullet collisions - // ###### - for (bullet, bullet_transform) in queries.bullets.iter() { - if entity_a == &bullet { - events.bullet_collision_events.send(BulletCollisionEvent { - bullet, - other: *entity_b, - at: *bullet_transform, - collision_type, - }); - } else if entity_b == &bullet { - events.bullet_collision_events.send(BulletCollisionEvent { - bullet, - other: *entity_a, - at: *bullet_transform, - collision_type, - }); - } - } } } \ No newline at end of file diff --git a/src/logic/core/guns/reticle.rs b/src/logic/core/guns/reticle.rs index 56307bb..a154b3d 100644 --- a/src/logic/core/guns/reticle.rs +++ b/src/logic/core/guns/reticle.rs @@ -1,39 +1,58 @@ -use bevy::prelude::*; +use bevy::{prelude::*, ecs::system::SystemParam}; -use crate::comps::core::{markers::proxy::weapons::{optic_sight_glass::OpticSightGlass, reticle::DrawnReticle}, weapons::firearm_state::FirearmState}; +use crate::comps::core::{markers::{proxy::weapons::{optic_sight_glass::OpticSightGlass, reticle::DrawnReticle}, camera::MainCamera}, weapons::{firearm_state::FirearmState, parts::firing_point::FiringPoint}}; + +#[derive(SystemParam)] +pub struct UpdateReticleQueryParams<'w, 's> { + sight_query_added: Query<'w, 's, (Entity, &'static OpticSightGlass), Added>, + sight_query: Query<'w, 's, (Entity, &'static OpticSightGlass)>, + reticle_query: Query<'w, 's, (&'static Parent, &'static Handle, &'static DrawnReticle, &'static mut Transform),>, + firearm_state: Query<'w, 's, &'static FirearmState, Changed>, + firing_point_query: Query<'w, 's, &'static GlobalTransform, (With, Without)>, + main_camera_query: Query<'w, 's, (&'static Camera, &'static GlobalTransform), (With, Without, Without)>, +} /// How? /// Achieve parallax on all reticles by drawing a line straight out of the barrel and a straight line from the sight that intersected at the specified zeroed range for the optic. /// Make sure that the reticle is where the bullets will land at the specified zeroed range. pub fn update_reticle( mut commands: Commands, // TODO: make this identify which optic is on the gun, but meanwhile just start doing it to all the optics in - sight_query_added: Query<(Entity, &OpticSightGlass), Added>, - sight_query: Query<(Entity, &OpticSightGlass)>, - reticle_query: Query<(&Parent, &Handle, &DrawnReticle),>, - firearm_state: Query<&FirearmState, Changed>, + mut query: UpdateReticleQueryParams, mut meshes: ResMut>, mut materials: ResMut>, ) { // Spawn reticles - for (entity, sight) in sight_query_added.iter() { + for (entity, sight) in query.sight_query_added.iter() { let reticle = sight.0.reticle(); reticle.spawn(&mut commands, entity, &mut meshes, &mut materials); - println!("Added reticle"); } // Modify reticle brightness - - for (optic_entity, optic_sight_glass) in sight_query.iter() { - for (reticle_parent, reticle_material, _) in reticle_query.iter() { + for (optic_entity, optic_sight_glass) in query.sight_query.iter() { + for (reticle_parent, reticle_material, _, mut reticle_transform) in query.reticle_query.iter_mut () { if optic_entity != reticle_parent.get() { continue; } - for firearm_state in firearm_state.iter() { + for firearm_state in query.firearm_state.iter() { if let Some(ref optic_data) = firearm_state.optic_data { if let Some(mat) = materials.get_mut(reticle_material) { mat.emissive = optic_sight_glass.0.reticle().get_color() * optic_data.reticle_brightness; } } } + for firing_point_transform in query.firing_point_query.iter() { + for (cam, cam_transform) in query.main_camera_query.iter() { + let point_at_range = firing_point_transform.translation() + firing_point_transform.forward() * 50.0; + /*match cam.world_to_viewport(cam_transform, point_at_range) { + Some(_) => todo!(), + None => todo!(), + }; + + + reticle_transform.translation.x = point_at_range.x; + reticle_transform.translation.y = point_at_range.y;*/ + } + + } } } } \ No newline at end of file diff --git a/src/logic/core/guns/shoot.rs b/src/logic/core/guns/shoot.rs index 50082c5..94b0104 100644 --- a/src/logic/core/guns/shoot.rs +++ b/src/logic/core/guns/shoot.rs @@ -92,6 +92,6 @@ pub fn spawn_bullet( }, RigidBody::KinematicPositionBased, ActiveEvents::COLLISION_EVENTS, - CollisionGroups::new(Group::from_bits_retain(ColliderFlags::BULLETS.bits()), Group::from_bits_retain((ColliderFlags::SOLIDS | ColliderFlags::FLOOR).bits())), + CollisionGroups::new(Group::from_bits_retain(ColliderFlags::BULLETS.bits()), Group::from_bits_retain((ColliderFlags::SOLIDS | ColliderFlags::FLOOR | ColliderFlags::ENEMY).bits())), )); } diff --git a/src/logic/core/guns/update_bullet.rs b/src/logic/core/guns/update_bullet.rs index bcb7a50..9f7b949 100644 --- a/src/logic/core/guns/update_bullet.rs +++ b/src/logic/core/guns/update_bullet.rs @@ -53,7 +53,7 @@ pub fn update_bullet( }, material: materials.add(StandardMaterial { base_color: Color::RED, - emissive: Color::RED, + emissive: Color::RED * 10.0, ..Default::default() }), visibility: Visibility::Visible, diff --git a/src/logic/core/player/hands.rs b/src/logic/core/player/hands.rs index 5f65b17..1e07924 100644 --- a/src/logic/core/player/hands.rs +++ b/src/logic/core/player/hands.rs @@ -60,6 +60,8 @@ pub fn capture_hand_usage( mut equipment_change_event_writer: EventWriter, mut inventory_changed_events: EventWriter, mut mouse_wheel_events: EventReader, + + mut gizmos: Gizmos, ) { if !resources.game_load_state.player_loaded { return; @@ -245,7 +247,9 @@ pub fn capture_hand_usage( if resources.mouse_buttons.pressed(MouseButton::Left) // Shooting (LClick) && !resources.game_ui_state.any_window() && player_firing_info.gun_ready_pose == GunReadyPose::HighReady { - if let Some(magazine_data) = &mut firearm_state.magazine_data { + let firearm_vert_recoil_modifier = firearm_state.get_vertical_recoil_modifier(); + let firearm_horiz_recoil_modifier = firearm_state.get_horizontal_recoil_modifier(); + if let Some(ref mut magazine_data) = firearm_state.magazine_data { if player_firing_info.full_auto_timer.finished() { if magazine_data.rounds_shot < magazine_data.max_capacity { // Get recoil numbers from patterns @@ -284,16 +288,24 @@ pub fn capture_hand_usage( player_firing_info.full_auto_timer.reset(); magazine_data.rounds_shot += 1; + + let crouch_recoil_modifier = if player_linear_xz_state.is_crouched() { + 0.6 + } else { + 1.0 + }; // Apply recoil in_hand_transform.rotate_x(radians_from_degrees( - //TODO: Recoil modifier firearm_data.vertical_recoil_modifier -1.0 * 2.0 - * vertical_recoil_number, + * vertical_recoil_number + * firearm_horiz_recoil_modifier + * crouch_recoil_modifier, )); in_hand_transform.rotate_y(radians_from_degrees( - //TODO: Recoil modifier firearm_data.horizontal_recoil_modifier 1.0 * 0.5 - * horizontal_recoil_number, + * horizontal_recoil_number + * firearm_vert_recoil_modifier + * crouch_recoil_modifier, )); } } @@ -308,6 +320,11 @@ pub fn capture_hand_usage( player_firing_info.current_round_index = 0; } } + if player_firing_info.gun_ready_pose == GunReadyPose::HighReady { + for (firing_point_transform, firing_point_parent) in queries.firing_point_query.iter() { + gizmos.line(firing_point_transform.translation(), firing_point_transform.forward() * 100000.0, Color::RED); + } + } } } } diff --git a/src/main.rs b/src/main.rs index e945ae1..894f492 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ fn setup_plugins(application: &mut App) { })) //.add_plugins(DefaultInspectorConfigPlugin) .add_plugins(RapierPhysicsPlugin::::default()) - //.add_plugins(RapierDebugRenderPlugin::default()) + .add_plugins(RapierDebugRenderPlugin::default()) .add_plugins(ComponentsFromGltfPlugin) //.add_plugins(bevy_egui::EguiPlugin) //.add_plugins(WorldInspectorPlugin::new()) diff --git a/src/scenes/scene1/spawn_points.rs b/src/scenes/scene1/spawn_points.rs index 76f5571..ecc746c 100644 --- a/src/scenes/scene1/spawn_points.rs +++ b/src/scenes/scene1/spawn_points.rs @@ -5,7 +5,7 @@ use crate::{ markers::player::{Player, PlayerData}, spawners::{ guns::ak105_spawner::Ak105SpawnPoint, - player::PlayerSpawnPoint, + player::PlayerSpawnPoint, dummy::DummySpawnPoint, }, }, utils, @@ -30,6 +30,10 @@ pub fn set_spawn_points(mut commands: Commands) { transform }, });*/ + + commands.spawn(DummySpawnPoint { + transform: Transform::from_xyz(15.0, 0.1, 20.0), + }); commands.spawn(Ak105SpawnPoint::new_fully_kitted_default({ let mut transform = Transform::from_xyz(18.0, 10.0, 18.0); diff --git a/src/setup/assets.rs b/src/setup/assets.rs index 7a77d3b..8149f74 100644 --- a/src/setup/assets.rs +++ b/src/setup/assets.rs @@ -7,8 +7,7 @@ pub enum GltfAssetType { Firearm(Firearm), Attachment(WeaponAttachment), Character, - #[allow(unused)] - Enemy, + OtherPlayer, } #[derive(Resource, Reflect, Default)] @@ -87,11 +86,16 @@ pub fn load_all_assets( asset_type: GltfAssetType::Character, asset: asset_server.load("character/main_character.glb"), }; + let other_player_character_asset = GltfAsset { + asset_type: GltfAssetType::OtherPlayer, + asset: asset_server.load("character/other_player_character.glb"), + }; commands.insert_resource(GltfAssets { assets: Vec::from([/*m4a1_gltf_asset, */ ak105_gltf_asset, glock17_gltf_asset, // Firearms main_character_asset, // Characters + other_player_character_asset, // Enemies aimpoint_t1_gltf_asset, pk5_foregrip_gltf_asset, ak105_magazine_gltf_asset, first_compensator_gltf_asset, tan_magpull_stock_gltf_asset // Attachments ]), }); diff --git a/src/setup/equipment.rs b/src/setup/equipment.rs index 1597029..aa7d8d0 100644 --- a/src/setup/equipment.rs +++ b/src/setup/equipment.rs @@ -43,38 +43,40 @@ pub fn change_equipment( loaded_gltf_assets: Res>, ) { for equipment_change_event in equipment_change_event_reader.read() { - let Ok((mut player, player_firing_info)) = player_query.get_single_mut() else { + let Ok((mut player, mut player_firing_info)) = player_query.get_single_mut() else { return; }; //if player_firing_info.is_reloading { return; } // Primary firearm change - let player_hands = player_hands_query.single_mut(); - - commands.entity(player_hands).despawn_descendants(); // TODO: Don't do this without keeping the state from the last mag + for player_hands in player_hands_query.iter_mut() { + commands.entity(player_hands).despawn_descendants(); // TODO: Don't do this without keeping the state from the last mag - if let Equipment::Firearm(new_firearm, firearm_state) = equipment_change_event.0.clone() { - // Add sensor to firearm collider if it is a child of player_hands - spawn_firearm_on_player_hands( - &mut commands, - player_firing_info, - player_hands, - &assets_gltf, - &loaded_gltf_assets, - new_firearm, - firearm_state, - equipment_change_event.1.clone().expect("Spawning firearm on player hands without an ItemId"), - ); + if let Equipment::Firearm(new_firearm, firearm_state) = equipment_change_event.0.clone() { + // Add sensor to firearm collider if it is a child of player_hands + spawn_firearm_on_player_hands( + &mut commands, + &mut player_firing_info, + player_hands, + &assets_gltf, + &loaded_gltf_assets, + new_firearm, + firearm_state, + equipment_change_event.1.clone().expect("Spawning firearm on player hands without an ItemId"), + ); + } + + // Set the player's equipment to the newly spawned equipment + player.0.equipment = equipment_change_event.0.clone(); + println!("Equipment change done"); } - // Set the player's equipment to the newly spawned equipment - player.0.equipment = equipment_change_event.0.clone(); - println!("Equipment change done"); + } } fn spawn_firearm_on_player_hands( commands: &mut Commands, - mut player_firing_info: Mut, + player_firing_info: &mut PlayerFiringInfo, player_hands: Entity, assets_gltf: &GltfAssets, loaded_gltf_assets: &Assets, diff --git a/src/ui/game/inspect/menu.rs b/src/ui/game/inspect/menu.rs index 4e3eba7..5082f72 100644 --- a/src/ui/game/inspect/menu.rs +++ b/src/ui/game/inspect/menu.rs @@ -74,12 +74,10 @@ pub fn update_inspect_screen( for player_firing_info in player_firing_info_query.iter() { if player_firing_info.is_inspecting { *inspect_screen_visibility = Visibility::Visible; - // TODO: Build for (camera, camera_transform) in camera_query.iter() { for (mut inspect_slot_visibility, mut inspect_slot, mut inspect_slot_style, mut inspect_slot_border_color) in inspect_slots_query.iter_mut() { for (slot, slot_transform) in slots_query.iter() { if &inspect_slot.slot == slot { - //inspect_slot_text.sections.first_mut().expect("No section in an already created text...").value = format!("{}", slot.get_index()); match camera.world_to_viewport(camera_transform, slot_transform.translation()) { Some(pos_vec) => { *inspect_slot_visibility = Visibility::Visible;