com.puzzlescapegames.services 1.1.8 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,3 @@
1
- using DG.Tweening;
2
1
  using UnityEngine;
3
2
  using UnityEngine.Audio;
4
3
 
@@ -13,9 +12,6 @@ namespace PuzzlescapeGames.Services.AudioService
13
12
  private const string SFX = "SFX";
14
13
  private const string MUSIC = "Music";
15
14
 
16
- private AudioSource _musicSource;
17
- private Tween _musicFade;
18
-
19
15
  private readonly AudioSourceFactory _audioFactory;
20
16
  private readonly AudioMixer _audioMixer;
21
17
 
@@ -25,55 +21,6 @@ namespace PuzzlescapeGames.Services.AudioService
25
21
  _audioMixer = audioMixer;
26
22
  }
27
23
 
28
- public void PlayMusic( AudioClip clip )
29
- {
30
- CreateMusic().PlayLoop( clip );
31
- }
32
-
33
- public void ChangedMusic( AudioClip clip, float fadeOut = 0.33f, float fadeIn = 0.33f )
34
- {
35
- _musicFade?.Kill();
36
-
37
- if ( _musicSource == null )
38
- {
39
- PlayMusic( clip );
40
- _musicSource.Source.volume = 0f;
41
- _musicFade = _musicSource.DoVolume( 1f, fadeIn );
42
- }
43
- else
44
- {
45
- _musicFade = _musicSource
46
- .DoVolume( 0f, fadeOut )
47
- .OnComplete( VolumeCompleted );
48
- }
49
-
50
- void VolumeCompleted()
51
- {
52
- PlayMusic( clip );
53
- _musicFade = _musicSource.DoVolume( 1f, fadeIn );
54
- }
55
- }
56
-
57
- public void MuteMusic( bool trigger )
58
- {
59
- CreateMusic().Mute( trigger );
60
- }
61
-
62
- public void PauseMusic( bool trigger )
63
- {
64
- CreateMusic().Pause( trigger );
65
- }
66
-
67
- public void SetMusicVolume( float value )
68
- {
69
- CreateMusic().Source.volume = value;
70
- }
71
-
72
- public Tween DoMusicVolume( float target, float duration )
73
- {
74
- return CreateMusic().DoVolume( target, duration );
75
- }
76
-
77
24
  public AudioSource PlaySound( AudioClip clip, bool isLoop = false )
78
25
  {
79
26
  var source = CreateSound();
@@ -116,19 +63,15 @@ namespace PuzzlescapeGames.Services.AudioService
116
63
  return source;
117
64
  }
118
65
 
119
- private AudioSource CreateMusic()
66
+ public MusicSource CreateMusic()
120
67
  {
121
- if ( _musicSource == null )
68
+ var source = _audioFactory.Create();
69
+ if ( _audioMixer != null )
122
70
  {
123
- _musicSource = _audioFactory.Create();
124
-
125
- if ( _audioMixer != null )
126
- {
127
- _musicSource.Source.outputAudioMixerGroup = _audioMixer.FindMatchingGroups( MUSIC )[ 0 ];
128
- }
71
+ source.Source.outputAudioMixerGroup = _audioMixer.FindMatchingGroups( MUSIC )[ 0 ];
129
72
  }
130
73
 
131
- return _musicSource;
74
+ return new( source );
132
75
  }
133
76
  }
134
77
  }
@@ -1,22 +1,15 @@
1
- using DG.Tweening;
2
1
  using UnityEngine;
3
2
 
4
3
  namespace PuzzlescapeGames.Services.AudioService
5
4
  {
6
5
  public interface IAudioService
7
6
  {
8
- void PlayMusic( AudioClip clip );
9
- void ChangedMusic( AudioClip clip, float fadeOut = 0.33f, float fadeIn = 0.33f );
10
7
  AudioSource PlaySound( AudioClip clip, bool isLoop = false );
11
-
12
- void SetMusicVolume( float value );
13
- Tween DoMusicVolume( float target, float duration );
14
8
 
15
9
  void SetMixerMasterVolume( float value );
16
10
  void SetMixerMusicVolume( float value );
17
11
  void SetMixerSoundVolume( float value );
18
12
 
19
- void MuteMusic( bool trigger );
20
- void PauseMusic( bool trigger );
13
+ MusicSource CreateMusic();
21
14
  }
22
15
  }
@@ -0,0 +1,67 @@
1
+ using DG.Tweening;
2
+ using System;
3
+ using UnityEngine;
4
+
5
+ namespace PuzzlescapeGames.Services.AudioService
6
+ {
7
+ public sealed class MusicSource
8
+ {
9
+ public AudioSource Audio { get; }
10
+
11
+ private Tween _fade;
12
+
13
+ public MusicSource( AudioSource audioSource )
14
+ {
15
+ Audio = audioSource ?? throw new ArgumentNullException( nameof(audioSource) );
16
+ }
17
+
18
+ public void Play( AudioClip clip )
19
+ {
20
+ Audio.PlayLoop( clip );
21
+ }
22
+
23
+ public void ChangedMusic( AudioClip clip, float fadeOut = 0.33f, float fadeIn = 0.33f )
24
+ {
25
+ _fade?.Kill();
26
+
27
+ if ( _fade == null )
28
+ {
29
+ Play( clip );
30
+ Audio.Source.volume = 0f;
31
+ _fade = Audio.DoVolume( 1f, fadeIn );
32
+ }
33
+ else
34
+ {
35
+ _fade = Audio
36
+ .DoVolume( 0f, fadeOut )
37
+ .OnComplete( VolumeCompleted );
38
+ }
39
+
40
+ void VolumeCompleted()
41
+ {
42
+ Play( clip );
43
+ _fade = Audio.DoVolume( 1f, fadeIn );
44
+ }
45
+ }
46
+
47
+ public void SetMusicVolume( float value )
48
+ {
49
+ Audio.Source.volume = value;
50
+ }
51
+
52
+ public Tween DoMusicVolume( float target, float duration )
53
+ {
54
+ return Audio.DoVolume( target, duration );
55
+ }
56
+
57
+ public void PauseMusic( bool trigger )
58
+ {
59
+ Audio.Pause( trigger );
60
+ }
61
+
62
+ public void MuteMusic( bool trigger )
63
+ {
64
+ Audio.Mute( trigger );
65
+ }
66
+ }
67
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 11ca154f888449738793f43b33653453
3
+ timeCreated: 1737110170
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name" : "com.puzzlescapegames.services",
3
3
  "displayName" : "Puzzlescape Games Services",
4
- "version" : "1.1.8",
4
+ "version" : "1.1.9",
5
5
  "author" : "Puzzlescape Games",
6
6
  "description" : "Common services.",
7
7
  "repository": {