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;
///
/// 旋转的轴
///
public ZhouEnum zhouEnum;
///
/// 旋转的速度(每秒/度)
///
public float speed;
///
/// 判断是否可以打开或关闭
///
public Func canOpen;
///
/// 安装拆除事件
///
public Action clickAction;
///
/// 关闭时的值
///
public Vector3 openAngle;
///
/// 打开时的值
///
public Vector3 closeAngle;
///
/// 当前旋转角度值
///
[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)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;
}
}
}