Files
JuegoSim/Assets/_Project/Scripts/Code/Sockets/SocketIterator.cs
T
2026-07-21 08:56:10 +03:00

82 lines
2.6 KiB
C#

using UnityEngine;
/// <summary>
/// Tracks which SocketPoint on the held ghost is currently "active" (the one used for snapping).
/// Scroll wheel cycles through the iterative sockets defined on the ghost's SocketContainer.
/// Uses BaseInputProvider.OnChangeAttachPoint instead of legacy Input.GetAxis.
/// </summary>
public class SocketIterator : MonoBehaviour
{
private static SocketIterator instance;
public static SocketIterator getInstance() => instance;
private SocketContainer currentActiveContainer;
private SocketPoint currentActiveSocket;
private BaseInputProvider inputProvider;
private void Awake()
{
if (instance == null) instance = this;
else Destroy(gameObject);
}
private void Start()
{
inputProvider = InputProviderManager.getInstance()?.getCurrentInputProvider();
if (inputProvider == null)
{
Debug.LogWarning("SocketIterator: No input provider found.");
return;
}
// direction: +1 = scroll up (next), -1 = scroll down (previous)
inputProvider.OnChangeAttachPoint.AddListener(direction =>
{
if (!InputModeManager.Is(InputMode.ObjectDragging)) return;
if (currentActiveContainer == null) return;
if (direction > 0)
currentActiveContainer.CycleToNextSocket();
else if (direction < 0)
currentActiveContainer.CycleToPreviousSocket();
currentActiveSocket = currentActiveContainer.GetActiveSocket();
if (currentActiveSocket != null)
Debug.Log($"SocketIterator: switched to socket '{currentActiveSocket.name}'");
});
}
/// <summary>
/// Sets the container to iterate over and resets to the first socket.
/// Call this when a new ghost is spawned.
/// </summary>
public void SetActiveContainer(SocketContainer container)
{
currentActiveContainer = container;
if (container != null)
{
container.ResetToFirstSocket();
currentActiveSocket = container.GetActiveSocket();
}
}
/// <summary>Returns the currently active (highlighted) socket on the held ghost.</summary>
public SocketPoint GetCurrentActiveSocket() => currentActiveSocket;
/// <summary>
/// Clears state and removes the active socket highlight.
/// Call this when a ghost is placed or cancelled.
/// </summary>
public void ClearActiveContainer()
{
if (currentActiveSocket != null)
currentActiveSocket.Highlight(HighlightState.Default);
currentActiveContainer = null;
currentActiveSocket = null;
}
}