1118OPSSNew/Assets/Zion/Scripts//点击平移/ClickMove.cs

122 lines
3.2 KiB
C#

using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ClickMove : Device_Base
{
/// <summary>
/// (打开状态,安装状态)
/// </summary>
public bool isOpen;
public Vector3 OpenLocalPos;
public Vector3 CloseLocalPos;
/// <summary>
/// 点击事件
/// </summary>
public Action<bool> clickAction;
public Func<bool> canOpen;
public Action<bool> onStartAction;
/// <summary>
/// 延迟操作时间
/// </summary>
public float delayTime=0;
/// <summary>
/// 验电位置
/// </summary>
public Transform testPosAndRot;
/// <summary>
/// 是否带电
/// </summary>
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<bool>)a).Invoke());
}
}
/// <summary>
/// 直接设置拨片开关状态
/// </summary>
/// <param name="isopen"></param>
public void SetState(bool isopen)
{
this.isOpen = isopen;
if (isOpen)
{
transform.localPosition = OpenLocalPos;
}
else
{
transform.localPosition = CloseLocalPos;
}
}
}