89 lines
2.6 KiB
C#
89 lines
2.6 KiB
C#
using System;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Utility;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class MinHand : MonoBehaviour, IInteraction
|
|
{
|
|
#region 索引
|
|
|
|
private EventTriggerEx _trigger;
|
|
|
|
public event Action<float> OnMinHandChanged;
|
|
public event Action OnMinHandReached;
|
|
|
|
#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 mouseAngle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
|
|
float currentAngle = CommonUtil.Angle360(mouseAngle - 90);
|
|
|
|
// 计算角度差
|
|
float deltaAngle = Mathf.DeltaAngle(transform.eulerAngles.z, currentAngle);
|
|
// 限制旋转方向(仅允许逆时针旋转)
|
|
if (deltaAngle < 0)
|
|
{
|
|
deltaAngle = 0;
|
|
}
|
|
|
|
// 限制旋转速度
|
|
if (Mathf.Abs(deltaAngle) > rotationSpeedLimit * Time.deltaTime)
|
|
{
|
|
deltaAngle = rotationSpeedLimit * Time.deltaTime;
|
|
}
|
|
|
|
// 更新分针的旋转
|
|
transform.Rotate(0, 0, deltaAngle);
|
|
|
|
OnMinHandChanged?.Invoke(transform.eulerAngles.z);
|
|
|
|
// 触发事件
|
|
if (transform.eulerAngles.z >= rotationThreshold)
|
|
{
|
|
// 触发事件逻辑
|
|
Debug.Log("Rotation threshold reached!");
|
|
IsActive = false;
|
|
OnMinHandReached?.Invoke();
|
|
}
|
|
}
|
|
|
|
#region 交互属性
|
|
|
|
public bool IsActive { get; set;}
|
|
public bool IsAvailable => true;
|
|
public GameObject GetGameObject()
|
|
{
|
|
return gameObject;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |