Viksy

Door

Sep 17th, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.17 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5.  
  6. public class Door : Interactable
  7. {
  8.     [Header("Key")]
  9.     [SerializeField] string sentence;
  10.     [SerializeField] bool isUsingKey = false;
  11.     [SerializeField] GameObject key = default;
  12.  
  13.     [Header("Audio")]
  14.     [SerializeField] AudioSource audioSource;
  15.     [SerializeField] AudioClip[] openSound;
  16.     [SerializeField] AudioClip closeSound;
  17.     [SerializeField] AudioClip lockSound;
  18.  
  19.     [Header("Auto Close")]
  20.     [SerializeField] bool shouldAutoClose = false;
  21.     [SerializeField] float timeBeforeClosing;
  22.     [SerializeField] float distanceBeforeClosing;
  23.  
  24.     public bool shouldCloseAfterOpen = false;
  25.     public bool isOpen = false;
  26.     public bool canBeInteractedWith = true;
  27.     public bool showHand = true;
  28.     public bool isForcing = false;
  29.     private bool forceAudioOnce = true;
  30.  
  31.     private Animator anim;
  32.     private TextMeshProUGUI textDisplay;
  33.  
  34.     private void Start()
  35.     {
  36.         anim = GetComponent<Animator>();
  37.         audioSource = GetComponent<AudioSource>();
  38.         textDisplay = GameObject.FindGameObjectWithTag("MainText").GetComponent<TextMeshProUGUI>();
  39.     }
  40.  
  41.     private void Update()
  42.     {
  43.         if (isForcing)
  44.         {
  45.             anim.SetBool("isForce", true);
  46.             isUsingKey = true;
  47.             showHand = true;
  48.             isOpen = false;
  49.             anim.SetFloat("dot", 0);
  50.  
  51.             if (forceAudioOnce == true)
  52.             {
  53.                 audioSource.PlayOneShot(closeSound);
  54.                 forceAudioOnce = false;
  55.             }
  56.         }
  57.         else
  58.         {
  59.             anim.SetBool("isForce", false);
  60.         }
  61.  
  62.         if (shouldCloseAfterOpen && isOpen == true)
  63.         {
  64.             canBeInteractedWith = false;
  65.             showHand = false;
  66.         }
  67.     }
  68.  
  69.     public override void OnInteract()
  70.     {
  71.         if (isUsingKey && showHand)
  72.         {
  73.             textDisplay.text = sentence;
  74.  
  75.             if (key.activeInHierarchy == false)
  76.             {
  77.                 audioSource.PlayOneShot(lockSound);
  78.                 canBeInteractedWith = false;
  79.             }
  80.             else
  81.             {
  82.                 canBeInteractedWith = true;
  83.                 key.SetActive(false);
  84.             }
  85.         }
  86.  
  87.         if (canBeInteractedWith)
  88.         {
  89.             isUsingKey = false;
  90.             PlayerController.instance.interactOB.SetActive(false);
  91.             textDisplay.text = "";
  92.  
  93.             if (isOpen)
  94.             {
  95.                 audioSource.PlayOneShot(closeSound);
  96.             }
  97.             if (isOpen == false)
  98.             {
  99.                 audioSource.PlayOneShot(openSound[Random.Range(0, openSound.Length)]);
  100.             }
  101.  
  102.             isOpen = !isOpen;
  103.  
  104.             Vector3 playerTransformDirection = PlayerController.instance.transform.position - transform.position;
  105.             Vector3 doorTransformDirection = transform.TransformDirection(Vector3.forward);
  106.             float dot = Vector3.Dot(playerTransformDirection, doorTransformDirection);
  107.  
  108.             Debug.Log(dot);
  109.  
  110.             anim.SetFloat("dot", dot);
  111.             anim.SetBool("isOpen", isOpen);
  112.  
  113.             if (shouldAutoClose)
  114.             {
  115.                 StartCoroutine(AutoClose());
  116.             }
  117.         }
  118.     }
  119.  
  120.     public override void OnFocus()
  121.     {
  122.         if (showHand)
  123.         {
  124.             PlayerController.instance.interactOB.SetActive(true);
  125.         }
  126.     }
  127.  
  128.     public override void OnLoseFocus()
  129.     {
  130.         textDisplay.text = "";      
  131.     }
  132.  
  133.     private void Animator_LockInteraction()
  134.     {
  135.         canBeInteractedWith = false;
  136.     }
  137.  
  138.     private void Animator_UnlockInteraction()
  139.     {
  140.         canBeInteractedWith = true;
  141.     }
  142.  
  143.     private IEnumerator AutoClose()
  144.     {
  145.         while (isOpen)
  146.         {
  147.             yield return new WaitForSeconds(timeBeforeClosing);
  148.  
  149.             if (Vector3.Distance(transform.position, PlayerController.instance.transform.position) > distanceBeforeClosing)
  150.             {
  151.                 isOpen = false;
  152.                 anim.SetFloat("dot", 0);
  153.                 anim.SetBool("isOpen", isOpen);
  154.             }
  155.         }
  156.     }
  157. }
Add Comment
Please, Sign In to add comment