-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryManager.cs
More file actions
79 lines (59 loc) · 2.18 KB
/
InventoryManager.cs
File metadata and controls
79 lines (59 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
public class InventoryManager : MonoBehaviour
{
[SerializeField] private ItemSO[] items;
[SerializeField] private Button addItemButton;
[SerializeField] private Button removeItemButton;
[SerializeField] private InputActionReference scrollAction;
private void Start()
{
PlayerInventory.Instance.SelectSlot(0);
}
private void OnEnable()
{
scrollAction.action.Enable();
scrollAction.action.performed += OnScroll;
addItemButton.onClick.AddListener(AddItem);
removeItemButton.onClick.AddListener(RemoveItem);
}
private void OnDisable()
{
scrollAction.action.Disable();
scrollAction.action.performed -= OnScroll;
addItemButton.onClick.RemoveListener(AddItem);
removeItemButton.onClick.RemoveListener(RemoveItem);
}
private void OnScroll(InputAction.CallbackContext context)
{
float scrollValue = context.ReadValue<float>();
if (scrollValue > 0)
CycleSlot(1);
else if (scrollValue < 0)
CycleSlot(-1);
}
private void CycleSlot(int direction)
{
int inventorySize = PlayerInventory.Instance.inventorySize;
int newSlot = PlayerInventory.Instance.selectedSlot + direction;
if (newSlot >= inventorySize)
newSlot = 0;
else if (newSlot < 0)
newSlot = inventorySize - 1;
PlayerInventory.Instance.SelectSlot(newSlot);
Debug.Log($"Selected slot {newSlot}");
}
private void AddItem()
{
int randomIndex = Random.Range(0, items.Length);
if (!PlayerInventory.Instance.TryAddItem(items[randomIndex])) return;
Debug.Log($"Added item {items[randomIndex].itemName}");
}
private void RemoveItem()
{
ItemSO item = PlayerInventory.Instance.currentHeldItem;
if (!PlayerInventory.Instance.TryRemoveItem(item) || item == null) return;
Debug.Log($"Removed item {item.itemName}");
}
}