RELOAD SYSTEM WITH ANIMATION DONE!

This commit is contained in:
Franklin Blanco 2023-09-17 18:01:37 -07:00
parent c186019b52
commit 7783c4a165
7 changed files with 89 additions and 71 deletions

Binary file not shown.

View File

@ -14,6 +14,7 @@ pub struct PlayerFiringInfo {
/// Decreases with time, each gun has its own rebound time /// Decreases with time, each gun has its own rebound time
pub current_round_index: usize, pub current_round_index: usize,
pub full_auto_timer: Timer, pub full_auto_timer: Timer,
pub is_reloading: bool,
} }
impl Default for PlayerFiringInfo { impl Default for PlayerFiringInfo {
@ -23,6 +24,7 @@ impl Default for PlayerFiringInfo {
rounds_in_last_spray: Default::default(), rounds_in_last_spray: Default::default(),
current_round_index: Default::default(), current_round_index: Default::default(),
full_auto_timer: Timer::new(Duration::from_secs_f32(0.0), TimerMode::Repeating), full_auto_timer: Timer::new(Duration::from_secs_f32(0.0), TimerMode::Repeating),
is_reloading: false,
} }
} }
} }

View File

@ -45,7 +45,6 @@ pub fn spawn_firearm_on_player_hands(
TimerMode::Once, TimerMode::Once,
); );
// Load animations // Load animations
commands.insert_resource(FirearmAnimations { reload_magazine: asset_server.load(format!("{}#Animation0", DEFAULT_PLAYER_FIREARM.firearm_data().asset_path)), commands.insert_resource(FirearmAnimations { reload_magazine: asset_server.load(format!("{}#Animation0", DEFAULT_PLAYER_FIREARM.firearm_data().asset_path))})
rock_charging_handle: asset_server.load(format!("{}#Animation1", DEFAULT_PLAYER_FIREARM.firearm_data().asset_path)) })
} }
} }

View File

@ -1,12 +0,0 @@
use bevy::prelude::*;
use crate::setup::animations::FirearmAnimations;
pub fn setup_scene_once_loaded(
animations: Res<FirearmAnimations>,
mut players: Query<&mut AnimationPlayer, Added<AnimationPlayer>>,
) {
for mut player in &mut players {
player.play(animations.reload_magazine.clone_weak()).repeat();
}
}

View File

@ -2,11 +2,12 @@ use bevy::prelude::*;
use crate::{ use crate::{
comps::core::markers::{firearm::{FirearmData, MagazineData}, holdable::InPlayerHands, player::PlayerHand}, comps::core::markers::{firearm::{FirearmData, MagazineData}, holdable::InPlayerHands, player::PlayerHand},
logic::core::guns::player_firing::PlayerFiringInfo, utils::rad_deg::radians_from_degrees, logic::core::guns::player_firing::PlayerFiringInfo, utils::rad_deg::radians_from_degrees, setup::animations::FirearmAnimations,
}; };
pub fn capture_hand_usage( pub fn capture_hand_usage(
mouse_buttons: Res<Input<MouseButton>>, mouse_buttons: Res<Input<MouseButton>>,
keyboard_input: Res<Input<KeyCode>>,
mut hand_query: Query<&mut Transform, With<PlayerHand>>, mut hand_query: Query<&mut Transform, With<PlayerHand>>,
time: Res<Time>, time: Res<Time>,
@ -15,11 +16,37 @@ pub fn capture_hand_usage(
(&mut Transform, &'static FirearmData, &mut MagazineData), (&mut Transform, &'static FirearmData, &mut MagazineData),
(With<InPlayerHands>, Without<PlayerHand>), (With<InPlayerHands>, Without<PlayerHand>),
>, >,
animation_clips: Res<Assets<AnimationClip>>,
animations: Res<FirearmAnimations>,
mut players: Query<&mut AnimationPlayer>,
) { ) {
player_firing_info.full_auto_timer.tick(time.delta()); player_firing_info.full_auto_timer.tick(time.delta());
for (mut _firearm_transform, firearm_data, mut magazine_data) in firearm_query.iter_mut() { for (mut _firearm_transform, firearm_data, mut magazine_data) in firearm_query.iter_mut() {
for mut hand_transform in hand_query.iter_mut() { for mut hand_transform in hand_query.iter_mut() {
if player_firing_info.is_reloading {
for mut player in &mut players {
if let Some(reload_animation) = animation_clips.get(&animations.reload_magazine) {
if player.elapsed() >= reload_animation.duration() {
magazine_data.rounds_shot = 0;
player_firing_info.is_reloading = false;
}
}
}
} else { // Player is not in a reload animation
if keyboard_input.just_pressed(KeyCode::R) {
// Start reload animation
for mut player in &mut players {
player.start(animations.reload_magazine.clone_weak());
player_firing_info.is_reloading = true;
}
// Set is_reloading = true
// At the end of reload animation, set magazine data to capacity = 0
}
// AIMING IN/OUT // AIMING IN/OUT
if mouse_buttons.pressed(MouseButton::Right) { if mouse_buttons.pressed(MouseButton::Right) {
let rotation_lerp_quat = hand_transform.rotation.lerp( let rotation_lerp_quat = hand_transform.rotation.lerp(
@ -79,6 +106,8 @@ pub fn capture_hand_usage(
} }
} }
} }
}
} }
} }

View File

@ -10,7 +10,7 @@ use crate::{
}, },
hands::capture_hand_usage, hands::capture_hand_usage,
player_vertical_sync::sync_player_y_state, player_vertical_sync::sync_player_y_state,
spawn_player::spawn_player, camera_effects::setup_scene_once_loaded, spawn_player::spawn_player,
}, },
}, },
}; };
@ -43,6 +43,6 @@ pub fn load_scene(application: &mut App) {
application.add_systems(Update, capture_hand_usage); application.add_systems(Update, capture_hand_usage);
application.add_systems(Startup, setup_lighting); application.add_systems(Startup, setup_lighting);
application.add_systems(Update, setup_scene_once_loaded); //application.add_systems(Update, setup_scene_once_loaded);
//application.add_systems(Update, play_animation); //application.add_systems(Update, play_animation);
} }

View File

@ -3,5 +3,5 @@ use bevy::prelude::*;
#[derive(Resource)] #[derive(Resource)]
pub struct FirearmAnimations { pub struct FirearmAnimations {
pub reload_magazine: Handle<AnimationClip>, pub reload_magazine: Handle<AnimationClip>,
pub rock_charging_handle: Handle<AnimationClip>,
} }