Skip to content

Commit 390c457

Browse files
committed
Video 22-23
1 parent 3c8baa9 commit 390c457

7 files changed

+162
-4
lines changed

AgentWeapon.cs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Inventory.Model;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
6+
public class AgentWeapon : MonoBehaviour
7+
{
8+
[SerializeField]
9+
private EquippableItemSO weapon;
10+
11+
[SerializeField]
12+
private InventorySO inventoryData;
13+
14+
[SerializeField]
15+
private List<ItemParameter> parametersToModify, itemCurrentState;
16+
17+
public void SetWeapon(EquippableItemSO weaponItemSO, List<ItemParameter> itemState)
18+
{
19+
if (weapon != null)
20+
{
21+
inventoryData.AddItem(weapon, 1, itemCurrentState);
22+
}
23+
24+
this.weapon = weaponItemSO;
25+
this.itemCurrentState = new List<ItemParameter>(itemState);
26+
ModifyParameters();
27+
}
28+
29+
private void ModifyParameters()
30+
{
31+
foreach (var parameter in parametersToModify)
32+
{
33+
if (itemCurrentState.Contains(parameter))
34+
{
35+
int index = itemCurrentState.IndexOf(parameter);
36+
float newValue = itemCurrentState[index].value + parameter.value;
37+
itemCurrentState[index] = new ItemParameter
38+
{
39+
itemParameter = parameter.itemParameter,
40+
value = newValue
41+
};
42+
}
43+
}
44+
}
45+
}

InventoryController.cs

+43-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public class InventoryController : MonoBehaviour
1818

1919
public List<InventoryItem> initialItems = new List<InventoryItem>();
2020

21+
[SerializeField]
22+
private AudioClip dropClip;
23+
24+
[SerializeField]
25+
private AudioSource audioSource;
26+
2127
private void Start()
2228
{
2329
PrepareUI();
@@ -60,15 +66,49 @@ private void HandleItemActionRequest(int itemIndex)
6066
InventoryItem inventoryItem = inventoryData.GetItemAt(itemIndex);
6167
if (inventoryItem.IsEmpty)
6268
return;
69+
6370
IItemAction itemAction = inventoryItem.item as IItemAction;
6471
if(itemAction != null)
6572
{
66-
itemAction.PerformAction(gameObject, null);
73+
74+
inventoryUI.ShowItemAction(itemIndex);
75+
inventoryUI.AddAction(itemAction.ActionName, () => PerformAction(itemIndex));
76+
}
77+
78+
IDestroyableItem destroyableItem = inventoryItem.item as IDestroyableItem;
79+
if (destroyableItem != null)
80+
{
81+
inventoryUI.AddAction("Drop", () => DropItem(itemIndex, inventoryItem.quantity));
6782
}
83+
84+
}
85+
86+
private void DropItem(int itemIndex, int quantity)
87+
{
88+
inventoryData.RemoveItem(itemIndex, quantity);
89+
inventoryUI.ResetSelection();
90+
audioSource.PlayOneShot(dropClip);
91+
}
92+
93+
public void PerformAction(int itemIndex)
94+
{
95+
InventoryItem inventoryItem = inventoryData.GetItemAt(itemIndex);
96+
if (inventoryItem.IsEmpty)
97+
return;
98+
6899
IDestroyableItem destroyableItem = inventoryItem.item as IDestroyableItem;
69-
if(destroyableItem != null)
100+
if (destroyableItem != null)
101+
{
102+
inventoryData.RemoveItem(itemIndex, 1);
103+
}
104+
105+
IItemAction itemAction = inventoryItem.item as IItemAction;
106+
if (itemAction != null)
70107
{
71-
inventoryData.RemoveItem(itemIndex,1);
108+
itemAction.PerformAction(gameObject, inventoryItem.itemState);
109+
audioSource.PlayOneShot(itemAction.actionSFX);
110+
if (inventoryData.GetItemAt(itemIndex).IsEmpty)
111+
inventoryUI.ResetSelection();
72112
}
73113
}
74114

Model/EdibleItemSO.cs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class EdibleItemSO : ItemSO, IDestroyableItem, IItemAction
1313

1414
public string ActionName => "Consume";
1515

16+
[field: SerializeField]
1617
public AudioClip actionSFX {get; private set;}
1718

1819
public bool PerformAction(GameObject character, List<ItemParameter> itemState = null)

Model/EquippableItemSO.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@
44

55
namespace Inventory.Model
66
{
7+
[CreateAssetMenu]
78
public class EquippableItemSO : ItemSO, IDestroyableItem, IItemAction
89
{
910
public string ActionName => "Equip";
1011

12+
[field: SerializeField]
1113
public AudioClip actionSFX { get; private set; }
1214

1315
public bool PerformAction(GameObject character, List<ItemParameter> itemState = null)
1416
{
15-
throw new System.NotImplementedException();
17+
AgentWeapon weaponSystem = character.GetComponent<AgentWeapon>();
18+
if (weaponSystem != null)
19+
{
20+
weaponSystem.SetWeapon(this, itemState == null ?
21+
DefaultParametersList : itemState);
22+
return true;
23+
}
24+
return false;
1625
}
1726
}
1827
}

UI/ItemActionPanel.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
using UnityEngine.UI;
6+
7+
namespace Inventory.UI
8+
{
9+
public class ItemActionPanel : MonoBehaviour
10+
{
11+
[SerializeField]
12+
private GameObject buttonPrefab;
13+
14+
public void AddButon(string name, Action onClickAction)
15+
{
16+
GameObject button = Instantiate(buttonPrefab, transform);
17+
button.GetComponent<Button>().onClick.AddListener(() => onClickAction());
18+
button.GetComponentInChildren<TMPro.TMP_Text>().text = name;
19+
}
20+
21+
public void Toggle(bool val)
22+
{
23+
if (val == true)
24+
RemoveOldButtons();
25+
gameObject.SetActive(val);
26+
}
27+
28+
public void RemoveOldButtons()
29+
{
30+
foreach (Transform transformChildObjects in transform)
31+
{
32+
Destroy(transformChildObjects.gameObject);
33+
}
34+
}
35+
}
36+
}

UI/ItemActionPanel.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UI/UIInventoryPage.cs

+16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public class UIInventoryPage : MonoBehaviour
2929

3030
public event Action<int, int> OnSwapItems;
3131

32+
[SerializeField]
33+
private ItemActionPanel actionPanel;
34+
3235
private void Awake()
3336
{
3437
Hide();
@@ -145,16 +148,29 @@ public void ResetSelection()
145148
DeselectAllItems();
146149
}
147150

151+
public void AddAction(string actionName, Action performAction)
152+
{
153+
actionPanel.AddButon(actionName, performAction);
154+
}
155+
156+
public void ShowItemAction(int itemIndex)
157+
{
158+
actionPanel.Toggle(true);
159+
actionPanel.transform.position = listOfUIItems[itemIndex].transform.position;
160+
}
161+
148162
private void DeselectAllItems()
149163
{
150164
foreach (UIInventoryItem item in listOfUIItems)
151165
{
152166
item.Deselect();
153167
}
168+
actionPanel.Toggle(false);
154169
}
155170

156171
public void Hide()
157172
{
173+
actionPanel.Toggle(false);
158174
gameObject.SetActive(false);
159175
ResetDraggedItem();
160176
}

0 commit comments

Comments
 (0)