123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- namespace ChivaXR.VR
- {
- using System.Collections.Generic;
- using Sirenix.OdinInspector;
- using System.Linq;
- using UnityEngine;
- using System;
- public struct CVR_TriggerElementEventArgs
- {
- public CVR_TriggerElement triggerElement;
- public float value;
- }
- /// <summary>
- /// Trigger
- /// </summary>
- /// <param name="sender">this object</param>
- /// <param name="e"><see cref="InteractableObjectEventArgs"/></param>
- public delegate void CVRTriggerElementEventHandler(object sender, CVR_TriggerElementEventArgs e);
- /// <summary>
- /// 交互触发器
- /// </summary>
- public class CVR_TriggerElement : SerializedMonoBehaviour, IVR_OperationElement
- {
- private CVR_SimulationTool simulationTool;
- /// <summary>
- /// 可被操作工具类型
- /// </summary>
- public bool isActive = false;
- private bool isSimulation = false;
- public CVR_ToolType toolType;
- [Header("触发点工具模拟动画")]
- [TypeFilter("GetFilteredTypeList")]
- public CVR_TriggerAniBase triggerAniBase;
- public IEnumerable<Type> GetFilteredTypeList()
- {
- var q = typeof(CVR_TriggerAniBase).Assembly.GetTypes()
- .Where(x => !x.IsAbstract)
- .Where(x => !x.IsGenericTypeDefinition)
- .Where(x => typeof(CVR_TriggerAniBase).IsAssignableFrom(x));
- return q;
- }
- private float value;
- public float Value
- {
- get { return value; }
- set
- {
- if (this.value != value)
- {
- this.value = value;
- ValueChanged?.Invoke(this, new CVR_TriggerElementEventArgs() { triggerElement = this, value = Value });
- }
- }
- }
- [HideInInspector]
- public CVRTriggerElementEventHandler ElementTrigger;
- [HideInInspector]
- public CVRTriggerElementEventHandler ValueChanged;
- public void Trigger(CVR_InteractableTriggerEventArgs e)
- {
- if (triggerAniBase == null)
- {
- OnElementTriggered();
- }
- else
- {
- simulationTool = e.triggerTool.GetComponent<CVR_SimulationTool>();
- if (triggerAniBase != null && simulationTool != null)
- {
- Debug.Log("EnterTriggerAni");
- isSimulation = true;
- triggerAniBase.Init(this, simulationTool);
- simulationTool.ExitSimulationMode += ToolCloseSimulation;
- }
- }
- }
- private void Update()
- {
- if (!isActive || !isSimulation || triggerAniBase == null) return;
- triggerAniBase.SimulationUpdate();
- }
- private void OnElementTriggered()
- {
- Debug.Log("Trigger" + this.name);
- ElementTrigger?.Invoke(this, new CVR_TriggerElementEventArgs()
- {
- triggerElement = this
- });
- }
- /// <summary>
- /// toolOpSimulation结束模拟时关闭当前ToolTarget模拟
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected virtual void ToolCloseSimulation(object sender, CVR_SimulationEventArgs e)
- {
- simulationTool.ExitSimulationMode -= ToolCloseSimulation;
- isSimulation = false;
- triggerAniBase.CloseSimulation();
- OnElementTriggered();
- }
- bool IVR_OperationElement.GetActive()
- {
- return isActive;
- }
- CVR_ToolType IVR_OperationElement.GetToolType()
- {
- return toolType;
- }
- }
- }
|