Files
aibis-dream/Assets/Scripts/SpecialPlay/MinHand.cs
T
2025-05-08 12:01:05 +08:00

82 lines
2.3 KiB
C#

using AibisDream.Framework;
using AibisDream.Utility;
using UnityEngine;
using UnityEngine.EventSystems;
namespace AibisDream
{
public class MinHand : MonoBehaviour, IInteraction
{
#region 索引
private EventTriggerEx _trigger;
#endregion
#region 参数
public float rotationSpeedLimit = 10f; // 旋转速度限制(度/秒)
public float rotationThreshold = 30f; // 触发事件的角度阈值(度)
#endregion
private void Awake()
{
_trigger = GetComponent<EventTriggerEx>();
_trigger.Register(EventTriggerType.Drag, OnDrag);
EventSystemEx.Instance.isLocked = false;
}
private void OnDrag(BaseEventData data)
{
// 这里是一些阻碍拖动的情况
if (!IsActive) return;
if (data is not PointerEventData pointerData) return;
// 计算鼠标位置与分针中心点的向量
Vector2 worldPoint = CommonUtil.GetMouseWorldPos(pointerData.position);
Vector2 direction = worldPoint - new Vector2(transform.position.x, transform.position.y);
// 计算当前角度
float currentAngle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
// 计算角度差
float deltaAngle = Mathf.DeltaAngle(transform.rotation.z, currentAngle);
// 限制旋转方向(仅允许逆时针旋转)
if (deltaAngle > 0)
{
deltaAngle = 0;
}
// 限制旋转速度
if (Mathf.Abs(deltaAngle) > rotationSpeedLimit * Time.deltaTime)
{
deltaAngle = -rotationSpeedLimit * Time.deltaTime;
}
// 更新分针的旋转
transform.Rotate(0, 0, deltaAngle);
// 触发事件
if (transform.rotation.z >= rotationThreshold)
{
// 触发事件逻辑
Debug.Log("Rotation threshold reached!");
// _totalRotation = 0f; // 重置总旋转角度
}
}
#region 交互属性
public bool IsActive => true;
public bool IsAvailable => true;
public GameObject GetGameObject()
{
return gameObject;
}
#endregion
}
}