HeavenStudioPlus/Assets/Scripts/Util/Sound.cs
minenice55 4c4d0a7a7a Play Mode Features Part 1 (#413)
* add pause menu assets

* layout and animation for pause

* make play mode prefab function

re-assign unused class inheritance

* remove filepath

* don't init medals twice

* remove PlayerActionObject

* initial attempt at anti-note lock

TODO: circumvent inputs clearing themselves making the functionality not work

* properly implement input lock prevention

* fix error on editor open

* functional pause menu

* bugfix

* make unpausing not reset current play statistics

* serialize initializer components in inspector instead of procedurally generating

* sanity check

* note for fade

* make flashes in the camera prefabs instead of in world space

remove / reorganize script files
address issue #411

* fix bug with perfect campaign

make minigame transitions hide the game canvas
adjust animation of the song credits textbox

* fully functional intro scene (placeholder for future title screen)

refactored entire game loading procedure
re-organized some files

* add interaction query to disclaimer text

* reword legal

* anchor section medals to section display

more tempo change placement controls

* operation order bugfix

* prep for future ratings and stats

* loading text

* autoload opening scene

* splash screen adjustments

added setting to force enable splash screen

* adjust setting entry
2023-05-07 20:33:15 +00:00

165 lines
4.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace HeavenStudio.Util
{
public class Sound : MonoBehaviour
{
public AudioClip clip;
public float pitch = 1;
public float volume = 1;
// For use with PlayOneShotScheduled
public bool scheduled;
public double scheduledTime;
public bool looping;
public float loopEndBeat = -1;
public float fadeTime;
int loopIndex = 0;
private AudioSource audioSource;
private int pauseTimes = 0;
private double startTime;
public float beat;
public float offset;
public float scheduledPitch = 1f;
bool playInstant = false;
bool played = false;
private void Start()
{
audioSource = GetComponent<AudioSource>();
audioSource.clip = clip;
audioSource.pitch = pitch;
audioSource.volume = volume;
audioSource.loop = looping;
Conductor cnd = Conductor.instance;
if (beat == -1 && !scheduled)
{
audioSource.Play();
playInstant = true;
played = true;
startTime = cnd.songPositionAsDouble;
StartCoroutine(NotRelyOnBeatSound());
}
else
{
playInstant = false;
scheduledPitch = cnd.SongPitch;
startTime = (AudioSettings.dspTime + (cnd.GetSongPosFromBeat(beat) - cnd.songPositionAsDouble)/(double)scheduledPitch) - offset;
audioSource.PlayScheduled(startTime);
}
}
private void Update()
{
Conductor cnd = Conductor.instance;
if (!played)
{
if (scheduled)
{
if (scheduledPitch != 0 && AudioSettings.dspTime > scheduledTime)
{
StartCoroutine(NotRelyOnBeatSound());
played = true;
}
}
else if (!playInstant)
{
if (scheduledPitch != 0 && AudioSettings.dspTime > startTime)
{
played = true;
StartCoroutine(NotRelyOnBeatSound());
}
else
{
if (!played && scheduledPitch != cnd.SongPitch)
{
if (cnd.SongPitch == 0)
{
scheduledPitch = cnd.SongPitch;
audioSource.Pause();
}
else
{
if (scheduledPitch == 0)
{
audioSource.UnPause();
}
scheduledPitch = cnd.SongPitch;
startTime = (AudioSettings.dspTime + (cnd.GetSongPosFromBeat(beat) - cnd.songPositionAsDouble)/(double)scheduledPitch);
audioSource.SetScheduledStartTime(startTime);
}
}
}
}
}
if (loopIndex < 1)
{
if (looping && loopEndBeat != -1) // Looping sounds play forever unless params are set.
{
if (cnd.songPositionInBeats > loopEndBeat)
{
KillLoop(fadeTime);
loopIndex++;
}
}
}
}
IEnumerator NotRelyOnBeatSound()
{
if (!looping) // Looping sounds are destroyed manually.
{
yield return new WaitForSeconds(clip.length / pitch);
Delete();
}
}
public void SetLoopParams(float endBeat, float fadeTime)
{
loopEndBeat = endBeat;
this.fadeTime = fadeTime;
}
public void Stop()
{
if (audioSource != null)
audioSource.Stop();
}
public void Delete()
{
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();
}
}
}