You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
324 lines
9.0 KiB
324 lines
9.0 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Video;
|
|
using OscJack;
|
|
using TMPro;
|
|
|
|
|
|
public class OscHandler : MonoBehaviour
|
|
{
|
|
|
|
[SerializeField]
|
|
public CanvasGroup intro;
|
|
|
|
[SerializeField]
|
|
public CanvasGroup main;
|
|
|
|
[SerializeField]
|
|
public CanvasGroup light_mask;
|
|
|
|
[SerializeField]
|
|
public VideoPlayer videoPlayer;
|
|
|
|
[SerializeField]
|
|
public Image mask;
|
|
|
|
|
|
[SerializeField]
|
|
public float _fadeDuration = 1f;
|
|
|
|
[SerializeField]
|
|
public float _hintDuration = 0.5f;
|
|
|
|
[SerializeField]
|
|
public float _maskDuration = 0.2f;
|
|
[SerializeField]
|
|
public float _lastDurationToShow = 30f;
|
|
|
|
[SerializeField]
|
|
public TextMeshProUGUI statusText;
|
|
|
|
[SerializeField]
|
|
public CanvasGroup hint;
|
|
|
|
private IEnumerator coroutine_hint;
|
|
private IEnumerator coroutine_countdown;
|
|
|
|
SetChoiceEffect setChoiceEffect;
|
|
|
|
[SerializeField]
|
|
public TextMeshProUGUI stepText;
|
|
|
|
[SerializeField]
|
|
public CanvasGroup hintforChat;
|
|
[SerializeField]
|
|
public CanvasGroup hintforStep;
|
|
|
|
private IEnumerator coroutine_breath;
|
|
|
|
public CanvasGroup hintforInput;
|
|
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
setChoiceEffect = main?.GetComponent<SetChoiceEffect>();
|
|
onStatus("reset");
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
private IEnumerator FadeCanvasGroup(CanvasGroup canvasGroup, float startAlpha, float endAlpha, float duration, float delay = 0f)
|
|
{
|
|
if (delay > 0f)
|
|
yield return new WaitForSeconds(delay);
|
|
|
|
canvasGroup.alpha = startAlpha;
|
|
float elapsedTime = 0f;
|
|
|
|
while (elapsedTime < duration)
|
|
{
|
|
elapsedTime += Time.deltaTime;
|
|
canvasGroup.alpha = Mathf.Lerp(startAlpha, endAlpha, elapsedTime / duration);
|
|
yield return null;
|
|
}
|
|
|
|
canvasGroup.alpha = endAlpha;
|
|
}
|
|
|
|
private IEnumerator DecreaseFill(float duration)
|
|
{
|
|
float elapsed = 0f;
|
|
mask.fillAmount = 1f;
|
|
|
|
while (elapsed < duration)
|
|
{
|
|
elapsed += Time.deltaTime;
|
|
|
|
if (duration - elapsed > _lastDurationToShow)
|
|
{
|
|
mask.fillAmount = 0;
|
|
mask.gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
mask.gameObject.SetActive(true);
|
|
mask.fillAmount = Mathf.Lerp(0f, 1f, (duration - elapsed) / _lastDurationToShow);
|
|
}
|
|
yield return null;
|
|
}
|
|
|
|
mask.fillAmount = 0f;
|
|
}
|
|
|
|
|
|
public void onStatus(string status)
|
|
{
|
|
Debug.Log("/status message received: " + status);
|
|
|
|
videoPlayer.Stop();
|
|
|
|
if (coroutine_hint != null) StopCoroutine(coroutine_hint);
|
|
|
|
mask.fillAmount = 0f;
|
|
|
|
switch (status)
|
|
{
|
|
case "reset":
|
|
intro.alpha = 0f;
|
|
main.alpha = 0f;
|
|
// light_mask.alpha = 0f;
|
|
hint.alpha = 0f;
|
|
setChoiceEffect?.Reset();
|
|
break;
|
|
case "intro":
|
|
// StartCoroutine(FadeCanvasGroup(light_mask, 0f, 1f, _maskDuration));
|
|
StartCoroutine(FadeCanvasGroup(intro, 0f, 1f, _fadeDuration, _maskDuration));
|
|
|
|
|
|
videoPlayer.time = 0f;
|
|
videoPlayer.Play();
|
|
videoPlayer.loopPointReached += vp =>
|
|
{
|
|
Debug.Log("Video playback completed.");
|
|
StartCoroutine(FadeCanvasGroup(intro, 1f, 0f, _fadeDuration));
|
|
// StartCoroutine(FadeCanvasGroup(main, 0f, 1f, _fadeDuration));
|
|
videoPlayer.loopPointReached -= null;
|
|
};
|
|
break;
|
|
case "go":
|
|
|
|
// if (light_mask.alpha < 1f) StartCoroutine(FadeCanvasGroup(light_mask, light_mask.alpha, 1f, _fadeDuration));
|
|
StartCoroutine(FadeCanvasGroup(intro, intro.alpha, 0f, _fadeDuration));
|
|
StartCoroutine(FadeCanvasGroup(main, main.alpha, 1f, _fadeDuration));
|
|
|
|
break;
|
|
case "end":
|
|
if (intro.alpha > 0f) StartCoroutine(FadeCanvasGroup(intro, 1f, 0f, _fadeDuration));
|
|
StartCoroutine(FadeCanvasGroup(main, 1f, 0f, _fadeDuration));
|
|
// StartCoroutine(FadeCanvasGroup(light_mask, 1f, 0f, _maskDuration, _fadeDuration));
|
|
if (hint.alpha > 0f)
|
|
{
|
|
coroutine_hint = FadeCanvasGroup(hint, hint.alpha, 0f, _hintDuration);
|
|
StartCoroutine(coroutine_hint);
|
|
}
|
|
|
|
videoPlayer.Stop();
|
|
break;
|
|
default:
|
|
Debug.LogWarning("Unknown status: " + status);
|
|
break;
|
|
}
|
|
|
|
}
|
|
public void onCountDown(string time)
|
|
{
|
|
Debug.Log("/countdown message received: " + time);
|
|
|
|
if (mask != null)
|
|
{
|
|
|
|
if (coroutine_countdown != null) StopCoroutine(coroutine_countdown);
|
|
|
|
if (time == "0")
|
|
{
|
|
mask.fillAmount = 0f;
|
|
mask.gameObject.SetActive(false);
|
|
|
|
}
|
|
else
|
|
{
|
|
coroutine_countdown = DecreaseFill(float.Parse(time));
|
|
StartCoroutine(coroutine_countdown);
|
|
}
|
|
}
|
|
|
|
}
|
|
public void onSpeechPause(string pause)
|
|
{
|
|
Debug.Log("/speechpause message received: " + pause);
|
|
float pause_time = float.Parse(pause);
|
|
if (pause_time > 0f)
|
|
{
|
|
StartCoroutine(FadeCanvasGroup(hintforInput, hintforInput.alpha, 0f, pause_time/1000.0f));
|
|
}
|
|
|
|
}
|
|
public void onInput(string input)
|
|
{
|
|
Debug.Log("/input message received: " + input);
|
|
|
|
|
|
|
|
|
|
if (input.Length == 0)
|
|
{
|
|
statusText.text = "";
|
|
// hintforChat.alpha = 0f;
|
|
if (hintforChat.alpha > 0f)
|
|
{
|
|
StartCoroutine(FadeCanvasGroup(hintforChat, hintforChat.alpha, 0f, _hintDuration));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
hintforInput.alpha = 1.0f;
|
|
|
|
if (hint.alpha < 1f)
|
|
{
|
|
if (coroutine_hint != null) StopCoroutine(coroutine_hint);
|
|
coroutine_hint = FadeCanvasGroup(hint, hint.alpha, 1f, _hintDuration);
|
|
StartCoroutine(coroutine_hint);
|
|
}
|
|
|
|
|
|
// statusText.text = input;
|
|
StartCoroutine(TypeWriter(statusText, input));
|
|
// hintforChat.alpha = 1f;
|
|
if (hintforChat.alpha < 1f)
|
|
{
|
|
StartCoroutine(FadeCanvasGroup(hintforChat, hintforChat.alpha, 1f, _hintDuration));
|
|
}
|
|
|
|
if (hint.alpha < 1f)
|
|
{
|
|
// if (coroutine_hint != null) StopCoroutine(coroutine_hint);
|
|
// coroutine_hint = FadeCanvasGroup(hint, hint.alpha, 1f, _hintDuration);
|
|
// StartCoroutine(coroutine_hint);
|
|
|
|
if (coroutine_hint != null) StopCoroutine(coroutine_hint);
|
|
coroutine_hint = BreathFade(hint);
|
|
StartCoroutine(coroutine_hint);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
public void onHint(string text)
|
|
{
|
|
Debug.Log("/hint message received: " + text);
|
|
|
|
// stepText.text = text;
|
|
StartCoroutine(TypeWriter(stepText, text));
|
|
|
|
StartCoroutine(FadeCanvasGroup(hintforStep, hintforStep.alpha, 1f, _hintDuration));
|
|
|
|
if (coroutine_breath != null) StopCoroutine(coroutine_breath);
|
|
// coroutine_hint = FadeCanvasGroup(hint, hint.alpha, text.Length > 0 ? 1f : 0f, _hintDuration);
|
|
// StartCoroutine(coroutine_hint);
|
|
|
|
hintforInput.alpha = 1.0f;
|
|
|
|
coroutine_breath = BreathFade(hintforStep);
|
|
StartCoroutine(coroutine_breath);
|
|
|
|
|
|
}
|
|
private IEnumerator BreathFade(CanvasGroup canvasGroup, float maxAlpha=1f, float minAlpha=0.5f,
|
|
float fadeInDuration=1.2f, float fadeOutDuration=1.2f, int cycles=20)
|
|
{
|
|
for (int i = 0; i < cycles; i++)
|
|
{
|
|
// Fade out
|
|
float elapsed = 0f;
|
|
while (elapsed < fadeOutDuration)
|
|
{
|
|
elapsed += Time.deltaTime;
|
|
canvasGroup.alpha = Mathf.Lerp(maxAlpha, minAlpha, elapsed / fadeOutDuration);
|
|
yield return null;
|
|
}
|
|
canvasGroup.alpha = minAlpha;
|
|
|
|
// Fade in
|
|
elapsed = 0f;
|
|
while (elapsed < fadeInDuration)
|
|
{
|
|
elapsed += Time.deltaTime;
|
|
canvasGroup.alpha = Mathf.Lerp(minAlpha, maxAlpha, elapsed / fadeInDuration);
|
|
yield return null;
|
|
}
|
|
canvasGroup.alpha = maxAlpha;
|
|
}
|
|
}
|
|
public IEnumerator TypeWriter(TextMeshProUGUI textComponent, string message, float typeSpeed = 0.2f)
|
|
{
|
|
|
|
textComponent.text = "";
|
|
foreach (char c in message)
|
|
{
|
|
textComponent.text += c;
|
|
yield return new WaitForSeconds(typeSpeed);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|