draggable items

This commit is contained in:
Janis
2023-02-22 13:39:32 +01:00
parent 5773aa0af7
commit 13902bc589
6 changed files with 218 additions and 29 deletions

View File

@@ -7,7 +7,17 @@ public class Inventory : MonoBehaviour
public bool isPlayerInventory = false;
public int maxSlots = 20; // maximum number of slots in the inventory
public List<InventoryItem> items = new List<InventoryItem>(); // list of items in the inventory
public InventoryItem[] items; // list of items in the inventory
private void Awake()
{
items = new InventoryItem[maxSlots];
for (int i = 0; i < maxSlots; i++)
{
items[i] = null;
}
}
// adds an item to the inventory, returns the quantity of items which could not be added
public int Add(Item item, int count)
@@ -17,10 +27,12 @@ public class Inventory : MonoBehaviour
// check if the item is stackable
if (item.stackable)
{
// look for an existing stack of the same item in the inventory
foreach (InventoryItem invItem in items)
for (int i = 0; i < items.Length; i++)
{
if (invItem.item == item && invItem.count < item.maxStackSize)
InventoryItem invItem = items[i];
if (invItem != null && invItem.item == item && invItem.count < item.maxStackSize)
{
// add as many items as possible to the stack
int space = item.maxStackSize - invItem.count;
@@ -43,11 +55,30 @@ public class Inventory : MonoBehaviour
}
// if there is still remaining count, add new stacks of the item
while (remainingCount > 0 && items.Count < maxSlots)
for (int i = 0; i < items.Length; i++)
{
int toAdd = Mathf.Min(item.maxStackSize, remainingCount);
items.Add(new InventoryItem(item, toAdd));
remainingCount -= toAdd;
InventoryItem invItem = items[i];
Debug.Log(invItem);
if (invItem.item == null)
{
int toAdd = Mathf.Min(item.maxStackSize, remainingCount);
items[i] = new InventoryItem(item, toAdd);
remainingCount -= toAdd;
// exit the loop if all items have been added
if (remainingCount == 0)
{
if (isPlayerInventory)
{
OnPlayerInventoryChanged?.Invoke();
}
return 0;
}
}
}
// return the quantity of items which could not be added
@@ -55,6 +86,7 @@ public class Inventory : MonoBehaviour
{
OnPlayerInventoryChanged?.Invoke();
}
return remainingCount;
}
@@ -65,10 +97,10 @@ public class Inventory : MonoBehaviour
int remainingCount = count;
// look for an existing stack of the item in the inventory
for (int i = 0; i < items.Count; i++)
for (int i = 0; i < items.Length; i++)
{
InventoryItem invItem = items[i];
if (invItem.item == item)
if (invItem != null && invItem.item == item)
{
// remove as many items as possible from the stack
int toRemove = Mathf.Min(invItem.count, remainingCount);
@@ -78,7 +110,7 @@ public class Inventory : MonoBehaviour
// remove the stack if it's now empty
if (invItem.count == 0)
{
items.RemoveAt(i);
items[i] = null;
}
// exit the loop if all items have been removed
@@ -100,6 +132,17 @@ public class Inventory : MonoBehaviour
}
return remainingCount;
}
public void SwapItems(int index1, int index2)
{
InventoryItem temp = items[index1];
items[index1] = items[index2];
items[index2] = temp;
if (isPlayerInventory)
{
OnPlayerInventoryChanged?.Invoke();
}
}
}

View File

@@ -0,0 +1,34 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DraggableSlotContentUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public int slotIndex;
[HideInInspector] public Transform parentAfterDrag;
public Image image;
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("Start Drag");
parentAfterDrag = transform.parent;
transform.SetParent(transform.root);
transform.SetAsLastSibling();
image.raycastTarget = false;
}
public void OnDrag(PointerEventData eventData)
{
Debug.Log("Dragging");
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("End drag");
transform.SetParent(parentAfterDrag);
image.raycastTarget = true;
// invoke
Inventory.OnPlayerInventoryChanged?.Invoke();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4b88f5aa1c0fe8349953801d1f513200
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -25,7 +25,9 @@ public class InventoryUI : MonoBehaviour
for (int i = 0; i < playerInventory.maxSlots; i++)
{
slotUIList.Add(Instantiate(slotPrefab, Vector3.zero, Quaternion.identity, slotListObj.transform).GetComponent<SlotUI>());
SlotUI newSlot = Instantiate(slotPrefab, Vector3.zero, Quaternion.identity, slotListObj.transform).GetComponent<SlotUI>();
newSlot.slotIndex = i;
slotUIList.Add(newSlot);
}
}
private void UpdateSlots()
@@ -34,14 +36,9 @@ public class InventoryUI : MonoBehaviour
int i = 0;
foreach (SlotUI slotUi in slotUIList)
{
if (i < playerInventory.items.Count)
{
slotUi.SetInventoryItem(playerInventory.items[i]);
}
else
{
slotUi.SetInventoryItem(null);
}
slotUi.SetInventoryItem(playerInventory.items[i]);
i++;
}

View File

@@ -2,16 +2,21 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SlotUI : MonoBehaviour
using UnityEngine.EventSystems;
public class SlotUI : MonoBehaviour, IDropHandler
{
public int slotIndex;
[SerializeField] private GameObject contentObject;
[SerializeField] private InventoryItem inventoryItem;
[SerializeField] private Image itemSpriteImage;
[SerializeField] private TMPro.TextMeshProUGUI quantityText;
private Inventory playerInventory;
private void Start()
{
UpdateSlot();
contentObject.GetComponent<DraggableSlotContentUI>().slotIndex = slotIndex;
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
}
public void SetInventoryItem(InventoryItem newInventoryItem)
{
@@ -24,11 +29,14 @@ public class SlotUI : MonoBehaviour
if (inventoryItem != null)
{
itemSpriteImage.sprite = inventoryItem?.item?.sprite ?? null;
Debug.Log(itemSpriteImage.sprite);
if (itemSpriteImage.sprite == null)
{
itemSpriteImage.color = new Color(0, 0, 0, 0);
}
else
{
itemSpriteImage.color = new Color(255, 255, 255, 1);
}
quantityText.SetText(inventoryItem.count != 0 ? inventoryItem.count.ToString() : "");
}
@@ -41,4 +49,13 @@ public class SlotUI : MonoBehaviour
}
public void OnDrop(PointerEventData eventData)
{
Debug.Log("DROP");
DraggableSlotContentUI dropped = eventData.pointerDrag.GetComponent<DraggableSlotContentUI>();
playerInventory.SwapItems(dropped.slotIndex, slotIndex);
}
}