Skip to content

Commit

Permalink
Merge pull request #1 from yuzhengwen/Features
Browse files Browse the repository at this point in the history
Major refactoring & use item feature
  • Loading branch information
yuzhengwen committed Mar 8, 2024
2 parents 6b5ce39 + 7a3479c commit b9233ea
Show file tree
Hide file tree
Showing 30 changed files with 1,376 additions and 973 deletions.
7 changes: 7 additions & 0 deletions Documentation.md.meta

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

7 changes: 7 additions & 0 deletions README.md.meta

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

8 changes: 8 additions & 0 deletions Runtime/CollectionSystem.meta

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

14 changes: 4 additions & 10 deletions Runtime/Collector.cs → Runtime/CollectionSystem/Collector.cs
Expand Up @@ -5,20 +5,14 @@
[RequireComponent(typeof(Inventory))]
public class Collector : MonoBehaviour
{
private Inventory inventory;
public Inventory Inventory { get; private set; }
[SerializeField] private UI_Inventory uiInventory;

public event Action<ItemDataSO> OnItemCollected;
public Action<ItemDataSO> OnItemCollected;
private void Start()
{
inventory = GetComponent<Inventory>();
uiInventory.AssignInventory(inventory);

}
public void AddCollectibleToInventory(ItemDataSO data)
{
inventory.AddItem(data, 1);
OnItemCollected?.Invoke(data);
Inventory = GetComponent<Inventory>();
uiInventory.AssignInventory(Inventory);
}

private void OnTriggerEnter2D(Collider2D collision)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 9 additions & 1 deletion Runtime/Inventory.cs
Expand Up @@ -81,7 +81,7 @@ public void AddItem(ItemDataSO itemData, int amount)
}
private InventorySlot AddNewItemInternal(ItemDataSO itemData, int amount)
{
return GetNextEmptySlot().SetItem(itemData, amount);
return GetNextEmptySlot().SetItem(itemData, amount, ItemDB.GetItem(itemData.id, gameObject));
}
public void RemoveItem(ItemDataSO itemData, int amount)
{
Expand Down Expand Up @@ -178,5 +178,13 @@ public bool IsEmpty()
{
return items.All(item => !item.IsOccupied());
}
public bool Contains(ItemDataSO itemData)
{
return itemHashSet.Contains(itemData);
}
public void SwapItems(InventorySlot slot1, InventorySlot slot2)
{
slot1.Swap(slot2);
}
}
}
21 changes: 18 additions & 3 deletions Runtime/InventorySlot.cs
@@ -1,4 +1,5 @@
using System;
using UnityEngine;

namespace InventorySystem
{
Expand All @@ -7,6 +8,7 @@ public class InventorySlot
{
public ItemDataSO itemData;
public int stackSize;
public BaseInventoryItem item;

public event Action<int, int> OnStackChanged;
public event Action<InventorySlot> OnItemChanged;
Expand All @@ -16,6 +18,7 @@ public void ClearSlot()
{
itemData = null;
stackSize = 0;
item = null;
OnSlotCleared?.Invoke();
}
public InventorySlot SetItem(InventorySlot slot)
Expand All @@ -24,14 +27,15 @@ public InventorySlot SetItem(InventorySlot slot)
{
ClearSlot(); return this;
}
return SetItem(slot.itemData, slot.stackSize);
return SetItem(slot.itemData, slot.stackSize, slot.item);
}
public InventorySlot SetItem(ItemDataSO itemData, int stackSize)
public InventorySlot SetItem(ItemDataSO itemData, int stackSize, BaseInventoryItem itemObj = null)
{
if (itemData == null || stackSize <= 0)
ClearSlot();
this.itemData = itemData;
this.stackSize = stackSize;
this.item = itemObj;
OnItemChanged?.Invoke(this);
return this;
}
Expand Down Expand Up @@ -68,7 +72,7 @@ public int RemoveFromStack(int amount)
{
stackSize -= amount;
int excess = 0;
if (stackSize < 0)
if (stackSize <= 0)
{
excess = -stackSize;
ClearSlot();
Expand Down Expand Up @@ -104,5 +108,16 @@ public void Swap(InventorySlot slot)
slot.SetItem(this);
SetItem(temp);
}

public void UseItem()
{
Debug.Log("Use Item");
(item as IUseable)?.Use(this);
}

public bool IsSameItem(InventorySlot slot)
{
return itemData == slot.itemData;
}
}
}
12 changes: 0 additions & 12 deletions Runtime/ItemData/AllItems.cs

