using UnityEngine; [RequireComponent(typeof(SpriteRenderer))] public class Door : MonoBehaviour { private bool locked = false; [SerializeField] Room parent; BoxCollider2D boundingBox; BoxCollider2D triggerBox; // Use this for initialization void Start() { BoxCollider2D[] colliders = GetComponents(); foreach ( BoxCollider2D collider in colliders ) { if ( collider.isTrigger ) { triggerBox = collider; //Debug.Log("Found Door trigger"); } else { boundingBox = collider; //Debug.Log("Found Door collider"); } } Unlock(); } /// /// Sets the parent Room Object reference. /// /// public void SetParent(Room room) { this.parent = room; } /// /// Locks the door. /// public void Lock() { locked = true; boundingBox.enabled = true; triggerBox.enabled = false; GetComponent().enabled = true; } /// /// Unlocks the door. /// public void Unlock() { locked = false; boundingBox.enabled = false; triggerBox.enabled = true; GetComponent().enabled = false; } /// /// Returns if the door is locked. /// /// public bool IsLocked() { return locked; } /// /// Check if a player moved inside of a room and notify the room. /// /// private void OnTriggerExit2D(Collider2D collision) { if ( collision.tag == "Player") { // TODO better checks Debug.Log("Leaving Trigger"); if (parent != null) { parent.OnPlayerEnter(collision.gameObject.GetComponent()); } } } }