mirror of
https://github.com/DerTyp7/harvestdale-unity.git
synced 2025-10-30 04:57:09 +01:00
inventory ui
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
public class Inventory : MonoBehaviour
|
||||
{
|
||||
public static Action OnPlayerInventoryChanged;
|
||||
|
||||
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
|
||||
|
||||
@@ -27,6 +31,11 @@ public class Inventory : MonoBehaviour
|
||||
// exit the loop if all items have been added
|
||||
if (remainingCount == 0)
|
||||
{
|
||||
if (isPlayerInventory)
|
||||
{
|
||||
OnPlayerInventoryChanged?.Invoke();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -42,6 +51,10 @@ public class Inventory : MonoBehaviour
|
||||
}
|
||||
|
||||
// return the quantity of items which could not be added
|
||||
if (isPlayerInventory)
|
||||
{
|
||||
OnPlayerInventoryChanged?.Invoke();
|
||||
}
|
||||
return remainingCount;
|
||||
}
|
||||
|
||||
@@ -71,12 +84,20 @@ public class Inventory : MonoBehaviour
|
||||
// exit the loop if all items have been removed
|
||||
if (remainingCount == 0)
|
||||
{
|
||||
if (isPlayerInventory)
|
||||
{
|
||||
OnPlayerInventoryChanged?.Invoke();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return the quantity of items which could not be removed
|
||||
if (isPlayerInventory)
|
||||
{
|
||||
OnPlayerInventoryChanged?.Invoke();
|
||||
}
|
||||
return remainingCount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ public class Item : ScriptableObject
|
||||
public string uuid;
|
||||
public string itemName;
|
||||
|
||||
public Sprite sprite;
|
||||
public bool stackable = true;
|
||||
public int maxStackSize = 100;
|
||||
|
||||
|
||||
@@ -7,42 +7,60 @@ public class PlayerController : MonoBehaviour
|
||||
|
||||
public Item testItem1;
|
||||
public Item testItem2;
|
||||
|
||||
[SerializeField] private int hotbarSlotCount = 9;
|
||||
[SerializeField] private int activeHotbarSlot = 0;
|
||||
[SerializeField] private Inventory playerInventory;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
playerInventory = GetComponent<Inventory>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
Inventory inventory = GetComponent<Inventory>();
|
||||
float scroll = Input.GetAxis("Mouse ScrollWheel");
|
||||
if (scroll != 0)
|
||||
{
|
||||
activeHotbarSlot += (int)Mathf.Sign(scroll);
|
||||
if (activeHotbarSlot < 0)
|
||||
{
|
||||
activeHotbarSlot = hotbarSlotCount - 1;
|
||||
}
|
||||
else if (activeHotbarSlot >= hotbarSlotCount)
|
||||
{
|
||||
activeHotbarSlot = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//! DEBUG
|
||||
if (Input.GetKeyDown(KeyCode.U))
|
||||
{
|
||||
// add 5 apples
|
||||
int remainingCount = inventory.Add(testItem1, 5);
|
||||
Debug.Log("Added " + (5 - remainingCount) + " apples to inventory.");
|
||||
int remainingCount = playerInventory.Add(testItem1, 5);
|
||||
Debug.Log("Added " + (5 - remainingCount) + " apples to playerInventory.");
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.I))
|
||||
{ // add 15 more apples
|
||||
int remainingCount = inventory.Add(testItem1, 15);
|
||||
Debug.Log("Added " + (15 - remainingCount) + " apples to inventory.");
|
||||
int remainingCount = playerInventory.Add(testItem1, 15);
|
||||
Debug.Log("Added " + (15 - remainingCount) + " apples to playerInventory.");
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.O))
|
||||
{
|
||||
// add 10 bananas
|
||||
int remainingCount = inventory.Add(testItem2, 10);
|
||||
Debug.Log("Added " + (10 - remainingCount) + " bananas to inventory.");
|
||||
int remainingCount = playerInventory.Add(testItem2, 10);
|
||||
Debug.Log("Added " + (10 - remainingCount) + " bananas to playerInventory.");
|
||||
}
|
||||
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.J))
|
||||
{
|
||||
int count = 3;
|
||||
int applesRemaining = inventory.Remove(testItem1, count);
|
||||
int applesRemaining = playerInventory.Remove(testItem1, count);
|
||||
Debug.Log("Removed " + (count - applesRemaining) + " apples.");
|
||||
|
||||
}
|
||||
@@ -50,7 +68,7 @@ public class PlayerController : MonoBehaviour
|
||||
if (Input.GetKeyDown(KeyCode.K))
|
||||
{
|
||||
int count = 5;
|
||||
int bananasRemaining = inventory.Remove(testItem2, count);
|
||||
int bananasRemaining = playerInventory.Remove(testItem2, count);
|
||||
Debug.Log("Removed " + (count - bananasRemaining) + " bananas.");
|
||||
|
||||
}
|
||||
|
||||
8
Assets/Scripts/UI.meta
Normal file
8
Assets/Scripts/UI.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c0b87d7e43eb42447bdb3434f854a8bb
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
50
Assets/Scripts/UI/InventoryUI.cs
Normal file
50
Assets/Scripts/UI/InventoryUI.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class InventoryUI : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject slotPrefab;
|
||||
[SerializeField] private GameObject slotListObj;
|
||||
[SerializeField] private List<SlotUI> slotUIList;
|
||||
|
||||
private Inventory playerInventory;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
|
||||
Inventory.OnPlayerInventoryChanged += UpdateSlots;
|
||||
CreateSlots();
|
||||
UpdateSlots();
|
||||
|
||||
}
|
||||
|
||||
private void CreateSlots()
|
||||
{
|
||||
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
|
||||
|
||||
for (int i = 0; i < playerInventory.maxSlots; i++)
|
||||
{
|
||||
slotUIList.Add(Instantiate(slotPrefab, Vector3.zero, Quaternion.identity, slotListObj.transform).GetComponent<SlotUI>());
|
||||
}
|
||||
}
|
||||
private void UpdateSlots()
|
||||
{
|
||||
Debug.Log("Update slots");
|
||||
int i = 0;
|
||||
foreach (SlotUI slotUi in slotUIList)
|
||||
{
|
||||
if (i < playerInventory.items.Count)
|
||||
{
|
||||
slotUi.SetInventoryItem(playerInventory.items[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
slotUi.SetInventoryItem(null);
|
||||
}
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/UI/InventoryUI.cs.meta
Normal file
11
Assets/Scripts/UI/InventoryUI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9269c8c372de3f5498d2af345ee9c855
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
44
Assets/Scripts/UI/SlotUI.cs
Normal file
44
Assets/Scripts/UI/SlotUI.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class SlotUI : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private InventoryItem inventoryItem;
|
||||
[SerializeField] private Image itemSpriteImage;
|
||||
[SerializeField] private TMPro.TextMeshProUGUI quantityText;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
UpdateSlot();
|
||||
}
|
||||
public void SetInventoryItem(InventoryItem newInventoryItem)
|
||||
{
|
||||
inventoryItem = newInventoryItem;
|
||||
UpdateSlot();
|
||||
}
|
||||
|
||||
private void UpdateSlot()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
quantityText.SetText(inventoryItem.count != 0 ? inventoryItem.count.ToString() : "");
|
||||
}
|
||||
else
|
||||
{
|
||||
itemSpriteImage.sprite = null;
|
||||
quantityText.SetText("");
|
||||
itemSpriteImage.color = new Color(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/UI/SlotUI.cs.meta
Normal file
11
Assets/Scripts/UI/SlotUI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6c881951842cc4e4799994f5eb91dd90
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user