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

193
Cargo.lock generated
View File

@@ -147,6 +147,7 @@ dependencies = [
"bevy",
"bevy_asset_loader",
"bevy_egui",
"bevy_kira_audio",
"bevy_rapier3d",
]
@@ -168,7 +169,7 @@ dependencies = [
"ndk-context",
"ndk-sys 0.6.0+11769913",
"num_enum",
"thiserror",
"thiserror 1.0.69",
]
[[package]]
@@ -593,7 +594,7 @@ dependencies = [
"bevy",
"ron",
"serde",
"thiserror",
"thiserror 1.0.69",
]
[[package]]
@@ -925,6 +926,20 @@ dependencies = [
"bevy_winit",
]
[[package]]
name = "bevy_kira_audio"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c929e59bffeb04011aab93880623163c0f8c31c7e99d752d74ba935a53546731"
dependencies = [
"anyhow",
"bevy",
"kira",
"parking_lot",
"thiserror 2.0.12",
"uuid",
]
[[package]]
name = "bevy_log"
version = "0.15.3"
@@ -1608,7 +1623,7 @@ dependencies = [
"polling",
"rustix",
"slab",
"thiserror",
"thiserror 1.0.69",
]
[[package]]
@@ -2141,7 +2156,7 @@ dependencies = [
"const_panic",
"encase_derive",
"glam",
"thiserror",
"thiserror 1.0.69",
]
[[package]]
@@ -2164,6 +2179,15 @@ dependencies = [
"syn",
]
[[package]]
name = "encoding_rs"
version = "0.8.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3"
dependencies = [
"cfg-if",
]
[[package]]
name = "epaint"
version = "0.31.1"
@@ -2518,6 +2542,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8babf46d4c1c9d92deac9f7be466f76dfc4482b6452fc5024b5e8daf6ffeb3ee"
dependencies = [
"bytemuck",
"mint",
"rand",
"serde",
]
@@ -2612,7 +2637,7 @@ checksum = "c151a2a5ef800297b4e79efa4f4bec035c5f51d5ae587287c9b952bdf734cacd"
dependencies = [
"log",
"presser",
"thiserror",
"thiserror 1.0.69",
"windows 0.58.0",
]
@@ -3020,7 +3045,7 @@ dependencies = [
"combine",
"jni-sys",
"log",
"thiserror",
"thiserror 1.0.69",
"walkdir",
"windows-sys 0.45.0",
]
@@ -3074,6 +3099,22 @@ version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc"
[[package]]
name = "kira"
version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb5a9f9dff5e262540b628b00d5e1a772270a962d6869f8695bb24832ff3b394"
dependencies = [
"cpal",
"glam",
"mint",
"paste",
"ringbuf",
"send_wrapper",
"symphonia",
"triple_buffer",
]
[[package]]
name = "ktx2"
version = "0.3.0"
@@ -3295,6 +3336,12 @@ dependencies = [
"simd-adler32",
]
[[package]]
name = "mint"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e53debba6bda7a793e5f99b8dacf19e626084f525f7829104ba9898f367d85ff"
[[package]]
name = "naga"
version = "23.1.0"
@@ -3313,7 +3360,7 @@ dependencies = [
"rustc-hash 1.1.0",
"spirv",
"termcolor",
"thiserror",
"thiserror 1.0.69",
"unicode-xid",
]
@@ -3332,7 +3379,7 @@ dependencies = [
"regex",
"regex-syntax 0.8.5",
"rustc-hash 1.1.0",
"thiserror",
"thiserror 1.0.69",
"tracing",
"unicode-ident",
]
@@ -3376,7 +3423,7 @@ dependencies = [
"log",
"ndk-sys 0.5.0+25.2.9519653",
"num_enum",
"thiserror",
"thiserror 1.0.69",
]
[[package]]
@@ -3391,7 +3438,7 @@ dependencies = [
"ndk-sys 0.6.0+11769913",
"num_enum",
"raw-window-handle",
"thiserror",
"thiserror 1.0.69",
]
[[package]]
@@ -3974,7 +4021,7 @@ dependencies = [
"slab",
"smallvec",
"spade",
"thiserror",
"thiserror 1.0.69",
]
[[package]]
@@ -4282,7 +4329,7 @@ dependencies = [
"profiling",
"rustc-hash 2.1.1",
"simba",
"thiserror",
"thiserror 1.0.69",
]
[[package]]
@@ -4315,7 +4362,7 @@ dependencies = [
"rand_chacha",
"simd_helpers",
"system-deps",
"thiserror",
"thiserror 1.0.69",
"v_frame",
"wasm-bindgen",
]
@@ -4457,6 +4504,15 @@ version = "0.8.50"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57397d16646700483b67d2dd6511d79318f9d057fdbd21a4066aeac8b41d310a"
[[package]]
name = "ringbuf"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79abed428d1fd2a128201cec72c5f6938e2da607c6f3745f769fabea399d950a"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "robust"
version = "1.1.0"
@@ -4471,7 +4527,7 @@ checksum = "6006a627c1a38d37f3d3a85c6575418cfe34a5392d60a686d0071e1c8d427acb"
dependencies = [
"cpal",
"lewton",
"thiserror",
"thiserror 1.0.69",
]
[[package]]
@@ -4753,7 +4809,7 @@ dependencies = [
"log",
"memmap2",
"rustix",
"thiserror",
"thiserror 1.0.69",
"wayland-backend",
"wayland-client",
"wayland-csd-frame",
@@ -4835,6 +4891,77 @@ dependencies = [
"zeno",
]
[[package]]
name = "symphonia"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "815c942ae7ee74737bb00f965fa5b5a2ac2ce7b6c01c0cc169bbeaf7abd5f5a9"
dependencies = [
"lazy_static",
"symphonia-codec-vorbis",
"symphonia-core",
"symphonia-format-ogg",
"symphonia-metadata",
]
[[package]]
name = "symphonia-codec-vorbis"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a98765fb46a0a6732b007f7e2870c2129b6f78d87db7987e6533c8f164a9f30"
dependencies = [
"log",
"symphonia-core",
"symphonia-utils-xiph",
]
[[package]]
name = "symphonia-core"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "798306779e3dc7d5231bd5691f5a813496dc79d3f56bf82e25789f2094e022c3"
dependencies = [
"arrayvec",
"bitflags 1.3.2",
"bytemuck",
"lazy_static",
"log",
]
[[package]]
name = "symphonia-format-ogg"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ada3505789516bcf00fc1157c67729eded428b455c27ca370e41f4d785bfa931"
dependencies = [
"log",
"symphonia-core",
"symphonia-metadata",
"symphonia-utils-xiph",
]
[[package]]
name = "symphonia-metadata"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc622b9841a10089c5b18e99eb904f4341615d5aa55bbf4eedde1be721a4023c"
dependencies = [
"encoding_rs",
"lazy_static",
"log",
"symphonia-core",
]
[[package]]
name = "symphonia-utils-xiph"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "484472580fa49991afda5f6550ece662237b00c6f562c7d9638d1b086ed010fe"
dependencies = [
"symphonia-core",
"symphonia-metadata",
]
[[package]]
name = "syn"
version = "2.0.100"
@@ -4926,7 +5053,16 @@ version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
dependencies = [
"thiserror-impl",
"thiserror-impl 1.0.69",
]
[[package]]
name = "thiserror"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [
"thiserror-impl 2.0.12",
]
[[package]]
@@ -4940,6 +5076,17 @@ dependencies = [
"syn",
]
[[package]]
name = "thiserror-impl"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "thread_local"
version = "1.1.8"
@@ -5141,6 +5288,15 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "triple_buffer"
version = "8.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de7a7d39da903eaef0d0fd14aae8c8c36cdd7dc1d5a251f88c84b676e8dc0a14"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "ttf-parser"
version = "0.20.0"
@@ -5271,6 +5427,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3758f5e68192bb96cc8f9b7e2c2cfdabb435499a28499a42f8f984092adad4b"
dependencies = [
"getrandom 0.2.15",
"rand",
"serde",
]
@@ -5602,7 +5759,7 @@ dependencies = [
"raw-window-handle",
"rustc-hash 1.1.0",
"smallvec",
"thiserror",
"thiserror 1.0.69",
"wgpu-hal",
"wgpu-types",
]
@@ -5644,7 +5801,7 @@ dependencies = [
"renderdoc-sys",
"rustc-hash 1.1.0",
"smallvec",
"thiserror",
"thiserror 1.0.69",
"wasm-bindgen",
"web-sys",
"wgpu-types",

View File

@@ -7,4 +7,5 @@ edition = "2024"
bevy = { version="0.15.3", features = ["jpeg"]}
bevy_asset_loader = { version ="0.22.0", features = ["standard_dynamic_assets"] }
bevy_egui = "0.33.0"
bevy_kira_audio = "0.22.0"
bevy_rapier3d = "0.29.0"

Binary file not shown.

Binary file not shown.

View File

@@ -1,7 +1,9 @@
({
"lebron": File (path: "images/KingLebron.png"),
"flash_hold_4": File (path: "images/pixelart/Flashlight_hold_4.png"),
"flash_hold_4_pressed": File (path: "images/pixelart/Flashlight_click_4.png"),
"house": File (path: "meshes/House.glb"),
"flashlight_click": File (path: "audio/flashlight-switch.ogg"),
"library": Folder (
path: "meshes/library",
),

View File

@@ -3,31 +3,35 @@ use bevy::{prelude::*, utils::HashMap};
use crate::GameState;
use bevy_asset_loader::prelude::*;
// use bevy_egui::{EguiContexts, egui, egui::ProgressBar};
// use bevy_kira_audio::AudioSource;
use bevy_kira_audio::{AudioPlugin, AudioSource};
// use iyes_progress::{ProgressCounter, ProgressPlugin};
/// Loads resources and assets for the game.
/// See assets/main.assets.ron for the actual paths used.
pub(super) fn plugin(app: &mut App) {
app.add_loading_state(
LoadingState::new(GameState::Loading)
.continue_to_state(GameState::Menu)
.with_dynamic_assets_file::<StandardDynamicAssetCollection>("main.assets.ron")
.load_collection::<ImageAssets>()
.load_collection::<FlashlightAssets>()
.load_collection::<GltfAssets>(),
// .load_collection::<AudioAssets>()
// .load_collection::<TextureAssets>()
// .load_collection::<GrassAssets>()
// .load_collection::<ConfigAssets>(),
);
app.add_plugins(AudioPlugin)
.add_loading_state(
LoadingState::new(GameState::Loading)
.continue_to_state(GameState::Menu)
.with_dynamic_assets_file::<StandardDynamicAssetCollection>("main.assets.ron")
.load_collection::<ImageAssets>()
.load_collection::<FlashlightAssets>()
.load_collection::<GltfAssets>()
.load_collection::<AudioAssets>(),
// .load_collection::<TextureAssets>()
// .load_collection::<GrassAssets>()
// .load_collection::<ConfigAssets>(),
);
}
// the following asset collections will be loaded during the State `GameState::InitialLoading`
// when done loading, they will be inserted as resources (see <https://github.com/NiklasEi/bevy_asset_loader>)
#[derive(AssetCollection, Resource, Clone)]
pub(crate) struct AudioAssets {}
pub(crate) struct AudioAssets {
#[asset(key = "flashlight_click")]
pub(crate) flash_click: Handle<AudioSource>,
}
#[derive(AssetCollection, Resource, Clone)]
pub(crate) struct GltfAssets {
@@ -54,4 +58,6 @@ pub(crate) struct ImageAssets {
pub(crate) struct FlashlightAssets {
#[asset(key = "flash_hold_4")]
pub(crate) flash_hold_4: Handle<Image>,
#[asset(key = "flash_hold_4_pressed")]
pub(crate) flash_hold_4_pressed: Handle<Image>,
}

View File

@@ -1,5 +1,5 @@
use asset_loading::ImageAssets;
use bevy::{prelude::*};
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
mod asset_loading;

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());
}
}
}
}