Permalink
Browse files

comments for 2d audioplayer

  • Loading branch information...
GandhiGames committed Sep 21, 2016
1 parent 67245eb commit 6f67e08530e90991db0a1f4d2c2e6a04cb7afa12
@@ -1,5 +1,8 @@
using UnityEngine;
/// <summary>
/// Raised when audio clip should be played. Picked up by AudioPlayer2D.
/// </summary>
public class AudioEvent2D : GameEvent, IAudioEvent2D
{
private AudioClip audioClip;
@@ -1,16 +1,32 @@
using UnityEngine;
using System.Collections;
/// <summary>
/// Listens to audio events. Adds audio events to circular array with MaxPending
/// and plays associated audio clip each time step.
/// </summary>
[RequireComponent(typeof(AudioSource))]
public class AudioPlayer2D : MonoBehaviour
{
/// <summary>
/// The maximum number of queued clips. Oldest clips are overwritten when max reached.
/// </summary>
public int MaxPending = 30;
private AudioSource source;
/// <summary>
/// Queue of events to play.
/// </summary>
private IAudioEvent2D[] pending;
/// <summary>
/// Reference to current head of cicular array (index to pop new audio event).
/// </summary>
private int head;
/// <summary>
/// Index to add new events.
/// </summary>
private int tail;
void Awake()
@@ -33,6 +49,9 @@ void OnDisable()
Events.instance.RemoveListener<AudioEvent2D>(OnAudio);
}
/// <summary>
/// Plays pending clips.
/// </summary>
void Update()
{
if (head == tail)
@@ -47,6 +66,8 @@ void Update()
void OnAudio(IAudioEvent2D e)
{
// Do not add duplicate events. Prevents situation where the same
// audio clips are played in parallel increasing the effects volume.
for (int i = head; i != tail; i = (i + 1) % MaxPending)
{
if (pending[i].Audio.name.Equals(e.Audio.name))
@@ -1,6 +1,8 @@
using UnityEngine;
/// <summary>
/// Implemented by all audio events. Provides access to audio clip.
/// </summary>
public interface IAudioEvent2D
{
AudioClip Audio { get; }

0 comments on commit 6f67e08

Please sign in to comment.