From e7c9da6f915a999452c9e464bafea208936faa99 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Wed, 3 Aug 2022 09:43:07 -0500 Subject: [PATCH] Switch to typed asset handling. --- src/sound/footstep.rs | 16 ++++++---------- src/sound/icon.rs | 19 +++++++------------ 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/sound/footstep.rs b/src/sound/footstep.rs index 7f53f94..61cb8b5 100644 --- a/src/sound/footstep.rs +++ b/src/sound/footstep.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; -use bevy::{asset::HandleId, prelude::*, transform::TransformSystem}; -use bevy_synthizer::{DistanceMax, DistanceRef, Rolloff, Sound}; +use bevy::{prelude::*, transform::TransformSystem}; +use bevy_synthizer::{Buffer, DistanceMax, DistanceRef, Rolloff, Sound}; use rand::random; use crate::{commands::RunIfExistsExt, core::PointLike}; @@ -9,7 +9,7 @@ use crate::{commands::RunIfExistsExt, core::PointLike}; #[derive(Component, Clone, Debug, Reflect)] #[reflect(Component)] pub struct Footstep { - pub sound: HandleId, + pub sound: Handle, pub step_length: f32, pub gain: f64, pub reference_distance: Option, @@ -22,7 +22,7 @@ pub struct Footstep { impl Default for Footstep { fn default() -> Self { Self { - sound: "".into(), + sound: default(), step_length: 0.8, gain: 1., reference_distance: None, @@ -41,13 +41,9 @@ pub struct FootstepBundle { pub global_transform: GlobalTransform, } -fn added( - mut commands: Commands, - asset_server: Res, - footsteps: Query<(Entity, &Footstep), Added>, -) { +fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added>) { 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| { entity.insert(Sound { buffer, diff --git a/src/sound/icon.rs b/src/sound/icon.rs index 6f11717..f0df503 100644 --- a/src/sound/icon.rs +++ b/src/sound/icon.rs @@ -1,7 +1,7 @@ use std::{fmt::Debug, hash::Hash, time::Duration}; -use bevy::{asset::HandleId, prelude::*, transform::TransformSystem}; -use bevy_synthizer::{DistanceMax, DistanceRef, Rolloff, Sound}; +use bevy::{prelude::*, transform::TransformSystem}; +use bevy_synthizer::{Buffer, DistanceMax, DistanceRef, Rolloff, Sound}; use rand::random; @@ -14,7 +14,7 @@ use crate::{ #[derive(Component, Clone, Debug)] pub struct SoundIcon { - pub sound: HandleId, + pub sound: Handle, pub gain: f64, pub pitch: f64, pub reference_distance: Option, @@ -27,7 +27,7 @@ impl Default for SoundIcon { fn default() -> Self { let seconds = random::() + 4.5; let mut icon = Self { - sound: "".into(), + sound: default(), gain: 1., pitch: 1., reference_distance: None, @@ -50,13 +50,9 @@ pub struct SoundIconBundle { pub global_transform: GlobalTransform, } -fn added( - mut commands: Commands, - asset_server: Res, - icons: Query<(Entity, &SoundIcon), Added>, -) { +fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added>) { for (entity, icon) in icons.iter() { - let buffer = asset_server.get_handle(icon.sound); + let buffer = icon.sound.clone(); let gain = icon.gain; let pitch = icon.pitch; let looping = icon.interval.is_none(); @@ -101,7 +97,6 @@ fn update( Option<&DistanceMax>, Option<&Rolloff>, )>, - asset_server: Res, ) where S: 'static + Clone + Debug + Eq + Hash + Send + Sync, { @@ -139,7 +134,7 @@ fn update( interval.reset(); } } - let buffer = asset_server.get_handle(icon.sound); + let buffer = icon.sound.clone(); if sound.buffer != buffer { sound.buffer = buffer; }