Switch to typed asset handling.
This commit is contained in:
parent
57091d7e9e
commit
e7c9da6f91
|
@ -1,7 +1,7 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use bevy::{asset::HandleId, prelude::*, transform::TransformSystem};
|
use bevy::{prelude::*, transform::TransformSystem};
|
||||||
use bevy_synthizer::{DistanceMax, DistanceRef, Rolloff, Sound};
|
use bevy_synthizer::{Buffer, DistanceMax, DistanceRef, Rolloff, Sound};
|
||||||
use rand::random;
|
use rand::random;
|
||||||
|
|
||||||
use crate::{commands::RunIfExistsExt, core::PointLike};
|
use crate::{commands::RunIfExistsExt, core::PointLike};
|
||||||
|
@ -9,7 +9,7 @@ use crate::{commands::RunIfExistsExt, core::PointLike};
|
||||||
#[derive(Component, Clone, Debug, Reflect)]
|
#[derive(Component, Clone, Debug, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Footstep {
|
pub struct Footstep {
|
||||||
pub sound: HandleId,
|
pub sound: Handle<Buffer>,
|
||||||
pub step_length: f32,
|
pub step_length: f32,
|
||||||
pub gain: f64,
|
pub gain: f64,
|
||||||
pub reference_distance: Option<f64>,
|
pub reference_distance: Option<f64>,
|
||||||
|
@ -22,7 +22,7 @@ pub struct Footstep {
|
||||||
impl Default for Footstep {
|
impl Default for Footstep {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
sound: "".into(),
|
sound: default(),
|
||||||
step_length: 0.8,
|
step_length: 0.8,
|
||||||
gain: 1.,
|
gain: 1.,
|
||||||
reference_distance: None,
|
reference_distance: None,
|
||||||
|
@ -41,13 +41,9 @@ pub struct FootstepBundle {
|
||||||
pub global_transform: GlobalTransform,
|
pub global_transform: GlobalTransform,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn added(
|
fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added<Footstep>>) {
|
||||||
mut commands: Commands,
|
|
||||||
asset_server: Res<AssetServer>,
|
|
||||||
footsteps: Query<(Entity, &Footstep), Added<Footstep>>,
|
|
||||||
) {
|
|
||||||
for (entity, footstep) in footsteps.iter() {
|
for (entity, footstep) in footsteps.iter() {
|
||||||
let buffer = asset_server.get_handle(footstep.sound);
|
let buffer = footstep.sound.clone();
|
||||||
commands.run_if_exists(entity, move |mut entity| {
|
commands.run_if_exists(entity, move |mut entity| {
|
||||||
entity.insert(Sound {
|
entity.insert(Sound {
|
||||||
buffer,
|
buffer,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::{fmt::Debug, hash::Hash, time::Duration};
|
use std::{fmt::Debug, hash::Hash, time::Duration};
|
||||||
|
|
||||||
use bevy::{asset::HandleId, prelude::*, transform::TransformSystem};
|
use bevy::{prelude::*, transform::TransformSystem};
|
||||||
use bevy_synthizer::{DistanceMax, DistanceRef, Rolloff, Sound};
|
use bevy_synthizer::{Buffer, DistanceMax, DistanceRef, Rolloff, Sound};
|
||||||
|
|
||||||
use rand::random;
|
use rand::random;
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ use crate::{
|
||||||
|
|
||||||
#[derive(Component, Clone, Debug)]
|
#[derive(Component, Clone, Debug)]
|
||||||
pub struct SoundIcon {
|
pub struct SoundIcon {
|
||||||
pub sound: HandleId,
|
pub sound: Handle<Buffer>,
|
||||||
pub gain: f64,
|
pub gain: f64,
|
||||||
pub pitch: f64,
|
pub pitch: f64,
|
||||||
pub reference_distance: Option<f64>,
|
pub reference_distance: Option<f64>,
|
||||||
|
@ -27,7 +27,7 @@ impl Default for SoundIcon {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let seconds = random::<f32>() + 4.5;
|
let seconds = random::<f32>() + 4.5;
|
||||||
let mut icon = Self {
|
let mut icon = Self {
|
||||||
sound: "".into(),
|
sound: default(),
|
||||||
gain: 1.,
|
gain: 1.,
|
||||||
pitch: 1.,
|
pitch: 1.,
|
||||||
reference_distance: None,
|
reference_distance: None,
|
||||||
|
@ -50,13 +50,9 @@ pub struct SoundIconBundle {
|
||||||
pub global_transform: GlobalTransform,
|
pub global_transform: GlobalTransform,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn added(
|
fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added<SoundIcon>>) {
|
||||||
mut commands: Commands,
|
|
||||||
asset_server: Res<AssetServer>,
|
|
||||||
icons: Query<(Entity, &SoundIcon), Added<SoundIcon>>,
|
|
||||||
) {
|
|
||||||
for (entity, icon) in icons.iter() {
|
for (entity, icon) in icons.iter() {
|
||||||
let buffer = asset_server.get_handle(icon.sound);
|
let buffer = icon.sound.clone();
|
||||||
let gain = icon.gain;
|
let gain = icon.gain;
|
||||||
let pitch = icon.pitch;
|
let pitch = icon.pitch;
|
||||||
let looping = icon.interval.is_none();
|
let looping = icon.interval.is_none();
|
||||||
|
@ -101,7 +97,6 @@ fn update<S>(
|
||||||
Option<&DistanceMax>,
|
Option<&DistanceMax>,
|
||||||
Option<&Rolloff>,
|
Option<&Rolloff>,
|
||||||
)>,
|
)>,
|
||||||
asset_server: Res<AssetServer>,
|
|
||||||
) where
|
) where
|
||||||
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
|
||||||
{
|
{
|
||||||
|
@ -139,7 +134,7 @@ fn update<S>(
|
||||||
interval.reset();
|
interval.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let buffer = asset_server.get_handle(icon.sound);
|
let buffer = icon.sound.clone();
|
||||||
if sound.buffer != buffer {
|
if sound.buffer != buffer {
|
||||||
sound.buffer = buffer;
|
sound.buffer = buffer;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user