using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System;
public class SimplePipe : VRUsableStepActionBase
{
private BezierPipe pipeMesh;
[Tooltip("与该线路连接的接头是否为固定位置的")]
public bool connectorIsFixed;
[HideInInspector]
//该线路插头插上时应保持的位置
public Vector3 thisFixedPos;
///
/// 断开时指定位置或者指定移动向量
///
public bool useMoveDir;
[PropertyActive("useMoveDir", true, CompareType.NotEqul)]
//线路断开时移动Dir
public Vector3 onFreeMoveDir;
[PropertyActive("useMoveDir", false, CompareType.NotEqul)]
//该线路插头断开时保持的位置
public Vector3 thisFreePos;
//断开时保持角度不变
public bool keepAngleOnFree = true;
//断开后的目标角度值
[PropertyActive("keepAngleOnFree", false, CompareType.NotEqul)]
public Vector3 freeAngle;
//连接时的角度
private Vector3 fixedAngle;
[PropertyActive("connectorIsFixed", true, CompareType.Equl)]
public Transform connector;
[PropertyActive("connectorIsFixed", true, CompareType.Equl)]
public Vector3 connectorFixedPos;
[PropertyActive("connectorIsFixed", true, CompareType.Equl)]
public Vector3 connectorFreePos;
protected override void Awake()
{
base.Awake();
//if (thisFixedPos == Vector3.zero)//未设置安装位置时 按照初始位置作为安装位置
thisFixedPos = this.transform.localPosition;//默认按照初始位置作为安装位置
if (useMoveDir)
{
thisFreePos = thisFixedPos + onFreeMoveDir;
}
if (connector)
{
connectorFixedPos = connector.transform.localPosition;
}
pipeMesh = this.transform.parent.GetComponentInChildren();
if (pipeMesh)
pipeMesh.update = false;//接头固定时不计算管路mesh 避免消耗性能
if (!keepAngleOnFree)
{
fixedAngle = this.transform.localEulerAngles;
}
}
public override void OnUsed()
{
SetUsable(false);
if (pipeMesh)
pipeMesh.update = true;
if (processName == ProcessEnum.teardown)
{
this.transform.DOLocalMove(thisFreePos, 1f).OnComplete(UnFixedMoveOver);
if (!keepAngleOnFree)
this.transform.DOLocalRotate(freeAngle, 1f);
if (!connectorIsFixed)
{
connector.DOLocalMove(connectorFreePos, 1f);
}
}
else if (processName == ProcessEnum.assemble)
{
this.transform.DOLocalMove(thisFixedPos, 1f).OnComplete(FixedMoveOver);
if (!keepAngleOnFree)
this.transform.DOLocalRotate(fixedAngle, 1f);
if (!connectorIsFixed)
{
connector.DOLocalMove(connectorFixedPos, 1f);
}
}
}
private void FixedMoveOver()
{
if (pipeMesh)
pipeMesh.update = false;
fixedOver?.Invoke();
fixedOver = null;
}
private void UnFixedMoveOver()
{
if (pipeMesh)
pipeMesh.update = false;
unFixedOver?.Invoke();
unFixedOver = null;
}
}