From 209b25c29feb35328a144d393558b0d5729f759d Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 2 Aug 2024 11:56:01 -0500 Subject: [PATCH] Spatial audio --- Cargo.lock | 1 + Cargo.toml | 1 + src/input.rs | 4 ++-- src/main.rs | 12 +++++------- src/player.rs | 47 ++++++++++++++++++++++++++++------------------- 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0842733..3d127f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -639,6 +639,7 @@ dependencies = [ "bevy_ecs", "bevy_gizmos_macros", "bevy_math", + "bevy_pbr", "bevy_reflect", "bevy_render", "bevy_sprite", diff --git a/Cargo.toml b/Cargo.toml index fbe28de..050b08f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ bevy = { version = "0.14.0", default-features = false, features = [ "bevy_text", # for writing characters to the screen "bevy_ui", "bevy_winit", + "bevy_pbr", "multi_threaded", # go faster "png", "x11", # run on non-wayland linuxes too diff --git a/src/input.rs b/src/input.rs index 01b9460..a2a385e 100644 --- a/src/input.rs +++ b/src/input.rs @@ -54,13 +54,13 @@ pub fn process_input( // with this in mind, to convert mouse coordinates to world coordinates for comparing against our logical window center, we take the absolute of the difference in height - gizmos.line(Vec3::ZERO, Vec3::X * 100., GREEN); + // gizmos.line(Vec3::ZERO, Vec3::X * 100., GREEN); if let Ok(window) = window.get_single() { if let Some(target) = window.cursor_position() { let size = window.size(); let center = size / 2.; let modified_target = target.with_y(size.y - target.y) - center; - gizmos.line(Vec2::ZERO.extend(0.), modified_target.extend(0.), RED); + // gizmos.line(Vec2::ZERO.extend(0.), modified_target.extend(0.), RED); input.angle = modified_target.to_angle(); } } diff --git a/src/main.rs b/src/main.rs index 067fc79..cead6e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,6 +75,11 @@ fn main() -> AppExit { .init_state::() .init_state::(); + app.insert_resource(AmbientLight { + color: Color::srgb(1., 1., 1.), + brightness: 1., + }); + app.configure_sets( Update, ( @@ -116,13 +121,6 @@ fn main() -> AppExit { ), ) .insert_resource(ClearColor(Color::srgb(0.3, 0.1, 0.5))); - // NOTE: would need to add PBR feature I think? - // Was intending to use this for day/night cycle type stuff a la V Rising? - // app.insert_resource(AmbientLight { - // color: Color::srgb(1., 1., 1.), - // brightness: 1., - // }); - app.run() } diff --git a/src/player.rs b/src/player.rs index 24d9034..ba73fd7 100644 --- a/src/player.rs +++ b/src/player.rs @@ -29,7 +29,7 @@ pub fn startup( mut materials: ResMut>, ) { let mesh = meshes.add(Capsule2d::new(3.0, 25.0)); - let material = materials.add(Color::hsl(360. * 1 as f32 / 3 as f32, 0.95, 0.7)); + let material = materials.add(Color::hsla(360. * 1 as f32 / 3 as f32, 0.95, 0.7, 0.5)); let transform = Transform::from_xyz(0.0, 0.0, 500.); let layout = assets.add(TextureAtlasLayout::from_grid( UVec2::new(32, 64), @@ -47,8 +47,8 @@ pub fn startup( }, YSortable, Watched, - Transform::default(), - GlobalTransform::default(), + TransformBundle::default(), + SpatialListener::new(20.), Mover { velocity: Velocity(Vec2::ZERO), heading: Heading(Vec2::ZERO), @@ -82,16 +82,22 @@ pub fn startup( text.transform.translation.z = 500.0; text.transform.translation.y = 24.; player.spawn(text); - player.spawn((SpatialBundle::default(), SpatialListener::new(10.))); - player.spawn(( - Crosshair, - MaterialMesh2dBundle { - mesh: mesh.into(), - transform, - material, - ..default() - }, - )); + player + .spawn(( + Crosshair, + TransformBundle { + local: transform, + ..default() + }, + )) + .with_children(|crosshair| { + crosshair.spawn(MaterialMesh2dBundle { + mesh: mesh.into(), + transform: Transform::from_xyz(0., 20., 0.), + material, + ..default() + }); + }); }); } @@ -105,7 +111,7 @@ pub fn control( heading.0 = input.movement; } if let Ok(mut transform) = crosshair.get_single_mut() { - transform.rotation = Quat::from_rotation_z(input.angle + (TAU / 4.)); + transform.rotation = Quat::from_rotation_z(input.angle - (TAU / 4.)); } } @@ -118,11 +124,14 @@ pub fn meow_on_r( if keys.just_pressed(KeyCode::KeyR) { if let Ok(player) = player.get_single() { let sound = commands - .spawn(AudioSourceBundle { - source: assets.load::("sfx/meow.wav"), - settings: PlaybackSettings::DESPAWN.with_spatial(true), - ..default() - }) + .spawn(( + SpatialBundle::default(), + AudioBundle { + source: assets.load::("sfx/meow.wav"), + settings: PlaybackSettings::DESPAWN.with_spatial(true), + ..default() + }, + )) .id(); commands.entity(player).push_children(&[sound]); }