Refactor SoundIcon and Footstep to new Audio type.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Nolan Darilek 2023-04-03 10:39:43 -05:00
parent 6eddde886e
commit 6f025d49aa
2 changed files with 12 additions and 16 deletions

View File

@ -1,15 +1,14 @@
use std::collections::HashMap; use std::collections::HashMap;
use bevy::{prelude::*, transform::TransformSystem}; use bevy::{prelude::*, transform::TransformSystem};
use bevy_synthizer::{Buffer, Sound}; use bevy_synthizer::{Audio, Sound};
use rand::random; use rand::random;
use crate::core::PointLike; use crate::core::PointLike;
#[derive(Component, Clone, Debug, Reflect)] #[derive(Component, Clone, Debug)]
#[reflect(Component)]
pub struct Footstep { pub struct Footstep {
pub buffer: Handle<Buffer>, pub audio: Audio,
pub step_length: f32, pub step_length: f32,
pub gain: f64, pub gain: f64,
pub pitch: Option<f64>, pub pitch: Option<f64>,
@ -19,7 +18,7 @@ pub struct Footstep {
impl Default for Footstep { impl Default for Footstep {
fn default() -> Self { fn default() -> Self {
Self { Self {
buffer: default(), audio: default(),
step_length: 0.8, step_length: 0.8,
gain: 1., gain: 1.,
pitch: None, pitch: None,
@ -30,7 +29,7 @@ impl Default for Footstep {
fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added<Footstep>>) { fn added(mut commands: Commands, footsteps: Query<(Entity, &Footstep), Added<Footstep>>) {
for (entity, footstep) in &footsteps { for (entity, footstep) in &footsteps {
let buffer = footstep.buffer.clone(); let buffer = footstep.audio.clone();
commands.entity(entity).insert(Sound { commands.entity(entity).insert(Sound {
audio: buffer.into(), audio: buffer.into(),
paused: true, paused: true,
@ -73,8 +72,7 @@ pub struct FootstepPlugin;
impl Plugin for FootstepPlugin { impl Plugin for FootstepPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.register_type::<Footstep>() app.add_system(added.in_base_set(CoreSet::PreUpdate))
.add_system(added.in_base_set(CoreSet::PreUpdate))
.add_system( .add_system(
update update
.after(TransformSystem::TransformPropagate) .after(TransformSystem::TransformPropagate)

View File

@ -1,7 +1,7 @@
use std::{fmt::Debug, time::Duration}; use std::{fmt::Debug, time::Duration};
use bevy::prelude::*; use bevy::prelude::*;
use bevy_synthizer::{Audio, Buffer, Sound}; use bevy_synthizer::{Audio, Sound};
use rand::random; use rand::random;
@ -11,10 +11,9 @@ use crate::{
visibility::{VisibilityChanged, Visible, VisibleEntities}, visibility::{VisibilityChanged, Visible, VisibleEntities},
}; };
#[derive(Component, Clone, Debug, Reflect)] #[derive(Component, Clone, Debug)]
#[reflect(Component)]
pub struct SoundIcon { pub struct SoundIcon {
pub buffer: Handle<Buffer>, pub audio: Audio,
pub gain: f64, pub gain: f64,
pub pitch: f64, pub pitch: f64,
pub interval: Option<Timer>, pub interval: Option<Timer>,
@ -24,7 +23,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 {
buffer: default(), audio: default(),
gain: 1., gain: 1.,
pitch: 1., pitch: 1.,
interval: Some(Timer::from_seconds(seconds, TimerMode::Repeating)), interval: Some(Timer::from_seconds(seconds, TimerMode::Repeating)),
@ -39,7 +38,7 @@ impl Default for SoundIcon {
fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added<SoundIcon>>) { fn added(mut commands: Commands, icons: Query<(Entity, &SoundIcon), Added<SoundIcon>>) {
for (entity, icon) in &icons { for (entity, icon) in &icons {
let buffer = icon.buffer.clone(); let buffer = icon.audio.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();
@ -93,7 +92,7 @@ fn update<S>(
interval.reset(); interval.reset();
} }
} }
let buffer = icon.buffer.clone(); let buffer = icon.audio.clone();
let audio: Audio = buffer.into(); let audio: Audio = buffer.into();
if sound.audio != audio { if sound.audio != audio {
sound.audio = audio; sound.audio = audio;
@ -198,7 +197,6 @@ where
{ {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.insert_resource(self.clone()) app.insert_resource(self.clone())
.register_type::<SoundIcon>()
.add_systems((added, reset_timer_on_visibility_gain).in_base_set(CoreSet::PreUpdate)) .add_systems((added, reset_timer_on_visibility_gain).in_base_set(CoreSet::PreUpdate))
.add_systems( .add_systems(
(exploration_focus_changed, update::<S>) (exploration_focus_changed, update::<S>)