235 lines
6.5 KiB
C#
235 lines
6.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using DG.Tweening;
|
|
using System.Drawing; // 需要引入 DOTween 命名空间
|
|
using UnityEngine.UI;
|
|
using AibisDream;
|
|
|
|
public class Module : MonoBehaviour, IPointerClickHandler
|
|
{
|
|
public Transform faceCanvas; // 需要移动和缩放的面板
|
|
public Vector3 repairCanvasPosition; // 维修模式下的面板位置
|
|
public Vector3 originalCanvasPosition; // 原始面板位置
|
|
public float repairCanvasScale = 1.5f; // 维修模式下的面板缩放比例
|
|
public float animationDuration = 0.5f; // 动画持续时间
|
|
|
|
public bool isallowedRepair = false; // 动画持续时间
|
|
|
|
public bool isalloweInstall = false; // 动画持续时间
|
|
|
|
public GameObject screwsContainer; // 螺丝的父对象
|
|
public RectTransform targetArea; // 拖拽的目标区域,用于判定维修完成
|
|
[HideInInspector]
|
|
public bool isRepairMode = false;
|
|
private bool canDrag = false;
|
|
private Vector3 initialPosition;
|
|
|
|
private Plug plug;
|
|
|
|
public bool isRepiarFinished=false;
|
|
|
|
public GameObject ObejctToInstall;
|
|
public bool isInstall=false;
|
|
|
|
public Socket socketToEnable;
|
|
|
|
private void Start()
|
|
{
|
|
if(ObejctToInstall!=null)
|
|
{
|
|
if(isInstall)
|
|
{
|
|
ObejctToInstall.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
ObejctToInstall.SetActive(false);
|
|
}
|
|
}
|
|
if(isRepiarFinished)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
plug=FindObjectOfType<Plug>();
|
|
initialPosition = transform.position;
|
|
if(faceCanvas!=null)
|
|
{
|
|
originalCanvasPosition = faceCanvas.position;
|
|
}
|
|
else
|
|
{
|
|
originalCanvasPosition=Vector3.zero;
|
|
}
|
|
|
|
//socketToEnable.raycastTarget=false;
|
|
if(socketToEnable!=null)
|
|
{
|
|
socketToEnable.IsAvailable=false;
|
|
}
|
|
if(screwsContainer!=null)
|
|
{
|
|
foreach (Transform screw in screwsContainer.transform)
|
|
{
|
|
screw.GetComponent<Screw>().enabled = false;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
public void SetReapirState(bool state)
|
|
{
|
|
isRepiarFinished=state;
|
|
if(isRepiarFinished)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
public void SetInstallState(bool state)
|
|
{
|
|
isInstall=state;
|
|
if(isInstall)
|
|
{
|
|
ObejctToInstall.SetActive(true);
|
|
isalloweInstall=false;
|
|
|
|
}
|
|
else
|
|
{
|
|
ObejctToInstall.SetActive(false);
|
|
}
|
|
}
|
|
// public void StartRepairIfToolIsScrewdriver(Tool tool)
|
|
// {
|
|
// EnterRepairMode();
|
|
// }
|
|
|
|
|
|
public void RemoveAllPlugsFromSockets()
|
|
{
|
|
Socket[] sockets = FindObjectsOfType<Socket>(); // 找到场景中的所有Socket组件
|
|
foreach (Socket socket in sockets)
|
|
{
|
|
socket.RemovePlug(plug); // 移除Plug
|
|
}
|
|
plug.ReturnToInit();
|
|
plug.enabled=false;
|
|
}
|
|
|
|
// public void EnterRepairMode()
|
|
// {
|
|
|
|
// DialogController.Instance.StartDialogNode("EnterRepairMode");
|
|
// isRepairMode = true;
|
|
// RemoveAllPlugsFromSockets();
|
|
// // 使用 DOTween 对 Canvas 的位置和缩放进行平滑过渡
|
|
// faceCanvas.DOMove(repairCanvasPosition, animationDuration)
|
|
// .SetEase(Ease.OutQuad) // 移动面板
|
|
// .OnComplete(() => EnableScrewsInteraction(true)); // 动画完成后才启用交互
|
|
|
|
// faceCanvas.DOScale(repairCanvasScale, animationDuration)
|
|
// .SetEase(Ease.OutQuad); // 缩放面板
|
|
// }
|
|
|
|
// public void ExitRepairMode()
|
|
// {
|
|
// isRepairMode = false;
|
|
|
|
// // 使用 DOTween 对 Canvas 的位置和缩放进行平滑过渡
|
|
// faceCanvas.DOMove(originalCanvasPosition, animationDuration)
|
|
// .SetEase(Ease.InQuad)
|
|
// .OnComplete(() => EnableScrewsInteraction(false)); // 动画完成后禁用交互
|
|
|
|
// faceCanvas.DOScale(1f, animationDuration)
|
|
// .SetEase(Ease.InQuad); // 恢复面板缩放
|
|
// ResetModulePosition();
|
|
// ToolManager.Instance.ShowToolBoxAfterTask();
|
|
// //socketToEnable.raycastTarget=true;
|
|
// socketToEnable.IsAvailable=true;
|
|
|
|
// foreach (Transform screw in screwsContainer.transform)
|
|
// {
|
|
// if(screw!=null)
|
|
// {
|
|
// screw.GetComponent<Screw>().enabled = false;
|
|
// }
|
|
|
|
// }
|
|
// }
|
|
public void Install()
|
|
{
|
|
Debug.Log(gameObject.name+"InStalled");
|
|
isInstall=true;
|
|
ObejctToInstall.SetActive(true);
|
|
DialogController.Instance.StartDialogNode("FinishInstall");
|
|
}
|
|
public void EnableScrewsInteraction(bool isEnabled)
|
|
{
|
|
foreach (Transform screw in screwsContainer.transform)
|
|
{
|
|
screw.GetComponent<Screw>().enabled = isEnabled;
|
|
}
|
|
plug.enabled=true;
|
|
}
|
|
|
|
private void ResetModulePosition()
|
|
{
|
|
transform.position = initialPosition;
|
|
canDrag = false; // 禁用拖拽,防止未拆完螺丝时拖动
|
|
}
|
|
|
|
public void EnableDragging()
|
|
{
|
|
canDrag = true;
|
|
}
|
|
|
|
public void CheckAllScrews()
|
|
{
|
|
bool allDropped = true;
|
|
foreach (Transform screw in screwsContainer.transform)
|
|
{
|
|
if (screw.gameObject.activeSelf)
|
|
{
|
|
allDropped = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (allDropped)
|
|
{
|
|
socketToEnable.IsAvailable=true;
|
|
foreach (Transform screw in screwsContainer.transform)
|
|
{
|
|
if(screw!=null)
|
|
{
|
|
screw.GetComponent<Screw>().enabled = false;
|
|
}
|
|
}
|
|
CompleteRepair();
|
|
Destroy(gameObject);
|
|
|
|
}
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
// 只有在玩家已获得工具且当前工具是螺丝刀时,才尝试进入维修模式
|
|
if (ToolManager.Instance.HasTool)
|
|
{
|
|
ToolManager.Instance.TryStartRepairMode(this);
|
|
ToolManager.Instance.TryInstall(this);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("No tool");
|
|
}
|
|
}
|
|
|
|
private void CompleteRepair()
|
|
{
|
|
DialogController.Instance.StartDialogNode("FinishedReapir");
|
|
Debug.Log("维修完成!");
|
|
//ExitRepairMode();
|
|
}
|
|
}
|