using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ClickMove : Device_Base
{
///
/// (打开状态,安装状态)
///
public bool isOpen;
public Vector3 OpenLocalPos;
public Vector3 CloseLocalPos;
///
/// 点击事件
///
public Action clickAction;
public Func canOpen;
public Action onStartAction;
///
/// 延迟操作时间
///
public float delayTime=0;
///
/// 验电位置
///
public Transform testPosAndRot;
///
/// 是否带电
///
public bool hasElectricity;
protected override void OnMDown()
{
base.OnMDown();
Click();
StepManager.Instance.FinishStep(triggerName);
}
private void Click()
{
if (!isMoving)
{
if(CheckCanOpen())
{
if (triggerAction?.Invoke(triggerName, false) == 0)
{
isMoving = true;
if (isOpen)
{
//关闭
onStartAction?.Invoke(false);
var tween = transform.DOLocalMove(CloseLocalPos, 0.5f).OnComplete(() =>
{
isOpen = false;
isMoving = false;
clickAction?.Invoke(isOpen);
triggerAction?.Invoke(triggerName, true);
CallScoreAction(false);
});
if (delayTime > 0)
{
tween.SetDelay(delayTime);
}
}
else
{
//打开
onStartAction?.Invoke(true);
var tween = transform.DOLocalMove(OpenLocalPos, 0.5f).OnComplete(() =>
{
isOpen = true;
isMoving = false;
clickAction?.Invoke(isOpen);
triggerAction?.Invoke(triggerName, true);
CallScoreAction(true);
});
if (delayTime > 0)
{
tween.SetDelay(delayTime);
}
}
}
}
}
}
private bool CheckCanOpen()
{
if(canOpen == null)
{
return true;
}
else
{
return canOpen.GetInvocationList().ToList().All(a=> ((Func)a).Invoke());
}
}
///
/// 直接设置拨片开关状态
///
///
public void SetState(bool isopen)
{
this.isOpen = isopen;
if (isOpen)
{
transform.localPosition = OpenLocalPos;
}
else
{
transform.localPosition = CloseLocalPos;
}
}
}