123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace ChivaXR.Tool
- {
- /// <summary>
- /// 锯断效果
- /// </summary>
- public class CVR_SawingEffect : Singleton<CVR_SawingEffect>
- {
- /// <summary>
- /// 锯断效果依附的旋转点
- /// </summary>
- public Transform m_RotationPoint;
- /// <summary>
- /// 带有刚体的目标
- /// </summary>
- public Rigidbody m_TargetRigidbody;
- /// <summary>
- /// 模型朝向
- /// </summary>
- private ModelTowards m_ModelTowards;
- /// <summary>
- /// 设置初始状态
- /// </summary>
- /// <param name="_point"></param>
- /// <param name="_rigidbody"></param>
- /// <param name="_state"></param>
- public void SetStartStateData(Transform _point, Rigidbody _rigidbody, ModelTowards _state)
- {
- this.m_RotationPoint = _point;
- this.m_TargetRigidbody = _rigidbody;
- this.m_ModelTowards = _state;
- CVR_SawingSystem.SetSawingOpState(this, 0, 0);
- }
- /// <summary>
- /// 每帧执行
- /// </summary>
- public void SimulationUpdate(float _currentMoveDis, float _value, Action _action)
- {
- CVR_SawingSystem.SawingOpSimulation(this, _currentMoveDis, _value, m_ModelTowards, _action);
- }
- }
- /// <summary>
- /// 锯切系统
- /// </summary>
- internal class CVR_SawingSystem
- {
- /// <summary>
- /// 锯切操作控制器
- /// </summary>
- /// <param name="_data"></param>
- /// <param name="_currentMoveDis"></param>
- /// <param name="_value"></param>
- /// <param name="_action"></param>
- public static void SawingOpSimulation(CVR_SawingEffect _data, float _currentMoveDis, float _value, ModelTowards _modelTowards, Action _action)
- {
- if (_value < 0.0f)
- {
- return;
- }
- if (_value < 0.6f)
- {
- _data.m_RotationPoint.localEulerAngles = Vector3.Lerp(Vector3.zero, new Vector3(0, 0, SetDistance(_modelTowards, 5)), (_currentMoveDis / 0.6f) * _value);
- }
- else if (_value < 0.8f)
- {
- _data.m_RotationPoint.localEulerAngles = Vector3.Lerp(new Vector3(0, 0, SetDistance(_modelTowards, 5)), new Vector3(0, 0, SetDistance(_modelTowards, 10)), (_currentMoveDis / 0.2f) * _value - 3);
- }
- else if (_value < 1f)
- {
- _data.m_RotationPoint.localEulerAngles = Vector3.Lerp(new Vector3(0, 0, SetDistance(_modelTowards, 10)), new Vector3(0, 0, SetDistance(_modelTowards, 75)), (_currentMoveDis / 0.2f) * _value - 4);
- }
- else
- {
- if (_data.m_TargetRigidbody != null)
- {
- _data.m_TargetRigidbody.useGravity = true;
- _data.m_TargetRigidbody.isKinematic = false;
- }
- if (_data.m_TargetRigidbody.GetComponent<Collider>() != null)
- {
- _data.m_TargetRigidbody.GetComponent<Collider>().isTrigger = false;
- }
- _action?.Invoke();
- }
- }
- /// <summary>
- /// 锯断电缆状态变化
- /// </summary>
- /// <param name="_data"></param>
- /// <param name="_value"></param>
- public static void SetSawingOpState(CVR_SawingEffect _data, float _currentMoveDis, float _value)
- {
- if (_value < 0.0f)
- {
- return;
- }
- if (_value < 0.6f)
- {
- _data.m_RotationPoint.localEulerAngles = Vector3.Lerp(Vector3.zero, new Vector3(0, 0, 5), (_currentMoveDis / 0.6f) * _value);
- }
- else if (_value < 0.8f)
- {
- _data.m_RotationPoint.localEulerAngles = Vector3.Lerp(new Vector3(0, 0, 5), new Vector3(0, 0, 10), (_currentMoveDis / 0.2f) * _value - 3);
- }
- else if (_value <= 1f)
- {
- _data.m_RotationPoint.localEulerAngles = Vector3.Lerp(new Vector3(0, 0, 10), new Vector3(0, 0, 75), (_currentMoveDis / 0.2f) * _value - 4);
- }
- }
- /// <summary>
- /// 设置距离值
- /// </summary>
- /// <param name="_state"></param>
- /// <param name="_dis"></param>
- /// <returns></returns>
- private static float SetDistance(ModelTowards _state, float _dis)
- {
- return _state == ModelTowards.Positive ? Mathf.Abs(_dis) : -Mathf.Abs(_dis);
- }
- }
- /// <summary>
- /// 模型朝向
- /// </summary>
- public enum ModelTowards
- {
- /// <summary>
- /// 正
- /// </summary>
- Positive,
- /// <summary>
- /// 负
- /// </summary>
- Negative,
- }
- }
|