using System; using System.Collections.Generic; using UI; using UnityEngine; namespace Level { public class LevelManager : MonoBehaviour { private static LevelManager _instance = null; public static LevelManager Instance { get { if (_instance == null) { GameObject gameObject = new GameObject("Level Manager"); _instance = gameObject.AddComponent(); _instance.Initialize(); } return _instance; } } private Dictionary _levels = null; public Dictionary Levels { get { if (_levels == null) Initialize(); return _levels; } } private Level currentLevel = null; private void Awake() { if (_instance == null) _instance = this; if (_instance != this) Destroy(this); } public GameObject Player { get; private set; } = null; private void Initialize() { GameObject[] levelPrefabs = Resources.LoadAll("Levels/"); Transform levelContainer = new GameObject("Levels").transform; _levels = new Dictionary(levelPrefabs.Length); System.Array.Sort(levelPrefabs, (x, y) => Int32.Parse(x.name).CompareTo(Int32.Parse(y.name))); GameObject levelInstance = null; Level level = null; foreach (GameObject levelPrefab in levelPrefabs) { levelInstance = new GameObject(levelPrefab.gameObject.name); levelInstance.transform.SetParent(levelContainer); level = levelInstance.AddComponent(); level.SetLevel(levelPrefab.name); _levels.Add(levelPrefab.gameObject.name, level); } Player = GameObject.FindWithTag("Player"); if (Player == null) Player = (GameObject)Instantiate(Resources.Load("Playable/Player"), transform.position, Quaternion.identity); Player.SetActive(false); } public void SwitchToLevel(string levelName) { DisableAllLevels(); currentLevel = Levels[levelName]; currentLevel.Enable(); Player.SetActive(true); Player.transform.position = currentLevel.StartingPoint.position; Player.GetComponent().velocity = Vector2.zero; UIManager.Instance.CloseAllCanvases(); } private void DisableAllLevels() { Player.SetActive(false); foreach (Level level in Levels.Values) level.Disable(); } } }