CVR_TriggerElement.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. namespace ChivaXR.VR
  2. {
  3. using System.Collections.Generic;
  4. using Sirenix.OdinInspector;
  5. using System.Linq;
  6. using UnityEngine;
  7. using System;
  8. public struct CVR_TriggerElementEventArgs
  9. {
  10. public CVR_TriggerElement triggerElement;
  11. public float value;
  12. }
  13. /// <summary>
  14. /// Trigger
  15. /// </summary>
  16. /// <param name="sender">this object</param>
  17. /// <param name="e"><see cref="InteractableObjectEventArgs"/></param>
  18. public delegate void CVRTriggerElementEventHandler(object sender, CVR_TriggerElementEventArgs e);
  19. /// <summary>
  20. /// 交互触发器
  21. /// </summary>
  22. public class CVR_TriggerElement : SerializedMonoBehaviour, IVR_OperationElement
  23. {
  24. private CVR_SimulationTool simulationTool;
  25. /// <summary>
  26. /// 可被操作工具类型
  27. /// </summary>
  28. public bool isActive = false;
  29. private bool isSimulation = false;
  30. public CVR_ToolType toolType;
  31. [Header("触发点工具模拟动画")]
  32. [TypeFilter("GetFilteredTypeList")]
  33. public CVR_TriggerAniBase triggerAniBase;
  34. public IEnumerable<Type> GetFilteredTypeList()
  35. {
  36. var q = typeof(CVR_TriggerAniBase).Assembly.GetTypes()
  37. .Where(x => !x.IsAbstract)
  38. .Where(x => !x.IsGenericTypeDefinition)
  39. .Where(x => typeof(CVR_TriggerAniBase).IsAssignableFrom(x));
  40. return q;
  41. }
  42. private float value;
  43. public float Value
  44. {
  45. get { return value; }
  46. set
  47. {
  48. if (this.value != value)
  49. {
  50. this.value = value;
  51. ValueChanged?.Invoke(this, new CVR_TriggerElementEventArgs() { triggerElement = this, value = Value });
  52. }
  53. }
  54. }
  55. [HideInInspector]
  56. public CVRTriggerElementEventHandler ElementTrigger;
  57. [HideInInspector]
  58. public CVRTriggerElementEventHandler ValueChanged;
  59. public void Trigger(CVR_InteractableTriggerEventArgs e)
  60. {
  61. if (triggerAniBase == null)
  62. {
  63. OnElementTriggered();
  64. }
  65. else
  66. {
  67. simulationTool = e.triggerTool.GetComponent<CVR_SimulationTool>();
  68. if (triggerAniBase != null && simulationTool != null)
  69. {
  70. Debug.Log("EnterTriggerAni");
  71. isSimulation = true;
  72. triggerAniBase.Init(this, simulationTool);
  73. simulationTool.ExitSimulationMode += ToolCloseSimulation;
  74. }
  75. }
  76. }
  77. private void Update()
  78. {
  79. if (!isActive || !isSimulation || triggerAniBase == null) return;
  80. triggerAniBase.SimulationUpdate();
  81. }
  82. private void OnElementTriggered()
  83. {
  84. Debug.Log("Trigger" + this.name);
  85. ElementTrigger?.Invoke(this, new CVR_TriggerElementEventArgs()
  86. {
  87. triggerElement = this
  88. });
  89. }
  90. /// <summary>
  91. /// toolOpSimulation结束模拟时关闭当前ToolTarget模拟
  92. /// </summary>
  93. /// <param name="sender"></param>
  94. /// <param name="e"></param>
  95. protected virtual void ToolCloseSimulation(object sender, CVR_SimulationEventArgs e)
  96. {
  97. simulationTool.ExitSimulationMode -= ToolCloseSimulation;
  98. isSimulation = false;
  99. triggerAniBase.CloseSimulation();
  100. OnElementTriggered();
  101. }
  102. bool IVR_OperationElement.GetActive()
  103. {
  104. return isActive;
  105. }
  106. CVR_ToolType IVR_OperationElement.GetToolType()
  107. {
  108. return toolType;
  109. }
  110. }
  111. }