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

105 lines
3.8 KiB
C#

using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Beyond
{
public class BrightnessAuraEffectController : MonoBehaviour
{
[SerializeField]
private List<Renderer> skinRenderers, clothesRenderers;
private List<Material> skinMaterials = new(), clothesMaterials = new();
private Material clothMaterial, hairMaterial;
[SerializeField]
private Color positiveBPColor, negativeBPColor;
[SerializeField]
private float flashDuration = 1f, currentDuration = 0f, delayDuration = 2f, minDelayDuration = 3f, maxDelayDuration = 4f;
private bool isGrowing = true;
private Color targetColor, baseColor, currentColor, clothesBaseColor = Color.black, clothesCurrentColor, clothesTargetColor;
// Start is called before the first frame update
private void Start()
{
//skinMaterial = skinRenderer.materials[0];
skinRenderers.ForEach(renderer => skinMaterials.Add(renderer.materials[0]));
clothesRenderers.ForEach(renderer => clothesMaterials.Add(renderer.materials[0]));
Player.Instance.BrightnessAttribute.OnValueChanged.AddListener(SetBrightnessEffect);
}
[Button]
public void SetBrightnessEffect(float newVal, float oldVal)
{
if (newVal >= 51)
{
targetColor = positiveBPColor;
baseColor = positiveBPColor;
targetColor.a = (newVal - 47f) / 50f;
clothesTargetColor = Color.Lerp(clothesBaseColor, positiveBPColor, targetColor.a);
}
else if (newVal <= 49)
{
targetColor = negativeBPColor;
baseColor = targetColor;
targetColor.a = (53f - newVal) / 50f;
clothesTargetColor = Color.Lerp(clothesBaseColor, negativeBPColor, targetColor.a);
}
else //dont animate
{
targetColor = Color.black;
baseColor = targetColor;
clothesTargetColor = clothesBaseColor;
}
currentColor = baseColor;
// skinMaterial.SetColor("_RimColor", baseColor);
}
// Update is called once per frame
private void Update()
{
currentDuration += Time.deltaTime;
if (currentDuration < 0)
{
return;
}
if (currentDuration < flashDuration && isGrowing)
{
currentColor.a = Mathf.Lerp(0, targetColor.a, currentDuration / flashDuration);
clothesCurrentColor = Color.Lerp(clothesBaseColor, clothesTargetColor, currentDuration / flashDuration);
}
else if (currentDuration >= flashDuration && isGrowing)
{
// Debug.LogError("flash1");
isGrowing = false;
currentDuration = 0;
}
else if (currentDuration < flashDuration && !isGrowing)
{
currentColor.a = Mathf.Lerp(targetColor.a, 0, currentDuration / flashDuration);
clothesCurrentColor = Color.Lerp(clothesTargetColor, clothesBaseColor, currentDuration / flashDuration);
}
else
{
// Debug.LogError("flash2");
isGrowing = true;
currentDuration = -GetRandomDelayDuration();
}
skinMaterials.ForEach(material => material.SetColor("_RimColor", currentColor));
clothesMaterials.ForEach(material => material.SetColor("_FresnelColor", clothesCurrentColor));
}
public float GetRandomDelayDuration()
{
return Random.Range(minDelayDuration, maxDelayDuration);
}
}
}