using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChargeAttack : BaseAttack
{
    public float ChargeTime;
    [Range(0, 1)]
    protected float _timeThreshold;

    [SerializeField]
    protected float _knockBack;

    protected float _startChargeTime;
    protected float _charge;

    public override void OnButtonPressed(PlayerController player)
    {
        player.OnChargeStarted();
        StartCharging();
    }

    public virtual void OnButtonReleased(PlayerController player)
    {
        Execute(player, player.ShootingPoint.position);
        player.Rigidbody.AddForce(-player.Transform.right * _knockBack);
    }

    protected override void OnWaveSpawned(Wave wave)
    {
        float totalChargeTime = (Time.time - _startChargeTime) / ChargeTime;
        totalChargeTime = Mathf.Clamp(totalChargeTime, _timeThreshold, 1);

        _charge = totalChargeTime * wave.Parameters.LifeTime;

        wave.SetDuration(_charge);
        _charge = 0;
    }

    public void StartCharging()
    {
        _startChargeTime = Time.time;
    }
}
