New bullet system #1

Merged
franklinblanco merged 2 commits from new_bullet_system into master 2023-11-28 14:45:31 +00:00
8 changed files with 52 additions and 29 deletions
Showing only changes of commit 1962d4d325 - Show all commits

View File

@ -1,14 +1,16 @@
use bevy::{
prelude::{Component, Vec3},
reflect::Reflect,
time::Timer,
time::Timer, ecs::reflect::ReflectComponent,
};
use crate::comps::core::weapons::caliber::Caliber;
#[derive(Component, Reflect)]
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
pub struct BulletMarker {
pub caliber: Caliber,
pub starting_point: Vec3,
pub timer: Timer,
pub current_velocity: Vec3,
}

View File

@ -1,13 +1,15 @@
use bevy::reflect::Reflect;
#[allow(unused)]
#[derive(Clone, Reflect)]
#[derive(Clone, Reflect, Default)]
pub enum Caliber {
/// 5.56x45mm
NATO556,
/// 9x19mm
#[default]
Parabellum9mm,
/// 5.45x39mm
/// 5.45x39mm (7N6M)
/// Reference: [Wikipedia](https://en.wikipedia.org/wiki/5.45%C3%9739mm)
RU545
}
impl Caliber {
@ -32,11 +34,12 @@ impl Caliber {
Caliber::RU545 => 75.0,
}
}
/// Unit = kg
pub fn mass(&self) -> f32 {
match self {
Caliber::NATO556 => 0.1,
Caliber::NATO556 => 0.00402,
Caliber::Parabellum9mm => 0.05,
Caliber::RU545 => 0.07,
Caliber::RU545 => 0.00343,
}
}
pub fn linear_damping(&self) -> f32 {
@ -60,4 +63,20 @@ impl Caliber {
Caliber::RU545 => 0.06
}
}
/// Unit = m/s
pub fn muzzle_velocity(&self) -> f32 {
match self {
Caliber::NATO556 => 991.0,
Caliber::Parabellum9mm => todo!(),
Caliber::RU545 => 880.0,
}
}
/// Unit = m/s
pub fn velocity_shed(&self) -> f32 {
match self {
Caliber::NATO556 => todo!(),
Caliber::Parabellum9mm => todo!(),
Caliber::RU545 => 120.0,
}
}
}

View File

@ -10,7 +10,6 @@ pub fn update_gun_position_on_collision(
mut events: EventReader<EquippedGunCollisionEvent>,
) {
for event in events.read() {
println!("Collision");
for mut player_firing_info in query.iter_mut() {
match event.collision_type {
CollisionEventType::Start => {
@ -19,7 +18,6 @@ pub fn update_gun_position_on_collision(
},
CollisionEventType::End => {
player_firing_info.gun_colliding_with_object = false;
//player_firing_info.gun_ready_pose = GunReadyPose::HighReady;
},
}
}

View File

@ -2,4 +2,5 @@ pub mod despawn_shots;
pub mod player_firing;
pub mod shoot;
pub mod inspect;
pub mod equipped_gun_collisions;
pub mod equipped_gun_collisions;
pub mod update_bullet;

View File

@ -64,8 +64,6 @@ pub fn shoot_bullet(
meshes,
materials,
firing_point,
forward,
up,
caliber,
);
}
@ -75,8 +73,6 @@ pub fn spawn_bullet(
meshes: &mut ResMut<Assets<Mesh>>,
materials: &mut ResMut<Assets<StandardMaterial>>,
firing_point: Transform,
forward: Vec3,
_up: Vec3,
caliber: Caliber,
) {
commands.spawn((
@ -88,6 +84,7 @@ pub fn spawn_bullet(
Duration::from_secs_f32(caliber.max_airtime_secs()),
TimerMode::Once,
),
current_velocity: firing_point.forward() * caliber.muzzle_velocity(),
},
MaterialMeshBundle {
mesh: {
@ -113,21 +110,8 @@ pub fn spawn_bullet(
transform: firing_point,
..Default::default()
},
RigidBody::Dynamic,
GravityScale(4.0),
Collider::ball(caliber.size()),
Velocity::zero(),
Damping {
linear_damping: caliber.linear_damping(),
angular_damping: caliber.angular_damping(),
},
ColliderMassProperties::Mass(caliber.mass()),
ExternalImpulse {
impulse: forward * caliber.impulse(),
torque_impulse: Vec3::ZERO,
},
RigidBody::KinematicPositionBased,
ActiveEvents::COLLISION_EVENTS,
CollisionGroups::new(Group::from_bits_retain(ColliderFlags::BULLETS.bits()), Group::from_bits_retain((ColliderFlags::SOLIDS | ColliderFlags::FLOOR).bits())),
Ccd::enabled(),
));
}

View File

@ -0,0 +1,17 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
use crate::comps::core::markers::bullet::BulletMarker;
pub fn update_bullet(
mut commands: Commands,
mut bullets: Query<(Entity, &mut BulletMarker, &mut Transform)>,
time: Res<Time>,
) {
for (bullet, mut marker, mut transform) in bullets.iter_mut() {
transform.translation += (marker.current_velocity * time.delta_seconds()) + (Vec3 { x: 0.0, y: -9.81, z: 0.0 } * time.delta_seconds() * marker.caliber.mass());
let vel_shed = marker.caliber.velocity_shed();
marker.current_velocity -= transform.forward() * vel_shed * time.delta_seconds();
}
}

View File

@ -7,7 +7,7 @@ use crate::{
spawners::{player::player_spawner, spawn::SpawnerPlugin}, events::{bullet_collision::BulletCollisionEvent, equipped_gun_collision::EquippedGunCollisionEvent},
},
logic::core::{
guns::{despawn_shots::{despawn_muzzle_flashes, despawn_stray_bullets}, inspect::inspect_firearm, equipped_gun_collisions::update_gun_position_on_collision},
guns::{despawn_shots::{despawn_muzzle_flashes, despawn_stray_bullets}, inspect::inspect_firearm, equipped_gun_collisions::update_gun_position_on_collision, update_bullet::update_bullet},
player::{
camera_player_sync::{
follow_cursor_with_camera, update_camera_vertical_position, MouseMovementSettings,
@ -62,6 +62,7 @@ pub fn load_scene(application: &mut App) {
application.add_systems(Update, inspect_firearm);
application.add_systems(Update, collision_handler);
application.add_systems(Update, update_gun_position_on_collision);
application.add_systems(Update, update_bullet);
//application.add_systems(Update, animate_player);
//application.add_systems(Update, register_bullet_hits);

View File

@ -5,7 +5,7 @@ use bevy_inspector_egui::bevy_egui::EguiPlugin;
use crate::{
comps::core::{markers::{
holdable::{HoldableObjectData, InPlayerHands},
player::{Player, PlayerData, PlayerHand}, inspect_screen::InspectScreenSlotUiMarker,
player::{Player, PlayerData, PlayerHand}, inspect_screen::InspectScreenSlotUiMarker, bullet::BulletMarker,
}, weapons::{firearm_data::FirearmData, magazine_data::MagazineData, caliber::Caliber, firearm::Firearm, spray_pattern::FirearmSprayPattern, firearm_state::FirearmState}},
logic::core::{
guns::player_firing::PlayerFiringInfo,
@ -39,6 +39,7 @@ impl Plugin for MainEditorUiPlugin {
.register_type::<PlayerValuesState>()
.register_type::<FirearmData>()
.register_type::<MagazineData>()
.register_type::<BulletMarker>()
.register_type::<HoldableObjectData>()
.register_type::<InPlayerHands>()
.register_type::<PlayerData>()