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. 93
      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()
{
MusicController musicController = MusicController.GetInstance();
IMusicController musicController = MusicController.GetInstance();
if (musicController != null)
{
float startTime = Mathf.Max(0, atTime);

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

@ -32,7 +32,7 @@ namespace Fungus
return;
}
MusicController musicController = MusicController.GetInstance();
IMusicController musicController = MusicController.GetInstance();
if (musicController != null)
{
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)
{
musicController.SetAudioPitch(pitch, fadeDuration, onComplete);

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

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

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

@ -16,7 +16,7 @@ namespace Fungus
{
public override void OnEnter()
{
MusicController musicController = MusicController.GetInstance();
IMusicController musicController = MusicController.GetInstance();
if (musicController != null)
{
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:

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

@ -5,19 +5,16 @@
namespace Fungus
{
/// <summary>
/// Singleton music manager component.
/// Provides basic music and sound effect functionality.
/// Music playback persists across scene loads.
/// </summary>
[RequireComponent(typeof(AudioSource))]
public class MusicController : MonoBehaviour
public class MusicController : MonoBehaviour, IMusicController
{
static MusicController instance;
static IMusicController instance;
// Returns the MusicController singleton instance.
// Will create a MusicController game object if none currently exists.
static public MusicController GetInstance()
/// <summary>
/// Returns the MusicController singleton instance.
/// Will create a MusicController game object if none currently exists.
/// </summary>
static public IMusicController GetInstance()
{
if (instance == null)
{
@ -35,10 +32,8 @@ namespace Fungus
GetComponent<AudioSource>().loop = true;
}
/// <summary>
/// // Plays game music using an audio clip.
/// One music clip may be played at a time.
/// </summary>
#region IMusicController implementation
public void PlayMusic(AudioClip musicClip, bool loop, float fadeDuration, float atTime)
{
AudioSource audioSource = GetComponent<AudioSource>();
@ -73,35 +68,30 @@ namespace Fungus
}
}
/// <summary>
/// Stops playing game music.
/// </summary>
public virtual void StopMusic()
public virtual void PlaySound(AudioClip soundClip, float volume)
{
GetComponent<AudioSource>().Stop();
GetComponent<AudioSource>().PlayOneShot(soundClip, volume);
}
/// <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>
public virtual void SetAudioVolume(float volume, float duration, System.Action onComplete)
public virtual void SetAudioPitch(float pitch, float duration, System.Action onComplete)
{
AudioSource audio = GetComponent<AudioSource>();
if (Mathf.Approximately(duration, 0f))
if (duration == 0f)
{
audio.volume = volume;
audio.pitch = pitch;
if (onComplete != null)
{
onComplete();
}
return;
}
LeanTween.value(gameObject,
audio.volume,
volume,
duration).setOnUpdate( (v) => {
audio.volume = v;
audio.pitch,
pitch,
duration).setOnUpdate( (p) => {
audio.pitch = p;
}).setOnComplete( () => {
if (onComplete != null)
{
@ -110,31 +100,21 @@ namespace Fungus
});
}
/// <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>
public virtual void SetAudioPitch(float pitch, float duration, System.Action onComplete)
public virtual void SetAudioVolume(float volume, float duration, System.Action onComplete)
{
AudioSource audio = GetComponent<AudioSource>();
if (duration == 0f)
{
audio.pitch = pitch;
if (onComplete != null)
if (Mathf.Approximately(duration, 0f))
{
onComplete();
}
audio.volume = volume;
return;
}
LeanTween.value(gameObject,
audio.pitch,
pitch,
duration).setOnUpdate( (p) => {
audio.pitch = p;
audio.volume,
volume,
duration).setOnUpdate( (v) => {
audio.volume = v;
}).setOnComplete( () => {
if (onComplete != null)
{
@ -143,20 +123,11 @@ namespace Fungus
});
}
/// <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>
public virtual void PlaySound(AudioClip soundClip, float volume)
public virtual void StopMusic()
{
GetComponent<AudioSource>().PlayOneShot(soundClip, volume);
GetComponent<AudioSource>().Stop();
}
public virtual void PlaySoundAtTime(AudioClip soundClip, float volume, float atTime)
{
GetComponent<AudioSource>().time = atTime; // This may not work BK
GetComponent<AudioSource>().PlayOneShot(soundClip, volume);
}
#endregion
}
}

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

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

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

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

Loading…
Cancel
Save