MiniGames/Assets/Scripts/Shared/Shared_DieZibu.cs

154 lines
4.9 KiB
C#
Raw Normal View History

2025-09-22 17:26:19 +05:00
using UnityEngine;
using DG.Tweening;
public enum DeathType { Normal, Laser }
public class Shared_DieZibu : MonoBehaviour
{
public Animator animator;
public Transform actor;
public Material actorMaterial;
public bool isAngel = true;
public bool ghostEnabled = true; // <-- new toggle
public float heavenHeight = 4f;
public float ascendDuration = 2.5f;
public float fadeDelay = 2.0f;
public float fadeDuration = 0.5f;
static readonly int ID_FadeAmount = Shader.PropertyToID("_FadeAmount");
static readonly int ID_FadeBurnWidth = Shader.PropertyToID("_FadeBurnWidth");
static readonly int ID_GreyscaleBlend = Shader.PropertyToID("_GreyscaleBlend");
static readonly int ID_GhostBlend = Shader.PropertyToID("_GhostBlend");
static readonly int ID_OverlayBlend = Shader.PropertyToID("_OverlayBlend");
const string KW_FADE = "FADE_ON";
const string KW_GREYSCALE = "GREYSCALE_ON";
const string KW_GHOST = "GHOST_ON";
const string KW_OVERLAY = "OVERLAY_ON";
const string KW_OVERLAYMULT = "OVERLAYMULT_ON";
private Sequence deathSeq;
private bool isDead;
void Awake()
{
if (!actor) actor = transform;
}
public void KillByLaser() => TriggerDeath(DeathType.Laser);
public void KillNormal() => TriggerDeath(DeathType.Normal);
public void TriggerDeath(DeathType type)
{
if (isDead) return;
isDead = true;
var rb = GetComponent<Rigidbody>();
if (rb)
{
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
rb.useGravity = false;
rb.isKinematic = true;
}
var col = GetComponent<Collider>();
if (col) col.enabled = false;
bool laserStyle = (type == DeathType.Laser);
if (isAngel && animator) animator.Play("Angel");
BeginDeathSequence(laserStyle);
}
void BeginDeathSequence(bool laserStyle)
{
if (deathSeq != null && deathSeq.IsActive()) deathSeq.Kill();
if (actorMaterial) DOTween.Kill(actorMaterial);
if (actorMaterial)
{
actorMaterial.EnableKeyword(KW_FADE);
if (laserStyle)
{
actorMaterial.DisableKeyword(KW_GREYSCALE);
actorMaterial.EnableKeyword(KW_OVERLAY);
actorMaterial.DisableKeyword(KW_OVERLAYMULT);
}
else
{
actorMaterial.EnableKeyword(KW_GREYSCALE);
actorMaterial.DisableKeyword(KW_OVERLAY);
actorMaterial.DisableKeyword(KW_OVERLAYMULT);
}
if (ghostEnabled) actorMaterial.EnableKeyword(KW_GHOST);
else actorMaterial.DisableKeyword(KW_GHOST);
actorMaterial.SetFloat(ID_FadeAmount, -0.1f);
actorMaterial.SetFloat(ID_FadeBurnWidth, 0f);
actorMaterial.SetFloat(ID_GreyscaleBlend, 0f);
actorMaterial.SetFloat(ID_GhostBlend, 0f);
actorMaterial.SetFloat(ID_OverlayBlend, 0f);
}
deathSeq = DOTween.Sequence();
if (actorMaterial)
{
if (laserStyle)
{
deathSeq.Append(actorMaterial.DOFloat(1f, ID_FadeBurnWidth, 0.5f));
if (ghostEnabled)
deathSeq.Join(actorMaterial.DOFloat(0.65f, ID_GhostBlend, 0.35f).SetEase(Ease.OutQuad));
deathSeq.Join(actorMaterial.DOFloat(1.0f, ID_OverlayBlend, 0.35f).SetEase(Ease.OutQuad));
}
else
{
deathSeq.Join(actorMaterial.DOFloat(0.7f, ID_GreyscaleBlend, 0.35f).SetEase(Ease.OutQuad));
if (ghostEnabled)
deathSeq.Join(actorMaterial.DOFloat(0.65f, ID_GhostBlend, 0.35f).SetEase(Ease.OutQuad));
}
}
if (isAngel && actor)
{
Vector3 endPos = actor.position + Vector3.up * heavenHeight;
deathSeq.Append(actor.DOMoveY(endPos.y, ascendDuration).SetEase(Ease.OutSine));
}
deathSeq.AppendInterval(fadeDelay);
if (actorMaterial)
{
if (laserStyle)
{
deathSeq.Append(actorMaterial.DOFloat(1f, ID_FadeAmount, fadeDuration));
deathSeq.Join(actorMaterial.DOFloat(0f, ID_FadeBurnWidth, fadeDuration));
}
else
{
deathSeq.Append(actorMaterial.DOFloat(1f, ID_FadeAmount, fadeDuration));
}
}
deathSeq.Play();
}
void OnDisable()
{
if (deathSeq != null && deathSeq.IsActive()) deathSeq.Kill();
if (actorMaterial)
{
DOTween.Kill(actorMaterial);
actorMaterial.SetFloat(ID_FadeAmount, 0f);
actorMaterial.SetFloat(ID_FadeBurnWidth, 0f);
actorMaterial.SetFloat(ID_GreyscaleBlend, 0f);
actorMaterial.SetFloat(ID_GhostBlend, 0f);
actorMaterial.SetFloat(ID_OverlayBlend, 0f);
}
}
}