using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ChivaXR.Tool
{
///
/// 锯断效果
///
public class CVR_SawingEffect : Singleton
{
///
/// 锯断效果依附的旋转点
///
public Transform m_RotationPoint;
///
/// 带有刚体的目标
///
public Rigidbody m_TargetRigidbody;
///
/// 模型朝向
///
private ModelTowards m_ModelTowards;
///
/// 设置初始状态
///
///
///
///
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);
}
///
/// 每帧执行
///
public void SimulationUpdate(float _currentMoveDis, float _value, Action _action)
{
CVR_SawingSystem.SawingOpSimulation(this, _currentMoveDis, _value, m_ModelTowards, _action);
}
}
///
/// 锯切系统
///
internal class CVR_SawingSystem
{
///
/// 锯切操作控制器
///
///
///
///
///
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() != null)
{
_data.m_TargetRigidbody.GetComponent().isTrigger = false;
}
_action?.Invoke();
}
}
///
/// 锯断电缆状态变化
///
///
///
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);
}
}
///
/// 设置距离值
///
///
///
///
private static float SetDistance(ModelTowards _state, float _dis)
{
return _state == ModelTowards.Positive ? Mathf.Abs(_dis) : -Mathf.Abs(_dis);
}
}
///
/// 模型朝向
///
public enum ModelTowards
{
///
/// 正
///
Positive,
///
/// 负
///
Negative,
}
}