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

53 lines
1.5 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 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;
public void SetLevel(int levelNumber)
2022-02-24 00:21:59 +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;
}
public void EndLevel() => PlayerPrefs.SetInt(LevelNumber.ToString(), 1);
2022-02-25 22:56:10 +03:00
[ContextMenu("Restart")]
public void Restart() => LevelManager.Instance.SwitchToLevel(LevelNumber);
2022-02-24 00:21:59 +03:00
}
}