Skip to content

Commit

Permalink
Feat: items now sync with ui and inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdelahunt committed Nov 12, 2021
1 parent 6885402 commit 2198f13
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 24 deletions.
6 changes: 5 additions & 1 deletion Assets/Prefabs/InventoryItem.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ GameObject:
- component: {fileID: 6195981408798853127}
m_Layer: 5
m_Name: InventoryItem
m_TagString: Untagged
m_TagString: InventoryItem
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
Expand Down Expand Up @@ -224,6 +224,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: df6ce7a69909db74abdeea33fadaf46a, type: 3}
m_Name:
m_EditorClassIdentifier:
ItemStack:
ItemId: -1
Size: 0
ItemSlot: {fileID: 0}
count: {fileID: 6195981407214780320}
onDragAlpha: 1
--- !u!225 &6195981408798853127
Expand Down
12 changes: 11 additions & 1 deletion Assets/Scripts/Inventories/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ public ItemStack RemoveFromItemStack(int slotIndex, int amount = 1)
}

stack.Size -= amount;

var toReturn = new ItemStack(stack.ItemId, amount);

if(stack.Size == 0)
ItemStacks[slotIndex].Empty();
return new ItemStack(stack.ItemId, amount);

return toReturn;
}

return new ItemStack();
Expand Down Expand Up @@ -124,5 +128,11 @@ public int NextEmptySlot()

return -1;
}

public void ClearSlot(int slotNumber)
{
ItemStacks[slotNumber] = new ItemStack();
IsDirty = true;
}
}
}
29 changes: 20 additions & 9 deletions Assets/Scripts/Inventories/InventoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;

