Files
Scampz/Assets/Scripts/GameManager/GameManager.cs
2022-08-21 01:59:54 -07:00

60 lines
1.6 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Scampz.GameJam
{
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(this);
return;
}
Instance = this;
}
public void LoadScene(string sceneName, Transform spawnPoint, LoadSceneMode loadSceneMode)
{
StartCoroutine(LoadSceneAsync(sceneName, spawnPoint, loadSceneMode));
}
public void LoadFromWorldMap(LoadSceneMode loadSceneMode)
{
SceneManager.LoadScene("WorldMap", loadSceneMode);
}
private IEnumerator LoadSceneAsync(string sceneName, Transform spawnPoint, LoadSceneMode loadSceneMode)
{
yield return null;
var player = GameObject.FindGameObjectWithTag("Player");
var levelChanger = GetComponentInChildren<LevelChanger>();
levelChanger.FadeAnimation();
yield return null;
var loadSceneOperation = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
yield return new WaitForSeconds(3f);
loadSceneOperation.allowSceneActivation = false;
while (!loadSceneOperation.isDone)
{
yield return null;
if (loadSceneOperation.progress >= 0.9f)
break;
}
loadSceneOperation.allowSceneActivation = true;
yield return loadSceneOperation;
var cc = player.GetComponent<CharacterController>();
cc.enabled = false;
player.transform.position = spawnPoint.position;
player.transform.rotation = spawnPoint.rotation;
yield return null;
}
}
}