diff --git a/MrBigsock/Assets/Code/UI/ItemSlot.cs b/MrBigsock/Assets/Code/UI/ItemSlot.cs index d4cbdcd1dc3120f3a1e7e43423c6088fc9f7dc15..bce1dbc3cfd661a46190c61120e50441be478c3e 100644 --- a/MrBigsock/Assets/Code/UI/ItemSlot.cs +++ b/MrBigsock/Assets/Code/UI/ItemSlot.cs @@ -14,48 +14,57 @@ namespace BigSock.UI public ItemPref item; public Inventory inventory; + // Indicates if the special update code has ran yet. bool firstRan = false; public void Start() { + // Set the change indicator to false. transform.hasChanged = false; - if(transform.hasChanged) { - //Debug.Log($"[ItemSlot.Start({GetInstanceID()})] {transform.position}"); - - } } public void Update() { - // If it hasn't ran before, and the position has changed, update the ItemPref. + /* + If it hasn't ran before, and the position has changed, update the ItemPref. + - This is to move the item to the item slot when it starts + - This has to be this way because the item slot is hoisted in the corner until this point. + */ if(!firstRan && transform.hasChanged) { transform.hasChanged = false; firstRan = true; - if(item != null) { - //Debug.Log($"[ItemSlot.Update({GetInstanceID()})] Item: {item?.transform?.position}, Slot: {transform.position}"); + if(item != null) item.transform.position = transform.position; - } + } } - public void OnDrop(PointerEventData eventData) - { - if (eventData.pointerDrag != null) - { - var itemPref = eventData.pointerDrag.GetComponent<ItemPref>(); - if (itemPref != null) - { - var didMove = inventory.MoveItem(itemPref.item, itemPref.itemSlot.inventoryType, - itemPref.itemSlot.position, inventoryType, position); - if (didMove) - { - itemPref.itemSlot.item = null; - itemPref.itemSlot = this; - item = itemPref; - eventData.pointerDrag.transform.position = transform.position; - itemPref.position = null; - } - } - } + /* + When the user drops an item on this slot, try to move them + */ + public void OnDrop(PointerEventData eventData) { + // Get the item prefab that was dropped. + var itemPref = eventData?.pointerDrag?.GetComponent<ItemPref>(); + + // Cancel if we didn't move an item prefab, or it was empty. + if(itemPref?.item == null) return; + + // Try to move the item in the inventory. + var didMove = inventory.MoveItem( + itemPref.item, itemPref.itemSlot.inventoryType, + itemPref.itemSlot.position, inventoryType, + position + ); + + // If we successfully moved the item in the inventory: update the gui. + if (didMove) { + itemPref.itemSlot.item = null; // Remove this from the other slot. + itemPref.itemSlot = this; // Make this its new slot. + item = itemPref; // Make it the item in this slot. + // Place the item centered over this slot. + eventData.pointerDrag.transform.position = transform.position; + itemPref.position = null; + } + } } }