using Godot;
using System;

namespace Newlon.Components;

public partial class FlashComponent : CanvasGroup
{
	[Export] private float _flashDuration = 0.1f;
	private Tween _tween;
	private ShaderMaterial _shaderMaterial;

    public override void _Ready()
    {
        _shaderMaterial = Material as ShaderMaterial;
    }
	
	public void DamageFlash(int damage,Node origin)
	{
		Flash();
	}
    
    
	public void Flash()
	{
		_tween?.Kill();
		_tween = CreateTween();

		Action<float> action = SetAmount;
		_tween.TweenMethod(Callable.From(action),0.8f,0.0f,_flashDuration);
	}

	private void SetAmount(float amount)
	{
		_shaderMaterial.SetShaderParameter("amount",amount);
	}
}
