136 lines
3.8 KiB
C#
136 lines
3.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.Playables;
|
|
using UnityEngine.Timeline;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class QTEController : MonoBehaviour
|
|
{
|
|
[SerializeField] private List<QTEElement> m_qteElements = new List<QTEElement>();
|
|
[SerializeField] private PlayableDirector m_director;
|
|
[SerializeField] private Animator m_animator;
|
|
|
|
public UnityEvent m_OnQTESequenceStart;
|
|
public UnityEvent m_OnQTESequenceFinish;
|
|
|
|
private int m_currentElementIndex = 0;
|
|
private bool m_isSequenceRunning;
|
|
|
|
private const string SEQUENCE_END_TAG = "QTEEnd";
|
|
|
|
//private bool startedFade = false;
|
|
|
|
public bool IsSequenceRunning { get => m_isSequenceRunning; }
|
|
|
|
[Button]
|
|
public void Debug_PlayTimeline()
|
|
{
|
|
m_isSequenceRunning = true;
|
|
m_director.Play();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (m_isSequenceRunning)
|
|
{
|
|
if (IsAnimatorSequenceFinish())
|
|
{
|
|
EndQteSequence();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SpawnQTEElement()
|
|
{
|
|
if (m_qteElements == null || m_qteElements.Count <= 0)
|
|
{
|
|
Debug.LogError("List with QTEElements is null or empty");
|
|
return;
|
|
}
|
|
|
|
if (m_currentElementIndex >= m_qteElements.Count)
|
|
{
|
|
return;
|
|
}
|
|
|
|
m_director.Pause();
|
|
TimeController.Instance.SetTimeScale(0.5f);
|
|
var btn = QTECanvas.Instance.SpawnButton(m_qteElements[m_currentElementIndex]);
|
|
btn.m_OnHit += OnQTEHit;
|
|
btn.m_OnMissed += OnQTEMissed;
|
|
m_currentElementIndex++;
|
|
}
|
|
|
|
public void OnQTEHit()
|
|
{
|
|
ResumeTimeLine();
|
|
m_animator.SetTrigger("QTEHit");
|
|
}
|
|
|
|
public void OnQTEMissed()
|
|
{
|
|
m_animator.SetTrigger("QTEMissed");
|
|
ResumeTimeLine();
|
|
}
|
|
|
|
public void ResumeTimeLine()
|
|
{
|
|
m_director.Resume();
|
|
}
|
|
|
|
private bool IsAnimatorSequenceFinish()
|
|
{
|
|
if (m_animator == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
AnimatorStateInfo stateInfo = m_animator.GetCurrentAnimatorStateInfo(0);
|
|
return stateInfo.IsTag(SEQUENCE_END_TAG);
|
|
}
|
|
|
|
private IEnumerator StartSequenceCoroutine()
|
|
{
|
|
//block inputs
|
|
Player.Instance.PlayerInput.enabled = false;
|
|
FadeCanvasGroup.Instance.FadeOut(1f);
|
|
yield return new WaitForSeconds(1f);
|
|
FadeCanvasGroup.Instance.FadeIn(1f);
|
|
Player.Instance.PlayerInput.enabled = true;
|
|
Player.Instance.ThirdPersonController.isImmortal = false;
|
|
m_isSequenceRunning = true;
|
|
m_OnQTESequenceStart?.Invoke();
|
|
m_director.Play();
|
|
}
|
|
|
|
public void StartQteSequence()
|
|
{
|
|
Player.Instance.ThirdPersonController.isImmortal = true;
|
|
StartCoroutine(StartSequenceCoroutine());
|
|
// HideUI.InvokeSetActiveUI(false);
|
|
}
|
|
|
|
public void EndQteSequence()
|
|
{
|
|
m_isSequenceRunning = false;
|
|
StartCoroutine(EndSequenceCoroutine());
|
|
// HideUI.InvokeSetActiveUI(true);
|
|
}
|
|
|
|
private IEnumerator EndSequenceCoroutine()
|
|
{
|
|
Debug.LogError("end qte deb");
|
|
//block inputs
|
|
// FadeCanvasGroup.Instance.FadeOut(1f);
|
|
yield return new WaitForSeconds(0.5f);
|
|
FadeCanvasGroup.Instance.FadeIn(1f);
|
|
m_OnQTESequenceFinish?.Invoke();
|
|
m_director.Stop();
|
|
}
|
|
}
|
|
} |