Files
aibis-dream/Assets/Scripts/FixSystem/Crowbar/Crowbar.cs
T
2024-08-31 19:24:29 +08:00

147 lines
4.4 KiB
C#

using UnityEngine;
using UnityEngine.EventSystems;
using DG.Tweening;
using UnityEngine.UI;
using Yarn.Unity;
public class Crowbar : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public float rotationSpeed = 10f; // 拖动旋转的速度
public float rotationThreshold = 30f; // 触发撬动完成的旋转阈值
public Transform crowbarVisual;
//public Transform crowbarBox;
public Transform crowbarInPosition;
//public Transform crowbarOutPosition;
public Transform crowbarSocket;
public bool IsInDialogue { get; set; } = false;
private Vector3 startPosition;
private Quaternion startRotation= Quaternion.Euler(0f, 0f, -225f);
private bool isInserted = false;
private Vector3 lastMousePosition;
private float progress = 0f;
private Image image;
private bool isPryingCompleted = false;
private void Awake()
{
image = GetComponent<Image>();
}
private void Start()
{
// startPosition = transform.position;
// startRotation = transform.rotation;
}
public void MoveIn()
{
//transform.DOMove(crowbarInPosition.position, 0.5f);
}
public void MoveOut()
{
//transform.DOMove(crowbarOutPosition.position, 0.5f);
}
public void OnBeginDrag(PointerEventData eventData)
{
if (IsInDialogue || isInserted) return;
image.raycastTarget = false;
//crowbarSocket.Activate();
RectTransform canvasRectTransform = (RectTransform)transform.root;
Vector3 worldPoint;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(canvasRectTransform, eventData.position, eventData.pressEventCamera, out worldPoint))
{
transform.position = worldPoint;
}
lastMousePosition = Input.mousePosition;
}
public void OnDrag(PointerEventData eventData)
{
if (IsInDialogue || isPryingCompleted) return;
if (isInserted)
{
float deltaY = lastMousePosition.y - Input.mousePosition.y;
lastMousePosition = Input.mousePosition; // 更新 lastMousePosition
progress += deltaY*0.3f * Time.deltaTime;
progress = Mathf.Clamp01(progress); // 确保 progress 不会小于0
// 计算插入旋转和插入旋转-45度之间的插值
float startAngle = crowbarSocket.transform.eulerAngles.z;
float endAngle = startAngle + 40f;
float currentAngle = Mathf.Lerp(startAngle, endAngle, progress);
// 更新撬棍的旋转角度
crowbarVisual.rotation = Quaternion.Euler(0f, 0f, currentAngle);
if (progress >= 1f)
{
// 触发撬动完成的逻辑
Debug.Log("Crowbar action completed.");
isPryingCompleted = true;
ResetCrowbar();
}
}
else
{
RectTransform canvasRectTransform = (RectTransform)transform.root;
Vector3 worldPoint;
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(canvasRectTransform, eventData.position, eventData.pressEventCamera, out worldPoint))
{
transform.position = worldPoint;
}
}
lastMousePosition = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
if (IsInDialogue) return;
if (isInserted)
{
ResetCrowbarProgress();
}
else
{
float snappingRange = 1f; // You might need to adjust this for WorldSpace Canvas
if (Vector3.Distance(transform.position, crowbarSocket.transform.position) <= snappingRange)
{
transform.position = crowbarSocket.transform.position;
crowbarVisual.rotation = crowbarSocket.transform.rotation;
isInserted = true;
//crowbarSocket.Deactivate();
}
else
{
transform.DOMove(crowbarInPosition.position, 0.1f).OnComplete(() =>
{
//crowbarSocket.Deactivate();
});
}
}
}
private void ResetCrowbarProgress()
{
//isInserted = false;
progress = 0f;
crowbarVisual.rotation = Quaternion.Euler(0f, 0f, 180f);
}
private void ResetCrowbar()
{
isInserted=false;
progress = 0f;
crowbarVisual.rotation = startRotation;
//isPryingCompleted = false;
transform.DOMove(crowbarInPosition.position, 0.1f);
}
}