209 lines
5.8 KiB
C#
209 lines
5.8 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using System.Linq;
|
|
using DG.Tweening;
|
|
using AibisDream;
|
|
using Yarn.Unity;
|
|
using System;
|
|
|
|
public class Plug : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUpHandler
|
|
{
|
|
public delegate void PlugEventHandler(Plug plug);
|
|
|
|
public event PlugEventHandler OnPlugStartDrag;
|
|
public event PlugEventHandler OnPlugEndDrag;
|
|
private Vector3 startPosition;
|
|
private Quaternion startRotation;
|
|
private bool isDragging = false;
|
|
[HideInInspector] public Socket currentSocket = null;
|
|
private Image image;
|
|
private Vector3 pickupOffset = new Vector3(0, 0, 0);
|
|
private Quaternion pickupRotationOffset = Quaternion.Euler(0, 0, 30);
|
|
public Socket initialSocket; // new variable
|
|
public Image plugSprite; // new variable
|
|
public bool IsInDialogue { get; set; } = false;
|
|
|
|
public bool IsActive { get; set; } = true;
|
|
|
|
private Cable cableref;
|
|
|
|
public Cable CableRef
|
|
{
|
|
get { return cableref; }
|
|
set { cableref = value; }
|
|
}
|
|
|
|
public event Action<Plug> OnPlugInserted;
|
|
public event Action OnPlugRemoved;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
image = GetComponent<Image>();
|
|
cableref = FindObjectOfType<Cable>();
|
|
cableref.End = transform;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
startPosition = initialSocket.GetPosition();
|
|
|
|
transform.position = startPosition;
|
|
initialSocket.InsertPlug(this);
|
|
plugSprite.enabled = false;
|
|
currentSocket = initialSocket;
|
|
DialogController.OnDialogueStart += HandleDialogueStart;
|
|
DialogController.OnDialogueComplete += HandleDialogueComplete;
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
// Unsubscribe from dialogue events
|
|
DialogController.OnDialogueStart -= HandleDialogueStart;
|
|
DialogController.OnDialogueComplete -= HandleDialogueComplete;
|
|
}
|
|
|
|
public void HandleDialogueStart()
|
|
{
|
|
// Dialogue has started, disable plug interactions
|
|
IsInDialogue = true;
|
|
//Debug.Log("in dialogue");
|
|
}
|
|
|
|
public void HandleDialogueComplete()
|
|
{
|
|
// Dialogue has completed, enable plug interactions
|
|
IsInDialogue = false;
|
|
//Debug.Log("dialoguecompleted");
|
|
}
|
|
|
|
[YarnCommand("Disable_plugInput")]
|
|
public void Disable_plugInput()
|
|
{
|
|
// Dialogue has started, disable plug interactions
|
|
IsActive = false;
|
|
//Debug.Log("in dialogue");
|
|
}
|
|
|
|
[YarnCommand("Enable_plugInput")]
|
|
public void Enable_plugInput()
|
|
{
|
|
// Dialogue has completed, enable plug interactions
|
|
IsActive = true;
|
|
//Debug.Log("dialoguecompleted");
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
if (!IsActive)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (IsInDialogue)
|
|
{
|
|
return;
|
|
}
|
|
|
|
isDragging = true;
|
|
image.raycastTarget = false;
|
|
// 通知 DialogueManager 开始拖拽
|
|
DialogueManager.Instance.SetDragging(true);
|
|
currentSocket?.RemovePlug(this);
|
|
OnPlugRemoved?.Invoke();
|
|
|
|
plugSprite.enabled = true;
|
|
|
|
transform.position += pickupOffset;
|
|
transform.rotation = pickupRotationOffset;
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
if (!IsActive)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (IsInDialogue)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (isDragging)
|
|
{
|
|
CableRef.End = transform.GetChild(0).transform;
|
|
Vector3 screenPosition = new Vector3(eventData.position.x, eventData.position.y,
|
|
Camera.main.WorldToScreenPoint(transform.position).z);
|
|
Vector3 move = Camera.main.ScreenToWorldPoint(screenPosition);
|
|
move.z = transform.position.z;
|
|
transform.position = move;
|
|
plugSprite.enabled = true;
|
|
}
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
if (!IsActive)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (IsInDialogue)
|
|
{
|
|
return;
|
|
}
|
|
|
|
isDragging = false;
|
|
image.raycastTarget = true;
|
|
// 通知 DialogueManager 结束拖拽
|
|
DialogueManager.Instance.SetDragging(false);
|
|
|
|
float closestDistance = Mathf.Infinity;
|
|
Socket closestSocket = null;
|
|
|
|
Socket[] sockets = FindObjectsOfType<MonoBehaviour>().OfType<Socket>().ToArray();
|
|
|
|
foreach (Socket socket in sockets)
|
|
{
|
|
float distance = Vector3.Distance(transform.position, socket.GetPosition());
|
|
|
|
if (distance < closestDistance)
|
|
{
|
|
closestDistance = distance;
|
|
closestSocket = socket;
|
|
}
|
|
}
|
|
|
|
float snappingRange = 1f; // You might need to adjust this for WorldSpace Canvas
|
|
if (closestSocket != null && closestDistance <= snappingRange)
|
|
{
|
|
currentSocket = closestSocket;
|
|
currentSocket.InsertPlug(this);
|
|
OnPlugInserted?.Invoke(this); // 触发插入事件
|
|
transform.position = currentSocket.GetPosition();
|
|
plugSprite.enabled = false;
|
|
//transform.rotation = startRotation;
|
|
}
|
|
else
|
|
{
|
|
transform.DOMove(startPosition, 0.1f).OnComplete(() =>
|
|
{
|
|
//transform.rotation = startRotation;
|
|
initialSocket.InsertPlug(this);
|
|
OnPlugInserted?.Invoke(this); // 触发插入事件
|
|
plugSprite.enabled =
|
|
false; // Make the plug sprite disappear when it is inserted into the initial socket.
|
|
});
|
|
}
|
|
}
|
|
|
|
void OnDrawGizmosSelected()
|
|
{
|
|
Gizmos.color = Color.yellow;
|
|
float snappingRange = 1.0f; // You might need to adjust this for WorldSpace Canvas
|
|
Gizmos.DrawWireSphere(transform.position, snappingRange);
|
|
}
|
|
} |