using System;
using UnityEngine;

public class Door : MonoBehaviour {

    // Attributes.
	private Vector3 destiny;
	private GameObject coll;
    public Vector3 position;
    
    // Public attributes inserted in the inspector.
    public string planeDestiny;
    public bool opened;
    public bool hori;
    public bool vert;

    // When the player enters, goes to other plane.
	void OnTriggerEnter(Collider other) {
        this.coll = other.gameObject;

        if (this.coll.name == "PlayerBody") {
            bool transition = this.coll.GetComponent<PlayerMovement>().transition;

            // Little fix. If the player is in transition (going from the doo to the exit point)
            // and enters in another door, it prevents the new transition.
            if (transition) {
                return;
            }

            Vector3 exitPosition = position;
            
            // If the door is opened (open world), only one axis of the exit point is fixed and it will 
            // depend of the orientation. If is not opened, both are defined values.
            if (opened){
                if (hori) {
                    exitPosition.z = this.coll.transform.position.z;
                } else if (vert) {
                    exitPosition.x = this.coll.transform.position.x;                
                }
            }

            this.coll.GetComponent<PlayerMovement>().changingRoom(planeDestiny, exitPosition);            
        }
        
	}
}