com.puzzlescapegames.services 1.1.7 → 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,45 +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
24
  public AudioSource PlaySound( AudioClip clip, bool isLoop = false )
68
25
  {
69
26
  var source = CreateSound();
@@ -80,17 +37,17 @@ namespace PuzzlescapeGames.Services.AudioService
80
37
  return source;
81
38
  }
82
39
 
83
- public void SetMasterVolume( float value )
40
+ public void SetMixerMasterVolume( float value )
84
41
  {
85
42
  _audioMixer.SetFloat( MIXER_MASTER, value );
86
43
  }
87
44
 
88
- public void SetMusicVolume( float value )
45
+ public void SetMixerMusicVolume( float value )
89
46
  {
90
47
  _audioMixer.SetFloat( MIXER_MUSIC, value );
91
48
  }
92
49
 
93
- public void SetSoundVolume( float value )
50
+ public void SetMixerSoundVolume( float value )
94
51
  {
95
52
  _audioMixer.SetFloat( MIXER_SFX, value );
96
53
  }
@@ -106,19 +63,15 @@ namespace PuzzlescapeGames.Services.AudioService
106
63
  return source;
107
64
  }
108
65
 
109
- private AudioSource CreateMusic()
66
+ public MusicSource CreateMusic()
110
67
  {
111
- if ( _musicSource == null )
68
+ var source = _audioFactory.Create();
69
+ if ( _audioMixer != null )
112
70
  {
113
- _musicSource = _audioFactory.Create();
114
-
115
- if ( _audioMixer != null )
116
- {
117
- _musicSource.Source.outputAudioMixerGroup = _audioMixer.FindMatchingGroups( MUSIC )[ 0 ];
118
- }
71
+ source.Source.outputAudioMixerGroup = _audioMixer.FindMatchingGroups( MUSIC )[ 0 ];
119
72
  }
120
73
 
121
- return _musicSource;
74
+ return new( source );
122
75
  }
123
76
  }
124
77
  }
@@ -4,15 +4,12 @@ namespace PuzzlescapeGames.Services.AudioService
4
4
  {
5
5
  public interface IAudioService
6
6
  {
7
- void PlayMusic( AudioClip clip );
8
- void ChangedMusic( AudioClip clip, float fadeOut = 0.33f, float fadeIn = 0.33f );
9
7
  AudioSource PlaySound( AudioClip clip, bool isLoop = false );
8
+
9
+ void SetMixerMasterVolume( float value );
10
+ void SetMixerMusicVolume( float value );
11
+ void SetMixerSoundVolume( float value );
10
12
 
11
- void SetMasterVolume( float value );
12
- void SetMusicVolume( float value );
13
- void SetSoundVolume( float value );
14
-
15
- void MuteMusic( bool trigger );
16
- void PauseMusic( bool trigger );
13
+ MusicSource CreateMusic();
17
14
  }
18
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.7",
4
+ "version" : "1.1.9",
5
5
  "author" : "Puzzlescape Games",
6
6
  "description" : "Common services.",
7
7
  "repository": {