Added caliber docs, refactored 9mm caliber name

This commit is contained in:
Franklin 2023-11-28 12:16:27 -04:00
parent 0a7f426ce2
commit dde59da879
6 changed files with 34 additions and 81 deletions

View File

@ -1,7 +1,6 @@
use bevy::{ use bevy::{
prelude::{Component, Vec3}, prelude::{Component, Vec3},
reflect::Reflect, reflect::Reflect, ecs::{reflect::ReflectComponent, entity::Entity},
time::Timer, ecs::{reflect::ReflectComponent, entity::Entity},
}; };
use crate::comps::core::weapons::caliber::Caliber; use crate::comps::core::weapons::caliber::Caliber;
@ -17,7 +16,6 @@ pub struct BulletHit {
pub struct BulletMarker { pub struct BulletMarker {
pub caliber: Caliber, pub caliber: Caliber,
pub starting_point: Vec3, pub starting_point: Vec3,
pub timer: Timer,
pub current_velocity: Vec3, pub current_velocity: Vec3,
pub hits: Vec<BulletHit>, pub hits: Vec<BulletHit>,
} }

View File

@ -1,82 +1,80 @@
use bevy::reflect::Reflect; use bevy::reflect::Reflect;
/// Unit = m/s
pub const MIN_LETHAL_BULLET_VELOCITY: f32 = 60.0;
#[allow(unused)] #[allow(unused)]
#[derive(Clone, Reflect, Default)] #[derive(Clone, Reflect, Default)]
pub enum Caliber { pub enum Caliber {
/// 5.56x45mm /// 5.56x45mm
NATO556, NATO556,
/// 9x19mm /// 9x19mm Parabellum (NATO)
/// Reference: [Wikipedia](https://en.wikipedia.org/wiki/9%C3%9719mm_Parabellum)
#[default] #[default]
Parabellum9mm, NATOParabellum9mm,
/// 5.45x39mm (7N6M) /// 5.45x39mm (7N6M)
/// Reference: [Wikipedia](https://en.wikipedia.org/wiki/5.45%C3%9739mm) /// Reference: [Wikipedia](https://en.wikipedia.org/wiki/5.45%C3%9739mm)
RU545 RU545
} }
impl Caliber { impl Caliber {
/// Unit = Meters
/// Not real for the moment as this is just to make sure the bullet despawns if it goes too far
pub fn range(&self) -> f32 { pub fn range(&self) -> f32 {
match self { match self {
Caliber::NATO556 => 7500.0, Caliber::NATO556 => 7500.0,
Caliber::Parabellum9mm => 2500.0, Caliber::NATOParabellum9mm => 2500.0,
Caliber::RU545 => 4500.0, Caliber::RU545 => 4500.0,
} }
} }
pub fn max_airtime_secs(&self) -> f32 {
match self {
Caliber::NATO556 => 3.0,
Caliber::Parabellum9mm => 3.0,
Caliber::RU545 => 3.0,
}
}
pub fn impulse(&self) -> f32 {
match self {
Caliber::NATO556 => 100.0,
Caliber::Parabellum9mm => 50.0,
Caliber::RU545 => 75.0,
}
}
/// Unit = kg /// Unit = kg
/// Real life value (BULLET WEIGHT)
pub fn mass(&self) -> f32 { pub fn mass(&self) -> f32 {
match self { match self {
Caliber::NATO556 => 0.00402, Caliber::NATO556 => 0.00402,
Caliber::Parabellum9mm => 0.05, Caliber::NATOParabellum9mm => 0.0052,
Caliber::RU545 => 0.00343, Caliber::RU545 => 0.00343,
} }
} }
pub fn linear_damping(&self) -> f32 { pub fn linear_damping(&self) -> f32 {
match self { match self {
Caliber::NATO556 => 1.0, Caliber::NATO556 => 1.0,
Caliber::Parabellum9mm => 2.0, Caliber::NATOParabellum9mm => 2.0,
Caliber::RU545 => 1.5, Caliber::RU545 => 1.5,
} }
} }
pub fn angular_damping(&self) -> f32 { pub fn angular_damping(&self) -> f32 {
match self { match self {
Caliber::NATO556 => 1.0, Caliber::NATO556 => 1.0,
Caliber::Parabellum9mm => 1.0, Caliber::NATOParabellum9mm => 1.0,
Caliber::RU545 => 1.0, Caliber::RU545 => 1.0,
} }
} }
pub fn size(&self) -> f32 { pub fn size(&self) -> f32 {
match self { match self {
Caliber::NATO556 => 0.07, Caliber::NATO556 => 0.07,
Caliber::Parabellum9mm => 0.05, Caliber::NATOParabellum9mm => 0.05,
Caliber::RU545 => 0.06 Caliber::RU545 => 0.06
} }
} }
/// Unit = m/s /// Unit = m/s
/// Real life value
pub fn muzzle_velocity(&self) -> f32 { pub fn muzzle_velocity(&self) -> f32 {
match self { match self {
Caliber::NATO556 => 991.0, Caliber::NATO556 => 991.0,
Caliber::Parabellum9mm => todo!(), Caliber::NATOParabellum9mm => 460.0,
Caliber::RU545 => 880.0, Caliber::RU545 => 880.0,
} }
} }
/// Unit = m/s /// Unit = m/s
/// Can't find accurate information for real bullets, so invented these values based on how good the wiki says they are long range.
pub fn velocity_shed(&self) -> f32 { pub fn velocity_shed(&self) -> f32 {
match self { match self {
Caliber::NATO556 => todo!(), Caliber::NATO556 => 150.0,
Caliber::Parabellum9mm => todo!(), Caliber::NATOParabellum9mm => 450.0,
Caliber::RU545 => 120.0, Caliber::RU545 => 300.0,
} }
} }
} }

View File

@ -62,7 +62,7 @@ impl Firearm {
}*/ }*/
Firearm::Glock17 => { Firearm::Glock17 => {
FirearmData { FirearmData {
caliber: Caliber::Parabellum9mm, caliber: Caliber::NATOParabellum9mm,
fire_rate: 600.0, fire_rate: 600.0,
aim_time_seconds: 0.1, aim_time_seconds: 0.1,
rebound_time_seconds: 0.5, rebound_time_seconds: 0.5,

View File

@ -1,4 +1,4 @@
use crate::comps::core::{markers::{bullet::BulletMarker, muzzle_flash::MuzzleFlashMarker}, events::{bullet_collision::BulletCollisionEvent, collision_event_type::CollisionEventType}}; use crate::comps::core::{markers::muzzle_flash::MuzzleFlashMarker, events::{bullet_collision::BulletCollisionEvent, collision_event_type::CollisionEventType}};
use bevy::prelude::*; use bevy::prelude::*;
pub fn despawn_muzzle_flashes( pub fn despawn_muzzle_flashes(
@ -18,18 +18,13 @@ pub fn despawn_stray_bullets(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
mut query: Query<(&mut BulletMarker, Entity, &Transform)>,
mut bullet_collisions: EventReader<BulletCollisionEvent>, mut bullet_collisions: EventReader<BulletCollisionEvent>,
time: Res<Time>,
) { ) {
let collisions_read: Vec<&BulletCollisionEvent> = bullet_collisions.read().collect(); let collisions_read: Vec<&BulletCollisionEvent> = bullet_collisions.read().collect();
for event in collisions_read.into_iter() { for event in collisions_read.into_iter() {
match event.collision_type { match event.collision_type {
CollisionEventType::Start => { CollisionEventType::Start => {
//commands.entity(event.bullet).remove::<Collider>();
//commands.entity(bullet_entity).insert(Sensor);
spawn_bullet_hit_marker( spawn_bullet_hit_marker(
&mut commands, &mut commands,
event.at.translation, event.at.translation,
@ -38,15 +33,6 @@ pub fn despawn_stray_bullets(
); );
}, },
CollisionEventType::End => { CollisionEventType::End => {
/*for (_, bullet_entity, _) in query.iter() {
if bullet_entity != event.bullet { continue; }
/*commands
.entity(event.bullet)
.insert(Collider::ball(bullet.caliber.size()));*/
}*/
//commands.entity(bullet_entity).remove::<Sensor>();
//commands.entity(*entity_b).despawn();
spawn_bullet_exit_marker( spawn_bullet_exit_marker(
&mut commands, &mut commands,
event.at.translation, event.at.translation,
@ -57,15 +43,6 @@ pub fn despawn_stray_bullets(
} //_ => {} } //_ => {}
} }
} }
for (mut bullet, bullet_entity, transform) in query.iter_mut() {
bullet.timer.tick(time.delta());
if bullet.timer.finished() {
commands.entity(bullet_entity).despawn();
}
if transform.translation.distance(bullet.starting_point) > bullet.caliber.range() {
commands.entity(bullet_entity).despawn();
}
}
} }
pub fn spawn_bullet_hit_marker( pub fn spawn_bullet_hit_marker(
@ -87,7 +64,6 @@ pub fn spawn_bullet_hit_marker(
}, },
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color: Color::GREEN, base_color: Color::GREEN,
//base_color_texture: Some(Color::GREEN),
emissive: Color::GREEN, emissive: Color::GREEN,
..Default::default() ..Default::default()
}), }),
@ -116,7 +92,6 @@ fn spawn_bullet_exit_marker(
}, },
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color: Color::RED, base_color: Color::RED,
//base_color_texture: Some(Color::GREEN),
emissive: Color::RED, emissive: Color::RED,
..Default::default() ..Default::default()
}), }),

View File

@ -38,26 +38,6 @@ pub fn shoot_bullet(
MuzzleFlashMarker(Timer::new(Duration::from_millis(10), TimerMode::Once)), MuzzleFlashMarker(Timer::new(Duration::from_millis(10), TimerMode::Once)),
)); ));
// Spawn Line
/*if player_settings.shot_lines_enabled {
commands.spawn(
MaterialMeshBundle {
mesh: {
let mut mesh = Mesh::new(PrimitiveTopology::LineStrip);
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, Vec::from([Vec3::ZERO, forward * caliber.range()]));
meshes.add(mesh)
},
material: materials.add(StandardMaterial {
base_color: Color::GREEN,
..Default::default()
}),
visibility: Visibility::Visible,
transform: firing_point,
..Default::default()
}
);
}*/
spawn_bullet( spawn_bullet(
commands, commands,
meshes, meshes,
@ -83,10 +63,6 @@ pub fn spawn_bullet(
BulletMarker { BulletMarker {
caliber: caliber.clone(), caliber: caliber.clone(),
starting_point: firing_point.translation, starting_point: firing_point.translation,
timer: Timer::new(
Duration::from_secs_f32(caliber.max_airtime_secs()),
TimerMode::Once,
),
current_velocity: forward * caliber.muzzle_velocity(), current_velocity: forward * caliber.muzzle_velocity(),
hits: Vec::new() hits: Vec::new()
}, },

View File

@ -1,7 +1,7 @@
use bevy::{prelude::*, render::render_resource::PrimitiveTopology, ecs::system::SystemParam}; use bevy::{prelude::*, render::render_resource::PrimitiveTopology, ecs::system::SystemParam};
use bevy_rapier3d::prelude::*; use bevy_rapier3d::prelude::*;
use crate::{comps::core::{markers::{bullet::{BulletMarker, BulletHit}, proxy::weapons::gun_colliders::GunFirearmCollider}, events::{bullet_collision::BulletCollisionEvent, collision_event_type::CollisionEventType}}, logic::core::player::player_settings::PlayerSettings}; use crate::{comps::core::{markers::{bullet::{BulletMarker, BulletHit}, proxy::weapons::gun_colliders::GunFirearmCollider}, events::{bullet_collision::BulletCollisionEvent, collision_event_type::CollisionEventType}, weapons::caliber::MIN_LETHAL_BULLET_VELOCITY}, logic::core::player::player_settings::PlayerSettings};
#[derive(SystemParam)] #[derive(SystemParam)]
pub struct UpdateBulletQueryParams<'w, 's> { pub struct UpdateBulletQueryParams<'w, 's> {
@ -20,6 +20,13 @@ pub fn update_bullet(
mut bullet_collision_event: EventWriter<BulletCollisionEvent>, mut bullet_collision_event: EventWriter<BulletCollisionEvent>,
) { ) {
for (bullet, mut marker, mut transform) in query.bullets.iter_mut() { for (bullet, mut marker, mut transform) in query.bullets.iter_mut() {
let normalized_vel = (marker.current_velocity.normalize() * MIN_LETHAL_BULLET_VELOCITY).abs();
if transform.translation.distance(marker.starting_point) > marker.caliber.range() || normalized_vel.x + normalized_vel.z > marker.current_velocity.abs().x + marker.current_velocity.abs().z {
commands.entity(bullet).despawn();
continue;
}
let future_frame_pos = (marker.current_velocity * time.delta_seconds()) + (Vec3 { x: 0.0, y: -9.81, z: 0.0 } * time.delta_seconds() * marker.caliber.mass()); let future_frame_pos = (marker.current_velocity * time.delta_seconds()) + (Vec3 { x: 0.0, y: -9.81, z: 0.0 } * time.delta_seconds() * marker.caliber.mass());
let max_toi = transform.translation.distance(future_frame_pos + transform.translation); let max_toi = transform.translation.distance(future_frame_pos + transform.translation);
@ -56,7 +63,6 @@ pub fn update_bullet(
); );
} }
// Raycast to future frame pos
transform.translation += future_frame_pos; transform.translation += future_frame_pos;
let vel_shed = marker.caliber.velocity_shed() * marker.current_velocity.normalize() * time.delta_seconds(); let vel_shed = marker.caliber.velocity_shed() * marker.current_velocity.normalize() * time.delta_seconds();
marker.current_velocity -= vel_shed; marker.current_velocity -= vel_shed;