From 11c39c2bb64817c5a4e4d05cb963dea42515eb08 Mon Sep 17 00:00:00 2001
From: Ny Bruker <robinhs@stud.ntnu.no>
Date: Sat, 29 Oct 2022 13:20:09 +0200
Subject: [PATCH] Improved commenting and code readability.

---
 MrBigsock/Assets/Code/UI/ItemSlot.cs | 63 ++++++++++++++++------------
 1 file changed, 36 insertions(+), 27 deletions(-)

diff --git a/MrBigsock/Assets/Code/UI/ItemSlot.cs b/MrBigsock/Assets/Code/UI/ItemSlot.cs
index d4cbdcd1..bce1dbc3 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;
+			}
+
 		}
 	}
 }
-- 
GitLab