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

76 lines
2 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-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>();
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;
}
/// <summary>
/// Locks the door.
/// </summary>
public void Lock() {
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-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") {
// TODO better checks
Debug.Log("Leaving Trigger");
if (parent != null) {
parent.OnPlayerEnter(collision.gameObject.GetComponent<Player>());
}
2018-04-22 11:27:09 +02:00
}
}
2018-04-21 15:39:48 +02:00
}