flashlight sound and animation

This commit is contained in:
Back777space
2025-04-06 15:41:22 +02:00
parent 99041827d4
commit f538c597f7
8 changed files with 246 additions and 36 deletions

View File

@@ -2,11 +2,12 @@
use bevy::{
input::mouse::AccumulatedMouseMotion, prelude::*, render::view::RenderLayers, window::{PrimaryWindow, WindowResized}
};
use bevy_kira_audio::{Audio, AudioControl};
use bevy_rapier3d::prelude::*;
pub mod toolbar;
use crate::{asset_loading::FlashlightAssets, GameState};
use crate::{asset_loading::{AudioAssets, FlashlightAssets}, GameState};
#[derive(Debug, Component, Default)]
pub struct Player {
@@ -54,6 +55,22 @@ pub struct BaseTransform(pub Transform);
#[derive(Debug, Component)]
pub struct Flashlight;
#[derive(Component)]
pub struct FlashlightButtonAnimation {
pub timer: Timer,
pub is_pressed: bool,
}
impl Default for FlashlightButtonAnimation {
fn default() -> Self {
Self {
timer: Timer::from_seconds(0.20, TimerMode::Once),
is_pressed: false,
}
}
}
pub fn plugin(app: &mut App) {
app.add_plugins(toolbar::plugin)
.add_systems(OnEnter(GameState::Playing), (init_player, hide_cursor))
@@ -65,7 +82,7 @@ pub fn plugin(app: &mut App) {
apply_head_bob,
on_resize_system,
handle_flashlight,
// handle_flashlight_movement,
update_flashlight_button_animation,
).run_if(in_state(GameState::Playing)),
)
.add_systems(
@@ -130,6 +147,7 @@ pub fn init_player(
transform.0.clone(),
transform,
RenderLayers::layer(STATIC_LAYER),
FlashlightButtonAnimation::default(),
));
// feitelijke pitslamp
@@ -355,6 +373,9 @@ pub fn apply_head_bob(
pub fn handle_flashlight(
player_query: Query<&PlayerAction, With<Player>>,
mut flashlight_query: Query<&mut SpotLight, With<Flashlight>>,
mut flashlight_sprite_query: Query<&mut FlashlightButtonAnimation>,
audio_assets: Res<AudioAssets>,
audio: Res<Audio>,
) {
let Ok(action) = player_query.get_single() else {
return;
@@ -362,12 +383,35 @@ pub fn handle_flashlight(
if *action != PlayerAction::ToggleFlashlight {
return;
}
if let Ok(mut animation) = flashlight_sprite_query.get_single_mut() {
animation.is_pressed = true;
animation.timer.reset();
}
if let Ok(mut spotlight) = flashlight_query.get_single_mut() {
audio.play(audio_assets.flash_click.clone());
spotlight.intensity = if spotlight.intensity > 0.0 {
0.0
} else {
300_000.0
};
println!("Flashlight {}", if spotlight.intensity > 0.0 { "on" } else { "off" });
}
}
pub fn update_flashlight_button_animation(
time: Res<Time>,
mut query: Query<(&mut FlashlightButtonAnimation, &mut Sprite)>,
flashlights: Res<FlashlightAssets>,
) {
for (mut animation, mut image) in query.iter_mut() {
if animation.is_pressed {
animation.timer.tick(time.delta());
if animation.timer.finished() {
*image = Sprite::from_image(flashlights.flash_hold_4.clone());
animation.is_pressed = false;
} else {
*image = Sprite::from_image(flashlights.flash_hold_4_pressed.clone());
}
}
}
}