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

public class Fan : MonoBehaviour
{
    public float forceStrength = 100;
    public Vector2 forceDirection;
    Vector2 force;
    Collider2D volume;
    public List<GameObject> affectedObjects;
    public bool enabled;
    

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (enabled)
        {
            force = forceDirection * forceStrength;
            for (int i = 0; i < affectedObjects.Count; i++)
            {
                if (affectedObjects[i].GetComponent<Rigidbody2D>() != null)
                {
                    affectedObjects[i].GetComponent<Rigidbody2D>().AddForce(force * Time.fixedDeltaTime);
                }
            }
        }
    }


    private void OnTriggerEnter2D(Collider2D col)
    {
        affectedObjects.Add(col.gameObject);
    }
    private void OnTriggerExit2D(Collider2D col)
    {
        affectedObjects.Remove(col.gameObject);
    }

    public void ToggleFan()
    {
        if (enabled)
        {
            enabled = false;
        }
        else
        {
            enabled = true;
        }
    }
}
