Files
AppleHillsProduction/Assets/Scripts/UI/CardSystem/SlotContainerHelper.cs
2025-11-07 01:51:03 +01:00

71 lines
2.5 KiB
C#

using System.Collections.Generic;
using UI.DragAndDrop.Core;
using UnityEngine;
namespace UI.CardSystem
{
/// <summary>
/// Helper utility for shuffling draggable objects in a SlotContainer.
/// Moves objects to occupy the first available slots (0, 1, 2, etc.)
/// </summary>
public static class SlotContainerHelper
{
/// <summary>
/// Shuffles draggable objects to always occupy the first available slots.
/// Unassigns all objects from their current slots, then reassigns them starting from slot 0.
/// </summary>
/// <param name="container">The slot container holding the slots</param>
/// <param name="objects">List of draggable objects to shuffle</param>
/// <param name="animate">Whether to animate the movement</param>
public static void ShuffleToFront(SlotContainer container, List<DraggableObject> objects, bool animate = true)
{
if (container == null || objects == null || objects.Count == 0)
return;
Debug.Log($"[SlotContainerHelper] Shuffling {objects.Count} objects to front slots");
// Unassign all objects from their current slots
foreach (var obj in objects)
{
if (obj.CurrentSlot != null)
{
obj.CurrentSlot.Vacate();
}
}
// Reassign objects to first N slots starting from slot 0
for (int i = 0; i < objects.Count; i++)
{
DraggableSlot targetSlot = FindSlotByIndex(container, i);
DraggableObject obj = objects[i];
if (targetSlot != null)
{
Debug.Log($"[SlotContainerHelper] Assigning object to slot with SlotIndex {i}");
obj.AssignToSlot(targetSlot, animate);
}
else
{
Debug.LogWarning($"[SlotContainerHelper] Could not find slot with SlotIndex {i}");
}
}
}
/// <summary>
/// Find a slot by its SlotIndex property (not list position)
/// </summary>
private static DraggableSlot FindSlotByIndex(SlotContainer container, int slotIndex)
{
foreach (var slot in container.Slots)
{
if (slot.SlotIndex == slotIndex)
{
return slot;
}
}
return null;
}
}
}