2021-03-02 14:24:18 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2021-05-14 20:08:27 +02:00
|
|
|
using System.Configuration;
|
2021-03-02 14:24:18 +01:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Serialization;
|
|
|
|
|
|
|
|
public class ScriptedDialogueTrigger : MonoBehaviour
|
|
|
|
{
|
2021-05-14 20:08:27 +02:00
|
|
|
[Serializable]
|
|
|
|
private enum Speaker
|
|
|
|
{
|
|
|
|
Radio,
|
|
|
|
Baz
|
|
|
|
}
|
|
|
|
|
2021-03-02 14:24:18 +01:00
|
|
|
[SerializeField] private DialogueSystem _dialogue;
|
2021-05-14 20:08:27 +02:00
|
|
|
|
|
|
|
[SerializeField] private Speaker _speaker = Speaker.Radio;
|
|
|
|
[Obsolete]
|
2021-03-02 14:24:18 +01:00
|
|
|
[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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 14:24:18 +01:00
|
|
|
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-17 04:29:00 +02:00
|
|
|
|
2021-05-15 16:08:58 +02:00
|
|
|
idx++;
|
2021-05-17 04:29:00 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-03-02 14:24:18 +01:00
|
|
|
}
|
|
|
|
}
|