Files
beyond/Assets/Scripts/Effects/CorpsBurnout.cs
2024-11-20 15:21:28 +01:00

116 lines
3.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Invector.vCharacterController.AI;
using Sirenix.OdinInspector;
using UnityEngine;
namespace Beyond
{
public class CorpsBurnout : MonoBehaviour
{
// Start is called before the first frame update
public Renderer[] m_meshesToDissolve;
public PSMeshRendererUpdater m_effect;
Material[] m_dissolveMaterial;
public float m_burnoutTime = 3f;
public float m_destroyTime = 5f;
public float m_delay = 1f;
public Light m_lightToAnimate;
private bool m_started = false;
private vControlAICombat m_AICombat;
void Start()
{
if (m_AICombat == null)
{
m_AICombat = GetComponent<vControlAICombat>();
if (m_AICombat)
{
m_AICombat.onDead.AddListener((arg0 => { StartBurnout();}));
}
}
int matNum = 0;
if (m_meshesToDissolve.Length == 0)
{
m_meshesToDissolve = transform.GetComponentsInChildren<Renderer>();
}
for (int i = 0; i < m_meshesToDissolve.Length; i++)
{
matNum += m_meshesToDissolve[i].materials.Length;
}
m_dissolveMaterial = new Material[matNum];
int cnt = 0;
for (int i = 0; i < m_meshesToDissolve.Length; i++)
{
for (int j=0; j<m_meshesToDissolve[i].materials.Length; j++)
m_dissolveMaterial[cnt++] = m_meshesToDissolve[i].materials[j];
}
}
void SetTh(float th)
{
foreach (var m in m_dissolveMaterial)
{
m.SetFloat("_Threshold", th);
}
}
public void OnHealthChanged(float h)
{
if (!m_started && h <= 0)
{
StartBurnout();
}
}
[Button]
public void StartBurnout()
{
if (!m_started)
StartCoroutine(Burnout());
}
IEnumerator Burnout()
{
m_started = true;
yield return new WaitForSeconds(m_delay);
try
{
if (m_effect)
{
m_effect.gameObject.SetActive(true);
m_effect.UpdateMeshEffect();
}
}
catch (Exception e)
{
Debug.LogError("Burnout error: "+e);
}
float th = 0f;
float time = 0f;
float startingIntensity = 0;
if (m_lightToAnimate)
startingIntensity = m_lightToAnimate.intensity;
while (time < m_burnoutTime)
{
th = time / m_burnoutTime;
SetTh(th);
time += Time.deltaTime;
if (m_lightToAnimate)
m_lightToAnimate.intensity = th * startingIntensity;
yield return null;
}
SetTh(1f);
if (m_effect)
m_effect.IsActive = false;
yield return new WaitForSeconds(m_destroyTime - m_burnoutTime);
Destroy(gameObject);
}
}
}