Global Game Jam 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.1 KiB

2 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using NaughtyAttributes;
  6. using Variables;
  7. public class LevelManager : MonoBehaviour
  8. {
  9. [SerializeField, Header("References")]
  10. Reference<bool> m_isPlayerDead;
  11. [SerializeField]
  12. Reference<bool> m_freezePlayer;
  13. [SerializeField]
  14. Reference<bool> m_isVictory;
  15. [SerializeField]
  16. Reference<float> m_timer;
  17. [SerializeField, BoxGroup("Level Settings")]
  18. private string m_levelName = "Level 1";
  19. [SerializeField, BoxGroup("Level Settings")]
  20. private float m_timeInLevel = 10;
  21. [SerializeField]
  22. private float m_resetTime = 1;
  23. [SerializeField]
  24. private string m_nextSceneToLoad = "Level 2";
  25. private IResettable[] m_allResetable;
  26. private void Start()
  27. {
  28. m_allResetable = FindObjectsOfType<MonoBehaviour>().OfType<IResettable>().ToArray();
  29. m_timer.Value = m_timeInLevel;
  30. foreach (IResettable resettable in m_allResetable)
  31. {
  32. resettable.OnLevelLoad();
  33. }
  34. }
  35. private void Update()
  36. {
  37. if (!m_freezePlayer)
  38. m_timer.Value -= Time.deltaTime;
  39. }
  40. private void OnEnable()
  41. {
  42. m_isPlayerDead.OnValueChanged += OnPlayerDeadChange;
  43. m_isVictory.OnValueChanged += OnPlayerVictoryChange;
  44. }
  45. private void OnDisable()
  46. {
  47. m_isPlayerDead.OnValueChanged -= OnPlayerDeadChange;
  48. m_isVictory.OnValueChanged -= OnPlayerVictoryChange;
  49. }
  50. private IEnumerator DeathRoutine()
  51. {
  52. m_freezePlayer.Value = true;
  53. m_timer.Value = m_timeInLevel;
  54. foreach (IResettable resettable in m_allResetable)
  55. StartCoroutine(resettable.OnResetStart(m_resetTime));
  56. yield return new WaitForSeconds(m_resetTime);
  57. foreach (IResettable resettable in m_allResetable)
  58. resettable.OnResetEnd();
  59. m_freezePlayer.Value = false;
  60. }
  61. private void OnPlayerDeadChange(bool value)
  62. {
  63. if (value)
  64. {
  65. StartCoroutine(DeathRoutine());
  66. }
  67. }
  68. private void OnPlayerVictoryChange(bool value)
  69. {
  70. }
  71. }