Firearm shooting with a line almost perfect. Only need to change the m4 rotation values as it's rotated -90 on y axis. Apply inverse rotation on all transforms that shoot. And TODO: uncomment muzzle flash light code

This commit is contained in:
Franklin Blanco 2023-11-08 16:39:02 -08:00
parent 0c3ab5d8ad
commit a64e646e78
5 changed files with 77 additions and 11 deletions

View File

@ -2,10 +2,19 @@ use bevy::{prelude::{Component, Quat, Vec3}, reflect::Reflect};
use crate::logic::core::guns::{caliber::Caliber, spray_pattern::FirearmSprayPattern};
#[derive(Default, Debug, Clone, Reflect)]
pub struct FiringPoint {
pub forward: f32,
pub up: f32,
pub right: f32,
}
#[derive(Component, Clone, Reflect)]
pub struct FirearmData {
/// Where the bullets will come out of, and muzzle flash will be spawned out of.
pub firing_point: Vec3,
/// Also, this transform's .forward() should be pointing the same Vec3 as the barrel is
/// Global transform
pub firing_point: FiringPoint,
pub caliber: Caliber,
/// Placeholder until mags get implemented
pub max_capacity: usize,

View File

@ -6,3 +6,11 @@ pub enum Caliber {
NATO556,
Parabellum9mm,
}
impl Caliber {
pub fn range(&self) -> f32 {
match self {
Caliber::NATO556 => 750.0,
Caliber::Parabellum9mm => 250.0,
}
}
}

View File

@ -1,4 +1,4 @@
use crate::comps::core::markers::{firearm::FirearmData, holdable::HoldableObjectData};
use crate::comps::core::markers::{firearm::{FirearmData, FiringPoint}, holdable::HoldableObjectData};
use bevy::prelude::*;
use super::{caliber::Caliber, spray_pattern::FirearmSprayPattern};
@ -14,7 +14,11 @@ impl Firearm {
match self {
Firearm::M4A1 => {
FirearmData {
firing_point: Vec3 { x: -2.5, y: 0.0, z: 0.0 },
firing_point: FiringPoint {
forward: 1.0,
up: 1.0,
right: 0.0,
},
caliber: Caliber::NATO556,
max_capacity: 30,
fire_rate: 800.0,
@ -51,7 +55,11 @@ impl Firearm {
},
Firearm::Glock17 => {
FirearmData {
firing_point: Vec3 { x: -2.5, y: 0.0, z: 0.0 },
firing_point: FiringPoint {
forward: 1.2,
up: 0.445,
right: 0.0,
},
caliber: Caliber::Parabellum9mm,
max_capacity: 17,
fire_rate: 600.0,

View File

@ -1,6 +1,6 @@
use std::time::Duration;
use bevy::prelude::*;
use bevy::{prelude::*, render::render_resource::PrimitiveTopology};
use crate::comps::core::markers::muzzle_flash::MuzzleFlashMarker;
@ -8,12 +8,16 @@ use super::caliber::Caliber;
pub fn shoot_bullet(
mut commands: &mut Commands,
mut meshes: &mut ResMut<Assets<Mesh>>,
mut materials: &mut ResMut<Assets<StandardMaterial>>,
firing_point: Transform,
towards: Vec3,
forward: Vec3,
up: Vec3,
caliber: Caliber
) {
//let transform = Transform::from_translation(firing_point).look_at(target, up)
// Spawn muzzle flash LIGHT
commands.spawn(
/*commands.spawn(
(PointLightBundle {
point_light: PointLight {
//252, 238, 128
@ -27,7 +31,33 @@ pub fn shoot_bullet(
visibility: Visibility::Visible,
..Default::default()
}, MuzzleFlashMarker(Timer::new(Duration::from_millis(10), TimerMode::Once)))
);*/
println!("origin: {}", firing_point.translation);
println!("end: {}", firing_point.forward() * caliber.range());
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()
}
);
}
/*
meshes.add(box_2_mesh.into()),
material: materials.add(StandardMaterial {
base_color: Color::RED,
perceptual_roughness: 1.0,
..default()
}), */

View File

@ -26,6 +26,9 @@ pub fn capture_hand_usage(
mut equipment_change_event_writer: EventWriter<EquipmentChangeEvent>,
settings_screen_config: Res<SettingsScreenUIConfiguration>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
if !game_load_state.player_loaded {
return;
@ -108,8 +111,16 @@ pub fn capture_hand_usage(
},
};
// TODO: Spawn Bullet & Muzzle flash
let firing_point_global = firearm_transform.translation().clone();
shoot_bullet(&mut commands, Transform::from_translation(firing_point_global + (firearm_transform.forward() * 1.5) + (firearm_transform.up() / 2.5)), firearm_data.firing_point, firearm_data.caliber.clone());
/*let firing_point_global = firearm_transform.translation().clone();
let firint_point_final = firing_point_global + (firearm_transform.forward() * 1.5) + (firearm_transform.up() / 2.5);
shoot_bullet(&mut commands, &mut meshes, &mut materials, Transform::from_translation(firint_point_final), firearm_transform.forward() * 10.0, firearm_data.caliber.clone());*/
let firearm_transform = firearm_transform.clone();
// TODO: M4 holdableobject data has a Y ROT of -90. Apply that rotation here if it exists.
let firing_point = firearm_transform.translation() + (firearm_transform.forward() * firearm_data.firing_point.forward) + (firearm_transform.up() * firearm_data.firing_point.up) + (firearm_transform.right() * firearm_data.firing_point.right);
let forward = firearm_transform.forward();
let up = firearm_transform.up();
shoot_bullet(&mut commands, &mut meshes, &mut materials, Transform::from_translation(firing_point), forward, up, firearm_data.caliber.clone());
// Increment indexes and timers
player_firing_info.current_round_index += 1;
player_firing_info.last_shot_timestamp = time.elapsed_seconds();