144 lines
3.7 KiB
C#
144 lines
3.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Beyond
|
|
{
|
|
[RequireComponent(typeof(Button))]
|
|
public class QTEButton : MonoBehaviour
|
|
{
|
|
private Button m_button;
|
|
public QTEElement m_QteElement;
|
|
[SerializeField] private Image m_outline;
|
|
|
|
public UnityAction m_OnHit;
|
|
public UnityAction m_OnMissed;
|
|
|
|
private float m_timer = 0f;
|
|
private Vector2 m_startSizeDelta;
|
|
|
|
private void Awake()
|
|
{
|
|
m_button = GetComponent<Button>();
|
|
m_startSizeDelta = m_outline.rectTransform.sizeDelta;
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
m_QteElement.m_currentState = QTEElement.ElementState.Active;
|
|
ButtonEventsInit();
|
|
m_timer = 0f;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
m_OnHit = null;
|
|
m_OnMissed = null;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (m_QteElement == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
switch (m_QteElement.m_currentState)
|
|
{
|
|
case QTEElement.ElementState.UnActive:
|
|
break;
|
|
case QTEElement.ElementState.Active:
|
|
ShrinkOutline();
|
|
if (m_timer >= m_QteElement.m_duration)
|
|
{
|
|
m_QteElement.m_currentState = QTEElement.ElementState.Missed;
|
|
OnMissed();
|
|
}
|
|
m_timer += Time.unscaledDeltaTime;
|
|
break;
|
|
case QTEElement.ElementState.Passed:
|
|
gameObject.SetActive(false);
|
|
TimeController.Instance.SetTimeScale(1f);
|
|
break;
|
|
case QTEElement.ElementState.Missed:
|
|
gameObject.SetActive(false);
|
|
TimeController.Instance.SetTimeScale(1f);
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
private void ButtonEventsInit()
|
|
{
|
|
if (m_button == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
m_button.onClick.AddListener(OnHit);
|
|
}
|
|
|
|
private void OnHit()
|
|
{
|
|
if (m_QteElement != null)
|
|
{
|
|
m_QteElement.m_currentState = QTEElement.ElementState.Passed;
|
|
}
|
|
|
|
m_OnHit?.Invoke();
|
|
}
|
|
|
|
private void OnMissed()
|
|
{
|
|
if (m_QteElement != null)
|
|
{
|
|
m_QteElement.m_currentState = QTEElement.ElementState.Missed;
|
|
}
|
|
|
|
m_OnMissed?.Invoke();
|
|
}
|
|
|
|
private void ShrinkOutline()
|
|
{
|
|
if (m_QteElement != null)
|
|
{
|
|
float t = m_timer / m_QteElement.m_duration;
|
|
m_outline.rectTransform.sizeDelta = Vector2.Lerp(m_startSizeDelta, Vector2.zero, t);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
[Serializable]
|
|
public class QTEElement
|
|
{
|
|
public Vector2 m_startingPos;
|
|
public float m_duration = 1f;
|
|
public ElementState m_currentState = ElementState.UnActive;
|
|
|
|
public QTEElement(QTEElement qteElement)
|
|
{
|
|
m_startingPos = qteElement.m_startingPos;
|
|
m_duration = qteElement.m_duration;
|
|
m_currentState = qteElement.m_currentState;
|
|
}
|
|
|
|
public QTEElement()
|
|
{
|
|
m_startingPos = Vector3.zero;
|
|
m_duration = 3f;
|
|
m_currentState = ElementState.Active;
|
|
}
|
|
|
|
public enum ElementState
|
|
{
|
|
UnActive,
|
|
Active,
|
|
Passed,
|
|
Missed
|
|
}
|
|
}
|
|
} |