BGJ-2022.1/Assets/Scripts/Level/LevelManager.cs

124 lines
3.6 KiB
C#
Raw Normal View History

2022-02-24 00:21:59 +03:00
using System;
using System.Collections.Generic;
using UI;
using UnityEngine;
2022-02-27 11:03:05 +03:00
using UnityEngine.Events;
2022-02-24 00:21:59 +03:00
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<LevelManager>();
_instance.Initialize();
}
return _instance;
}
}
private Dictionary<int, Level> _levels = null;
public Dictionary<int, Level> Levels
2022-02-24 00:21:59 +03:00
{
get
{
if (_levels == null)
Initialize();
return _levels;
}
}
private Level currentLevel = null;
2022-02-26 15:09:41 +03:00
public Level CurrentLevel => currentLevel;
2022-02-24 00:21:59 +03:00
2022-02-27 11:03:05 +03:00
public UnityEvent OnLevelChanged = new UnityEvent();
2022-02-24 00:21:59 +03:00
private void Awake()
{
if (_instance == null)
_instance = this;
if (_instance != this)
Destroy(this);
}
public GameObject Player { get; private set; } = null;
2022-02-24 00:21:59 +03:00
private void Initialize()
{
GameObject[] levelPrefabs = Resources.LoadAll<GameObject>("Levels/");
Transform levelContainer = new GameObject("Levels").transform;
_levels = new Dictionary<int, Level>(levelPrefabs.Length);
2022-02-24 00:21:59 +03:00
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>();
level.SetLevel(System.Int32.Parse(levelPrefab.name));
_levels.Add(System.Int32.Parse(levelPrefab.gameObject.name), level);
2022-02-24 00:21:59 +03:00
}
Player = GameObject.FindWithTag("Player");
if (Player == null)
Player = (GameObject)Instantiate(Resources.Load("Playable/Player"), transform.position, Quaternion.identity);
Player.SetActive(false);
2022-02-24 00:21:59 +03:00
}
public void SwitchToLevel(int levelNumber)
2022-02-24 00:21:59 +03:00
{
DisableAllLevels();
2022-02-26 12:59:30 +03:00
if (!Levels.ContainsKey(levelNumber))
{
UIManager.Instance.SwitchToCanvas("Level Selection Menu");
return;
}
currentLevel = Levels[levelNumber];
2022-02-24 00:21:59 +03:00
currentLevel.Enable();
Player.SetActive(true);
Player.transform.position = currentLevel.StartingPoint.position;
Player.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
2022-02-24 00:21:59 +03:00
UIManager.Instance.CloseAllCanvases();
2022-02-27 11:03:05 +03:00
OnLevelChanged?.Invoke();
2022-02-24 00:21:59 +03:00
}
public void DisableAllLevels()
2022-02-24 00:21:59 +03:00
{
Player.SetActive(false);
2022-02-26 20:10:51 +03:00
currentLevel = null;
2022-02-24 00:21:59 +03:00
foreach (Level level in Levels.Values)
level.Disable();
}
public bool IsLevelUnlocked(int levelNumber)
{
levelNumber -= 1;
if (levelNumber == 0)
return true;
return PlayerPrefs.GetInt(levelNumber.ToString(), 0) != 0;
}
[ContextMenu("Lock All Levels")]
public void LockAllLevels() => PlayerPrefs.DeleteAll();
2022-02-24 00:21:59 +03:00
}
}