Added caliber docs, refactored 9mm caliber name
This commit is contained in:
parent
0a7f426ce2
commit
dde59da879
|
@ -1,7 +1,6 @@
|
|||
use bevy::{
|
||||
prelude::{Component, Vec3},
|
||||
reflect::Reflect,
|
||||
time::Timer, ecs::{reflect::ReflectComponent, entity::Entity},
|
||||
reflect::Reflect, ecs::{reflect::ReflectComponent, entity::Entity},
|
||||
};
|
||||
|
||||
use crate::comps::core::weapons::caliber::Caliber;
|
||||
|
@ -17,7 +16,6 @@ pub struct BulletHit {
|
|||
pub struct BulletMarker {
|
||||
pub caliber: Caliber,
|
||||
pub starting_point: Vec3,
|
||||
pub timer: Timer,
|
||||
pub current_velocity: Vec3,
|
||||
pub hits: Vec<BulletHit>,
|
||||
}
|
||||
|
|
|
@ -1,82 +1,80 @@
|
|||
use bevy::reflect::Reflect;
|
||||
|
||||
/// Unit = m/s
|
||||
pub const MIN_LETHAL_BULLET_VELOCITY: f32 = 60.0;
|
||||
|
||||
#[allow(unused)]
|
||||
#[derive(Clone, Reflect, Default)]
|
||||
pub enum Caliber {
|
||||
/// 5.56x45mm
|
||||
NATO556,
|
||||
/// 9x19mm
|
||||
/// 9x19mm Parabellum (NATO)
|
||||
/// Reference: [Wikipedia](https://en.wikipedia.org/wiki/9%C3%9719mm_Parabellum)
|
||||
#[default]
|
||||
Parabellum9mm,
|
||||
NATOParabellum9mm,
|
||||
/// 5.45x39mm (7N6M)
|
||||
/// Reference: [Wikipedia](https://en.wikipedia.org/wiki/5.45%C3%9739mm)
|
||||
RU545
|
||||
}
|
||||
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 {
|
||||
match self {
|
||||
Caliber::NATO556 => 7500.0,
|
||||
Caliber::Parabellum9mm => 2500.0,
|
||||
Caliber::NATOParabellum9mm => 2500.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
|
||||
/// Real life value (BULLET WEIGHT)
|
||||
pub fn mass(&self) -> f32 {
|
||||
match self {
|
||||
Caliber::NATO556 => 0.00402,
|
||||
Caliber::Parabellum9mm => 0.05,
|
||||
Caliber::NATOParabellum9mm => 0.0052,
|
||||
Caliber::RU545 => 0.00343,
|
||||
}
|
||||
}
|
||||
pub fn linear_damping(&self) -> f32 {
|
||||
match self {
|
||||
Caliber::NATO556 => 1.0,
|
||||
Caliber::Parabellum9mm => 2.0,
|
||||
Caliber::NATOParabellum9mm => 2.0,
|
||||
Caliber::RU545 => 1.5,
|
||||
}
|
||||
}
|
||||
pub fn angular_damping(&self) -> f32 {
|
||||
match self {
|
||||
Caliber::NATO556 => 1.0,
|
||||
Caliber::Parabellum9mm => 1.0,
|
||||
Caliber::NATOParabellum9mm => 1.0,
|
||||
Caliber::RU545 => 1.0,
|
||||
}
|
||||
}
|
||||
pub fn size(&self) -> f32 {
|
||||
match self {
|
||||
Caliber::NATO556 => 0.07,
|
||||
Caliber::Parabellum9mm => 0.05,
|
||||
Caliber::NATOParabellum9mm => 0.05,
|
||||
Caliber::RU545 => 0.06
|
||||
}
|
||||
}
|
||||
|
||||
/// Unit = m/s
|
||||
/// Real life value
|
||||
pub fn muzzle_velocity(&self) -> f32 {
|
||||
match self {
|
||||
Caliber::NATO556 => 991.0,
|
||||
Caliber::Parabellum9mm => todo!(),
|
||||
Caliber::NATOParabellum9mm => 460.0,
|
||||
Caliber::RU545 => 880.0,
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
match self {
|
||||
Caliber::NATO556 => todo!(),
|
||||
Caliber::Parabellum9mm => todo!(),
|
||||
Caliber::RU545 => 120.0,
|
||||
Caliber::NATO556 => 150.0,
|
||||
Caliber::NATOParabellum9mm => 450.0,
|
||||
Caliber::RU545 => 300.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,7 +62,7 @@ impl Firearm {
|
|||
}*/
|
||||
Firearm::Glock17 => {
|
||||
FirearmData {
|
||||
caliber: Caliber::Parabellum9mm,
|
||||
caliber: Caliber::NATOParabellum9mm,
|
||||
fire_rate: 600.0,
|
||||
aim_time_seconds: 0.1,
|
||||
rebound_time_seconds: 0.5,
|
||||
|
|
|
@ -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::*;
|
||||
|
||||
pub fn despawn_muzzle_flashes(
|
||||
|
@ -18,18 +18,13 @@ pub fn despawn_stray_bullets(
|
|||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
mut query: Query<(&mut BulletMarker, Entity, &Transform)>,
|
||||
|
||||
mut bullet_collisions: EventReader<BulletCollisionEvent>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let collisions_read: Vec<&BulletCollisionEvent> = bullet_collisions.read().collect();
|
||||
for event in collisions_read.into_iter() {
|
||||
match event.collision_type {
|
||||
CollisionEventType::Start => {
|
||||
|
||||
//commands.entity(event.bullet).remove::<Collider>();
|
||||
//commands.entity(bullet_entity).insert(Sensor);
|
||||
spawn_bullet_hit_marker(
|
||||
&mut commands,
|
||||
event.at.translation,
|
||||
|
@ -38,15 +33,6 @@ pub fn despawn_stray_bullets(
|
|||
);
|
||||
},
|
||||
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(
|
||||
&mut commands,
|
||||
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(
|
||||
|
@ -87,7 +64,6 @@ pub fn spawn_bullet_hit_marker(
|
|||
},
|
||||
material: materials.add(StandardMaterial {
|
||||
base_color: Color::GREEN,
|
||||
//base_color_texture: Some(Color::GREEN),
|
||||
emissive: Color::GREEN,
|
||||
..Default::default()
|
||||
}),
|
||||
|
@ -116,7 +92,6 @@ fn spawn_bullet_exit_marker(
|
|||
},
|
||||
material: materials.add(StandardMaterial {
|
||||
base_color: Color::RED,
|
||||
//base_color_texture: Some(Color::GREEN),
|
||||
emissive: Color::RED,
|
||||
..Default::default()
|
||||
}),
|
||||
|
|
|
@ -38,26 +38,6 @@ pub fn shoot_bullet(
|
|||
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(
|
||||
commands,
|
||||
meshes,
|
||||
|
@ -83,10 +63,6 @@ pub fn spawn_bullet(
|
|||
BulletMarker {
|
||||
caliber: caliber.clone(),
|
||||
starting_point: firing_point.translation,
|
||||
timer: Timer::new(
|
||||
Duration::from_secs_f32(caliber.max_airtime_secs()),
|
||||
TimerMode::Once,
|
||||
),
|
||||
current_velocity: forward * caliber.muzzle_velocity(),
|
||||
hits: Vec::new()
|
||||
},
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use bevy::{prelude::*, render::render_resource::PrimitiveTopology, ecs::system::SystemParam};
|
||||
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)]
|
||||
pub struct UpdateBulletQueryParams<'w, 's> {
|
||||
|
@ -20,6 +20,13 @@ pub fn update_bullet(
|
|||
mut bullet_collision_event: EventWriter<BulletCollisionEvent>,
|
||||
) {
|
||||
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 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;
|
||||
let vel_shed = marker.caliber.velocity_shed() * marker.current_velocity.normalize() * time.delta_seconds();
|
||||
marker.current_velocity -= vel_shed;
|
||||
|
|
Loading…
Reference in New Issue