using UnityEngine;

public class AntiFriction : MonoBehaviour
{
	public float dragCoefficient = 0.01f;
	public float activationFactor;
	public bool activated = false;

	private Rigidbody2D rb2;

	public void Activate()
	{
		activated = true;
		activationFactor = 1.0f;
	}

	public void Recharge()
	{
		activationFactor = 1.0f;
	}

    void FixedUpdate()
	{
		if (!activated)
			return;

		ApplyAntiDrag();
		activationFactor = Mathf.Max(0, activationFactor - 0.15f * Time.fixedDeltaTime);
	}

	private void ApplyAntiDrag()
	{
		float dragMagnitude = activationFactor * dragCoefficient * Mathf.Pow(rb2.velocity.magnitude, 2);
		Vector2 accel = rb2.velocity.normalized * dragMagnitude;
		Vector2 force = rb2.mass * accel;
		rb2.AddForce(force);
	}

	void Start()
	{
		rb2 = GetComponent<Rigidbody2D>();
	}

}
