@@ -33,7 +33,7 @@ impl Volume {
3333}
3434
3535/// The way Bevy manages the sound playback.
36- #[ derive( Debug , Clone , Copy , Reflect ) ]
36+ #[ derive( Debug , Clone , Copy , Reflect , Default ) ]
3737pub enum PlaybackMode {
3838 /// Play the sound once. Do nothing when it ends.
3939 Once ,
@@ -42,6 +42,7 @@ pub enum PlaybackMode {
4242 /// Despawn the entity and its children when the sound finishes playing.
4343 Despawn ,
4444 /// Remove the audio components from the entity, when the sound finishes playing.
45+ #[ default]
4546 Remove ,
4647}
4748
@@ -53,8 +54,6 @@ pub enum PlaybackMode {
5354#[ derive( Component , Clone , Copy , Debug , Reflect ) ]
5455#[ reflect( Default , Component , Debug ) ]
5556pub struct PlaybackSettings {
56- /// The desired playback behavior.
57- pub mode : PlaybackMode ,
5857 /// Volume to play at.
5958 pub volume : Volume ,
6059 /// Speed to play at.
@@ -77,40 +76,17 @@ pub struct PlaybackSettings {
7776
7877impl Default for PlaybackSettings {
7978 fn default ( ) -> Self {
80- // TODO: what should the default be: ONCE/DESPAWN/REMOVE?
81- Self :: ONCE
79+ Self {
80+ volume : Volume ( 1.0 ) ,
81+ speed : 1.0 ,
82+ paused : false ,
83+ spatial : false ,
84+ spatial_scale : None ,
85+ }
8286 }
8387}
8488
8589impl PlaybackSettings {
86- /// Will play the associated audio source once.
87- pub const ONCE : PlaybackSettings = PlaybackSettings {
88- mode : PlaybackMode :: Once ,
89- volume : Volume ( 1.0 ) ,
90- speed : 1.0 ,
91- paused : false ,
92- spatial : false ,
93- spatial_scale : None ,
94- } ;
95-
96- /// Will play the associated audio source in a loop.
97- pub const LOOP : PlaybackSettings = PlaybackSettings {
98- mode : PlaybackMode :: Loop ,
99- ..PlaybackSettings :: ONCE
100- } ;
101-
102- /// Will play the associated audio source once and despawn the entity afterwards.
103- pub const DESPAWN : PlaybackSettings = PlaybackSettings {
104- mode : PlaybackMode :: Despawn ,
105- ..PlaybackSettings :: ONCE
106- } ;
107-
108- /// Will play the associated audio source once and remove the audio components afterwards.
109- pub const REMOVE : PlaybackSettings = PlaybackSettings {
110- mode : PlaybackMode :: Remove ,
111- ..PlaybackSettings :: ONCE
112- } ;
113-
11490 /// Helper to start in a paused state.
11591 pub const fn paused ( mut self ) -> Self {
11692 self . paused = true ;
@@ -251,7 +227,7 @@ pub type AudioBundle = AudioSourceBundle<AudioSource>;
251227#[ derive( Component , Reflect ) ]
252228#[ reflect( Component ) ]
253229#[ require( PlaybackSettings ) ]
254- pub struct AudioPlayer < Source = AudioSource > ( pub Handle < Source > )
230+ pub struct AudioPlayer < Source = AudioSource > ( pub Handle < Source > , pub PlaybackMode )
255231where
256232 Source : Asset + Decodable ;
257233
@@ -260,7 +236,7 @@ where
260236 Source : Asset + Decodable ,
261237{
262238 fn clone ( & self ) -> Self {
263- Self ( self . 0 . clone ( ) )
239+ Self ( self . 0 . clone ( ) , self . 1 . clone ( ) )
264240 }
265241}
266242
@@ -271,7 +247,27 @@ impl AudioPlayer<AudioSource> {
271247 /// initialize an [`AudioPlayer`] with a different type, just initialize it directly using normal
272248 /// tuple struct syntax.
273249 pub fn new ( source : Handle < AudioSource > ) -> Self {
274- Self ( source)
250+ Self ( source, PlaybackMode :: default ( ) )
251+ }
252+
253+ /// Creates a new [`AudioPlayer`] that plays the sound once.
254+ pub fn with_once ( source : Handle < AudioSource > ) -> Self {
255+ Self ( source, PlaybackMode :: Once )
256+ }
257+
258+ /// Creates a new [`AudioPlayer`] that loops the sound forever.
259+ pub fn with_loop ( source : Handle < AudioSource > ) -> Self {
260+ Self ( source, PlaybackMode :: Loop )
261+ }
262+
263+ /// Creates a new [`AudioPlayer`] that despawns the entity when the sound finishes playing.
264+ pub fn with_despawn ( source : Handle < AudioSource > ) -> Self {
265+ Self ( source, PlaybackMode :: Despawn )
266+ }
267+
268+ /// Creates a new [`AudioPlayer`] that removes the audio component from the entity when the sound finishes playing.
269+ pub fn with_remove ( source : Handle < AudioSource > ) -> Self {
270+ Self ( source, PlaybackMode :: Remove )
275271 }
276272}
277273
@@ -314,7 +310,7 @@ impl<T: Asset + Decodable> Clone for AudioSourceBundle<T> {
314310impl < T : Decodable + Asset > Default for AudioSourceBundle < T > {
315311 fn default ( ) -> Self {
316312 Self {
317- source : AudioPlayer ( Handle :: default ( ) ) ,
313+ source : AudioPlayer ( Handle :: default ( ) , PlaybackMode :: Once ) ,
318314 settings : Default :: default ( ) ,
319315 }
320316 }
0 commit comments