-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventoryController.cs
179 lines (155 loc) · 5.74 KB
/
InventoryController.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using Inventory.Model;
using Inventory.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace Inventory
{
public class InventoryController : MonoBehaviour
{
[SerializeField]
private UIInventoryPage inventoryUI;
[SerializeField]
private InventorySO inventoryData;
public List<InventoryItem> initialItems = new List<InventoryItem>();
[SerializeField]
private AudioClip dropClip;
[SerializeField]
private AudioSource audioSource;
private void Start()
{
PrepareUI();
PrepareInventoryData();
}
private void PrepareInventoryData()
{
inventoryData.Initialize();
inventoryData.OnInventoryUpdated += UpdateInventoryUI;
foreach (InventoryItem item in initialItems)
{
if (item.IsEmpty)
continue;
inventoryData.AddItem(item);
}
}
private void UpdateInventoryUI(Dictionary<int, InventoryItem> inventoryState)
{
inventoryUI.ResetAllItems();
foreach (var item in inventoryState)
{
inventoryUI.UpdateData(item.Key, item.Value.item.ItemImage,
item.Value.quantity);
}
}
private void PrepareUI()
{
inventoryUI.InitializeInventoryUI(inventoryData.Size);
inventoryUI.OnDescriptionRequested += HandleDescriptionRequest;
inventoryUI.OnSwapItems += HandleSwapItems;
inventoryUI.OnStartDragging += HandleDragging;
inventoryUI.OnItemActionRequested += HandleItemActionRequest;
}
private void HandleItemActionRequest(int itemIndex)
{
InventoryItem inventoryItem = inventoryData.GetItemAt(itemIndex);
if (inventoryItem.IsEmpty)
return;
IItemAction itemAction = inventoryItem.item as IItemAction;
if(itemAction != null)
{
inventoryUI.ShowItemAction(itemIndex);
inventoryUI.AddAction(itemAction.ActionName, () => PerformAction(itemIndex));
}
IDestroyableItem destroyableItem = inventoryItem.item as IDestroyableItem;
if (destroyableItem != null)
{
inventoryUI.AddAction("Drop", () => DropItem(itemIndex, inventoryItem.quantity));
}
}
private void DropItem(int itemIndex, int quantity)
{
inventoryData.RemoveItem(itemIndex, quantity);
inventoryUI.ResetSelection();
audioSource.PlayOneShot(dropClip);
}
public void PerformAction(int itemIndex)
{
InventoryItem inventoryItem = inventoryData.GetItemAt(itemIndex);
if (inventoryItem.IsEmpty)
return;
IDestroyableItem destroyableItem = inventoryItem.item as IDestroyableItem;
if (destroyableItem != null)
{
inventoryData.RemoveItem(itemIndex, 1);
}
IItemAction itemAction = inventoryItem.item as IItemAction;
if (itemAction != null)
{
itemAction.PerformAction(gameObject, inventoryItem.itemState);
audioSource.PlayOneShot(itemAction.actionSFX);
if (inventoryData.GetItemAt(itemIndex).IsEmpty)
inventoryUI.ResetSelection();
}
}
private void HandleDragging(int itemIndex)
{
InventoryItem inventoryItem = inventoryData.GetItemAt(itemIndex);
if (inventoryItem.IsEmpty)
return;
inventoryUI.CreateDraggedItem(inventoryItem.item.ItemImage, inventoryItem.quantity);
}
private void HandleSwapItems(int itemIndex_1, int itemIndex_2)
{
inventoryData.SwapItems(itemIndex_1, itemIndex_2);
}
private void HandleDescriptionRequest(int itemIndex)
{
InventoryItem inventoryItem = inventoryData.GetItemAt(itemIndex);
if (inventoryItem.IsEmpty)
{
inventoryUI.ResetSelection();
return;
}
ItemSO item = inventoryItem.item;
string description = PrepareDescription(inventoryItem);
inventoryUI.UpdateDescription(itemIndex, item.ItemImage,
item.name, description);
}
private string PrepareDescription(InventoryItem inventoryItem)
{
StringBuilder sb = new StringBuilder();
sb.Append(inventoryItem.item.Description);
sb.AppendLine();
for (int i = 0; i < inventoryItem.itemState.Count; i++)
{
sb.Append($"{inventoryItem.itemState[i].itemParameter.ParameterName} " +
$": {inventoryItem.itemState[i].value} / " +
$"{inventoryItem.item.DefaultParametersList[i].value}");
sb.AppendLine();
}
return sb.ToString();
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.I))
{
if (inventoryUI.isActiveAndEnabled == false)
{
inventoryUI.Show();
foreach (var item in inventoryData.GetCurrentInventoryState())
{
inventoryUI.UpdateData(item.Key,
item.Value.item.ItemImage,
item.Value.quantity);
}
}
else
{
inventoryUI.Hide();
}
}
}
}
}