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

48 lines
1.3 KiB
C#
Raw Normal View History

2022-02-24 00:21:59 +03:00
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;
}
[ContextMenu("Restart")]
public void Restart() => LevelManager.Instance.SwitchToLevel(LevelName);
2022-02-24 00:21:59 +03:00
}
}