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