Switch to typed asset handling.

This commit is contained in:
Nolan Darilek 2022-08-03 09:43:07 -05:00
parent 57091d7e9e
commit e7c9da6f91
2 changed files with 13 additions and 22 deletions

View File

@ -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<Buffer>,
pub step_length: f32,
pub gain: f64,
pub reference_distance: Option<f64>,
@ -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<AssetServer>,
footsteps: Query<(Entity, &Footstep), Added<Footstep>>,
) {
fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added<Footstep>>) {
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,

View File

@ -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<Buffer>,
pub gain: f64,
pub pitch: f64,
pub reference_distance: Option<f64>,
@ -27,7 +27,7 @@ impl Default for SoundIcon {
fn default() -> Self {
let seconds = random::<f32>() + 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<AssetServer>,
icons: Query<(Entity, &SoundIcon), Added<SoundIcon>>,
) {
fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added<SoundIcon>>) {
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<S>(
Option<&DistanceMax>,
Option<&Rolloff>,
)>,
asset_server: Res<AssetServer>,
) where
S: 'static + Clone + Debug + Eq + Hash + Send + Sync,
{
@ -139,7 +134,7 @@ fn update<S>(
interval.reset();
}
}
let buffer = asset_server.get_handle(icon.sound);
let buffer = icon.sound.clone();
if sound.buffer != buffer {
sound.buffer = buffer;
}