using UnityEngine;
using System.Collections;

public class TrapFloor : MonoBehaviour {

    GameObject go;
    public string floor;
    public string floorDestiny;
    public Vector3 obj;
    public int time = 1;
    public bool done = false;
    PlayerMovement p;
    GameObject fl1;
    GameObject fl2;
    public bool rotate = false;
    public bool doneRotate = false;


    void Start() {
        this.go = GameObject.Find(floor);
        this.fl1 = GameObject.Find("FakePlane1-7-3");
        this.fl2 = GameObject.Find("FakePlane1-7-4");
    }


    void OnCollisionEnter(Collision other) {
        if (!done) {
            if (other.gameObject.tag == "Player") {
                p = other.gameObject.GetComponent<PlayerMovement>();
                rotate = true;
            }
        }
    }

    void OnCollisionExit(Collision other) {
        if (!done) {
            if (other.gameObject.tag == "Player") {
                this.go.SetActive(false);
                Invoke("disable", time);
            }
        }
    }

    void disable() {
         this.go.SetActive(true);
         p.falling = false;
         done = true;
         Vector3 obj1 = new Vector3(this.fl1.transform.localEulerAngles.x,
                         this.fl1.transform.localEulerAngles.y,
                         0);

         this.fl1.transform.localEulerAngles = obj1;
         this.fl2.transform.localEulerAngles = obj1;
         this.gameObject.SetActive(false);
         
    }

    void Update() {
        if (rotate) {
            float ro1 = this.fl1.transform.localEulerAngles.z + 5;
            ro1 = Mathf.Clamp(ro1, 0, 75);
            Vector3 obj1 = new Vector3(this.fl1.transform.localEulerAngles.x,
                        this.fl1.transform.localEulerAngles.y,
                        ro1);

            float ro2 = this.fl2.transform.localEulerAngles.z + 5;
            ro2 = Mathf.Clamp(ro2, 0, 75);
            Vector3 obj2 = new Vector3(this.fl2.transform.localEulerAngles.x,
                        this.fl2.transform.localEulerAngles.y,
                        -ro2);

            this.fl1.transform.localEulerAngles = obj1;
            this.fl2.transform.localEulerAngles = obj2;
            
            if (ro1 == 75) doneRotate = true;
        }

        if (doneRotate)
        {
            rotate = false;
            doneRotate = false;
            go.GetComponent<BoxCollider>().enabled = false;
            p.fallingRoom(-20, floorDestiny);
        }


    }

}
