153 lines
3.9 KiB
C#
153 lines
3.9 KiB
C#
using GeometryTD.Definition;
|
|
using GeometryTD.CustomEvent;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
namespace GeometryTD.UI
|
|
{
|
|
public class CombineArea : MonoBehaviour, IDropHandler
|
|
{
|
|
[SerializeField] private CombineSlotItem[] _slots;
|
|
|
|
public void OnInit(CombineAreaContext context)
|
|
{
|
|
if (_slots == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < _slots.Length; i++)
|
|
{
|
|
if (_slots[i] != null)
|
|
{
|
|
_slots[i].OnInit(i);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnReset()
|
|
{
|
|
if (_slots == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var slot in _slots)
|
|
{
|
|
if (slot != null)
|
|
{
|
|
slot.OnReset();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnCombineClick()
|
|
{
|
|
if (!TryGetBoundItemId(TowerCompSlotType.Muzzle, out long muzzleItemId) ||
|
|
!TryGetBoundItemId(TowerCompSlotType.Bearing, out long bearingItemId) ||
|
|
!TryGetBoundItemId(TowerCompSlotType.Base, out long baseItemId))
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameEntry.Event.Fire(this, RepoCombineRequestedEventArgs.Create(muzzleItemId, bearingItemId, baseItemId));
|
|
}
|
|
|
|
public bool TryAssignItem(RepoItemContext itemContext)
|
|
{
|
|
if (itemContext == null || itemContext.ComponentSlotType == TowerCompSlotType.None)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
CombineSlotItem targetSlot = FindSlot(itemContext.ComponentSlotType);
|
|
if (targetSlot == null || targetSlot.HasItem)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
targetSlot.BindItem(itemContext);
|
|
return true;
|
|
}
|
|
|
|
public bool TryClearSlot(int slotIndex, out long removedItemId)
|
|
{
|
|
removedItemId = 0;
|
|
if (_slots == null || slotIndex < 0 || slotIndex >= _slots.Length)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
CombineSlotItem slot = _slots[slotIndex];
|
|
if (slot == null || !slot.HasItem)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
removedItemId = slot.BoundItemId;
|
|
slot.ClearSlot();
|
|
return true;
|
|
}
|
|
|
|
public void OnDrop(PointerEventData eventData)
|
|
{
|
|
if (eventData == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameObject pointerDrag = eventData.pointerDrag;
|
|
if (pointerDrag == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
RepoItem repoItem = pointerDrag.GetComponent<RepoItem>();
|
|
if (repoItem == null)
|
|
{
|
|
repoItem = pointerDrag.GetComponentInParent<RepoItem>();
|
|
}
|
|
|
|
if (repoItem == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
bool assigned = TryAssignItem(repoItem.Context);
|
|
repoItem.SetDropResult(assigned);
|
|
}
|
|
|
|
private CombineSlotItem FindSlot(TowerCompSlotType slotType)
|
|
{
|
|
if (_slots == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
for (int i = 0; i < _slots.Length; i++)
|
|
{
|
|
CombineSlotItem slot = _slots[i];
|
|
if (slot != null && slot.AcceptSlotType == slotType)
|
|
{
|
|
return slot;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private bool TryGetBoundItemId(TowerCompSlotType slotType, out long boundItemId)
|
|
{
|
|
boundItemId = 0;
|
|
CombineSlotItem slot = FindSlot(slotType);
|
|
if (slot == null || !slot.HasItem || slot.BoundItemId <= 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
boundItemId = slot.BoundItemId;
|
|
return true;
|
|
}
|
|
}
|
|
}
|