Added temp weapon switching system, finished glock17 firearmdata

This commit is contained in:
Franklin Blanco 2023-09-18 14:52:54 -07:00
parent af2e23b275
commit 2f0a056900
10 changed files with 108 additions and 42 deletions

View File

@ -29,6 +29,8 @@ pub struct FirearmData {
pub final_aimed_position: Vec3,
/// Final position of hands when not aimed in
pub final_position: Vec3,
pub scale_factor: f32,
}
#[derive(Component)]

View File

@ -2,4 +2,5 @@
#[derive(Clone)]
pub enum Caliber {
NATO556,
Parabellum9mm,
}

View File

@ -3,52 +3,102 @@ use bevy::prelude::*;
use super::{caliber::Caliber, spray_pattern::FirearmSprayPattern};
#[allow(unused)]
#[derive(Component, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub enum Firearm {
M4A1,
Glock17,
}
impl Firearm {
pub fn firearm_data(&self) -> FirearmData {
FirearmData {
firing_point: Vec3 { x: -2.5, y: 0.0, z: 0.0 },
caliber: Caliber::NATO556,
max_capacity: 30,
fire_rate: 800.0,
rebound_time_seconds: 0.1,
vertical_recoil_modifier: 2.0,
horizontal_recoil_modifier: 0.5,
recoil_pattern: FirearmSprayPattern {
vertical: Vec::from([
1.0, 1.2, 1.3, 1.6, 1.5, 1.7, 1.5, 1.5, 1.5, 2.0 // 10 for now
]),
horizontal: Vec::from([
1.0, 1.2, 1.3, -1.6, 1.5, -1.7, -1.5, 1.5, -1.5, 2.0 // 10 for now
]),
},
match self {
Firearm::M4A1 => {
FirearmData {
firing_point: Vec3 { x: -2.5, y: 0.0, z: 0.0 },
caliber: Caliber::NATO556,
max_capacity: 30,
fire_rate: 800.0,
rebound_time_seconds: 0.2,
vertical_recoil_modifier: 2.0,
horizontal_recoil_modifier: 0.5,
recoil_pattern: FirearmSprayPattern {
vertical: Vec::from([
1.0, 1.2, 1.3, 1.6, 1.5, 1.7, 1.5, 1.5, 1.5, 2.0 // 10 for now
]),
horizontal: Vec::from([
1.0, 1.2, 1.3, -1.6, 1.5, -1.7, -1.5, 1.5, -1.5, 2.0 // 10 for now
]),
},
final_aimed_rotation: Quat::from_rotation_x(0.026),
final_rotation: Quat::default(),
final_aimed_position: Vec3 {
// x: -0.003,
// y: -0.35,
// z: -10.6,
x: -0.003,
y: -0.35,
z: -1.6,
final_aimed_rotation: Quat::from_rotation_x(0.026),
final_rotation: Quat::default(),
final_aimed_position: Vec3 {
// x: -0.003,
// y: -0.35,
// z: -10.6,
x: -0.003,
y: -0.35,
z: -1.6,
},
final_position: Vec3 {
x: 0.6,
y: -0.45,
z: -2.7,
},
scale_factor: 1.0,
asset_path: String::from("weapons/m4a1_rifle.glb"),
}
},
final_position: Vec3 {
x: 0.6,
y: -0.45,
z: -2.7,
Firearm::Glock17 => {
FirearmData {
firing_point: Vec3 { x: -2.5, y: 0.0, z: 0.0 },
caliber: Caliber::Parabellum9mm,
max_capacity: 17,
fire_rate: 600.0,
rebound_time_seconds: 0.1,
vertical_recoil_modifier: 5.0,
horizontal_recoil_modifier: 1.0,
recoil_pattern: FirearmSprayPattern {
vertical: Vec::from([
1.0, 1.2, 1.3, 1.6, 1.5, 1.7, 1.5, 1.5, 1.5, 2.0 // 10 for now
]),
horizontal: Vec::from([
1.0, 1.2, 1.3, -1.6, 1.5, -1.7, -1.5, 1.5, -1.5, 2.0 // 10 for now
]),
},
final_aimed_rotation: Quat::from_rotation_x(0.026),
final_rotation: Quat::default(),
final_aimed_position: Vec3 {
// x: -0.003,
// y: -0.35,
// z: -10.6,
x: -0.003,
y: -0.5,
z: -1.6,
},
final_position: Vec3 {
x: 1.0,
y: -0.45,
z: -2.7,
},
scale_factor: 0.25,
asset_path: String::from("weapons/glock_17_pistol.glb"),
}
},
asset_path: String::from("weapons/m4a1_rifle.glb"),
}
}
pub fn holdable_object_data(&self) -> HoldableObjectData {
HoldableObjectData {
held_at: Vec3::ZERO,
y_rot: -90.0,
match self {
Firearm::M4A1 => HoldableObjectData {
held_at: Vec3::ZERO,
y_rot: -90.0,
},
Firearm::Glock17 => HoldableObjectData {
held_at: Vec3::ZERO,
y_rot: 0.0,
},
}
}
}

View File

@ -2,7 +2,7 @@ use bevy::prelude::*;
use crate::{
comps::core::markers::{firearm::{FirearmData, MagazineData}, holdable::InPlayerHands, player::{PlayerHand, Player}},
logic::core::guns::player_firing::PlayerFiringInfo, utils::rad_deg::radians_from_degrees, setup::{animations::AllFirearmAnimations, load_state::GameLoadState},
logic::core::guns::{player_firing::PlayerFiringInfo, firearm::Firearm}, utils::rad_deg::radians_from_degrees, setup::{animations::AllFirearmAnimations, load_state::GameLoadState, equipment::{EquipmentChangeEvent, Equipment}},
};
pub fn capture_hand_usage(
@ -22,10 +22,15 @@ pub fn capture_hand_usage(
animation_clips: Res<Assets<AnimationClip>>,
game_load_state: Res<GameLoadState>,
mut equipment_change_event_writer: EventWriter<EquipmentChangeEvent>,
) {
if !game_load_state.player_loaded {
return;
}
if player_query.iter().len() == 0 {
return;
}
if let Some(player_firearm) = player_query.single().0.equipment.primary_firearm.clone() {
for mut player_firing_info in player_firing_info_query.iter_mut() {
player_firing_info.full_auto_timer.tick(time.delta());
@ -77,6 +82,11 @@ pub fn capture_hand_usage(
(time.delta_seconds() / firearm_data.rebound_time_seconds).clamp(0.0, 1.0),
);
}
if keyboard_input.just_pressed(KeyCode::Key1) {
equipment_change_event_writer.send(EquipmentChangeEvent(Equipment{ primary_firearm: Some(Firearm::M4A1) }));
} else if keyboard_input.just_pressed(KeyCode::Key2) {
equipment_change_event_writer.send(EquipmentChangeEvent(Equipment{ primary_firearm: Some(Firearm::Glock17) }));
}
// SHOOTING & RECOIL
if mouse_buttons.pressed(MouseButton::Left) {
if player_firing_info.full_auto_timer.finished() {

View File

@ -3,6 +3,6 @@ use bevy::prelude::*;
use crate::{setup::{spawn::SpawnPoint, equipment::Equipment}, comps::core::markers::player::{Player, PlayerData}, logic::core::guns::firearm::Firearm};
pub fn set_spawn_points(mut commands: Commands) {
commands.spawn(SpawnPoint { at: Transform::from_xyz(3.0, 5.0, 2.0), what: Player(PlayerData{ equipment: Equipment { primary_firearm: Some(Firearm::M4A1) }}) });
commands.spawn(SpawnPoint { at: Transform::from_xyz(3.0, 5.0, 2.0), what: Player(PlayerData{ equipment: Equipment { primary_firearm: Some(Firearm::Glock17) }}) });
}

View File

@ -31,10 +31,11 @@ pub fn load_all_assets(
image_handle: skybox_handle,
});
let m4a1_gltf_asset = GltfAsset { asset_type: GltfAssetType::Firearm(Firearm::M4A1), asset: asset_server.load(Firearm::M4A1.firearm_data().asset_path) } ;
let glock17_gltf_asset = GltfAsset { asset_type: GltfAssetType::Firearm(Firearm::Glock17), asset: asset_server.load(Firearm::Glock17.firearm_data().asset_path) };
commands.insert_resource(GltfAssets {
assets: Vec::from([
m4a1_gltf_asset
m4a1_gltf_asset,
glock17_gltf_asset
])
});
game_load_state.assets_loaded = true;

View File

@ -32,7 +32,7 @@ pub fn change_equipment(
let player_hands = player_hands_query.single_mut();
commands.entity(player_hands).clear_children(); // Don't do this without keeping the state from the last mag
commands.entity(player_hands).despawn_descendants(); // Don't do this without keeping the state from the last mag
if let Some(new_firearm) = equipment_change_event.0.primary_firearm.clone() {
spawn_firearm_on_player_hands(&mut commands, player_firing_info, player_hands, &assets_gltf, &loaded_gltf_assets, new_firearm);
}
@ -54,13 +54,15 @@ fn spawn_firearm_on_player_hands(
) {
if let Some(asset_handle) = assets_gltf.assets.iter().find(|asset| asset.asset_type == GltfAssetType::Firearm(firearm.clone())) {
if let Some(gltf) = loaded_gltf_assets.get(&asset_handle.asset) {
let firearm_data: FirearmData = firearm.firearm_data();
let mut firearm_transform = Transform::from_xyz(0.0, 0.0, 0.0);
firearm_transform.rotate_y(utils::rad_deg::radians_from_degrees(
firearm.holdable_object_data().y_rot,
));
firearm_transform.scale = firearm_transform.scale * firearm_data.scale_factor;
let scene = gltf.scenes[0].clone();
let firearm_data: FirearmData = firearm.firearm_data();
let firearm_entity = commands
.spawn((
SceneBundle {

View File

@ -94,8 +94,8 @@ pub fn player_spawner(
.insert(PlayerFiringInfo::default())
.push_children(&[camera]);
game_load_state.player_loaded = true;
equipment_change_event_writer.send(EquipmentChangeEvent(player_spawn_point.what.0.equipment.clone()));
commands.entity(player_spawn_point_entity).despawn();
game_load_state.player_loaded = true;
}
}