diff --git a/Cargo.lock b/Cargo.lock index 92620f2..c04da23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index e6ba7b6..37bea78 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/assets/audio/flashlight-switch.mp3 b/assets/audio/flashlight-switch.mp3 new file mode 100644 index 0000000..c7d13ef Binary files /dev/null and b/assets/audio/flashlight-switch.mp3 differ diff --git a/assets/audio/flashlight-switch.ogg b/assets/audio/flashlight-switch.ogg new file mode 100644 index 0000000..d51e450 Binary files /dev/null and b/assets/audio/flashlight-switch.ogg differ diff --git a/assets/main.assets.ron b/assets/main.assets.ron index c1ceecf..80405c4 100644 --- a/assets/main.assets.ron +++ b/assets/main.assets.ron @@ -1,10 +1,12 @@ ({ "lebron": File (path: "images/KingLebron.png"), - "flash_hold_4": File (path: "images/pixelart/Flashlight_hold_4.png"), "house": File (path: "meshes/House.glb"), "library": Folder ( path: "meshes/library", ), - "id_card": File(path: "meshes/id_card.glb") - "id_card_toolbar": File(path: "images/id_card_toolbar.png"") + "id_card": File(path: "meshes/id_card.glb"), + "id_card_toolbar": File(path: "images/id_card_toolbar.png"), + "flash_hold_4": File (path: "images/pixelart/Flashlight_hold_4.png"), + "flash_hold_4_pressed": File (path: "images/pixelart/Flashlight_click_4.png"), + "flashlight_click": File (path: "audio/flashlight-switch.ogg"), }) diff --git a/src/asset_loading/mod.rs b/src/asset_loading/mod.rs index 1a1c74d..ecd367f 100644 --- a/src/asset_loading/mod.rs +++ b/src/asset_loading/mod.rs @@ -3,20 +3,20 @@ 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( + app.add_plugins(AudioPlugin).add_loading_state( LoadingState::new(GameState::Loading) .continue_to_state(GameState::Menu) .with_dynamic_assets_file::("main.assets.ron") .load_collection::() .load_collection::() - .load_collection::(), - // .load_collection::() + .load_collection::() + .load_collection::(), // .load_collection::() // .load_collection::() // .load_collection::(), @@ -27,7 +27,10 @@ pub(super) fn plugin(app: &mut App) { // when done loading, they will be inserted as resources (see ) #[derive(AssetCollection, Resource, Clone)] -pub(crate) struct AudioAssets {} +pub(crate) struct AudioAssets { + #[asset(key = "flashlight_click")] + pub(crate) flash_click: Handle, +} #[derive(AssetCollection, Resource, Clone)] pub(crate) struct GltfAssets { @@ -58,4 +61,6 @@ pub(crate) struct ImageAssets { pub(crate) struct FlashlightAssets { #[asset(key = "flash_hold_4")] pub(crate) flash_hold_4: Handle, + #[asset(key = "flash_hold_4_pressed")] + pub(crate) flash_hold_4_pressed: Handle, } diff --git a/src/level_instantiation/mod.rs b/src/level_instantiation/mod.rs index a7fdb7b..e2f1c48 100644 --- a/src/level_instantiation/mod.rs +++ b/src/level_instantiation/mod.rs @@ -1,7 +1,12 @@ -use bevy::{prelude::*, reflect::DynamicTypePath}; +use bevy::{image, prelude::*, reflect::DynamicTypePath}; use bevy_rapier3d::prelude::*; -use crate::{GameState, asset_loading::GltfAssets, interaction::Interact}; +use crate::{ + GameState, + asset_loading::{GltfAssets, ImageAssets}, + interaction::Interact, + player::toolbar::ItemIcon, +}; pub fn map_plugin(app: &mut App) { app.add_systems( @@ -112,7 +117,12 @@ fn spawn_level(mut commands: Commands, models: Res>, gltf_assets: R )); } -fn spawn_objects(mut commands: Commands, models: Res>, gltf_assets: Res) { +fn spawn_objects( + mut commands: Commands, + models: Res>, + gltf_assets: Res, + image_assets: Res, +) { // let hammer = gltf_assets // .library // .get("meshes/library/hammer.glb") @@ -147,6 +157,7 @@ fn spawn_objects(mut commands: Commands, models: Res>, gltf_assets: Interact, RigidBody::Dynamic, Name::new("Id Card"), + ItemIcon::new(image_assets.id_card.clone()), SceneRoot(asset.clone()), )) .with_children(|parent| { diff --git a/src/main.rs b/src/main.rs index 2b4f1b4..a8ae9c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ fn main() { RapierPhysicsPlugin::::default(), RapierDebugRenderPlugin::default(), player::plugin, - debugging::plugin, + // debugging::plugin )) .init_state::() .add_systems(OnEnter(GameState::Playing), setup) @@ -62,11 +62,11 @@ fn setup( Collider::cuboid(0.5, 0.5, 0.5), )); // light - commands.spawn(( - PointLight { - shadows_enabled: true, - ..default() - }, - Transform::from_xyz(4.0, 8.0, 4.0), - )); + // commands.spawn(( + // PointLight { + // shadows_enabled: true, + // ..default() + // }, + // Transform::from_xyz(4.0, 8.0, 4.0), + // )); } diff --git a/src/player.rs b/src/player.rs index be909ac..f969cf2 100644 --- a/src/player.rs +++ b/src/player.rs @@ -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 { @@ -51,12 +52,38 @@ impl Default for HeadBob { #[derive(Component, Debug)] 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)) .add_systems( Update, - (move_camera, handle_input, apply_head_bob, on_resize_system).run_if(in_state(GameState::Playing)), + ( + move_camera, + handle_input, + apply_head_bob, + on_resize_system, + handle_flashlight, + update_flashlight_button_animation, + ).run_if(in_state(GameState::Playing)), ) .add_systems( FixedUpdate, @@ -112,6 +139,7 @@ pub fn init_player( RenderLayers::layer(STATIC_LAYER), )); + // pitslamp sprite let window = window.single(); let transform = flashlight_base_transform(window.width(), window.height()); parent.spawn(( @@ -119,6 +147,24 @@ pub fn init_player( transform.0.clone(), transform, RenderLayers::layer(STATIC_LAYER), + FlashlightButtonAnimation::default(), + )); + + // feitelijke pitslamp + parent.spawn(( + Flashlight, + SpotLight { + intensity: 0.0, + color: Color::WHITE, + range: 4.0, + outer_angle: 30.0_f32.to_radians(), // wide cone for flashlight + inner_angle: 10.0_f32.to_radians(), // narrower inner cone + shadows_enabled: false, // (set to true for realism) + radius: 0.35, // low value for hard light + ..default() + }, + Transform::from_xyz(0.0, -0.15, 0.5), + GlobalTransform::default(), )); }); } @@ -194,6 +240,8 @@ pub fn handle_input( mut query: Query<(&Transform, &mut PlayerInput, &mut PlayerAction, &mut Player), With>, ) { for (transform, mut input, mut action, mut player) in query.iter_mut() { + *action = PlayerAction::Move; + let forward = transform.forward(); let right = transform.right(); let mut movement_direction = Vec3::ZERO; @@ -221,10 +269,10 @@ pub fn handle_input( } else { player.speed_factor = 1.0; } - if keyboard_input.pressed(KeyCode::KeyE) { + if keyboard_input.just_pressed(KeyCode::KeyE) { *action = PlayerAction::Interact } - if keyboard_input.pressed(KeyCode::KeyA) { + if keyboard_input.just_pressed(KeyCode::KeyF) { *action = PlayerAction::ToggleFlashlight; } @@ -295,7 +343,7 @@ pub fn apply_head_bob( if is_moving { transform.translation.x = horizontal_offset; } else { - // decrease bobbing magnitued + // decrease bobbing magnitude transform.translation.x *= 0.8; } } @@ -320,4 +368,50 @@ pub fn apply_head_bob( } } } +} + +pub fn handle_flashlight( + player_query: Query<&PlayerAction, With>, + mut flashlight_query: Query<&mut SpotLight, With>, + mut flashlight_sprite_query: Query<&mut FlashlightButtonAnimation>, + audio_assets: Res, + audio: Res