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.

83 lines
1.6 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using Variables;
  6. public class MonsterController : MonoBehaviour, IResettable
  7. {
  8. [SerializeField, Header("References")]
  9. Reference<float> m_light;
  10. [SerializeField]
  11. private GameObject[] m_possibleFrames;
  12. [SerializeField]
  13. private Transform m_player;
  14. [SerializeField]
  15. public GameObject m_defaultObject;
  16. private void OnEnable()
  17. {
  18. m_light.OnValueChanged += OnLightChange;
  19. }
  20. private void OnDisable()
  21. {
  22. m_light.OnValueChanged -= OnLightChange;
  23. }
  24. private void Start()
  25. {
  26. foreach (Transform child in transform)
  27. {
  28. if (child.gameObject.activeInHierarchy)
  29. m_defaultObject = child.gameObject;
  30. }
  31. }
  32. private void ChangeRandomMonster()
  33. {
  34. GameObject[] inactiveFrames = m_possibleFrames.Where(p => !p.activeInHierarchy).ToArray();
  35. GameObject nextFrame = inactiveFrames[Random.Range(0, inactiveFrames.Length)];
  36. foreach(Transform child in transform)
  37. {
  38. child.gameObject.SetActive(child.gameObject == nextFrame);
  39. }
  40. }
  41. private void OnLightChange(float value)
  42. {
  43. if (value <= 0)
  44. {
  45. ChangeRandomMonster();
  46. }
  47. }
  48. public void OnLevelLoad()
  49. {
  50. }
  51. public IEnumerator OnResetStart(float time)
  52. {
  53. yield break;
  54. }
  55. public void OnResetEnd()
  56. {
  57. foreach (Transform child in transform)
  58. {
  59. child.gameObject.SetActive(child.gameObject == m_defaultObject);
  60. }
  61. }
  62. }