Files
aibis-dream/Assets/Scripts/FixSystem/Cable/Plug.cs
T

142 lines
4.2 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Linq;
using DG.Tweening;
using AibisDream;
public class Plug : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUpHandler
{
private Vector3 startPosition;
private Quaternion startRotation;
private bool isDragging = false;
private ISocket 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;
private void Awake()
{
image = GetComponent<Image>();
}
private void Start()
{
startPosition = initialSocket.GetPosition();
transform.position = startPosition;
initialSocket.InsertPlug(this);
plugSprite.enabled = false;
DialogController.OnDialogueStart += HandleDialogueStart;
DialogController.OnDialogueComplete += HandleDialogueComplete;
}
void OnDestroy()
{
// Unsubscribe from dialogue events
DialogController.OnDialogueStart -= HandleDialogueStart;
DialogController.OnDialogueComplete -= HandleDialogueComplete;
}
void HandleDialogueStart()
{
// Dialogue has started, disable plug interactions
IsInDialogue = true;
Debug.Log("in dialogue");
}
void HandleDialogueComplete()
{
// Dialogue has completed, enable plug interactions
IsInDialogue = false;
Debug.Log("dialoguecompleted");
}
public void OnPointerDown(PointerEventData eventData)
{
if (IsInDialogue)
{
return;
}
isDragging = true;
image.raycastTarget = false;
currentSocket?.RemovePlug(this);
plugSprite.enabled = true;
transform.position += pickupOffset;
transform.rotation = pickupRotationOffset;
}
public void OnDrag(PointerEventData eventData)
{
if (IsInDialogue)
{
return;
}
if (isDragging)
{
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;
}
}
public void OnPointerUp(PointerEventData eventData)
{
if (IsInDialogue)
{
return;
}
isDragging = false;
image.raycastTarget = true;
float closestDistance = Mathf.Infinity;
ISocket closestSocket = null;
ISocket[] sockets = FindObjectsOfType<MonoBehaviour>().OfType<ISocket>().ToArray();
foreach (ISocket 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);
transform.position = currentSocket.GetPosition();
plugSprite.enabled = false;
//transform.rotation = startRotation;
}
else
{
transform.DOMove(startPosition, 0.1f).OnComplete(() =>
{
//transform.rotation = startRotation;
initialSocket.InsertPlug(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);
}
}