148 lines
3.3 KiB
C#
148 lines
3.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using DG.Tweening;
|
|
using System.Linq;
|
|
|
|
public enum ZhouEnum
|
|
{
|
|
X,
|
|
Y,
|
|
Z
|
|
}
|
|
|
|
public class ClickRotate : Device_Base
|
|
{
|
|
public bool isOpen;
|
|
/// <summary>
|
|
/// 旋转的轴
|
|
/// </summary>
|
|
public ZhouEnum zhouEnum;
|
|
/// <summary>
|
|
/// 旋转的速度(每秒/度)
|
|
/// </summary>
|
|
public float speed;
|
|
|
|
/// <summary>
|
|
/// 判断是否可以打开或关闭
|
|
/// </summary>
|
|
public Func<bool> canOpen;
|
|
/// <summary>
|
|
/// 安装拆除事件
|
|
/// </summary>
|
|
public Action<bool> clickAction;
|
|
/// <summary>
|
|
/// 关闭时的值
|
|
/// </summary>
|
|
public Vector3 openAngle;
|
|
/// <summary>
|
|
/// 打开时的值
|
|
/// </summary>
|
|
public Vector3 closeAngle;
|
|
|
|
/// <summary>
|
|
/// 当前旋转角度值
|
|
/// </summary>
|
|
[SerializeField]
|
|
private Vector3 currentAngle;
|
|
|
|
protected override void OnAwake()
|
|
{
|
|
base.OnAwake();
|
|
currentAngle = isOpen ? openAngle : closeAngle;
|
|
}
|
|
|
|
protected override void OnMDown()
|
|
{
|
|
base.OnMDown();
|
|
if (!isMoving)
|
|
{
|
|
if (CheckCanOpen())
|
|
{
|
|
if (triggerAction == null || triggerAction.Invoke(triggerName, false) == 0)
|
|
{
|
|
if (isOpen)
|
|
{
|
|
Close();
|
|
}
|
|
else
|
|
{
|
|
Open();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
StepManager.Instance.FinishStep(triggerName);
|
|
}
|
|
|
|
private bool CheckCanOpen()
|
|
{
|
|
if (canOpen == null)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return canOpen.GetInvocationList().ToList().All(a => ((Func<bool>)a).Invoke());
|
|
}
|
|
}
|
|
|
|
private void Open()
|
|
{
|
|
if (!isOpen && !isMoving)
|
|
{
|
|
isMoving = true;
|
|
DOTween.To(() => currentAngle, x =>
|
|
{
|
|
currentAngle = x;
|
|
transform.localEulerAngles = currentAngle;
|
|
}, openAngle, 0.5f).OnComplete(() =>
|
|
{
|
|
isOpen = true;
|
|
isMoving = false;
|
|
clickAction?.Invoke(true);
|
|
triggerAction.Invoke(triggerName, true);
|
|
base.CallScoreAction(true);
|
|
});
|
|
}
|
|
}
|
|
|
|
private void Close()
|
|
{
|
|
if (isOpen && !isMoving)
|
|
{
|
|
isMoving = true;
|
|
DOTween.To(() => currentAngle, x =>
|
|
{
|
|
currentAngle = x;
|
|
transform.localEulerAngles = currentAngle;
|
|
}, closeAngle, 0.5f).OnComplete(() =>
|
|
{
|
|
isOpen = false;
|
|
isMoving = false;
|
|
clickAction?.Invoke(false);
|
|
triggerAction.Invoke(triggerName, true);
|
|
base.CallScoreAction(false);
|
|
});
|
|
}
|
|
}
|
|
|
|
public void SetState(bool isopen)
|
|
{
|
|
if(isopen)
|
|
{
|
|
isOpen = true;
|
|
transform.localEulerAngles = openAngle;
|
|
currentAngle = openAngle;
|
|
}
|
|
else
|
|
{
|
|
isOpen = false;
|
|
transform.localEulerAngles = closeAngle;
|
|
currentAngle = closeAngle;
|
|
}
|
|
}
|
|
}
|