campfire/Assets/Scripts/CasettePickup.cs

59 lines
1.7 KiB
C#
Raw Normal View History

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