You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
2.8 KiB
143 lines
2.8 KiB
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
public class SlotGroup<T> : IEnumerable<SlotGroup<T>.Slot>
|
|
{
|
|
public struct Slot
|
|
{
|
|
public int slotIndex;
|
|
public T item;
|
|
|
|
public bool IsEmpty => item == null;
|
|
}
|
|
|
|
Slot[] slots;
|
|
|
|
public T this[int index]
|
|
{
|
|
get => slots[index].item;
|
|
set => slots[index].item = value;
|
|
}
|
|
|
|
public int SlotCount => slots.Length;
|
|
public bool HasEmptySlot => Array.Exists(slots, slot => slot.IsEmpty);
|
|
|
|
public SlotGroup(int slotCount)
|
|
{
|
|
slots = new Slot[slotCount];
|
|
|
|
for(int i = 0; i < slotCount; ++i)
|
|
{
|
|
slots[i] = new Slot();
|
|
slots[i].slotIndex = i;
|
|
}
|
|
}
|
|
|
|
public SlotGroup(uint slotCount)
|
|
{
|
|
slots = new Slot[slotCount];
|
|
|
|
for(int i = 0; i < slotCount; ++i)
|
|
{
|
|
slots[i] = new Slot();
|
|
slots[i].slotIndex = i;
|
|
}
|
|
}
|
|
|
|
public void Switch(int slotIndex1, int slotIndex2)
|
|
{
|
|
if(!IsValidSlotIndex(slotIndex1) || !IsValidSlotIndex(slotIndex2))
|
|
{
|
|
Logger.LogError("Invalid slot number");
|
|
return;
|
|
}
|
|
|
|
T temp = slots[slotIndex1].item;
|
|
slots[slotIndex1].item = slots[slotIndex2].item;
|
|
slots[slotIndex2].item = temp;
|
|
}
|
|
|
|
public bool TryGetItem(int slotIndex, out T item)
|
|
{
|
|
item = default;
|
|
|
|
if(!IsValidSlotIndex(slotIndex))
|
|
{
|
|
Logger.LogError("Invalid slot number");
|
|
return false;
|
|
}
|
|
|
|
item = slots[slotIndex].item;
|
|
return item != null;
|
|
}
|
|
|
|
public void LoopValidSlot(Func<T, bool> action)
|
|
{
|
|
foreach(var slot in slots)
|
|
{
|
|
if (slot.IsEmpty) continue;
|
|
|
|
if (!action(slot.item))
|
|
break;
|
|
}
|
|
}
|
|
|
|
public Slot GetSlot(int slotIndex)
|
|
{
|
|
if(!IsValidSlotIndex(slotIndex))
|
|
{
|
|
Logger.LogError("Invalid slot number");
|
|
return default;
|
|
}
|
|
|
|
return slots[slotIndex];
|
|
}
|
|
|
|
public bool TryGetSlot(T item, out Slot slot)
|
|
{
|
|
slot = default;
|
|
|
|
for(int i = 0; i < slots.Length; ++i)
|
|
{
|
|
if (item.Equals(slots[i].item))
|
|
{
|
|
slot = slots[i];
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void GetEmptySlots(out List<Slot> emptySlots)
|
|
{
|
|
emptySlots = new List<Slot>();
|
|
foreach(var slot in slots)
|
|
{
|
|
if(slot.IsEmpty)
|
|
{
|
|
emptySlots.Add(slot);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ClearSlot(int slotIndex)
|
|
{
|
|
if(!IsValidSlotIndex(slotIndex))
|
|
{
|
|
Logger.LogError("Invalid slot number");
|
|
return;
|
|
}
|
|
|
|
slots[slotIndex].item = default;
|
|
}
|
|
|
|
public bool IsValidSlotIndex(int slotIndex)
|
|
{
|
|
return slotIndex >= 0 && slotIndex < slots.Length;
|
|
}
|
|
|
|
public IEnumerator<Slot> GetEnumerator() => ((IEnumerable<Slot>)slots).GetEnumerator();
|
|
IEnumerator IEnumerable.GetEnumerator() => slots.GetEnumerator();
|
|
}
|