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

106 lines
2.5 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;
2021-05-17 15:31:13 +02:00
[SerializeField] private Transform _speakerTransform;
2021-05-14 20:08:27 +02:00
[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)
{
2021-05-17 15:31:13 +02:00
PlayLine(_key, _speakerTransform);
2021-05-14 20:08:27 +02:00
}
else
{
StartCoroutine(PlayKeys());
}
}
private IEnumerator PlayKeys()
{
var idx = 0;
var elapsed = 0f;
// play first line
2021-05-17 15:31:13 +02:00
PlayLine(_keys[0], _speakerTransform);
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-17 15:31:13 +02:00
PlayLine(_keys[idx], _speakerTransform);
2021-05-14 20:08:27 +02:00
}
yield return null;
}
}
2021-05-17 15:31:13 +02:00
private void PlayLine(string key, Transform speakerObject)
2021-05-14 20:08:27 +02:00
{
switch (_speaker)
{
case Speaker.Radio:
_dialogue.PlayLineRadio(key);
break;
case Speaker.Baz:
2021-05-17 15:31:13 +02:00
_dialogue.PlayLineBaz(key, speakerObject);
2021-05-14 20:08:27 +02:00
break;
}
}
}