Sound is now clonable.

This commit is contained in:
Nolan Darilek 2021-05-25 09:12:21 -05:00
parent de94dd5322
commit 9018f3cf15

View File

@ -1,7 +1,9 @@
use std::collections::HashMap; use std::{
use std::io::Cursor; collections::HashMap,
use std::ops::{Deref, DerefMut}; io::Cursor,
use std::sync::Arc; ops::{Deref, DerefMut},
sync::{Arc, Mutex},
};
pub use alto::efx; pub use alto::efx;
pub use alto::Context; pub use alto::Context;
@ -147,7 +149,7 @@ impl Default for SoundState {
} }
} }
#[derive(Reflect)] #[derive(Clone, Reflect)]
pub struct Sound { pub struct Sound {
pub buffer: Handle<Buffer>, pub buffer: Handle<Buffer>,
pub state: SoundState, pub state: SoundState,
@ -159,7 +161,7 @@ pub struct Sound {
pub rolloff_factor: f32, pub rolloff_factor: f32,
pub bypass_global_effects: bool, pub bypass_global_effects: bool,
#[reflect(ignore)] #[reflect(ignore)]
pub source: Option<StaticSource>, pub source: Option<Arc<Mutex<StaticSource>>>,
} }
impl Default for Sound { impl Default for Sound {
@ -255,14 +257,16 @@ fn source_update(
match &sound.state { match &sound.state {
SoundState::Stopped => { SoundState::Stopped => {
if let Some(source) = sound.source.as_mut() { if let Some(source) = sound.source.as_mut() {
let mut source = source.lock().unwrap();
source.stop(); source.stop();
sound.source = None;
} }
sound.source = None;
} }
SoundState::Playing => { SoundState::Playing => {
if let Some(source) = sound.source.as_mut() { if let Some(source) = sound.source.as_mut() {
let mut source = source.lock().unwrap();
sync_source_and_components( sync_source_and_components(
source, &mut source,
transform, transform,
global_transform, global_transform,
gain, gain,
@ -296,15 +300,16 @@ fn source_update(
&mut **global_effects, &mut **global_effects,
); );
source.play(); source.play();
sound.source = Some(source); sound.source = Some(Arc::new(Mutex::new(source)));
} }
} }
SoundState::Paused => { SoundState::Paused => {
if let Some(source) = sound.source.as_mut() { if let Some(source) = sound.source.as_mut() {
let mut source = source.lock().unwrap();
if source.state() != SourceState::Paused { if source.state() != SourceState::Paused {
source.pause(); source.pause();
sync_source_and_components( sync_source_and_components(
source, &mut source,
transform, transform,
global_transform, global_transform,
gain, gain,
@ -336,12 +341,13 @@ fn source_update(
&mut **global_effects, &mut **global_effects,
); );
source.pause(); source.pause();
sound.source = Some(source); sound.source = Some(Arc::new(Mutex::new(source)));
} }
} }
} }
if let Some(source) = &sound.source { if let Some(source) = sound.source.clone() {
sound.state = match source.state() { let source = source.lock().unwrap();
sound.state = match &source.state() {
SourceState::Initial => SoundState::Stopped, SourceState::Initial => SoundState::Stopped,
SourceState::Playing => SoundState::Playing, SourceState::Playing => SoundState::Playing,
SourceState::Paused => SoundState::Paused, SourceState::Paused => SoundState::Paused,
@ -355,6 +361,7 @@ fn source_update(
impl Sound { impl Sound {
pub fn stop(&mut self) { pub fn stop(&mut self) {
if let Some(source) = self.source.as_mut() { if let Some(source) = self.source.as_mut() {
let mut source = source.lock().unwrap();
source.stop(); source.stop();
} }
self.state = SoundState::Stopped; self.state = SoundState::Stopped;
@ -363,6 +370,7 @@ impl Sound {
pub fn play(&mut self) { pub fn play(&mut self) {
if let Some(source) = self.source.as_mut() { if let Some(source) = self.source.as_mut() {
let mut source = source.lock().unwrap();
source.play(); source.play();
} }
self.state = SoundState::Playing; self.state = SoundState::Playing;
@ -370,6 +378,7 @@ impl Sound {
pub fn pause(&mut self) { pub fn pause(&mut self) {
if let Some(source) = self.source.as_mut() { if let Some(source) = self.source.as_mut() {
let mut source = source.lock().unwrap();
source.pause(); source.pause();
} }
self.state = SoundState::Paused; self.state = SoundState::Paused;