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

public class GravitySwitch : MonoBehaviour {

    public GameObject[] gravityBoxes;
    private Rigidbody[] gravityBoxesRB = new Rigidbody[10];
    public GameObject text;

    private bool canSwitch = false;

    void Start()
    {
        for(int i = 0; i < gravityBoxes.Length; i++)
        {
            gravityBoxesRB[i] = gravityBoxes[i].GetComponent<Rigidbody>();
        }

        text.SetActive(false);
    }

	// Update is called once per frame
	void Update () {
		if(canSwitch)
        {
            if(Input.GetKeyDown(KeyCode.Space))
            {
                for (int i = 0; i < gravityBoxesRB.Length; i++)
                {
                    if (gravityBoxesRB[i])
                    {
                        if (gravityBoxesRB[i].constraints == RigidbodyConstraints.FreezeAll)
                        {
                            gravityBoxesRB[i].constraints = RigidbodyConstraints.None;
                            gravityBoxesRB[i].constraints = RigidbodyConstraints.FreezeRotation;
                        }
                        else
                        {
                            gravityBoxesRB[i].constraints = RigidbodyConstraints.FreezeAll;
                        }
                    }
                }
                SoundManager.instance.PlaySingle(4);
            }
        }
	}

    void OnTriggerStay(Collider col)
    {
        canSwitch = true;
        text.SetActive(true);
    }

    void OnTriggerExit(Collider col)
    {
        canSwitch = false;
        text.SetActive(false);
    }
}
