Files
beyond/Assets/Scripts/Effects/ShieldCreationEffect.cs
2025-08-11 18:17:39 +02:00

224 lines
6.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using DG;
using DG.Tweening;
using PixelCrushers.DialogueSystem;
using System;
public class ShieldCreationEffect : MonoBehaviour
{
[SerializeField] private MeshRenderer m_mesh;
private Material m_material;
// Handler for block start event
private void OnBlockStartEvent()
{
// Add your event handling logic here
Debug.Log("BlockStart event triggered.", gameObject);
}
[ColorUsageAttribute(true, true)]
public Color m_FresnelColor;
public float m_dissolveTime = 1f;
public float m_fresnelTime = 0.3f;
public float m_lightIntensity = 3f;
public Light m_light;
[SerializeField] private ParticleSystem m_particles;
[SerializeField] private ParticleSystem m_distortions;
private bool hasValidBehaviours;
private bool hasAnimator;
const string blockStartEvent = "BlockStart";
const string blockEndEvent = "BlockEnd";
private void OnDisable()
{
RemoveEvents();
}
public void OnEnable()
{
if (hasAnimator && hasValidBehaviours)
{
RemoveEvents();
RegisterEvents();
}
}
private void OnDestroy()
{
RemoveEvents();
}
public virtual void RegisterEvents()
{
//if (animatorEvents.Count > 0)
{
var animator = GetComponentInParent<Animator>();
if (animator)
{
hasAnimator = true;
var behaviours = animator.GetBehaviours<Invector.vEventSystems.vAnimatorEvent>();
{
var hasEvent = false;
for (int i = 0; i < behaviours.Length; i++)
{
if (behaviours[i].HasEvent(blockStartEvent))
{
behaviours[i].RegisterEvents(blockStartEvent, OnBlockStartEvent);
hasValidBehaviours = true;
hasEvent = true;
}
if (behaviours[i].HasEvent(blockEndEvent))
{
behaviours[i].RegisterEvents(blockEndEvent, OnBlockEndEvent);
hasValidBehaviours = true;
hasEvent = true;
}
}
}
}
else
{
Debug.LogWarning("Can't Find Animator to register Events in " + gameObject.name, gameObject);
}
}
}
private void OnBlockStartEvent(string eventName)
{
Create();
}
private void OnBlockEndEvent(string eventName)
{
Uncreate();
}
public virtual void RemoveEvents()
{
if (!hasAnimator || !hasValidBehaviours)
{
return;
}
var animator = GetComponentInParent<Animator>();
if (animator)
{
var behaviours = animator.GetBehaviours<Invector.vEventSystems.vAnimatorEvent>();
for (int i = 0; i < behaviours.Length; i++)
{
if (behaviours[i].HasEvent(blockStartEvent))
{
behaviours[i].RemoveEvents(blockStartEvent, OnBlockStartEvent);
}
if (behaviours[i].HasEvent(blockEndEvent))
{
behaviours[i].RemoveEvents(blockEndEvent, OnBlockEndEvent);
}
}
}
}
private void Awake()
{
if (m_mesh == null)
m_mesh = GetComponent<MeshRenderer>();
if (m_mesh == null)
{
Debug.LogError("No mesh renderer found!");
return;
}
m_material = m_mesh.material;
if (m_light)
m_light.intensity = 0f;
ParticlesEmission(false);
SetLight(false);
m_mesh.enabled = false;
}
private void SetLight(bool enabled)
{
if (m_light)
{
m_light.intensity = enabled ? m_lightIntensity : 0f;
m_light.enabled = enabled;
}
}
private void Start()
{
RegisterEvents();
}
private void ParticlesEmission(bool enabled)
{
if (m_particles != null)
{
var emm = m_particles.emission;//.enabled = false;
emm.enabled = enabled;
}
if (m_distortions != null)
{
var emm = m_distortions.emission;//.enabled = false;
emm.enabled = enabled;
}
}
[Button]
public void Uncreate()
{
m_mesh.enabled = false;
ParticlesEmission(false);
SetLight(false);
}
/*
private IEnumerator CreateCoroutine()
{
m_material.SetFloat("_Threshold", 1f);
Sequence sq = DOTween.Sequence();
sq.Append(m_material.DOFloat(0f, "_Threshold", m_dissolveTime));
ParticlesEmission(true);
yield return new WaitForSeconds(1f);
ParticlesEmission(false);
//yield return new WaitForSeconds(1f);
DOTween.Kill(m_material);
m_material.SetColor("_FresnelColor", Color.black);
m_material.SetFloat("_EffectThreshold", 1f);
sq.Append(m_material.DOColor(m_FresnelColor, "_FresnelColor", m_fresnelTime).SetEase(Ease.InSine));
sq.Append(m_material.DOColor(Color.black, "_FresnelColor", m_fresnelTime).SetEase(Ease.InSine));
m_material.DOFloat(0f, "_EffectThreshold", m_dissolveTime * 2f).SetEase(Ease.Linear).SetLoops(-1, LoopType.Yoyo);//.SetDelay(m_dissolveTime+2f* m_fresnelTime);
if (m_light)
{
m_light.intensity = 0f;
sq = DOTween.Sequence();
sq.Append(m_light.DOIntensity(m_lightIntensity, m_dissolveTime + m_fresnelTime).SetEase(Ease.InSine));
sq.Append(m_light.DOIntensity(0f, m_fresnelTime).SetEase(Ease.InSine));
}
//m_particles.DO
//m_material.DOs
// m_material.DOFloat(0f, "_Threshold", 1f);
//set fresnel color
//m_material.DOColor(m_FresnelColor, "_FresnelColor", 2f);
yield return null;
}
*/
[Button]
public void Create()
{
m_mesh.enabled = true;
ParticlesEmission(true);
SetLight(true);
//StartCoroutine(CreateCoroutine());
}
}