public class InventoryItem : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
{
public ItemStack ItemStack;
public ItemSlot ItemSlot;

[SerializeField] private TMP_Text count = null;
[SerializeField] private float onDragAlpha = 1.0f;

private ItemSlot lastItemSlot;
private RectTransform rectTransform = null;
private Canvas canvas = null;
private CanvasGroup canvasGroup = null;
private Image image = null;
private ItemStack itemStack;

private void Awake()
{
Expand All @@ -28,16 +32,10 @@ private void Awake()
image = GetComponent<Image>();
}

public void Init(ItemStack itemStack)
{
this.itemStack = itemStack;
Refresh();
}

public void Refresh()
{
count.text = itemStack.Size.ToString();
image.sprite = Register.GetItemById(itemStack.ItemId).sprite;
count.text = ItemStack.Size.ToString();
image.sprite = Register.GetItemById(ItemStack.ItemId).sprite;
}

public void OnPointerDown(PointerEventData eventData)
Expand All @@ -54,11 +52,24 @@ public void OnBeginDrag(PointerEventData eventData)
{
canvasGroup.blocksRaycasts = false;
canvasGroup.alpha = onDragAlpha;

Debug.Log("drag start");
ItemSlot.ItemPopped();
lastItemSlot = ItemSlot;
ItemSlot = null;
}

public void OnEndDrag(PointerEventData eventData)
{
if (ItemSlot == null)
{
ItemSlot = lastItemSlot;
ItemSlot.ItemPlaced(this);
}
else
{
lastItemSlot = null;
}
canvasGroup.blocksRaycasts = true;
canvasGroup.alpha = 1.0f;
Debug.Log("drag end");
Expand Down
10 changes: 10 additions & 0 deletions Assets/Scripts/Inventories/InventoryUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Items;

namespace Inventories
{
public interface InventoryUI
{
void ItemPopped(int itemSlot);
void ItemPlaced(int itemSlot, ItemStack itemStack);
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Inventories/InventoryUI.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 41 additions & 8 deletions Assets/Scripts/Inventories/ItemSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,51 @@
using System.Collections.Generic;
using Eiram;
using Events;
using Inventories;
using Items;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;

public class ItemSlot : MonoBehaviour, IDropHandler
{
public InventoryItem inventoryItem;
public int slotNumber = -1;
public InventoryUI InventoryUI;

private RectTransform rectTransform;
private InventoryItem inventoryItem;

private void Awake()
{
rectTransform = GetComponent<RectTransform>();
}

public void Init(InventoryItem inventoryItem)
public void Refresh()
{
if(inventoryItem != null)
inventoryItem.Refresh();
}

public void ItemPopped()
{
inventoryItem = null;
InventoryUI.ItemPopped(slotNumber);
}

public void ItemPlaced(InventoryItem inventoryItem)
{
this.inventoryItem = inventoryItem;
InventoryUI.ItemPlaced(slotNumber, inventoryItem.ItemStack);
AlignInventoryItem();
}

public void Refresh()
public void Clear()
{
if(inventoryItem != null)
inventoryItem.Refresh();
if (inventoryItem != null)
{
Destroy(inventoryItem.gameObject);
inventoryItem = null;
}
}

public bool IsEmpty()
Expand All @@ -35,11 +57,22 @@ public bool IsEmpty()

public void OnDrop(PointerEventData eventData)
{
var otherTransform = eventData.pointerDrag.GetComponent<RectTransform>();
if (eventData == null || eventData.pointerDrag == null) return;

if (!eventData.pointerDrag.CompareTag("InventoryItem")) return;


inventoryItem = eventData.pointerDrag.GetComponent<InventoryItem>();
inventoryItem.ItemSlot = this;
InventoryUI.ItemPlaced(slotNumber, inventoryItem.ItemStack);
AlignInventoryItem();
}

public void AlignInventoryItem()
{
var otherTransform = inventoryItem.GetComponent<RectTransform>();
otherTransform.SetParent(rectTransform);
//otherTransform.anchoredPosition = rectTransform.anchoredPosition;
otherTransform.anchoredPosition = new Vector3(0.0f, 0.0f, 0.0f);
inventoryItem = eventData.pointerDrag.GetComponent<InventoryItem>();
}

}
33 changes: 28 additions & 5 deletions Assets/Scripts/Inventories/PlayerInventoryUI.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;
using Events;
using Items;
using Players;
using Registers;
using UnityEngine;
using UnityEngine.UI;

namespace Inventories
{
public class PlayerInventoryUI : MonoBehaviour
public class PlayerInventoryUI : MonoBehaviour, InventoryUI
{
[SerializeField] private Vector3 pointerOffset = new Vector3();
[SerializeField] private GameObject slotPrefab = null;
Expand Down Expand Up @@ -51,12 +52,26 @@ private void OnDestroy()
EiramEvents.SelectedSlotChangedEvent -= OnSelectedSlotChanged;
}

public void ItemPopped(int slotNumber)
{
playerInventory.ClearSlot(slotNumber);
}

public void ItemPlaced(int itemSlot, ItemStack itemStack)
{
playerInventory.ItemStacks[itemSlot] = itemStack;
playerInventory.IsDirty = true;
}

private void GenerateUI()
{
for (int i = 0; i < PlayerInventory.Slots; i++)
{
var go = Instantiate(slotPrefab, contentTransform);
itemSlots.Add(go.GetComponent<ItemSlot>());
var itemSlot = go.GetComponent<ItemSlot>();
itemSlot.slotNumber = i;
itemSlot.InventoryUI = this;
itemSlots.Add(itemSlot);
}

}
Expand Down Expand Up @@ -90,9 +105,17 @@ private void Refresh()
{
var inventoryItemGo = Instantiate(inventoryItemPrefab, itemSlots[i].transform);
var inventoryItem = inventoryItemGo.GetComponent<InventoryItem>();
inventoryItem.Init(itemStack);
itemSlot.Init(inventoryItem);
}

inventoryItem.ItemStack = itemStack;
inventoryItem.ItemSlot = itemSlot;

itemSlot.inventoryItem = inventoryItem;
}

if (itemStack.IsEmpty())
{
itemSlot.Clear();
}

itemSlot.Refresh();
}
Expand Down

0 comments on commit 2198f13

Please sign in to comment.