HeavenStudioPlus/Assets/Scripts/Util/Sound.cs

83 lines
2.0 KiB
C#
Raw Normal View History

2021-12-21 01:10:49 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RhythmHeavenMania.Util
{
public class Sound : MonoBehaviour
{
public AudioClip clip;
2022-01-15 07:08:23 +00:00
public float pitch = 1;
2021-12-21 01:10:49 +00:00
// For use with PlayOneShotScheduled
public bool scheduled;
public double scheduledTime;
2021-12-21 01:10:49 +00:00
private AudioSource audioSource;
2022-01-15 07:08:23 +00:00
private int pauseTimes = 0;
private float startTime;
2022-01-21 01:24:30 +00:00
public float beat;
bool playInstant = false;
int playIndex = 0;
2021-12-21 01:10:49 +00:00
private void Start()
{
audioSource = GetComponent<AudioSource>();
2022-01-15 07:08:23 +00:00
audioSource.clip = clip;
audioSource.pitch = pitch;
if (beat == -1 && !scheduled)
2022-01-21 01:24:30 +00:00
{
audioSource.PlayScheduled(Time.time);
playInstant = true;
playIndex++;
}
else
{
2022-01-21 01:24:30 +00:00
playInstant = false;
}
2022-01-21 01:24:30 +00:00
startTime = Conductor.instance.songPosition;
if (!scheduled)
StartCoroutine(NotRelyOnBeatSound());
2021-12-21 01:10:49 +00:00
}
2022-01-15 07:08:23 +00:00
private void Update()
2021-12-21 01:10:49 +00:00
{
if (scheduled)
{
2022-02-05 15:24:07 +00:00
if (AudioSettings.dspTime > scheduledTime && playIndex < 1)
{
StartCoroutine(NotRelyOnBeatSound());
playIndex++;
}
}
else if (!playInstant)
2022-01-15 07:08:23 +00:00
{
2022-01-21 01:24:30 +00:00
if (Conductor.instance.songPositionInBeats > beat && playIndex < 1)
2022-01-20 01:48:52 +00:00
{
2022-01-21 01:24:30 +00:00
audioSource.PlayScheduled(Time.time);
playIndex++;
2022-01-20 01:48:52 +00:00
}
2022-01-15 07:08:23 +00:00
}
}
2022-01-15 07:08:23 +00:00
IEnumerator NotRelyOnBeatSound()
{
yield return new WaitForSeconds(clip.length);
Delete();
}
public void Delete()
{
GameManager.instance.SoundObjects.Remove(gameObject);
Destroy(gameObject);
2021-12-21 01:10:49 +00:00
}
}
}