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.

185 lines
3.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7. using NaughtyAttributes;
  8. using Variables;
  9. public class LevelManager : MonoBehaviour
  10. {
  11. [SerializeField, Header("References")]
  12. Reference<bool> m_isPlayerDead;
  13. [SerializeField]
  14. Reference<bool> m_freezePlayer;
  15. [SerializeField]
  16. Reference<bool> m_isVictory;
  17. [SerializeField]
  18. Reference<float> m_timer;
  19. [SerializeField]
  20. Reference<float> m_light;
  21. [SerializeField]
  22. Reference<bool> m_manualDarkness;
  23. [SerializeField]
  24. private Image m_backGround;
  25. [SerializeField, BoxGroup("Level Settings")]
  26. public string levelName = "Level 1";
  27. [SerializeField, BoxGroup("Level Settings")]
  28. private float m_timeInLevel = 10;
  29. [SerializeField, BoxGroup("Level Settings")]
  30. public string levelPrompt = "Get to the Chest";
  31. [SerializeField,BoxGroup("Default Settings")]
  32. private float m_resetTime = 1;
  33. [SerializeField, BoxGroup("Default Settings")]
  34. private float m_nextSceneWaitTime = 3;
  35. [SerializeField, BoxGroup("Level Settings")]
  36. private string m_nextSceneToLoad = "Level 2";
  37. private IResettable[] m_allResetable;
  38. private void Start()
  39. {
  40. m_allResetable = FindObjectsOfType<MonoBehaviour>().OfType<IResettable>().ToArray();
  41. m_timer.Value = m_timeInLevel;
  42. m_isVictory.Value = false;
  43. foreach (IResettable resettable in m_allResetable)
  44. {
  45. resettable.OnLevelLoad();
  46. }
  47. }
  48. private void Update()
  49. {
  50. if (!m_freezePlayer)
  51. m_timer.Value -= Time.deltaTime;
  52. if (m_timer <= 0 && !m_isPlayerDead)
  53. {
  54. m_isPlayerDead.Value = true;
  55. }
  56. }
  57. private void OnEnable()
  58. {
  59. m_isPlayerDead.OnValueChanged += OnPlayerDeadChange;
  60. m_isVictory.OnValueChanged += OnPlayerVictoryChange;
  61. }
  62. private void OnDisable()
  63. {
  64. m_isPlayerDead.OnValueChanged -= OnPlayerDeadChange;
  65. m_isVictory.OnValueChanged -= OnPlayerVictoryChange;
  66. }
  67. private IEnumerator DeathRoutine()
  68. {
  69. m_freezePlayer.Value = true;
  70. m_timer.Value = m_timeInLevel;
  71. foreach (IResettable resettable in m_allResetable)
  72. StartCoroutine(resettable.OnResetStart(m_resetTime));
  73. yield return new WaitForSeconds(m_resetTime);
  74. foreach (IResettable resettable in m_allResetable)
  75. resettable.OnResetEnd();
  76. m_freezePlayer.Value = false;
  77. }
  78. private IEnumerator VictoryRoutine()
  79. {
  80. yield return new WaitForSeconds(m_nextSceneWaitTime);
  81. m_manualDarkness.Value = true;
  82. float elapsedTime = 0;
  83. float fadeTime = 1;
  84. while (elapsedTime < fadeTime)
  85. {
  86. m_light.Value = 1 - elapsedTime / fadeTime;
  87. yield return new WaitForEndOfFrame();
  88. elapsedTime += Time.deltaTime;
  89. }
  90. m_light.Value = 0;
  91. yield return new WaitForSeconds(1);
  92. elapsedTime = 0;
  93. fadeTime = 2;
  94. Color color = m_backGround.color;
  95. m_backGround.gameObject.SetActive(true);
  96. while (elapsedTime < fadeTime)
  97. {
  98. color.a = elapsedTime / fadeTime;
  99. m_backGround.color = color;
  100. Debug.Log(color.a);
  101. yield return new WaitForEndOfFrame();
  102. elapsedTime += Time.deltaTime;
  103. }
  104. yield return new WaitForSeconds(1);
  105. m_manualDarkness.Value = false;
  106. SceneManager.LoadScene(m_nextSceneToLoad);
  107. }
  108. private void OnPlayerDeadChange(bool value)
  109. {
  110. if (value)
  111. {
  112. StartCoroutine(DeathRoutine());
  113. }
  114. }
  115. private void OnPlayerVictoryChange(bool value)
  116. {
  117. if (value)
  118. {
  119. StartCoroutine(VictoryRoutine());
  120. }
  121. }
  122. }