This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
project-undercover/Project Undercover/Assets/Scripts/Sound.cs

118 lines
3 KiB
C#
Raw Permalink Normal View History

2017-11-30 14:32:12 -06:00
using UnityEngine;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class Sound
{
public AudioManager audioManager;
public string name;
public AudioClip clip;
public AudioSource source;
public Action<Sound> callback;
public bool loop;
public bool interrupts;
private HashSet<Sound> interruptedSounds =
new HashSet<Sound>();
/// returns a float from 0.0 to 1.0 representing how much
/// of the sound has been played so far
public float progress
{
get
{
if (source == null || clip == null)
return 0f;
return (float)source.timeSamples / (float)clip.samples;
}
}
/// returns true if the sound has finished playing
/// will always be false for looping sounds
public bool finished
{
get
{
return !loop && progress >= 1f;
}
}
/// returns true if the sound is currently playing,
/// false if it is paused or finished
/// can be set to true or false to play/pause the sound
/// will register the sound before playing
public bool playing
{
get
{
return source != null && source.isPlaying;
}
set
{
if (value)
{
audioManager.RegisterSound(this);
}
PlayOrPause(value, interrupts);
}
}
/// Try to avoid calling this directly
/// Use AudioManager.NewSound instead
public Sound(string newName)
{
name = newName;
clip = (AudioClip)Resources.Load(name, typeof(AudioClip));
if (clip == null)
throw new Exception("Couldn't find AudioClip with name '" + name + "'. Are you sure the file is in a folder named 'Resources'?");
}
public void Update()
{
if (source != null)
source.loop = loop;
if (finished)
Finish();
}
/// Try to avoid calling this directly
/// Use the Sound.playing property instead
public void PlayOrPause(bool play, bool pauseOthers)
{
if (pauseOthers)
{
if (play)
{
interruptedSounds = new HashSet<Sound>(audioManager.sounds.Where(snd => snd.playing &&
snd != this));
}
interruptedSounds.ToList().ForEach(sound => sound.PlayOrPause(!play, false));
}
if (play && !source.isPlaying)
{
source.Play();
}
else
{
source.Pause();
}
}
/// performs necessary actions when a sound finishes
public void Finish()
{
PlayOrPause(false, true);
if (callback != null)
callback(this);
MonoBehaviour.Destroy(source);
source = null;
}
/// Reset the sound to its beginning
public void Reset()
{
source.time = 0f;
}
}