This file was deleted.

22 changes: 22 additions & 0 deletions Runtime/ItemData/BaseInventoryItem.cs
@@ -0,0 +1,22 @@
using InventorySystem;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseInventoryItem
{

}
public interface IUseable
{
void Use(InventorySlot slot);
}
public interface IEquippable
{
void Equip(InventorySlot slot);
void Unequip(InventorySlot slot);
}
public interface IDroppable
{
void Drop();
}

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

11 changes: 0 additions & 11 deletions Runtime/ItemData/CollectibleItem.cs

This file was deleted.

17 changes: 17 additions & 0 deletions Runtime/ItemData/CollectibleObject.cs
@@ -0,0 +1,17 @@
using InventorySystem;
using UnityEngine;

public class CollectibleObject : MonoBehaviour, ICollectible
{
public ItemDataSO itemData;
public virtual void Collect(Collector collector)
{
AddToInventory(collector);
Destroy(gameObject);
}
protected virtual void AddToInventory(Collector collector)
{
collector.Inventory.AddItem(itemData, 1);
collector.OnItemCollected?.Invoke(itemData);
}
}
File renamed without changes.
16 changes: 16 additions & 0 deletions Runtime/ItemData/CrystalItem.cs
@@ -0,0 +1,16 @@
using InventorySystem;
using UnityEngine;

public class CrystalItem : BaseInventoryItem, IUseable
{
private GameObject owner;
public CrystalItem(GameObject owner)
{
this.owner = owner;
}
public void Use(InventorySlot slot)
{
slot.RemoveFromStack(1);
Debug.Log("Crystal used");
}
}
11 changes: 11 additions & 0 deletions Runtime/ItemData/CrystalItem.cs.meta

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

27 changes: 27 additions & 0 deletions Runtime/ItemData/ItemDB.cs
@@ -0,0 +1,27 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemDB : ScriptableObject
{
public static ItemDataSO[] allItems;

public const int COIN = 0, CRYSTAL = 1;

public static BaseInventoryItem GetItem(int id, GameObject owner)
{
switch (id)
{
case CRYSTAL:
return new CrystalItem(owner);
default:
return null;
}
}
public static ItemDataSO GetItemData(int id)
{
return allItems[id];
}
}

File renamed without changes.
3 changes: 2 additions & 1 deletion Runtime/ItemData/ItemDataSO.cs
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Expand All @@ -9,4 +10,4 @@ public class ItemDataSO : ScriptableObject
public Sprite sprite;
public int id;
public int maxStackSize;
}
}
8 changes: 8 additions & 0 deletions Runtime/UI.meta

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

27 changes: 27 additions & 0 deletions Runtime/UI/MouseDragItem.cs
@@ -0,0 +1,27 @@
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace InventorySystem
{
public class MouseDragItem : MonoBehaviour
{
private Image img;
private TextMeshProUGUI stackDisplay;
public UI_InventorySlot from;
private void Awake()
{
img = GetComponentInChildren<Image>();
stackDisplay = GetComponentInChildren<TextMeshProUGUI>();
}
public void SetItem(UI_InventorySlot from)
{
this.from = from;
img.sprite = from.GetItem().itemData.sprite;
int stackSize = from.GetItem().stackSize;
stackDisplay.text = stackSize > 1 ? stackSize.ToString() : "";
}
}
}
11 changes: 11 additions & 0 deletions Runtime/UI/MouseDragItem.cs.meta

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

File renamed without changes.
File renamed without changes.

0 comments on commit b9233ea

Please sign in to comment.