Unlockable Levels and Added empty 7 levels
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
@@ -7,10 +7,15 @@ namespace UI
|
||||
public class LevelButton : MonoBehaviour
|
||||
{
|
||||
private TMP_Text text = null;
|
||||
public int LevelNumber { get; private set; } = 0;
|
||||
|
||||
private void Awake() => text = GetComponentInChildren<TMP_Text>();
|
||||
|
||||
public void SetLevel(string levelName) => text.text = levelName;
|
||||
public void StartLevel() => LevelManager.Instance.SwitchToLevel(text.text);
|
||||
public void SetLevel(string levelName)
|
||||
{
|
||||
text.text = levelName;
|
||||
LevelNumber = System.Int32.Parse(levelName);
|
||||
}
|
||||
public void StartLevel() => LevelManager.Instance.SwitchToLevel(LevelNumber);
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Level;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
@@ -8,13 +10,15 @@ namespace UI
|
||||
{
|
||||
[SerializeField] private Vector2Int maxGridSize = Vector2Int.one;
|
||||
private GameObject levelButtonPrefab = null;
|
||||
private List<GameObject> instances = null;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
levelButtonPrefab = Resources.Load<GameObject>("UI/Level Button Variant");
|
||||
instances = new List<GameObject>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
private void OnEnable()
|
||||
{
|
||||
LevelButton instance = null;
|
||||
|
||||
@@ -48,8 +52,17 @@ namespace UI
|
||||
instancePosition.x = columnOffset + columnIndex * instanceRectTransform.rect.width * 1.5f;
|
||||
instanceRectTransform.localPosition = instancePosition;
|
||||
|
||||
instance.GetComponent<Button>().interactable = LevelManager.Instance.IsLevelUnlocked(instance.LevelNumber);
|
||||
|
||||
instances.Add(instance.gameObject);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
foreach (GameObject gameObject in instances)
|
||||
Destroy(gameObject.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user