using System; using AibisDream.Framework; using AibisDream.Kit; using AibisDream.Utility; using DG.Tweening; using UnityEngine; using UnityEngine.EventSystems; namespace AibisDream.FixSystem { [RequireComponent(typeof(EventTriggerEx))] public class Plug : MonoBehaviour, IInteraction { private static readonly int ShineFade = Shader.PropertyToID("_ShineFade"); private const string SortingLayerDragging = "Tools"; private const int SortingOrderDragging = 48; #region 插头当前信息 [SerializeField] private Vector3 startPosOffset = new(0.3f, -2f, 0); private Vector3 _startPos; private readonly Quaternion _pickupRotation = Quaternion.Euler(0, 0, 30); private readonly Quaternion _defaultRotation = Quaternion.Euler(0, 0, 180); #endregion #region 索引 private CablePanel cablePanel; private SpriteRenderer _sprite; private Transform _plugRootPos; private EventTriggerEx _trigger; public Transform PlugRoot => _plugRootPos; #endregion public event Action OnInsertSocket; public event Action OnPullUpSocket; public event Action OnPlugPointerDown; private bool _isDragging; private void Awake() { InitComponentRef(); EventRegister(); _startPos = transform.position + startPosOffset; } private void InitComponentRef() { cablePanel = transform.GetComponentInParent(true); _sprite = GetComponent(); _plugRootPos = transform.Find("Plug Root Pos"); _trigger = GetComponent(); // 处理插头高亮 _sprite.material.SetFloat(ShineFade, GlobalVariableKit.IsPlugHighLight ? 1 : 0); } private void EventRegister() { _trigger.Register(EventTriggerType.Drag, OnDrag); _trigger.Register(EventTriggerType.PointerDown, OnPointerDown); _trigger.Register(EventTriggerType.PointerUp, OnPointerUp); } /// /// 插入某个socket /// /// 目标socket private void InsertSocket(ISocket targetSocket) { // 修改插头位置 transform.position = targetSocket.GetSocketPos().position; // 处理插槽插入 targetSocket.PlugIn(); _sprite.enabled = false; OnInsertSocket?.Invoke(targetSocket); } /// /// 从某个Socket拔出来 /// public void PullUpSocket() { _sprite.enabled = true; OnPullUpSocket?.Invoke(); } private void OnDrag(BaseEventData eventData) { if (!_isDragging) return; if (eventData is PointerEventData pointerData) { transform.position = CameraKit.GetMouseWorldPos(pointerData.position); } } private void OnPointerDown(BaseEventData eventData) { if (eventData is PointerEventData { button: PointerEventData.InputButton.Right }) return; _sprite.sortingLayerName = SortingLayerDragging; _sprite.sortingOrder = SortingOrderDragging; OnPlugPointerDown?.Invoke(); TryCloseHighlight(); _isDragging = true; AdjustPlugPos(); AudioManager.Instance.PlaySfx("event:/FollowInput/plugpick"); } private void TryCloseHighlight() { if (!GlobalVariableKit.IsPlugHighLight) return; GlobalVariableKit.IsPlugHighLight = false; _sprite.material.SetFloat(ShineFade, 0); } private void OnPointerUp(BaseEventData eventData) { if (eventData is PointerEventData { button: PointerEventData.InputButton.Right }) { return; } // 停止拖拽 _isDragging = false; // 寻找可吸附的Module if (cablePanel.TryFindClosestModule(transform.position, out var targetModule)) { InsertSocket(targetModule); } else { ReturnToStartPosition(); } } public void ReturnToStartPosition() { transform.rotation = _defaultRotation; transform.DOMove(_startPos, 0.1f).OnComplete(() => { cablePanel.CableReelRef.ResetRotation(); cablePanel.SetCableMode(CableMode.Physic, transform); }); } private void AdjustPlugPos() { transform.rotation = _pickupRotation; } void OnDrawGizmosSelected() { Gizmos.color = Color.yellow; Gizmos.DrawWireSphere(transform.position, 0.5f); } public bool IsActive => true; public bool IsAvailable => !cablePanel.isCableRetracted; public GameObject GetGameObject() { return gameObject; } public Vector3 GetStartPos() { return _startPos; } } }