471 lines
15 KiB
C#
471 lines
15 KiB
C#
using Invector;
|
|
using Invector.vCharacterController;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.Rendering.Universal;
|
|
using TMPro;
|
|
using System.Collections;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class bHUDController : MonoBehaviour
|
|
{
|
|
#region General Variables
|
|
|
|
#region Health/Stamina Variables
|
|
|
|
[SerializeField]
|
|
private Renderer healthBarRenderer, staminaBarRenderer, faithRenderer;
|
|
|
|
[SerializeField]
|
|
private Image healthImage;
|
|
|
|
private Material healthBarMaterial, staminaBarMaterial, brightnessBarMaterial, brightnessBarAdditiveMaterial,
|
|
faithBarMaterial;
|
|
|
|
[SerializeField]
|
|
private TMP_Text maturityText;
|
|
|
|
[Header("DamageHUD")]
|
|
public Image damageImage;
|
|
|
|
public float flashSpeed = 5f;
|
|
public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
|
|
[HideInInspector] public bool damaged;
|
|
|
|
#endregion Health/Stamina Variables
|
|
|
|
#region Display Controls Variables
|
|
|
|
[Header("Controls Display")]
|
|
[HideInInspector]
|
|
public bool controllerInput;
|
|
|
|
public Image displayControls;
|
|
public Sprite joystickControls;
|
|
public Sprite keyboardControls;
|
|
|
|
#endregion Display Controls Variables
|
|
|
|
#region Brightness
|
|
|
|
[SerializeField] private float m_brightnesColorIntensity = 1f;
|
|
// private float m_brightnessTimer = 0f;
|
|
// private BrightnessState m_bState = BrightnessState.NORMAL;
|
|
|
|
[SerializeField]
|
|
private AnimationCurve m_brightnessAnimCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
|
|
|
|
[SerializeField]
|
|
private float m_brightnessAnimTime = 1f;
|
|
|
|
private Color initialColor = new Color(1, 1, 1, 0), finalColorGained = Color.green, finalColorLost = Color.red;
|
|
|
|
private enum BrightnessState
|
|
{
|
|
NORMAL, RECEIVED, LOST
|
|
}
|
|
|
|
#endregion Brightness
|
|
|
|
#region Debug Info Variables
|
|
|
|
[Header("Debug Window")]
|
|
public GameObject debugPanel;
|
|
|
|
[HideInInspector]
|
|
public Text debugText;
|
|
|
|
#endregion Debug Info Variables
|
|
|
|
#region Change Input Text Variables
|
|
|
|
[Header("Text with FadeIn/Out")]
|
|
public Text fadeText;
|
|
|
|
private float textDuration, fadeDuration, durationTimer, timer;
|
|
private Color startColor, endColor;
|
|
private bool fade;
|
|
|
|
private PlayerAttribute faith, brightness, maturity;
|
|
|
|
[SerializeField]
|
|
private ScalableSliderController healthSlider, faithSlider, staminaSlider;
|
|
|
|
[SerializeField]
|
|
private Image brightnessBar, brightnessBarEffect;
|
|
|
|
#endregion Change Input Text Variables
|
|
|
|
#endregion General Variables
|
|
|
|
private static bHUDController _instance;
|
|
|
|
public static bHUDController instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = GameObject.FindObjectOfType<bHUDController>();
|
|
//Tell unity not to destroy this object when loading a new scene
|
|
//DontDestroyOnLoad(_instance.gameObject);
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
InitFadeText();
|
|
if (debugPanel != null)
|
|
debugText = debugPanel.GetComponentInChildren<Text>();
|
|
}
|
|
|
|
public void Init(vThirdPersonController cc)
|
|
{
|
|
cc.onDead.AddListener(OnDead);
|
|
cc.onReceiveDamage.AddListener(EnableDamageSprite);
|
|
damageImage.color = new Color(0f, 0f, 0f, 0f);
|
|
|
|
faith = Player.Instance.GetAttribute("Faith");
|
|
brightness = Player.Instance.GetAttribute("BrightnessPoints");
|
|
maturity = Player.Instance.GetAttribute("Maturity");
|
|
brightness.OnValueChanged.AddListener(PlayBrightnessEffectOnValueChanged);
|
|
Player.Instance.onStatsUpdated += SetSlidersScales;
|
|
Player.Instance.UodatePlayerStatistics();
|
|
// Player.Instance.brig
|
|
}
|
|
|
|
private void OnDead(GameObject arg0)
|
|
{
|
|
ShowText("You are Dead!");
|
|
}
|
|
|
|
public virtual void UpdateHUD(vThirdPersonController cc)
|
|
{
|
|
UpdateDebugWindow(cc);
|
|
UpdateBars(cc);
|
|
ChangeInputDisplay();
|
|
ShowDamageSprite();
|
|
FadeEffect();
|
|
}
|
|
|
|
public void ShowText(string message, float textTime = 2f, float fadeTime = 0.5f)
|
|
{
|
|
if (fadeText != null && !fade)
|
|
{
|
|
fadeText.text = message;
|
|
textDuration = textTime;
|
|
fadeDuration = fadeTime;
|
|
durationTimer = 0f;
|
|
timer = 0f;
|
|
fade = true;
|
|
}
|
|
else if (fadeText != null)
|
|
{
|
|
fadeText.text += "\n" + message;
|
|
textDuration = textTime;
|
|
fadeDuration = fadeTime;
|
|
durationTimer = 0f;
|
|
timer = 0f;
|
|
}
|
|
}
|
|
|
|
public void ShowText(string message)
|
|
{
|
|
if (fadeText != null && !fade)
|
|
{
|
|
fadeText.text = message;
|
|
textDuration = 2f;
|
|
fadeDuration = 0.5f;
|
|
durationTimer = 0f;
|
|
timer = 0f;
|
|
fade = true;
|
|
}
|
|
else if (fadeText != null)
|
|
{
|
|
fadeText.text += "\n" + message;
|
|
textDuration = 2f;
|
|
fadeDuration = 0.5f;
|
|
durationTimer = 0f;
|
|
timer = 0f;
|
|
}
|
|
}
|
|
|
|
public void PlayBrightnessEffectOnValueChanged(float newValue, float oldValue)
|
|
{
|
|
float difference = newValue - oldValue;
|
|
//because on scene loaded we had had popups
|
|
if (Time.timeSinceLevelLoad < 2f)
|
|
{
|
|
return;
|
|
}
|
|
if (difference > 0)
|
|
{
|
|
OnBrightnessReceived(difference);
|
|
}
|
|
else
|
|
{
|
|
OnBrightnessLost(difference);
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void OnBrightnessReceived(float gain)
|
|
{
|
|
Player.Instance.PlayBrightnessGained();
|
|
if (gameObject.activeInHierarchy)
|
|
{
|
|
StartCoroutine(FlashBrightnessGainedCoroutine(gain));
|
|
}
|
|
//m_brightnessTimer = 0f;
|
|
// startColor = brightnessBarMaterial.GetColor("_TintColor");
|
|
}
|
|
|
|
[Button]
|
|
public void OnBrightnessLost(float loss)
|
|
{
|
|
Player.Instance.PlayBrightnessLost();
|
|
if (gameObject.activeSelf)
|
|
StartCoroutine(FlashBrightnessLostCoroutine(loss));
|
|
|
|
// m_brightnessTimer = 0f;
|
|
// startColor = brightnessBarMaterial.GetColor("_TintColor");
|
|
}
|
|
|
|
private void UpdateBars(vThirdPersonController cc)
|
|
{
|
|
float value = cc.currentStamina / cc.maxStamina;
|
|
staminaSlider.SetSliderValue(value);
|
|
//SetTextureOffset(staminaBarMaterial, value);
|
|
|
|
value = cc.currentHealth / cc.maxHealth;
|
|
healthSlider.SetSliderValue(value);
|
|
// SetTextureOffset(healthBarMaterial, value);
|
|
|
|
value = (float)faith.AttributeCurrentValue / (float)faith.AttributeMaxValue;
|
|
faithSlider.SetSliderValue(value);
|
|
// SetTextureOffset(faithBarMaterial, value);
|
|
|
|
value = (float)brightness.AttributeCurrentValue / (float)brightness.AttributeMaxValue;
|
|
|
|
brightnessBar.fillAmount = value;
|
|
brightnessBarEffect.fillAmount = value;
|
|
|
|
maturityText.text = Mathf.RoundToInt(maturity.AttributeCurrentValue).ToString();
|
|
/*
|
|
SetTextureOffset(brightnessBarMaterial, value);
|
|
brightnessBarAdditiveMaterial.SetFloat("_clipTh", value);
|
|
endColor = Color.Lerp(m_brighnessLostColor, m_brighnessReceivedColor, value);
|
|
|
|
maturityText.text = Mathf.RoundToInt(maturity.AttributeCurrentValue).ToString();
|
|
|
|
Color c = brightnessBarAdditiveMaterial.GetColor("_Color");
|
|
|
|
float intensity = 1f;
|
|
|
|
if (m_bState == BrightnessState.NORMAL)
|
|
{
|
|
// intensity = Mathf.Sin(m_brightnessTimer * Mathf.PI * m_brightnessChangeSpeed);
|
|
// intensity = m_brightnessAnimCurve.Evaluate(m_brightnessTimer / m_brightnessAnimTime);
|
|
//intensity *= intensity * m_brightnesColorIntensity;
|
|
}
|
|
else if (m_bState == BrightnessState.LOST)
|
|
{
|
|
c = m_brighnessLostColor;
|
|
intensity = m_brightnessAnimCurve.Evaluate(m_brightnessTimer / m_brightnessAnimTime);
|
|
intensity *= intensity * m_brightnesColorIntensity;
|
|
}
|
|
else if (m_bState == BrightnessState.RECEIVED)
|
|
{
|
|
c = m_brighnessReceivedColor;
|
|
intensity = m_brightnessAnimCurve.Evaluate(m_brightnessTimer / m_brightnessAnimTime);
|
|
intensity *= intensity * m_brightnesColorIntensity;
|
|
}
|
|
if (m_bState != BrightnessState.NORMAL && m_brightnessTimer > m_brightnessAnimTime)
|
|
{
|
|
m_bState = BrightnessState.NORMAL;
|
|
// m_brightnessTimer = 0f;
|
|
}
|
|
c *= intensity;
|
|
brightnessBarMaterial.SetColor("_TintColor", Color.Lerp(startColor, endColor, m_brightnessTimer));
|
|
|
|
brightnessBarAdditiveMaterial.SetColor("_Color", c);
|
|
m_brightnessTimer += Time.deltaTime;
|
|
*/
|
|
}
|
|
|
|
private IEnumerator FlashBrightnessLostCoroutine(float gain)
|
|
{
|
|
yield return StartCoroutine(FlashBrightnesLostOnce(m_brightnessAnimTime / 3));
|
|
|
|
if (gain <= -1)
|
|
{
|
|
yield return StartCoroutine(FlashBrightnesLostOnce(m_brightnessAnimTime / 3));
|
|
yield return StartCoroutine(FlashBrightnesLostOnce(m_brightnessAnimTime / 3));
|
|
}
|
|
}
|
|
|
|
private IEnumerator FlashBrightnesOnce(Color finalColor, float brighntessAnimTime)
|
|
{
|
|
float timer = 0f;
|
|
while (timer < brighntessAnimTime / 2)
|
|
{
|
|
brightnessBarEffect.color = Color.Lerp(initialColor, finalColor, (timer / (brighntessAnimTime / 2)));
|
|
timer += Time.deltaTime;
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
timer = 0f;
|
|
while (timer < brighntessAnimTime / 2)
|
|
{
|
|
brightnessBarEffect.color = Color.Lerp(finalColor, initialColor, (timer / (brighntessAnimTime / 2)));
|
|
timer += Time.deltaTime;
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
}
|
|
|
|
private IEnumerator FlashBrightnesLostOnce(float brighntessAnimTime)
|
|
{
|
|
yield return StartCoroutine(FlashBrightnesOnce(finalColorLost, brighntessAnimTime));
|
|
}
|
|
|
|
private IEnumerator FlashBrightnessGainedCoroutine(float gain)
|
|
{
|
|
yield return StartCoroutine(FlashBrightnesGainedOnce(m_brightnessAnimTime / 3));
|
|
|
|
if (gain >= 1)
|
|
{
|
|
yield return StartCoroutine(FlashBrightnesGainedOnce(m_brightnessAnimTime / 3));
|
|
yield return StartCoroutine(FlashBrightnesGainedOnce(m_brightnessAnimTime / 3));
|
|
}
|
|
}
|
|
|
|
private IEnumerator FlashBrightnesGainedOnce(float brighntessAnimTime)
|
|
{
|
|
yield return StartCoroutine(FlashBrightnesOnce(finalColorGained, brighntessAnimTime));
|
|
}
|
|
|
|
public void SetSlidersScales(float value)
|
|
{
|
|
staminaSlider.SetScale(value);
|
|
healthSlider.SetScale(value);
|
|
faithSlider.SetScale(value);
|
|
}
|
|
|
|
private void SetTextureOffset(Material material, float value)
|
|
{
|
|
material.SetTextureOffset("_MaskAlpha", new Vector2(0.99f - value, 0));
|
|
}
|
|
|
|
public void ShowDamageSprite()
|
|
{
|
|
if (damaged)
|
|
{
|
|
damaged = false;
|
|
if (damageImage != null)
|
|
damageImage.color = flashColour;
|
|
}
|
|
else if (damageImage != null)
|
|
{
|
|
damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
|
|
}
|
|
}
|
|
|
|
public void EnableDamageSprite(vDamage damage)
|
|
{
|
|
if (damageImage != null)
|
|
damageImage.enabled = true;
|
|
damaged = true;
|
|
}
|
|
|
|
private void UpdateDebugWindow(vThirdPersonController cc)
|
|
{
|
|
if (cc.debugWindow)
|
|
{
|
|
if (debugPanel != null && !debugPanel.activeSelf)
|
|
debugPanel.SetActive(true);
|
|
if (debugText)
|
|
debugText.text = cc.DebugInfo();
|
|
}
|
|
else
|
|
{
|
|
if (debugPanel != null && debugPanel.activeSelf)
|
|
debugPanel.SetActive(false);
|
|
}
|
|
}
|
|
|
|
private void ChangeInputDisplay()
|
|
{
|
|
if (displayControls == null) return;
|
|
#if MOBILE_INPUT
|
|
displayControls.enabled = false;
|
|
#else
|
|
if (controllerInput)
|
|
displayControls.sprite = joystickControls;
|
|
else
|
|
displayControls.sprite = keyboardControls;
|
|
#endif
|
|
}
|
|
|
|
private void InitFadeText()
|
|
{
|
|
if (fadeText != null)
|
|
{
|
|
fadeText.verticalOverflow = VerticalWrapMode.Overflow;
|
|
startColor = fadeText.color;
|
|
endColor.a = 0f;
|
|
fadeText.color = endColor;
|
|
}
|
|
else
|
|
Debug.Log("Please assign a Text object on the field Fade Text");
|
|
}
|
|
|
|
private void FadeEffect()
|
|
{
|
|
if (fadeText != null)
|
|
{
|
|
if (fade)
|
|
{
|
|
fadeText.color = Color.Lerp(endColor, startColor, timer);
|
|
|
|
if (timer < 1)
|
|
timer += Time.deltaTime / fadeDuration;
|
|
|
|
if (fadeText.color.a >= 1)
|
|
{
|
|
fade = false;
|
|
timer = 0f;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (fadeText.color.a >= 1)
|
|
durationTimer += Time.deltaTime;
|
|
|
|
if (durationTimer >= textDuration)
|
|
{
|
|
fadeText.color = Color.Lerp(startColor, endColor, timer);
|
|
if (timer < 1)
|
|
timer += Time.deltaTime / fadeDuration;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
//Destroy the instance
|
|
Destroy(healthBarMaterial);
|
|
Destroy(staminaBarMaterial);
|
|
Destroy(brightnessBarMaterial);
|
|
Destroy(faithBarMaterial);
|
|
|
|
if (Player.Instance)
|
|
{
|
|
Player.Instance.onStatsUpdated -= SetSlidersScales;
|
|
}
|
|
}
|
|
}
|
|
} |