1
0
Fork 0
Bildschirmflausch-LD41/Assets/Scripts/Door.cs

84 lines
2.3 KiB
C#
Raw Normal View History

using UnityEngine;
[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-22 11:27:09 +02:00
[SerializeField]
Room parent;
2018-04-22 11:27:09 +02:00
BoxCollider2D boundingBox;
2018-04-21 18:21:31 +02:00
BoxCollider2D triggerBox;
// 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>();
foreach ( BoxCollider2D collider in colliders ) {
if ( collider.isTrigger ) {
2018-04-21 18:21:31 +02:00
triggerBox = collider;
//Debug.Log("Found Door trigger");
2018-04-21 18:21:31 +02:00
} else {
boundingBox = collider;
//Debug.Log("Found Door collider");
2018-04-21 18:21:31 +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
/// <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;
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
}
/// <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;
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
}
/// <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
/// <summary>
/// Check if a player moved inside of a room and notify the room.
/// </summary>
/// <param name="collision"></param>
private void OnTriggerExit2D(Collider2D collision) {
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;
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-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
}