add basic inventory, fading walls

This commit is contained in:
2026-02-17 15:26:34 -06:00
parent 6baa6956bf
commit 7123238daa
30 changed files with 6594 additions and 255 deletions
+220
View File
@@ -0,0 +1,220 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public ItemSlot[] itemSlots;
public BackPackType ownedBackpackType;
InventorySlotUI inventoryDisplay;
private bool showingInventory = false;
private void Update()
{
if (inventoryDisplay == null)
{
inventoryDisplay = GameObject.FindWithTag("InventorySlots").GetComponent<InventorySlotUI>();
inventoryDisplay.gameObject.SetActive(false);
}
if (Input.GetKeyDown(KeyCode.Tab))
{
if (showingInventory)
{
inventoryDisplay.HideSlots();
Time.timeScale = 1;
}
else
{
inventoryDisplay.ShowSlots();
Time.timeScale = 0;
}
showingInventory = !showingInventory;
}
}
// we'll set this up to only try to add one item to the inventory at a time for now
// there won't be stacked items in the wild for right now
public bool AddItem(Item item, int quantity)
{
// first try to stack
for (int i = 0; i < itemSlots.Length; i++)
{
if (itemSlots[i].item == item.id && itemSlots[i].quantity < item.maxStack)
{
itemSlots[i].quantity += 1;
return true;
}
}
// no stack to add to, try to find an empty slot
for (int i = 0; i < itemSlots.Length; i++)
{
if (itemSlots[i].item == ItemIdEnum.NONE)
{
itemSlots[i] = new ItemSlot();
itemSlots[i].quantity = 1;
itemSlots[i].item = item.id;
return true;
}
}
// no open slots, no open stacks, we can't add the item
return false;
}
public bool RemoveItemQuantity(ItemIdEnum itemId, int quantity)
{
if (GetItemQuantity(itemId) >= quantity)
{
int currentQuantity = quantity;
for (int i = 0; i < itemSlots.Length; i++)
{
if (itemSlots[i].item == itemId)
{
// we are trying to remove less items than what exists in the current
// stack, so just subtract from the stack
if (currentQuantity < itemSlots[i].quantity)
{
itemSlots[i].quantity -= currentQuantity;
break;
}
else if (currentQuantity == itemSlots[i].quantity)
{
// we have exactly enough in the current slot, so nullify it and
// exit
itemSlots[i].item = ItemIdEnum.NONE;
break;
}
else
{
// we don't have enough in the stack, but we DO have enough in our
// inventory. nullify the stack, subtract that number from the current
// count, and move to the next item
currentQuantity -= itemSlots[i].quantity;
itemSlots[i].item = ItemIdEnum.NONE;
}
}
}
return true;
}
else
{
return false;
}
}
public int GetItemQuantity(ItemIdEnum itemId)
{
int itemCount = 0;
foreach (ItemSlot slot in itemSlots)
{
if (slot.item == itemId)
{
itemCount += slot.quantity;
}
}
return itemCount;
}
private int GetItemSlotNum()
{
switch (ownedBackpackType)
{
case BackPackType.Pockets:
return 8;
case BackPackType.SmallBag:
return 16;
case BackPackType.LargeBag:
return 32;
}
return 0;
}
#region Inventory Save/Load
public void SaveInventory(ref InventorySaveData data)
{
data.itemSlots = itemSlots;
data.ownedBackpack = ownedBackpackType;
}
public void LoadInventory(InventorySaveData data)
{
ownedBackpackType = data.ownedBackpack;
if (data.itemSlots != null && data.itemSlots.Length > 0)
{
itemSlots = data.itemSlots;
// list is somehow corrupted, fill in the rest of the slots
if (itemSlots.Length < GetItemSlotNum())
{
ItemSlot[] newSlots = new ItemSlot[GetItemSlotNum()];
for (int i = 0; i < itemSlots.Length; i++)
{
if (itemSlots[i] != null)
newSlots[i] = itemSlots[i];
else
newSlots[i] = new ItemSlot();
}
itemSlots = newSlots;
}
}
else if (data.itemSlots != null && data.itemSlots.Length > GetItemSlotNum())
{
// list is bigger than it should be, generate new slots of the proper size
// and save just the items in the slots we SHOULD have. (shouldn't ever happen)
ItemSlot[] newSlots = new ItemSlot[GetItemSlotNum()];
for (int i = 0; i < newSlots.Length; i++)
{
if (itemSlots[i] != null)
newSlots[i] = itemSlots[i];
else
newSlots[i] = new ItemSlot();
}
itemSlots = newSlots;
}
else
{
itemSlots = new ItemSlot[GetItemSlotNum()];
for (int i = 0; i < itemSlots.Length; i++)
{
itemSlots[i] = new ItemSlot();
}
}
}
#endregion
}
public enum BackPackType
{
Pockets = 0,
SmallBag = 1,
LargeBag = 2
}
[System.Serializable]
public class ItemSlot
{
public int quantity;
public ItemIdEnum item;
}
[System.Serializable]
public struct InventorySaveData
{
public ItemSlot[] itemSlots;
public BackPackType ownedBackpack;
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 40e5d5183cb59cf4d8603dca70c1a9b5
@@ -0,0 +1,42 @@
using UnityEngine;
using UnityEngine.UI;
// This is the container for the inventory UI slots
public class InventorySlotUI : MonoBehaviour
{
public ItemSlotUI[] slots;
ItemNameUI itemNameUI;
ItemDescriptionUI itemDescriptionUI;
private void Start()
{
slots = GetComponentsInChildren<ItemSlotUI>();
itemNameUI = GetComponentInChildren<ItemNameUI>();
itemDescriptionUI = GetComponentInChildren<ItemDescriptionUI>();
}
public void ShowSlots()
{
gameObject.SetActive(true);
for (int i = 0; i < slots.Length; i++)
{
if (GameManager.Instance.Inventory.itemSlots[i] != null)
{
slots[i].SetItem(GameManager.Instance.Inventory.itemSlots[i]);
}
}
foreach (ItemSlotUI slot in slots)
{
slot.LoadImage(itemNameUI, itemDescriptionUI);
}
}
public void HideSlots()
{
itemNameUI.nameText = "";
itemDescriptionUI.descriptionText = "";
gameObject.SetActive(false);
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6035a8fed1049624f8d8077553b85847
@@ -0,0 +1,20 @@
using TMPro;
using UnityEngine;
public class ItemDescriptionUI : MonoBehaviour
{
public string descriptionText = "";
TextMeshProUGUI text;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
text = GetComponent<TextMeshProUGUI>();
}
// Update is called once per frame
void Update()
{
text.text = descriptionText;
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: bd581500fc7211347bbe165c6ea8d780
+20
View File
@@ -0,0 +1,20 @@
using TMPro;
using UnityEngine;
public class ItemNameUI : MonoBehaviour
{
public string nameText = "";
TextMeshProUGUI text;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
text = GetComponent<TextMeshProUGUI>();
}
// Update is called once per frame
void Update()
{
text.text = nameText;
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 798c4437c91e97b43893dcdfeb918617
+53
View File
@@ -0,0 +1,53 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ItemSlotUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
// Kind of confusing here... ItemSlot refers to the class that is the DATA for a
// "slot". It is used for serialization and keeping track of the inventory in data.
// ItemSlot simply has an item ID and a quantity. ItemSlotUI is the visual represenation of this.
private ItemSlot slotData;
private int quantity;
private Image image;
private Item itemObj;
// (I hate this approach) passed in from InventorySlotUI
private ItemNameUI itemNameUI;
private ItemDescriptionUI itemDescriptionUI;
private void Start()
{
image = GetComponentsInChildren<Image>()[1]; //ignore parent
}
public void LoadImage(ItemNameUI itemNameUI, ItemDescriptionUI itemDescriptionUI)
{
if (this.itemNameUI == null) this.itemNameUI = itemNameUI;
if (this.itemDescriptionUI == null) this.itemDescriptionUI = itemDescriptionUI;
itemObj = new Item(slotData.item);
image.sprite = Resources.Load<Sprite>(itemObj.itemImagePath);
}
public void SetItem(ItemSlot itemSlot)
{
this.slotData = itemSlot;
}
public void OnPointerEnter(PointerEventData pointerEventData)
{
if (itemObj != null && itemObj.id != ItemIdEnum.NONE)
{
itemNameUI.nameText = itemObj.itemName;
itemDescriptionUI.descriptionText = itemObj.description;
}
}
public void OnPointerExit(PointerEventData pointerEventData)
{
itemNameUI.nameText = "";
itemDescriptionUI.descriptionText = "";
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 26900fa2242c1514fb991ab59cf5f883