Unlockable Levels and Added empty 7 levels

This commit is contained in:
2022-02-26 12:56:49 +03:00
parent b7ab8e2a27
commit 05f2cdb62b
21 changed files with 1325 additions and 32 deletions

View File

@@ -5,7 +5,7 @@ namespace Level
public class Level : MonoBehaviour
{
public const string ResourcesDirectory = "Levels/";
public string LevelName { get; private set; } = "";
public int LevelNumber { get; private set; } = 0;
public Transform StartingPoint { get; private set; } = null;
@@ -13,11 +13,10 @@ namespace Level
private GameObject instance = null;
private bool needsRestart = true;
public void SetLevel(string levelName)
public void SetLevel(int levelNumber)
{
LevelName = levelName;
prefab = Resources.Load<GameObject>($"{ ResourcesDirectory }{ levelName }");
LevelNumber = levelNumber;
prefab = Resources.Load<GameObject>($"{ ResourcesDirectory }{ levelNumber }");
Disable();
}
@@ -41,9 +40,9 @@ namespace Level
needsRestart = false;
}
public void EndLevel() => PlayerPrefs.SetInt(LevelName, 1);
public void EndLevel() => PlayerPrefs.SetInt(LevelNumber.ToString(), 1);
[ContextMenu("Restart")]
public void Restart() => LevelManager.Instance.SwitchToLevel(LevelName);
public void Restart() => LevelManager.Instance.SwitchToLevel(LevelNumber);
}
}

View File

@@ -7,6 +7,12 @@ namespace Level
private Level level = null;
private void Awake() => level = GetComponentInParent<Level>();
public void EndLevel() => level.EndLevel();
public void EndLevel()
{
level.EndLevel();
Debug.Log(level.LevelNumber);
LevelManager.Instance.SwitchToLevel(level.LevelNumber + 1);
}
}
}

View File

@@ -23,8 +23,8 @@ namespace Level
}
}
private Dictionary<string, Level> _levels = null;
public Dictionary<string, Level> Levels
private Dictionary<int, Level> _levels = null;
public Dictionary<int, Level> Levels
{
get
{
@@ -52,7 +52,7 @@ namespace Level
{
GameObject[] levelPrefabs = Resources.LoadAll<GameObject>("Levels/");
Transform levelContainer = new GameObject("Levels").transform;
_levels = new Dictionary<string, Level>(levelPrefabs.Length);
_levels = new Dictionary<int, Level>(levelPrefabs.Length);
System.Array.Sort(levelPrefabs, (x, y) => Int32.Parse(x.name).CompareTo(Int32.Parse(y.name)));
@@ -63,8 +63,8 @@ namespace Level
levelInstance = new GameObject(levelPrefab.gameObject.name);
levelInstance.transform.SetParent(levelContainer);
level = levelInstance.AddComponent<Level>();
level.SetLevel(levelPrefab.name);
_levels.Add(levelPrefab.gameObject.name, level);
level.SetLevel(System.Int32.Parse(levelPrefab.name));
_levels.Add(System.Int32.Parse(levelPrefab.gameObject.name), level);
}
Player = GameObject.FindWithTag("Player");
@@ -73,11 +73,11 @@ namespace Level
Player.SetActive(false);
}
public void SwitchToLevel(string levelName)
public void SwitchToLevel(int levelNumber)
{
DisableAllLevels();
currentLevel = Levels[levelName];
currentLevel = Levels[levelNumber];
currentLevel.Enable();
Player.SetActive(true);
@@ -93,5 +93,18 @@ namespace Level
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();
}
}