revival/game/Assets/Scripts/Dialogue/ScriptedDialogueTrigger.cs

104 lines
2.3 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
2021-05-14 20:08:27 +02:00
using System.Configuration;
using UnityEngine;
using UnityEngine.Serialization;
public class ScriptedDialogueTrigger : MonoBehaviour
{
2021-05-14 20:08:27 +02:00
[Serializable]
private enum Speaker
{
Radio,
Baz
}
[SerializeField] private DialogueSystem _dialogue;
2021-05-14 20:08:27 +02:00
[SerializeField] private Speaker _speaker = Speaker.Radio;
[Obsolete]
[SerializeField] private string _key;
2021-04-27 01:57:07 +02:00
2021-05-14 20:08:27 +02:00
[SerializeField] private string[] _keys;
private float[] _durations;
2021-04-27 01:57:07 +02:00
[SerializeField] private bool _log;
2021-05-14 20:08:27 +02:00
private void Awake()
{
if (_keys.Length == 0) return;
_durations = new float[_keys.Length];
for (int i = 0; i < _keys.Length; i++)
{
_durations[i] = _dialogue.GetLineDuration(_keys[i]);
if (_speaker == Speaker.Radio)
{
// fudge because radio has a bit of a delay before starting the clip
_durations[i] += 0.3f;
}
}
}
private void OnTriggerEnter(Collider other)
{
2021-05-12 21:10:58 +02:00
if ( !other.CompareTag( "Player" ) )
return;
2021-04-27 01:57:07 +02:00
if (_log)
{
Debug.Log($"{other} triggered dialogue {_key}");
}
2021-05-14 20:08:27 +02:00
if (_keys.Length == 0)
{
PlayLine(_key);
}
else
{
StartCoroutine(PlayKeys());
}
}
private IEnumerator PlayKeys()
{
var idx = 0;
var elapsed = 0f;
// play first line
PlayLine(_keys[0]);
2021-04-27 01:57:07 +02:00
2021-05-14 20:08:27 +02:00
while (idx < _keys.Length)
{
elapsed += Time.deltaTime;
if (elapsed > _durations[idx])
{
elapsed = 0;
2021-05-15 16:08:58 +02:00
idx++;
if (idx >= _keys.Length) break;
2021-05-15 16:08:58 +02:00
PlayLine(_keys[idx]);
2021-05-14 20:08:27 +02:00
}
yield return null;
}
}
private void PlayLine(string key)
{
switch (_speaker)
{
case Speaker.Radio:
_dialogue.PlayLineRadio(key);
break;
case Speaker.Baz:
_dialogue.PlayLineBaz(key);
break;
}
}
}