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.
246 lines
6.7 KiB
246 lines
6.7 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 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;
|
|
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
setChoiceEffect = main?.GetComponent<SetChoiceEffect>();
|
|
}
|
|
|
|
// 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;
|
|
mask.fillAmount = Mathf.Lerp(1f, 0f, elapsed / duration);
|
|
|
|
// fade out hint
|
|
// StartCoroutine(FadeCanvasGroup(hint, 1f, 0f, _hintDuration));
|
|
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 (time == "0")
|
|
{
|
|
mask.fillAmount = 0f;
|
|
}
|
|
else
|
|
{
|
|
if (coroutine_countdown != null) StopCoroutine(coroutine_countdown);
|
|
coroutine_countdown = DecreaseFill(float.Parse(time));
|
|
StartCoroutine(coroutine_countdown);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public void onInput(string input)
|
|
{
|
|
Debug.Log("/input message received: " + input);
|
|
|
|
|
|
if (hint.alpha < 1f)
|
|
{
|
|
if (coroutine_hint != null) StopCoroutine(coroutine_hint);
|
|
coroutine_hint = FadeCanvasGroup(hint, hint.alpha, 1f, _hintDuration);
|
|
StartCoroutine(coroutine_hint);
|
|
}
|
|
|
|
|
|
switch (input)
|
|
{
|
|
case "processing":
|
|
statusText.text = "我想一下";
|
|
hintforChat.alpha = 1f;
|
|
stepText.text = "";
|
|
break;
|
|
case "system":
|
|
statusText.text = "等我一下,\n換我說囉";
|
|
hintforChat.alpha = 1f;
|
|
stepText.text = "";
|
|
break;
|
|
case "user":
|
|
statusText.text = "換你說了";
|
|
hintforChat.alpha = 1f;
|
|
stepText.text = "";
|
|
break;
|
|
case "clear":
|
|
statusText.text = "";
|
|
break;
|
|
default:
|
|
Debug.LogWarning("Unknown input: " + input);
|
|
break;
|
|
}
|
|
|
|
}
|
|
public void onHint(string text)
|
|
{
|
|
Debug.Log("/hint message received: " + text);
|
|
|
|
// if (hint.alpha < 1f)
|
|
// {
|
|
// if (coroutine_hint != null) StopCoroutine(coroutine_hint);
|
|
// coroutine_hint = FadeCanvasGroup(hint, hint.alpha, 1f, _hintDuration);
|
|
// StartCoroutine(coroutine_hint);
|
|
// }
|
|
|
|
stepText.text = text;
|
|
|
|
StartCoroutine(FadeCanvasGroup(hintforStep, hintforStep.alpha, 1f, _hintDuration));
|
|
|
|
if (coroutine_hint != null) StopCoroutine(coroutine_hint);
|
|
coroutine_hint = FadeCanvasGroup(hint, hint.alpha, 0f, _hintDuration);
|
|
StartCoroutine(coroutine_hint);
|
|
}
|
|
}
|
|
|