Level Selection related stuff added
This commit is contained in:
8
Assets/Scripts/Level.meta
Normal file
8
Assets/Scripts/Level.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9152a48d03f7bde468d1a3e428225713
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
44
Assets/Scripts/Level/Level.cs
Normal file
44
Assets/Scripts/Level/Level.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Level
|
||||
{
|
||||
public class Level : MonoBehaviour
|
||||
{
|
||||
public const string ResourcesDirectory = "Levels/";
|
||||
public string LevelName { get; private set; } = "";
|
||||
|
||||
public Transform StartingPoint { get; private set; } = null;
|
||||
|
||||
private GameObject prefab = null;
|
||||
private GameObject instance = null;
|
||||
private bool needsRestart = true;
|
||||
|
||||
public void SetLevel(string levelName)
|
||||
{
|
||||
LevelName = levelName;
|
||||
|
||||
prefab = Resources.Load<GameObject>($"{ ResourcesDirectory }{ levelName }");
|
||||
Disable();
|
||||
}
|
||||
|
||||
public void Enable()
|
||||
{
|
||||
gameObject.SetActive(true);
|
||||
needsRestart = true;
|
||||
}
|
||||
|
||||
public void Disable()
|
||||
{
|
||||
if (!needsRestart)
|
||||
return;
|
||||
|
||||
if (instance != null)
|
||||
Destroy(instance);
|
||||
|
||||
instance = Instantiate(prefab, transform.position, Quaternion.identity, transform);
|
||||
StartingPoint = instance.transform.Find("Starting Point");
|
||||
gameObject.SetActive(false);
|
||||
needsRestart = false;
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Level/Level.cs.meta
Normal file
11
Assets/Scripts/Level/Level.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 69df2de2c96af5a4484964cc90840292
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
87
Assets/Scripts/Level/LevelManager.cs
Normal file
87
Assets/Scripts/Level/LevelManager.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
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<LevelManager>();
|
||||
_instance.Initialize();
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<string, Level> _levels = null;
|
||||
public Dictionary<string, Level> 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);
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
GameObject[] levelPrefabs = Resources.LoadAll<GameObject>("Levels/");
|
||||
Transform levelContainer = new GameObject("Levels").transform;
|
||||
_levels = new Dictionary<string, Level>(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>();
|
||||
level.SetLevel(levelPrefab.name);
|
||||
_levels.Add(levelPrefab.gameObject.name, level);
|
||||
}
|
||||
}
|
||||
|
||||
public void SwitchToLevel(string levelName)
|
||||
{
|
||||
DisableAllLevels();
|
||||
|
||||
currentLevel = Levels[levelName];
|
||||
currentLevel.Enable();
|
||||
|
||||
// TODO Move Player To currentLevel.StartingPoint
|
||||
|
||||
UIManager.Instance.CloseAllCanvases();
|
||||
}
|
||||
|
||||
private void DisableAllLevels()
|
||||
{
|
||||
foreach (Level level in Levels.Values)
|
||||
level.Disable();
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Level/LevelManager.cs.meta
Normal file
11
Assets/Scripts/Level/LevelManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10b4751f48f29094981414367190e392
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
16
Assets/Scripts/UI/LevelButton.cs
Normal file
16
Assets/Scripts/UI/LevelButton.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Level;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class LevelButton : MonoBehaviour
|
||||
{
|
||||
private TMP_Text text = null;
|
||||
|
||||
private void Awake() => text = GetComponentInChildren<TMP_Text>();
|
||||
|
||||
public void SetLevel(string levelName) => text.text = levelName;
|
||||
public void StartLevel() => LevelManager.Instance.SwitchToLevel(text.text);
|
||||
}
|
||||
}
|
11
Assets/Scripts/UI/LevelButton.cs.meta
Normal file
11
Assets/Scripts/UI/LevelButton.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 159a88a420256634c8758e2b1501c29a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
55
Assets/Scripts/UI/LevelSelectionMenu.cs
Normal file
55
Assets/Scripts/UI/LevelSelectionMenu.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using Level;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class LevelSelectionMenu : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Vector2Int maxGridSize = Vector2Int.one;
|
||||
private GameObject levelButtonPrefab = null;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
levelButtonPrefab = Resources.Load<GameObject>("UI/Level Button Variant");
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
LevelButton instance = null;
|
||||
|
||||
int rowIndex = 0;
|
||||
int columnIndex = 0;
|
||||
int i = 0;
|
||||
int count = LevelManager.Instance.Levels.Values.Count;
|
||||
|
||||
RectTransform rectTransform = levelButtonPrefab.transform.GetComponent<RectTransform>();
|
||||
float rowOffset = (int)(count / maxGridSize.x) * rectTransform.rect.height * 0.75f;
|
||||
float columnOffset = -maxGridSize.x * rectTransform.rect.width * 0.75f;
|
||||
if (maxGridSize.x % 2 == 1)
|
||||
columnOffset += rectTransform.rect.width * 0.75f;
|
||||
|
||||
RectTransform instanceRectTransform = null;
|
||||
Vector3 instancePosition = Vector3.zero;
|
||||
foreach (var level in LevelManager.Instance.Levels.Values)
|
||||
{
|
||||
if (i > maxGridSize.x * maxGridSize.y)
|
||||
break;
|
||||
|
||||
|
||||
rowIndex = i / maxGridSize.x;
|
||||
columnIndex = i % maxGridSize.x;
|
||||
|
||||
instance = Instantiate(levelButtonPrefab, transform.position, Quaternion.identity, transform).GetComponent<LevelButton>();
|
||||
instance.SetLevel(level.name);
|
||||
instanceRectTransform = instance.transform.GetComponent<RectTransform>();
|
||||
|
||||
instancePosition.y = rowOffset - rowIndex * instanceRectTransform.rect.height * 1.5f;
|
||||
instancePosition.x = columnOffset + columnIndex * instanceRectTransform.rect.width * 1.5f;
|
||||
instanceRectTransform.localPosition = instancePosition;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/UI/LevelSelectionMenu.cs.meta
Normal file
11
Assets/Scripts/UI/LevelSelectionMenu.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c861d2bd3b90214da0c159643ba1528
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -29,6 +29,8 @@ namespace UI
|
||||
|
||||
public void CloseAllCanvases()
|
||||
{
|
||||
Initialize();
|
||||
|
||||
foreach (Canvas canvas in canvases.Values)
|
||||
canvas.gameObject.SetActive(false);
|
||||
}
|
||||
|
Reference in New Issue
Block a user