lucid-super-dream/Assets/Scripts/DisableOnDeath.cs

72 lines
2.0 KiB
C#
Raw Normal View History

using System.Net.Http;
2021-01-05 13:10:20 +01:00
using DG.Tweening;
using UnityEngine;
using Weapons.Scripts;
[RequireComponent(typeof(EntityHealth))]
public class DisableOnDeath : MonoBehaviour
{
private EntityHealth _health;
2021-01-06 16:10:11 +01:00
private Renderer _renderer;
private Collider _collider;
2021-01-09 17:08:57 +01:00
private Color _color1;
private Color _color2;
2021-01-10 17:51:34 +01:00
private static readonly int Color1 = Shader.PropertyToID(COLOR_1);
private static readonly int Color2 = Shader.PropertyToID(COLOR_2);
private static readonly int Alpha = Shader.PropertyToID("_Alpha");
2021-01-09 17:08:57 +01:00
private const string COLOR_1 = "_Color1";
private const string COLOR_2 = "_Color2";
2021-01-06 16:10:11 +01:00
2021-01-05 13:10:20 +01:00
private void Awake()
{
_health = GetComponent<EntityHealth>();
2021-01-06 16:10:11 +01:00
_renderer = GetComponentInChildren<Renderer>();
_collider = GetComponentInChildren<Collider>();
2021-01-09 17:08:57 +01:00
2021-01-10 17:51:34 +01:00
_color1 = _renderer.material.GetColor(Color1);
_color2 = _renderer.material.GetColor(Color2);
2021-01-05 13:10:20 +01:00
}
private void OnEnable()
{
_health.Die += Die;
_collider.enabled = true;
2021-01-10 17:51:34 +01:00
_renderer.material.SetColor(Color1, _color1);
_renderer.material.SetColor(Color2, _color2);
2021-01-05 13:10:20 +01:00
}
private void OnDisable()
{
_health.Die -= Die;
}
private void Die()
{
var oldName = gameObject.name;
gameObject.transform.parent.name = "disabled";
_collider.enabled = false;
float duration = 0.4f;
2021-01-09 17:08:57 +01:00
float x = 0;
DOTween.To(
() => x,
t =>
{
var c1 = Color.Lerp(_color1, Color.white*1000f, 1f-t);
var c2 = Color.Lerp(_color2, Color.white*1000f, 1f-t);
2021-01-09 17:08:57 +01:00
2021-01-10 17:51:34 +01:00
_renderer.material.SetColor(Color1, c1);
2021-01-09 17:08:57 +01:00
},
1.0f,
duration);
2021-01-09 17:08:57 +01:00
transform.DOScale(Vector3.one * 3.0f, duration).SetEase(Ease.OutQuint).OnComplete(() =>
2021-01-05 13:10:20 +01:00
{
gameObject.name = oldName;
gameObject.SetActive(false);
_health.Reset();
});
}
}