YanCheng_Metrology/Assets/Scripts/CXX/Devices/直接接入式电能计量装置/Device_Cover.cs

212 lines
6.3 KiB
C#

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting.Antlr3.Runtime;
using UnityEngine;
/// <summary>
/// 接线盖子
/// </summary>
public class Device_Cover : Device_Base
{
/// <summary>
/// 盖子是否已打开
/// </summary>
public bool isOpen;
/// <summary>
/// 是否检查接线完好
/// </summary>
public bool isCheckOK;
[Tooltip("左盖子螺丝")]
public Tool_Screw cover_screw_Left;
[Tooltip("右盖子螺丝")]
public Tool_Screw cover_screw_Right;
[Tooltip("左盖子封印")]
public Device_Seal cover_seal_Left;
[Tooltip("右盖子封印")]
public Device_Seal cover_seal_Right;
[Tooltip("左盖子封印触发区域")]
public DeviceTrigger cover_seal_left_Trigger;
[Tooltip("右盖子封印触发区域")]
public DeviceTrigger cover_seal_right_Trigger;
protected override void OnAwake()
{
base.OnAwake();
//左螺丝判断条件
cover_screw_Left.AddCheckAction(() =>
{
if (!cover_seal_Left.isCut)
{
Debug.Log("封印未剪断");
return false;
}
if (isOpen)
{
Debug.Log("盖子打开的");
return false;
}
return true;
});
//右螺丝判断条件
cover_screw_Right.AddCheckAction(() =>
{
if (!cover_seal_Right.isCut)
{
Debug.Log("封印未剪断");
return false;
}
if (isOpen)
{
Debug.Log("盖子打开的");
return false;
}
return true;
});
//左出触发区域回调
cover_seal_left_Trigger.clickAction += () =>
{
//盖上了盖子
//螺丝拧上了就安装封印
if (!isOpen && cover_screw_Left.isInstall)
{
if ((triggerAction == null ? 0 : triggerAction.Invoke(cover_seal_left_Trigger.triggerName, true)) == 0)
{
//盖子左封印
cover_seal_Left.Install();
//触发区域隐藏
cover_seal_left_Trigger.gameObject.SetActive(false);
}
}
};
//右触发区域回调
cover_seal_right_Trigger.clickAction += () =>
{
if (!isOpen && cover_screw_Right.isInstall)
{
if ((triggerAction == null ? 0 : triggerAction.Invoke(cover_seal_right_Trigger.triggerName, true)) == 0)
{
//盖子右封印
cover_seal_Right.Install();
//触发区域隐藏
cover_seal_right_Trigger.gameObject.SetActive(false);
}
}
};
//注册工具拿出和收回,显示隐藏物体的封印的触发
EventCenter.Instance.AddEventListener<GameObject>(Enum_EventType.TakeOutAndRetrievingTheTools, CheckTriggerShow);
}
protected override void OnMDown()
{
base.OnMDown();
if (!isMoving)
{
if ((triggerAction == null ? 0 : triggerAction.Invoke(triggerName, false)) == 0)
{
isCheckOK = true;
if (!isOpen)
{
//螺丝都拧松才能拆盖子
if (!cover_screw_Left.isInstall && !cover_screw_Right.isInstall)
{
Open();
}
}
else
{
Close();
}
}
}
}
/// <summary>
/// 检查显示或隐藏封印触发器
/// </summary>
/// <param name="obj"></param>
private void CheckTriggerShow(GameObject obj)
{
if (obj == null)
{
//收回
Debug.Log("收回");
cover_seal_left_Trigger.gameObject.SetActive(false);
cover_seal_right_Trigger.gameObject.SetActive(false);
}
else
{
//拿出
Debug.Log("拿出");
if (obj.name == "盒装封印")
{
if (!isOpen && cover_seal_Left.isCut && cover_screw_Left.isInstall)
cover_seal_left_Trigger.gameObject.SetActive(true);
if (!isOpen && cover_seal_Right.isCut && cover_screw_Right.isInstall)
cover_seal_right_Trigger.gameObject.SetActive(true);
}
}
}
/// <summary>
/// 打开盖子
/// </summary>
public void Open()
{
//盖子螺丝不在动才能动盖子
if (!cover_screw_Left.isMoving && !cover_screw_Right.isMoving)
{
isMoving = true;
Debug.Log("打开盖子");
Transform parent = cover_screw_Left.transform.parent;
cover_screw_Left.transform.parent = transform;
cover_screw_Right.transform.parent = transform;
transform.DOLocalMove(new Vector3(transform.localPosition.x, 0.1388763f, -0.2485413f), 2).OnComplete(() =>
{
isOpen = true;
cover_screw_Left.transform.parent = parent;
cover_screw_Right.transform.parent = parent;
isMoving = false;
int result = (triggerAction == null ? 0 : triggerAction.Invoke(triggerName, true));
});
}
}
/// <summary>
/// 盖上盖子
/// </summary>
public void Close()
{
//盖子螺丝不在动才能动盖子
if (!cover_screw_Left.isMoving && !cover_screw_Right.isMoving)
{
isMoving=true;
Debug.Log("盖上盖子");
Transform parent = cover_screw_Left.transform.parent;
cover_screw_Left.transform.parent = transform;
cover_screw_Right.transform.parent = transform;
transform.DOLocalMove(new Vector3(transform.localPosition.x, -0.01112366f, -0.09854126f), 2).OnComplete(() =>
{
isOpen = false;
cover_screw_Left.transform.parent = parent;
cover_screw_Right.transform.parent = parent;
isMoving = false;
int result = (triggerAction == null ? 0 : triggerAction.Invoke(triggerName, true));
});
}
}
protected override void OnDestroy()
{
base.OnDestroy();
EventCenter.Instance.RemoveEventListener<GameObject>(Enum_EventType.TakeOutAndRetrievingTheTools, CheckTriggerShow);
}
}