From f78d4c7f4042d9753f97e6cc19b6eb470f673f4c Mon Sep 17 00:00:00 2001 From: Franklin Date: Sat, 25 Nov 2023 14:39:02 -0400 Subject: [PATCH] EventCollision system that takes into account more than one type of collider AND Auto Low ready when gun collider hits object OR when player starts sprinting --- Design.md | 8 +- src/comps/core/events/bullet_collision.rs | 11 +++ src/comps/core/events/collision_event_type.rs | 6 ++ .../core/events/equipped_gun_collision.rs | 10 +++ src/comps/core/events/mod.rs | 3 + .../markers/proxy/weapons/gun_colliders.rs | 14 +-- src/logic/core/collisions.rs | 87 +++++++++++++++++++ src/logic/core/guns/despawn_shots.rs | 80 +++++++---------- .../core/guns/equipped_gun_collisions.rs | 27 ++++++ src/logic/core/guns/mod.rs | 3 +- src/logic/core/guns/player_firing.rs | 4 + src/logic/core/mod.rs | 1 + src/logic/core/player/hands.rs | 3 +- src/scenes/scene1/init.rs | 10 ++- 14 files changed, 202 insertions(+), 65 deletions(-) create mode 100644 src/comps/core/events/bullet_collision.rs create mode 100644 src/comps/core/events/collision_event_type.rs create mode 100644 src/comps/core/events/equipped_gun_collision.rs create mode 100644 src/logic/core/collisions.rs create mode 100644 src/logic/core/guns/equipped_gun_collisions.rs diff --git a/Design.md b/Design.md index 1a3da2f..2c29815 100644 --- a/Design.md +++ b/Design.md @@ -31,14 +31,18 @@ Multiplayer - [x] High Ready & Low Ready system with state - [x] High ready animation (procedural) - [x] Low ready animation (procedural) - - [ ] TODO: Auto Low ready when gun collider hits object OR when player starts sprinting +- [x] EventCollision system that takes into account more than one type of collider. + - [x] Auto Low ready when gun collider hits object OR when player starts sprinting + - [ ] (Not a priority) Collision with wall and attempting to Aim can look very twitchy, maybe add a cooldown of 0.1-0.4s on each change from low ready to high ready +- [ ] Optics + - [ ] All optics implementing a fn/trait that gives a specific gun offset to use the optic correctly + - [ ] Find some way to implement a shader for the optics - [ ] Bobbing - [ ] Gun Bob on run - [ ] Gun Bob on walk - [ ] Reload animation (procedural) - [ ] Real world magazines - [ ] Rewriting bullet physics to use raycasts & kinematic rigidbodies (logic controlled) - - [ ] Create a Controls struct that holds mappings to all the game keys and replace them in all the game's code diff --git a/src/comps/core/events/bullet_collision.rs b/src/comps/core/events/bullet_collision.rs new file mode 100644 index 0000000..77f9e2d --- /dev/null +++ b/src/comps/core/events/bullet_collision.rs @@ -0,0 +1,11 @@ +use bevy::{ecs::{event::Event, entity::Entity}, transform::components::Transform}; + +use super::collision_event_type::CollisionEventType; + +#[derive(Event)] +pub struct BulletCollisionEvent { + pub bullet: Entity, + pub other: Entity, + pub at: Transform, + pub collision_type: CollisionEventType, +} \ No newline at end of file diff --git a/src/comps/core/events/collision_event_type.rs b/src/comps/core/events/collision_event_type.rs new file mode 100644 index 0000000..37980bc --- /dev/null +++ b/src/comps/core/events/collision_event_type.rs @@ -0,0 +1,6 @@ + +#[derive(PartialEq, Copy, Clone)] +pub enum CollisionEventType { + Start, + End, +} \ No newline at end of file diff --git a/src/comps/core/events/equipped_gun_collision.rs b/src/comps/core/events/equipped_gun_collision.rs new file mode 100644 index 0000000..45362ca --- /dev/null +++ b/src/comps/core/events/equipped_gun_collision.rs @@ -0,0 +1,10 @@ +use bevy::ecs::{event::Event, entity::Entity}; + +use super::collision_event_type::CollisionEventType; + +#[derive(Event)] +pub struct EquippedGunCollisionEvent { + pub gun: Entity, + pub other: Entity, + pub collision_type: CollisionEventType, +} \ No newline at end of file diff --git a/src/comps/core/events/mod.rs b/src/comps/core/events/mod.rs index aa3a379..a157e27 100644 --- a/src/comps/core/events/mod.rs +++ b/src/comps/core/events/mod.rs @@ -1,3 +1,6 @@ //pub mod loot_container; pub mod inventory_changed; pub mod pickup_item; +pub mod bullet_collision; +pub mod equipped_gun_collision; +pub mod collision_event_type; \ No newline at end of file diff --git a/src/comps/core/markers/proxy/weapons/gun_colliders.rs b/src/comps/core/markers/proxy/weapons/gun_colliders.rs index 4d281ca..c022a28 100644 --- a/src/comps/core/markers/proxy/weapons/gun_colliders.rs +++ b/src/comps/core/markers/proxy/weapons/gun_colliders.rs @@ -1,5 +1,5 @@ use bevy::prelude::*; -use bevy_rapier3d::geometry::Sensor; +use bevy_rapier3d::geometry::{Sensor, ActiveEvents}; use crate::{comps::core::markers::holdable::InPlayerHands, utils::hierarchy::find_child_in_parent_children}; @@ -9,14 +9,6 @@ use crate::{comps::core::markers::holdable::InPlayerHands, utils::hierarchy::fin #[reflect(Component)] pub struct GunFirearmCollider; - -/*for (gun_firearm_collider_entity, _) in gun_firearm_collider_query.iter() { - println!("a"); - if find_child_in_parent_children(commands, firearm_asset_entity, gun_firearm_collider_entity, &children) { - println!("Sensor"); - commands.entity(gun_firearm_collider_entity).insert(Sensor); - } - } */ pub fn update_gun_collider( mut commands: Commands, gun_firearm_collider_query: Query>, @@ -27,11 +19,11 @@ pub fn update_gun_collider( let mut found = false; for in_player_hands_entity in in_player_hands_query.iter() { if find_child_in_parent_children(&mut commands, in_player_hands_entity, gun_firearm_collider_entity, &children) { - commands.entity(gun_firearm_collider_entity).insert(Sensor); + commands.entity(gun_firearm_collider_entity).insert(Sensor).insert(ActiveEvents::COLLISION_EVENTS); found = true; } } if found { continue; } - commands.entity(gun_firearm_collider_entity).remove::(); + commands.entity(gun_firearm_collider_entity).remove::().remove::(); } } \ No newline at end of file diff --git a/src/logic/core/collisions.rs b/src/logic/core/collisions.rs new file mode 100644 index 0000000..bdbaea4 --- /dev/null +++ b/src/logic/core/collisions.rs @@ -0,0 +1,87 @@ +//! +//! The goal of this module is to provide collision hydration to all other systems in the game. +//! The reason is, well, rapier does not give a way to read collisions in multiple systems. +//! If you read a collision event you automatically starve the systems relying on it. +//! +//! Have Different collision events for different systems, don't call the system directly, just use events to pass in collisions. +//! + +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}}; + +#[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>, +} + +pub fn collision_handler( + queries: CollisionHandlerQueryParams, + mut events: CollisionHandlerEventParams, + mut collisions: EventReader, + //time: Res