Browse Source

Refactored MusicController to use IMusicController interface

master
Christopher 8 years ago
parent
commit
a3e19d83a3
  1. 2
      Assets/Fungus/Audio/Scripts/Commands/PlayMusic.cs
  2. 2
      Assets/Fungus/Audio/Scripts/Commands/PlaySound.cs
  3. 2
      Assets/Fungus/Audio/Scripts/Commands/SetAudioPitch.cs
  4. 2
      Assets/Fungus/Audio/Scripts/Commands/SetAudioVolume.cs
  5. 2
      Assets/Fungus/Audio/Scripts/Commands/StopMusic.cs
  6. 45
      Assets/Fungus/Audio/Scripts/IMusicController.cs
  7. 12
      Assets/Fungus/Audio/Scripts/IMusicController.cs.meta
  8. 95
      Assets/Fungus/Audio/Scripts/MusicController.cs
  9. 1
      Assets/Fungus/Narrative/Scripts/IMenuDialog.cs
  10. 1
      Assets/Fungus/Narrative/Scripts/ISayDialog.cs

2
Assets/Fungus/Audio/Scripts/Commands/PlayMusic.cs

@ -28,7 +28,7 @@ namespace Fungus
public override void OnEnter() public override void OnEnter()
{ {
MusicController musicController = MusicController.GetInstance(); IMusicController musicController = MusicController.GetInstance();
if (musicController != null) if (musicController != null)
{ {
float startTime = Mathf.Max(0, atTime); float startTime = Mathf.Max(0, atTime);

2
Assets/Fungus/Audio/Scripts/Commands/PlaySound.cs

@ -32,7 +32,7 @@ namespace Fungus
return; return;
} }
MusicController musicController = MusicController.GetInstance(); IMusicController musicController = MusicController.GetInstance();
if (musicController != null) if (musicController != null)
{ {
musicController.PlaySound(soundClip, volume); musicController.PlaySound(soundClip, volume);

2
Assets/Fungus/Audio/Scripts/Commands/SetAudioPitch.cs

@ -34,7 +34,7 @@ namespace Fungus
} }
}; };
MusicController musicController = MusicController.GetInstance(); IMusicController musicController = MusicController.GetInstance();
if (musicController != null) if (musicController != null)
{ {
musicController.SetAudioPitch(pitch, fadeDuration, onComplete); musicController.SetAudioPitch(pitch, fadeDuration, onComplete);

2
Assets/Fungus/Audio/Scripts/Commands/SetAudioVolume.cs

@ -27,7 +27,7 @@ namespace Fungus
public override void OnEnter() public override void OnEnter()
{ {
MusicController musicController = MusicController.GetInstance(); IMusicController musicController = MusicController.GetInstance();
if (musicController != null) if (musicController != null)
{ {
musicController.SetAudioVolume(volume, fadeDuration, () => { musicController.SetAudioVolume(volume, fadeDuration, () => {

2
Assets/Fungus/Audio/Scripts/Commands/StopMusic.cs

@ -16,7 +16,7 @@ namespace Fungus
{ {
public override void OnEnter() public override void OnEnter()
{ {
MusicController musicController = MusicController.GetInstance(); IMusicController musicController = MusicController.GetInstance();
if (musicController != null) if (musicController != null)
{ {
musicController.StopMusic(); musicController.StopMusic();

45
Assets/Fungus/Audio/Scripts/IMusicController.cs

@ -0,0 +1,45 @@
using UnityEngine;
namespace Fungus
{
/// <summary>
/// Music manager which provides basic music and sound effect functionality.
/// Music playback persists across scene loads.
/// </summary>
public interface IMusicController
{
/// <summary>
/// Plays game music using an audio clip.
/// One music clip may be played at a time.
/// </summary>
void PlayMusic(AudioClip musicClip, bool loop, float fadeDuration, float atTime);
/// <summary>
/// Plays a sound effect once, at the specified volume.
/// </summary>
/// <param name="soundClip">The sound effect clip to play.</param>
/// <param name="volume">The volume level of the sound effect.</param>
void PlaySound(AudioClip soundClip, float volume);
/// <summary>
/// Shifts the game music pitch to required value over a period of time.
/// </summary>
/// <param name="pitch">The new music pitch value.</param>
/// <param name="duration">The length of time in seconds needed to complete the pitch change.</param>
/// <param name="onComplete">A delegate method to call when the pitch shift has completed.</param>
void SetAudioPitch(float pitch, float duration, System.Action onComplete);
/// <summary>
/// Fades the game music volume to required level over a period of time.
/// </summary>
/// <param name="volume">The new music volume value [0..1]</param>
/// <param name="duration">The length of time in seconds needed to complete the volume change.</param>
/// <param name="onComplete">Delegate function to call when fade completes.</param>
void SetAudioVolume(float volume, float duration, System.Action onComplete);
/// <summary>
/// Stops playing game music.
/// </summary>
void StopMusic();
}
}

12
Assets/Fungus/Audio/Scripts/IMusicController.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: efedec0242be143678aaa7075aa5467e
timeCreated: 1473429999
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

95
Assets/Fungus/Audio/Scripts/MusicController.cs

@ -5,19 +5,16 @@
namespace Fungus namespace Fungus
{ {
/// <summary>
/// Singleton music manager component.
/// Provides basic music and sound effect functionality.
/// Music playback persists across scene loads.
/// </summary>
[RequireComponent(typeof(AudioSource))] [RequireComponent(typeof(AudioSource))]
public class MusicController : MonoBehaviour public class MusicController : MonoBehaviour, IMusicController
{ {
static MusicController instance; static IMusicController instance;
// Returns the MusicController singleton instance. /// <summary>
// Will create a MusicController game object if none currently exists. /// Returns the MusicController singleton instance.
static public MusicController GetInstance() /// Will create a MusicController game object if none currently exists.
/// </summary>
static public IMusicController GetInstance()
{ {
if (instance == null) if (instance == null)
{ {
@ -34,11 +31,9 @@ namespace Fungus
GetComponent<AudioSource>().playOnAwake = false; GetComponent<AudioSource>().playOnAwake = false;
GetComponent<AudioSource>().loop = true; GetComponent<AudioSource>().loop = true;
} }
#region IMusicController implementation
/// <summary>
/// // Plays game music using an audio clip.
/// One music clip may be played at a time.
/// </summary>
public void PlayMusic(AudioClip musicClip, bool loop, float fadeDuration, float atTime) public void PlayMusic(AudioClip musicClip, bool loop, float fadeDuration, float atTime)
{ {
AudioSource audioSource = GetComponent<AudioSource>(); AudioSource audioSource = GetComponent<AudioSource>();
@ -72,36 +67,31 @@ namespace Fungus
}); });
} }
} }
/// <summary> public virtual void PlaySound(AudioClip soundClip, float volume)
/// Stops playing game music.
/// </summary>
public virtual void StopMusic()
{ {
GetComponent<AudioSource>().Stop(); GetComponent<AudioSource>().PlayOneShot(soundClip, volume);
} }
/// <summary> public virtual void SetAudioPitch(float pitch, float duration, System.Action onComplete)
/// Fades the game music volume to required level over a period of time.
/// </summary>
/// <param name="volume">The new music volume value [0..1]</param>
/// <param name="duration">The length of time in seconds needed to complete the volume change.</param>
/// <param name="onComplete">Delegate function to call when fade completes.</param>
public virtual void SetAudioVolume(float volume, float duration, System.Action onComplete)
{ {
AudioSource audio = GetComponent<AudioSource>(); AudioSource audio = GetComponent<AudioSource>();
if (Mathf.Approximately(duration, 0f)) if (duration == 0f)
{ {
audio.volume = volume; audio.pitch = pitch;
if (onComplete != null)
{
onComplete();
}
return; return;
} }
LeanTween.value(gameObject, LeanTween.value(gameObject,
audio.volume, audio.pitch,
volume, pitch,
duration).setOnUpdate( (v) => { duration).setOnUpdate( (p) => {
audio.volume = v; audio.pitch = p;
}).setOnComplete( () => { }).setOnComplete( () => {
if (onComplete != null) if (onComplete != null)
{ {
@ -110,31 +100,21 @@ namespace Fungus
}); });
} }
/// <summary> public virtual void SetAudioVolume(float volume, float duration, System.Action onComplete)
/// Shifts the game music pitch to required value over a period of time.
/// </summary>
/// <param name="pitch">The new music pitch value.</param>
/// <param name="duration">The length of time in seconds needed to complete the pitch change.</param>
/// <param name="onComplete">A delegate method to call when the pitch shift has completed.</param>
public virtual void SetAudioPitch(float pitch, float duration, System.Action onComplete)
{ {
AudioSource audio = GetComponent<AudioSource>(); AudioSource audio = GetComponent<AudioSource>();
if (duration == 0f) if (Mathf.Approximately(duration, 0f))
{ {
audio.pitch = pitch; audio.volume = volume;
if (onComplete != null)
{
onComplete();
}
return; return;
} }
LeanTween.value(gameObject, LeanTween.value(gameObject,
audio.pitch, audio.volume,
pitch, volume,
duration).setOnUpdate( (p) => { duration).setOnUpdate( (v) => {
audio.pitch = p; audio.volume = v;
}).setOnComplete( () => { }).setOnComplete( () => {
if (onComplete != null) if (onComplete != null)
{ {
@ -143,20 +123,11 @@ namespace Fungus
}); });
} }
/// <summary> public virtual void StopMusic()
/// Plays a sound effect once, at the specified volume.
/// </summary>
/// <param name="soundClip">The sound effect clip to play.</param>
/// <param name="volume">The volume level of the sound effect.</param>
public virtual void PlaySound(AudioClip soundClip, float volume)
{ {
GetComponent<AudioSource>().PlayOneShot(soundClip, volume); GetComponent<AudioSource>().Stop();
} }
public virtual void PlaySoundAtTime(AudioClip soundClip, float volume, float atTime) #endregion
{
GetComponent<AudioSource>().time = atTime; // This may not work BK
GetComponent<AudioSource>().PlayOneShot(soundClip, volume);
}
} }
} }

1
Assets/Fungus/Narrative/Scripts/IMenuDialog.cs

@ -1,5 +1,4 @@
using UnityEngine; using UnityEngine;
using System.Collections;
namespace Fungus namespace Fungus
{ {

1
Assets/Fungus/Narrative/Scripts/ISayDialog.cs

@ -1,5 +1,4 @@
using UnityEngine; using UnityEngine;
using System.Collections;
namespace Fungus namespace Fungus
{ {

Loading…
Cancel
Save