105 lines
2.2 KiB
C#
105 lines
2.2 KiB
C#
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
|
|
public class Door : MonoBehaviour, IInteractable
|
|
{
|
|
[SerializeField] bool autoDoor;
|
|
[SerializeField] bool locked;
|
|
[SerializeField] bool isOpen;
|
|
[SerializeField] GameObject door;
|
|
[SerializeField] float openHeight = 3f;
|
|
[SerializeField] float openTime = 2f;
|
|
void Awake()
|
|
{
|
|
|
|
}
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
void OnTriggerEnter(Collider other)
|
|
{
|
|
if(other.CompareTag("Player") && autoDoor && !locked && !isOpen)
|
|
{
|
|
OpenDoor(openHeight);
|
|
isOpen = true;
|
|
}
|
|
else if(other.CompareTag("Player") && autoDoor && locked)
|
|
{
|
|
Debug.Log("The door is locked.");
|
|
}
|
|
}
|
|
void OnTriggerExit(Collider other)
|
|
{
|
|
if(other.CompareTag("Player") && autoDoor && isOpen)
|
|
{
|
|
CloseDoor(openHeight);
|
|
isOpen = false;
|
|
}
|
|
}
|
|
void OpenDoor(float height)
|
|
{
|
|
door.transform.Translate(0f,-height,0f);
|
|
}
|
|
|
|
void CloseDoor(float height)
|
|
{
|
|
door.transform.Translate(0f,height,0f);
|
|
}
|
|
|
|
public void Interact(GameObject interactor)
|
|
{
|
|
if (!autoDoor)
|
|
{
|
|
if (!locked && !isOpen)
|
|
{
|
|
OpenDoor(openHeight);
|
|
isOpen = true;
|
|
}
|
|
else if (!locked && isOpen)
|
|
{
|
|
CloseDoor(openHeight);
|
|
isOpen = false;
|
|
}
|
|
else if (locked)
|
|
{
|
|
Debug.Log("The door is locked.");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
public bool CanInteract(GameObject interactor)
|
|
{
|
|
return !locked;
|
|
}
|
|
|
|
public string GetInteractionPrompt()
|
|
{
|
|
if (locked)
|
|
return "Door is locked";
|
|
return isOpen ? "Press [E] to close door" : "Press [E] to open door";
|
|
}
|
|
|
|
public void OnHighlight()
|
|
{
|
|
// e.g. outline effect
|
|
}
|
|
|
|
public void OnUnhighlight()
|
|
{
|
|
// remove outline
|
|
}
|
|
}
|