StellarXipher/Assets/Scripts/Inventory.cs
nlevin6 ef45838c22
Some checks failed
Build project / Build for (StandaloneWindows64, 6000.0.37f1) (push) Has been cancelled
Build project / Publish to itch.io (StandaloneLinux64) (push) Has been cancelled
Build project / Publish to itch.io (StandaloneWindows64) (push) Has been cancelled
Build project / Build for (StandaloneLinux64, 6000.0.37f1) (push) Has been cancelled
clean up code
2025-03-29 21:40:27 -04:00

85 lines
1.9 KiB
C#

using UnityEngine;
using System.Collections.Generic;
public class Inventory : MonoBehaviour
{
[Header("10 Inventory Slots")]
public Item[] slots = new Item[10];
public int currentSlot = 0;
[SerializeField] private Transform handSlot;
private GameObject currentSpawnedItem;
void Update()
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (scroll > 0f)
{
SelectPreviousSlot();
}
else if (scroll < 0f)
{
SelectNextSlot();
}
}
public void AddItem(Item newItem)
{
for (int i = 0; i < slots.Length; i++)
{
if (slots[i] == null)
{
slots[i] = newItem;
break;
}
}
}
private void SelectNextSlot()
{
currentSlot = (currentSlot + 1) % slots.Length;
EquipSlot(currentSlot);
var ui = FindObjectOfType<InventoryUI>();
if (ui != null)
{
ui.RefreshUI();
}
}
private void SelectPreviousSlot()
{
currentSlot = (currentSlot - 1 + slots.Length) % slots.Length;
EquipSlot(currentSlot);
var ui = FindObjectOfType<InventoryUI>();
if (ui != null)
{
ui.RefreshUI();
}
}
public void EquipSlot(int slotIndex)
{
if (currentSpawnedItem != null)
{
Destroy(currentSpawnedItem);
}
Item itemInSlot = slots[slotIndex];
GameObject prefab = itemInSlot != null ? itemInSlot.GetPrefab() : null;
if (prefab != null && handSlot != null)
{
currentSpawnedItem = Instantiate(prefab, handSlot.position, handSlot.rotation, handSlot);
currentSpawnedItem.transform.localPosition = Vector3.zero;
currentSpawnedItem.transform.localRotation = Quaternion.identity;
}
else
{
currentSpawnedItem = null;
}
}
}