Merge branch 'master' of https://github.com/megaminerjenny/HeavenStudio into megaminerjenny-master

This commit is contained in:
Slaith
2022-03-07 20:53:30 -08:00
177 changed files with 15561 additions and 3728 deletions

View File

@ -40,7 +40,7 @@ namespace RhythmHeavenMania.Util
FindJukebox().GetComponent<AudioSource>().volume = volume;
}
public static AudioSource PlayOneShot(string name, float beat = -1)
public static AudioSource PlayOneShot(string name, float beat = -1, float pitch = 1f, float volume = 1f, bool looping = false)
{
GameObject oneShot = new GameObject("oneShot");
@ -52,6 +52,9 @@ namespace RhythmHeavenMania.Util
AudioClip clip = Resources.Load<AudioClip>($"Sfx/{name}");
snd.clip = clip;
snd.beat = beat;
snd.pitch = pitch;
snd.volume = volume;
snd.looping = looping;
// snd.pitch = (clip.length / Conductor.instance.secPerBeat);
GameManager.instance.SoundObjects.Add(oneShot);
@ -59,7 +62,7 @@ namespace RhythmHeavenMania.Util
return audioSource;
}
public static AudioSource PlayOneShotScheduled(string name, double targetTime)
public static AudioSource PlayOneShotScheduled(string name, double targetTime, float pitch = 1f, float volume = 1f, bool looping = false)
{
GameObject oneShot = new GameObject("oneShotScheduled");
@ -71,6 +74,9 @@ namespace RhythmHeavenMania.Util
var clip = Resources.Load<AudioClip>($"Sfx/{name}");
audioSource.clip = clip;
snd.clip = clip;
snd.pitch = pitch;
snd.volume = volume;
snd.looping = looping;
snd.scheduled = true;
snd.scheduledTime = targetTime;
@ -81,25 +87,34 @@ namespace RhythmHeavenMania.Util
return audioSource;
}
public static AudioSource PlayOneShotGame(string name, float beat = -1, bool forcePlay = false)
public static AudioSource PlayOneShotGame(string name, float beat = -1, bool forcePlay = false , float pitch = 1f, float volume = 1f, bool looping = false)
{
if (GameManager.instance.currentGame == name.Split('/')[0] || forcePlay)
{
return PlayOneShot($"games/{name}", beat);
return PlayOneShot($"games/{name}", beat, pitch, volume, looping);
}
return null;
}
public static AudioSource PlayOneShotScheduledGame(string name, double targetTime, bool forcePlay = false)
public static AudioSource PlayOneShotScheduledGame(string name, double targetTime, bool forcePlay = false, float pitch = 1f, float volume = 1f, bool looping = false)
{
if (GameManager.instance.currentGame == name.Split('/')[0] || forcePlay)
{
return PlayOneShotScheduled($"games/{name}", targetTime);
return PlayOneShotScheduled($"games/{name}", targetTime, pitch, volume, looping);
}
return null;
}
public static void KillLoop(AudioSource source, float fadeTime)
{
// Safeguard against previously-destroyed sounds.
if (source == null)
return;
source.GetComponent<Sound>().KillLoop(fadeTime);
}
}
}

View File

@ -8,11 +8,14 @@ namespace RhythmHeavenMania.Util
{
public AudioClip clip;
public float pitch = 1;
public float volume = 1;
// For use with PlayOneShotScheduled
public bool scheduled;
public double scheduledTime;
public bool looping;
private AudioSource audioSource;
private int pauseTimes = 0;
@ -29,6 +32,8 @@ namespace RhythmHeavenMania.Util
audioSource = GetComponent<AudioSource>();
audioSource.clip = clip;
audioSource.pitch = pitch;
audioSource.volume = volume;
audioSource.loop = looping;
if (beat == -1 && !scheduled)
{
@ -43,34 +48,40 @@ namespace RhythmHeavenMania.Util
startTime = Conductor.instance.songPosition;
if (!scheduled)
if (!scheduled && !looping)
StartCoroutine(NotRelyOnBeatSound());
}
private void Update()
{
if (scheduled)
if (playIndex < 1)
{
if (AudioSettings.dspTime > scheduledTime && playIndex < 1)
if (scheduled)
{
StartCoroutine(NotRelyOnBeatSound());
playIndex++;
if (AudioSettings.dspTime > scheduledTime)
{
StartCoroutine(NotRelyOnBeatSound());
playIndex++;
}
}
}
else if (!playInstant)
{
if (Conductor.instance.songPositionInBeats > beat && playIndex < 1)
else if (!playInstant)
{
audioSource.PlayScheduled(Time.time);
playIndex++;
if (Conductor.instance.songPositionInBeats > beat)
{
audioSource.PlayScheduled(Time.time);
playIndex++;
}
}
}
}
IEnumerator NotRelyOnBeatSound()
{
yield return new WaitForSeconds(clip.length);
Delete();
if (!looping) // Looping sounds are destroyed manually.
{
yield return new WaitForSeconds(clip.length);
Delete();
}
}
public void Delete()
@ -78,5 +89,25 @@ namespace RhythmHeavenMania.Util
GameManager.instance.SoundObjects.Remove(gameObject);
Destroy(gameObject);
}
public void KillLoop(float fadeTime)
{
StartCoroutine(FadeLoop(fadeTime));
}
float loopFadeTimer = 0f;
IEnumerator FadeLoop(float fadeTime)
{
float startingVol = audioSource.volume;
while (loopFadeTimer < fadeTime)
{
loopFadeTimer += Time.deltaTime;
audioSource.volume = Mathf.Max((1f - (loopFadeTimer / fadeTime)) * startingVol, 0f);
yield return null;
}
Delete();
}
}
}