新增拆除功能
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using DG.Tweening;
|
||||
using System.Drawing; // 需要引入 DOTween 命名空间
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class Module : MonoBehaviour, IPointerClickHandler
|
||||
{
|
||||
public RectTransform faceCanvas; // 需要移动和缩放的面板
|
||||
public Vector3 repairCanvasPosition; // 维修模式下的面板位置
|
||||
public Vector3 originalCanvasPosition; // 原始面板位置
|
||||
public float repairCanvasScale = 1.5f; // 维修模式下的面板缩放比例
|
||||
public float animationDuration = 0.5f; // 动画持续时间
|
||||
|
||||
public GameObject screwsContainer; // 螺丝的父对象
|
||||
public RectTransform targetArea; // 拖拽的目标区域,用于判定维修完成
|
||||
|
||||
private bool isRepairMode = false;
|
||||
private bool canDrag = false;
|
||||
private Vector3 initialPosition;
|
||||
|
||||
public Graphic socketToEnable;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
initialPosition = transform.position;
|
||||
originalCanvasPosition = faceCanvas.position;
|
||||
socketToEnable.raycastTarget=false;
|
||||
EnableScrewsInteraction(false);
|
||||
}
|
||||
|
||||
public void StartRepairIfToolIsScrewdriver(Tool tool)
|
||||
{
|
||||
EnterRepairMode();
|
||||
}
|
||||
|
||||
public void EnterRepairMode()
|
||||
{
|
||||
isRepairMode = true;
|
||||
|
||||
// 使用 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;
|
||||
}
|
||||
|
||||
private void EnableScrewsInteraction(bool isEnabled)
|
||||
{
|
||||
foreach (Transform screw in screwsContainer.transform)
|
||||
{
|
||||
screw.GetComponent<Screw>().enabled = isEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
CompleteRepair();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPointerClick(PointerEventData eventData)
|
||||
{
|
||||
// 只有在玩家已获得工具且当前工具是螺丝刀时,才尝试进入维修模式
|
||||
if (ToolManager.Instance.HasTool)
|
||||
{
|
||||
ToolManager.Instance.TryStartRepairMode(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("No tool");
|
||||
}
|
||||
}
|
||||
|
||||
private void CompleteRepair()
|
||||
{
|
||||
Debug.Log("维修完成!");
|
||||
ExitRepairMode();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user