StellarXipher/Assets/Scripts/Inventory.cs
nlevin6 930173c383
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
added inventory
2025-03-29 21:27:24 -04:00

92 lines
2.2 KiB
C#

using UnityEngine;
using System.Collections.Generic;
public class Inventory : MonoBehaviour
{
// A fixed array of 10 slots
[Header("Exactly 10 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();
}
}
/// <summary>
/// Adds an item to the first available slot (if any).
/// </summary>
public void AddItem(Item newItem)
{
for (int i = 0; i < slots.Length; i++)
{
if (slots[i] == null)
{
slots[i] = newItem;
break;
}
}
// Optional: if no item is equipped yet, equip slot 0, etc.
// if (currentSpawnedItem == null) EquipSlot(0);
}
private void SelectNextSlot()
{
currentSlot = (currentSlot + 1) % slots.Length;
EquipSlot(currentSlot);
// Force the UI to re-check which slot is active
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;
}
}
}