DetectorLogicGate.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. namespace Leap.Unity
  6. {
  7. public class DetectorLogicGate : Detector
  8. {
  9. public void AddDetector(Detector detector)
  10. {
  11. if (!this.Detectors.Contains(detector))
  12. {
  13. this.Detectors.Add(detector);
  14. this.activateDetector(detector);
  15. }
  16. }
  17. public void RemoveDetector(Detector detector)
  18. {
  19. detector.OnActivate.RemoveListener(new UnityAction(this.CheckDetectors));
  20. detector.OnDeactivate.RemoveListener(new UnityAction(this.CheckDetectors));
  21. this.Detectors.Remove(detector);
  22. }
  23. public void AddAllSiblingDetectors()
  24. {
  25. Detector[] components = base.GetComponents<Detector>();
  26. for (int i = 0; i < components.Length; i++)
  27. {
  28. if (components[i] != this && components[i].enabled)
  29. {
  30. this.AddDetector(components[i]);
  31. }
  32. }
  33. }
  34. private void Awake()
  35. {
  36. for (int i = 0; i < this.Detectors.Count; i++)
  37. {
  38. this.activateDetector(this.Detectors[i]);
  39. }
  40. if (this.AddAllSiblingDetectorsOnAwake)
  41. {
  42. this.AddAllSiblingDetectors();
  43. }
  44. }
  45. private void activateDetector(Detector detector)
  46. {
  47. detector.OnActivate.RemoveListener(new UnityAction(this.CheckDetectors));
  48. detector.OnDeactivate.RemoveListener(new UnityAction(this.CheckDetectors));
  49. detector.OnActivate.AddListener(new UnityAction(this.CheckDetectors));
  50. detector.OnDeactivate.AddListener(new UnityAction(this.CheckDetectors));
  51. }
  52. private void OnEnable()
  53. {
  54. this.CheckDetectors();
  55. }
  56. private void OnDisable()
  57. {
  58. this.Deactivate();
  59. }
  60. protected void CheckDetectors()
  61. {
  62. if (this.Detectors.Count < 1)
  63. {
  64. return;
  65. }
  66. bool flag = this.Detectors[0].IsActive;
  67. for (int i = 1; i < this.Detectors.Count; i++)
  68. {
  69. if (this.GateType == LogicType.AndGate)
  70. {
  71. flag = (flag && this.Detectors[i].IsActive);
  72. }
  73. else
  74. {
  75. flag = (flag || this.Detectors[i].IsActive);
  76. }
  77. }
  78. if (this.Negate)
  79. {
  80. flag = !flag;
  81. }
  82. if (flag)
  83. {
  84. this.Activate();
  85. }
  86. else
  87. {
  88. this.Deactivate();
  89. }
  90. }
  91. [SerializeField]
  92. [Tooltip("The list of observed detectors.")]
  93. private List<Detector> Detectors;
  94. [Tooltip("Add all detectors on this object automatically.")]
  95. public bool AddAllSiblingDetectorsOnAwake = true;
  96. [Tooltip("The type of logic used to combine detector state.")]
  97. public LogicType GateType;
  98. [Tooltip("Whether to negate the gate output.")]
  99. public bool Negate;
  100. }
  101. }