2022-02-24 00:21:59 +03:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Level
|
|
|
|
{
|
|
|
|
public class Level : MonoBehaviour
|
|
|
|
{
|
|
|
|
public const string ResourcesDirectory = "Levels/";
|
2022-02-26 12:56:49 +03:00
|
|
|
public int LevelNumber { get; private set; } = 0;
|
2022-02-24 00:21:59 +03:00
|
|
|
|
|
|
|
public Transform StartingPoint { get; private set; } = null;
|
|
|
|
|
|
|
|
private GameObject prefab = null;
|
|
|
|
private GameObject instance = null;
|
|
|
|
private bool needsRestart = true;
|
|
|
|
|
2022-02-26 15:09:41 +03:00
|
|
|
public Transform FixedCameraPoint { get; private set; } = null;
|
|
|
|
|
2022-02-26 12:56:49 +03:00
|
|
|
public void SetLevel(int levelNumber)
|
2022-02-24 00:21:59 +03:00
|
|
|
{
|
2022-02-26 12:56:49 +03:00
|
|
|
LevelNumber = levelNumber;
|
|
|
|
prefab = Resources.Load<GameObject>($"{ ResourcesDirectory }{ levelNumber }");
|
2022-02-24 00:21:59 +03:00
|
|
|
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");
|
2022-02-26 15:09:41 +03:00
|
|
|
FixedCameraPoint = instance.transform.Find("Fixed Camera Point");
|
|
|
|
|
2022-02-24 00:21:59 +03:00
|
|
|
gameObject.SetActive(false);
|
|
|
|
needsRestart = false;
|
|
|
|
}
|
2022-02-25 13:49:11 +03:00
|
|
|
|
2022-02-26 12:56:49 +03:00
|
|
|
public void EndLevel() => PlayerPrefs.SetInt(LevelNumber.ToString(), 1);
|
2022-02-25 22:56:10 +03:00
|
|
|
|
2022-02-25 13:49:11 +03:00
|
|
|
[ContextMenu("Restart")]
|
2022-02-26 12:56:49 +03:00
|
|
|
public void Restart() => LevelManager.Instance.SwitchToLevel(LevelNumber);
|
2022-02-24 00:21:59 +03:00
|
|
|
}
|
|
|
|
}
|