2018-04-22 15:59:14 +02:00
|
|
|
|
using UnityEngine;
|
2018-04-22 19:43:29 +02:00
|
|
|
|
[RequireComponent(typeof(SpriteRenderer))]
|
2018-04-21 15:39:48 +02:00
|
|
|
|
public class Door : MonoBehaviour {
|
|
|
|
|
private bool locked = false;
|
2018-04-23 19:39:18 +02:00
|
|
|
|
private Animator animator;
|
2018-04-21 16:24:58 +02:00
|
|
|
|
|
2018-04-22 11:27:09 +02:00
|
|
|
|
[SerializeField]
|
2018-04-22 15:59:14 +02:00
|
|
|
|
Room parent;
|
2018-04-22 11:27:09 +02:00
|
|
|
|
|
2018-04-21 16:24:58 +02:00
|
|
|
|
BoxCollider2D boundingBox;
|
2018-04-21 18:21:31 +02:00
|
|
|
|
BoxCollider2D triggerBox;
|
2018-04-22 15:59:14 +02:00
|
|
|
|
|
|
|
|
|
// Use this for initialization
|
2018-04-23 02:23:06 +02:00
|
|
|
|
void Awake() {
|
2018-04-21 18:21:31 +02:00
|
|
|
|
BoxCollider2D[] colliders = GetComponents<BoxCollider2D>();
|
2018-04-23 19:39:18 +02:00
|
|
|
|
animator = GetComponent<Animator>();
|
2018-04-22 15:59:14 +02:00
|
|
|
|
foreach ( BoxCollider2D collider in colliders ) {
|
|
|
|
|
if ( collider.isTrigger ) {
|
2018-04-21 18:21:31 +02:00
|
|
|
|
triggerBox = collider;
|
2018-04-22 19:43:29 +02:00
|
|
|
|
//Debug.Log("Found Door trigger");
|
2018-04-21 18:21:31 +02:00
|
|
|
|
} else {
|
|
|
|
|
boundingBox = collider;
|
2018-04-22 19:43:29 +02:00
|
|
|
|
//Debug.Log("Found Door collider");
|
2018-04-21 18:21:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-22 15:59:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-22 19:43:29 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the parent Room Object reference.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="room"></param>
|
|
|
|
|
public void SetParent(Room room) {
|
|
|
|
|
this.parent = room;
|
|
|
|
|
}
|
2018-04-23 19:39:18 +02:00
|
|
|
|
|
2018-04-22 15:59:14 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Locks the door.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Lock() {
|
2018-04-23 19:39:18 +02:00
|
|
|
|
animator.SetBool("open", false);
|
2018-04-21 15:39:48 +02:00
|
|
|
|
locked = true;
|
2018-04-21 16:24:58 +02:00
|
|
|
|
boundingBox.enabled = true;
|
2018-04-21 18:21:31 +02:00
|
|
|
|
triggerBox.enabled = false;
|
2018-04-22 20:37:16 +02:00
|
|
|
|
//GetComponent<SpriteRenderer>().enabled = true;
|
2018-04-21 15:39:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-22 15:59:14 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Unlocks the door.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Unlock() {
|
2018-04-23 19:39:18 +02:00
|
|
|
|
animator.SetBool("open", true);
|
2018-04-21 15:39:48 +02:00
|
|
|
|
locked = false;
|
2018-04-21 16:24:58 +02:00
|
|
|
|
boundingBox.enabled = false;
|
2018-04-21 18:21:31 +02:00
|
|
|
|
triggerBox.enabled = true;
|
2018-04-22 20:37:16 +02:00
|
|
|
|
//GetComponent<SpriteRenderer>().enabled = false;
|
2018-04-21 15:39:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-22 15:59:14 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns if the door is locked.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool IsLocked() {
|
2018-04-21 15:39:48 +02:00
|
|
|
|
return locked;
|
|
|
|
|
}
|
2018-04-22 11:27:09 +02:00
|
|
|
|
|
2018-04-22 15:59:14 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Check if a player moved inside of a room and notify the room.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="collision"></param>
|
|
|
|
|
private void OnTriggerExit2D(Collider2D collision) {
|
2018-04-22 19:43:29 +02:00
|
|
|
|
if ( collision.tag == "Player") {
|
2018-04-23 17:21:41 +02:00
|
|
|
|
Player player = collision.gameObject.GetComponent<Player>();
|
|
|
|
|
if ((boundingBox.offset - parent.GetCenter()).sqrMagnitude < ((Vector2) player.transform.position - parent.GetCenter()).sqrMagnitude)
|
|
|
|
|
return;
|
2018-04-22 15:59:14 +02:00
|
|
|
|
Debug.Log("Leaving Trigger");
|
2018-04-23 14:59:34 +02:00
|
|
|
|
if(parent == null) {
|
|
|
|
|
Debug.Log("This door has no parent Room!");
|
|
|
|
|
return;
|
2018-04-22 19:43:29 +02:00
|
|
|
|
}
|
2018-04-23 17:21:41 +02:00
|
|
|
|
parent.OnPlayerEnter(player);
|
2018-04-22 11:27:09 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-21 15:39:48 +02:00
|
|
|
|
}
|