Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using Audio;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- using InteractionSystem;
- using Movement;
- public class LevelLoader : Interactable
- {
- [Header("Level to load by name")]
- public string levelToLoad;
- [Header("Time to fade in and out")]
- [SerializeField] private float fadeOutTime;
- [SerializeField] private float fadeInTime;
- [SerializeField] private LoaderType loaderType;
- [Header("")]
- [SerializeField] private BoxCollider2D collider;
- [SerializeField] private bool customLocationWhenLoaded;
- [SerializeField] private Vector2 loadedInPos;
- public bool active;
- [Header("Optional")]
- [SerializeField] private PlayOneShot _playOneShot;
- private FreeMovement player;
- private Canvas _canvas;
- private Image _fadeImg;
- private bool _hasFadedOut;
- private bool _beenUsed;
- public LoaderType LoaderType => loaderType;
- private GameObject newLevelPlayerRef;
- public override void Interact(PlayerInteract player)
- {
- if (loaderType != LoaderType.Interact) return;
- if(active && _beenUsed == false)
- {
- LoadLevelInit();
- player.SuccessfulInteraction();
- SuccessfulInteraction();
- }
- else
- {
- player.FailedInteraction();
- FailedInteraction();
- }
- }
- public bool IsInteractable()
- {
- if (loaderType != LoaderType.Interact) return false;
- return (active && !_beenUsed);
- }
- private void Awake()
- {
- player = FindObjectOfType<FreeMovement>();
- }
- private void OnEnable()
- {
- player.OnMove += AttemptLoadLevel;
- }
- private void OnDisable()
- {
- player.OnMove -= AttemptLoadLevel;
- }
- private void AttemptLoadLevel()
- {
- if (!active) return;
- if (loaderType != LoaderType.Proximity) return;
- Rect rect = new Rect(this.transform.position.x + collider.offset.x - (collider.size.x / 2), this.transform.position.y + collider.offset.y - (collider.size.y / 2), collider.size.x, collider.size.y);
- if (rect.Contains(player.transform.position))
- {
- LoadLevelInit();
- }
- //if (Vector3.Distance(player.DiscretePosition, transform.position) <= 1f && _beenUsed == false)
- //{
- // LoadLevelInit();
- //}
- }
- private void LoadLevelInit()
- {
- //Disable further interactions
- _beenUsed = true;
- //disable player movement
- GameObject thisLevelPlayerRef = GameObject.Find("Protagonist");
- thisLevelPlayerRef.GetComponent<FreeMovement>().enabled = false;
- //reset values
- _hasFadedOut = false;
- //Instantiate objects from path
- var fadeEffect = Resources.Load("FadeEffect");
- GameObject fadeInstance = Instantiate(fadeEffect) as GameObject;
- this.transform.SetParent(null);
- DontDestroyOnLoad(fadeInstance);
- DontDestroyOnLoad(this.gameObject);
- _canvas = fadeInstance.GetComponent<Canvas>();
- _fadeImg = _canvas.GetComponentInChildren<Image>();
- //start operations
- StartCoroutine(LoadLevelAsync());
- StartCoroutine(Fade(fadeOutTime, 0, 1));
- if (_playOneShot) _playOneShot.Play();
- }
- //coroutine to load level
- IEnumerator LoadLevelAsync()
- {
- AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(levelToLoad);
- asyncLoad.allowSceneActivation = false;
- // Wait until the asynchronous scene fully loads and the fade is done
- while (asyncLoad.progress < 0.9f || !_hasFadedOut)
- {
- yield return null;
- }
- //loads the new scene and fades back in
- asyncLoad.allowSceneActivation = true;
- StartCoroutine(Fade(fadeInTime, 1,0));
- active = false;
- yield return null;
- }
- //coroutine to fade img
- IEnumerator Fade(float seconds, float startVal, float finalVal)
- {
- float elapsedTime = 0;
- float alphaVal = startVal;
- //lerp color from startVal to finalVal
- while (elapsedTime < seconds)
- {
- alphaVal = Mathf.Lerp(startVal, finalVal, (elapsedTime / seconds));
- elapsedTime += Time.deltaTime;
- if(finalVal == 0f && customLocationWhenLoaded)
- {
- //search for player until level is completely loaded then set their pos. Stop once the level is fully in
- newLevelPlayerRef = GameObject.Find("Protagonist");
- if(newLevelPlayerRef != null)
- {
- newLevelPlayerRef.transform.position = new Vector3(loadedInPos.x, loadedInPos.y, newLevelPlayerRef.transform.position.z);
- }
- if(SceneManager.GetActiveScene().name == levelToLoad)
- {
- customLocationWhenLoaded = false;
- }
- }
- //Updates alpha value of the image
- _fadeImg.color = new Color(_fadeImg.color.r, _fadeImg.color.g, _fadeImg.color.b, alphaVal);
- yield return null;
- }
- if(finalVal == 1)
- {
- //tells level loader that fade out has happened
- _hasFadedOut = true;
- }
- else
- {
- //if fade in is done terminate entire process
- Destroy(_canvas.gameObject);
- Destroy(this.gameObject);
- }
- yield return null;
- }
- private void OnDrawGizmos()
- {
- if(active)
- {
- DrawRect(new Rect(this.transform.position.x + collider.offset.x - (collider.size.x / 2), this.transform.position.y + collider.offset.y - (collider.size.y / 2), collider.size.x, collider.size.y));
- Gizmos.DrawIcon(transform.position, "LVLgizmoBLU.png", true);
- }
- else
- {
- Gizmos.DrawIcon(transform.position, "LVLgizmoRED.png", true);
- }
- }
- void DrawRect(Rect rect)
- {
- Gizmos.DrawWireCube(new Vector3(rect.center.x, rect.center.y, 0.01f), new Vector3(rect.size.x, rect.size.y, 0.01f));
- }
- }
- public enum LoaderType
- {
- Interact,
- Proximity
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement