tmoneygames

Portal.cs

Oct 13th, 2021 (edited)
1,305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | Source Code | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.AI;
  4. using UnityEngine.SceneManagement;
  5. using Sirenix.OdinInspector;
  6.  
  7. namespace RPG.SceneManagement
  8. {
  9.     public class Portal : MonoBehaviour
  10.     {
  11.         enum DestinationIdentifier
  12.         {
  13.             A, B, C, D, E, F, G, H, I, J, K, L, M,
  14.             N, O, P, Q, R, S, T, U, V, W, X, Y, Z
  15.         }
  16.  
  17.         // Variables
  18.         [BoxGroup("Portal Info")]
  19.         [GUIColor(1f, 0.3f, 0.3f)]
  20.         [SerializeField] int sceneToLoad = -1;
  21.         [BoxGroup("Portal Info")]
  22.         [GUIColor(0.3f, 0.3f, 1f)]
  23.         [SerializeField] Transform spawnPoint;
  24.         [BoxGroup("Portal Info")]
  25.         [GUIColor(0.3f, 01f, 0.3f)]
  26.         [SerializeField] DestinationIdentifier destination;
  27.         [BoxGroup("Portal Info")]
  28.         [GUIColor(0.3f, 0.3f, 0.3f)]
  29.         [Range(0f, 10f)]
  30.         [SerializeField] float fadeOutTime = 1f;
  31.         [BoxGroup("Portal Info")]
  32.         [GUIColor(1f, 0.3f, 1f)]
  33.         [Range(0f, 10f)]
  34.         [SerializeField] float fadeInTime = 2f;
  35.         [BoxGroup("Portal Info")]
  36.         [GUIColor(0f, 1f, 0.3f)]
  37.         [Range(0f, 10f)]
  38.         [SerializeField] float fadeWaitTime = 0.5f;
  39.  
  40.         void OnTriggerEnter(Collider other)
  41.         {
  42.             if(other.tag == "Player")
  43.             {
  44.                 StartCoroutine(Transition());
  45.             }
  46.         }
  47.  
  48.         private IEnumerator Transition()
  49.         {
  50.  
  51.             if(sceneToLoad < 0)
  52.             {
  53.                 Debug.LogError("Scene to load not set.");
  54.                 yield break;
  55.             }
  56.  
  57.             DontDestroyOnLoad(gameObject);
  58.  
  59.             Fader fader = FindObjectOfType<Fader>();
  60.  
  61.             yield return fader.FadeOut(fadeOutTime);
  62.  
  63.             SavingWrapper wrapper = FindObjectOfType<SavingWrapper>();
  64.             wrapper.Save();
  65.  
  66.             yield return SceneManager.LoadSceneAsync(sceneToLoad);
  67.  
  68.             wrapper.Load();
  69.  
  70.             Portal otherPortal = GetOtherPortal();
  71.             UpdatePlayer(otherPortal);
  72.  
  73.             yield return new WaitForSeconds(fadeWaitTime);
  74.             yield return fader.FadeIn(fadeInTime);
  75.  
  76.             Destroy(gameObject);
  77.         }
  78.  
  79.         private void UpdatePlayer(Portal otherPortal)
  80.         {
  81.             GameObject player = GameObject.FindWithTag("Player");
  82.             player.GetComponent<NavMeshAgent>().Warp(otherPortal.spawnPoint.position);
  83.             player.transform.rotation = otherPortal.spawnPoint.rotation;
  84.         }
  85.  
  86.         private Portal GetOtherPortal()
  87.         {
  88.             foreach(Portal portal in FindObjectsOfType<Portal>())
  89.             {
  90.                 if(portal == this) { continue; }
  91.                 if(portal.destination != destination) { continue; }
  92.  
  93.                 return portal;
  94.             }
  95.  
  96.             return null;
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment