Spatial audio

This commit is contained in:
Daniel Flanagan 2024-08-02 11:56:01 -05:00
parent 06548a0249
commit 209b25c29f
5 changed files with 37 additions and 28 deletions

1
Cargo.lock generated
View file

@ -639,6 +639,7 @@ dependencies = [
"bevy_ecs",
"bevy_gizmos_macros",
"bevy_math",
"bevy_pbr",
"bevy_reflect",
"bevy_render",
"bevy_sprite",

View file

@ -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

View file

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

View file

@ -75,6 +75,11 @@ fn main() -> AppExit {
.init_state::<View>()
.init_state::<Game>();
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()
}

View file

@ -29,7 +29,7 @@ pub fn startup(
mut materials: ResMut<Assets<ColorMaterial>>,
) {
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::<AudioSource>("sfx/meow.wav"),
settings: PlaybackSettings::DESPAWN.with_spatial(true),
..default()
})
.spawn((
SpatialBundle::default(),
AudioBundle {
source: assets.load::<AudioSource>("sfx/meow.wav"),
settings: PlaybackSettings::DESPAWN.with_spatial(true),
..default()
},
))
.id();
commands.entity(player).push_children(&[sound]);
}