using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Profiling;

public class WaterImpact : MonoBehaviour
{
    public ParticleSystem splashParticles;
    public Renderer waterRenderer;
    public Color splashColor = new Color(0.5f, 0.7f, 1f, 1f);
    public float splashDuration = 0.5f;

    private Material waterMaterial;
    private Color originalShallowColor;
    private Color originalDeepColor;
    private float splashTimer = 0f;
    private bool isSplashing = false;

    void Start()
    {
        if (waterRenderer != null)
        {
            waterMaterial = waterRenderer.material;
            originalShallowColor = waterMaterial.GetColor("_ShallowColor");
            originalDeepColor = waterMaterial.GetColor("_DeepColor");
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.attachedRigidbody != null)
        {
            // Instancia partícula en el punto de contacto
            if (splashParticles != null)
            {
                splashParticles.transform.position = other.ClosestPoint(transform.position);
                splashParticles.Play();
            }

            // Cambiar color
            if (waterMaterial != null && !isSplashing)
            {
                waterMaterial.SetColor("_ShallowColor", splashColor);
                waterMaterial.SetColor("_DeepColor", splashColor * 0.5f); // Oscurecer un poco
                isSplashing = true;
                splashTimer = splashDuration;
            }
        }
    }

    void Update()
    {
        if (isSplashing)
        {
            splashTimer -= Time.deltaTime;
            if (splashTimer <= 0f)
            {
                // Restaurar colores originales
                waterMaterial.SetColor("_ShallowColor", originalShallowColor);
                waterMaterial.SetColor("_DeepColor", originalDeepColor);
                isSplashing = false;
            }
        }
    }
}


