campfire/Assets/Scripts/CasettePickup.cs
2020-04-21 02:55:24 +03:00

58 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class CasettePickup : MonoBehaviour {
public AudioClip[] Clips;
public string DisplayPrompt {
get {
return Playing ? "Queue casette" : "Play casette";
}
}
public bool Playing {
get {
return Source.isPlaying;
}
}
private AudioSource Source;
private CasettePickup PreviousInQueue;
private int WaitingIndex;
private bool Disappear;
private void Awake() {
Source = GetComponent<AudioSource>();
CasetteProgress.CasetteCount = Clips.Length;
Disappear = false;
}
private void Update() {
if (Disappear) {
transform.localScale = Vector3.Lerp(transform.localScale, Vector3.zero, 10f * Time.deltaTime);
}
if (PreviousInQueue != null) {
if (!PreviousInQueue.Playing) {
PreviousInQueue = null;
Source.PlayOneShot(Clips[WaitingIndex]);
}
} else if (CasetteProgress.AllCasettesPlayed && !Playing) {
Destroy(gameObject);
}
}
public void Play(CasettePickup previous) {
int Index = CasetteProgress.PlayNext();
if (Index != -1) {
Disappear = true;
if (previous == null) {
Source.PlayOneShot(Clips[Index]);
} else {
PreviousInQueue = previous;
WaitingIndex = Index;
}
}
}
}