Skip to content

Commit 3c8baa9

Browse files
committed
Video 21 - ItemParameters (durability)
1 parent 13f77c2 commit 3c8baa9

13 files changed

+142
-18
lines changed

InventoryController.cs

+19-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections;
55
using System.Collections.Generic;
6+
using System.Text;
67
using UnityEngine;
78

89
namespace Inventory
@@ -62,7 +63,7 @@ private void HandleItemActionRequest(int itemIndex)
6263
IItemAction itemAction = inventoryItem.item as IItemAction;
6364
if(itemAction != null)
6465
{
65-
itemAction.PerformAction(gameObject);
66+
itemAction.PerformAction(gameObject, null);
6667
}
6768
IDestroyableItem destroyableItem = inventoryItem.item as IDestroyableItem;
6869
if(destroyableItem != null)
@@ -93,8 +94,24 @@ private void HandleDescriptionRequest(int itemIndex)
9394
return;
9495
}
9596
ItemSO item = inventoryItem.item;
97+
string description = PrepareDescription(inventoryItem);
9698
inventoryUI.UpdateDescription(itemIndex, item.ItemImage,
97-
item.name, item.Description);
99+
item.name, description);
100+
}
101+
102+
private string PrepareDescription(InventoryItem inventoryItem)
103+
{
104+
StringBuilder sb = new StringBuilder();
105+
sb.Append(inventoryItem.item.Description);
106+
sb.AppendLine();
107+
for (int i = 0; i < inventoryItem.itemState.Count; i++)
108+
{
109+
sb.Append($"{inventoryItem.itemState[i].itemParameter.ParameterName} " +
110+
$": {inventoryItem.itemState[i].value} / " +
111+
$"{inventoryItem.item.DefaultParametersList[i].value}");
112+
sb.AppendLine();
113+
}
114+
return sb.ToString();
98115
}
99116

100117
public void Update()

Model/EdibleItemSO.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class EdibleItemSO : ItemSO, IDestroyableItem, IItemAction
1515

1616
public AudioClip actionSFX {get; private set;}
1717

18-
public bool PerformAction(GameObject character)
18+
public bool PerformAction(GameObject character, List<ItemParameter> itemState = null)
1919
{
2020
foreach (ModifierData data in modifiersData)
2121
{
@@ -34,7 +34,7 @@ public interface IItemAction
3434
{
3535
public string ActionName { get; }
3636
public AudioClip actionSFX { get; }
37-
bool PerformAction(GameObject character);
37+
bool PerformAction(GameObject character, List<ItemParameter> itemState);
3838
}
3939

4040
[Serializable]

Model/EquippableItemSO.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44

5-
public class EquippableItemSO : MonoBehaviour
5+
namespace Inventory.Model
66
{
7-
// Start is called before the first frame update
8-
void Start()
7+
public class EquippableItemSO : ItemSO, IDestroyableItem, IItemAction
98
{
10-
11-
}
9+
public string ActionName => "Equip";
1210

13-
// Update is called once per frame
14-
void Update()
15-
{
16-
11+
public AudioClip actionSFX { get; private set; }
12+
13+
public bool PerformAction(GameObject character, List<ItemParameter> itemState = null)
14+
{
15+
throw new System.NotImplementedException();
16+
}
1717
}
18-
}
18+
}

Model/InventorySO.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ public void Initialize()
2626
}
2727
}
2828

29-
public int AddItem(ItemSO item, int quantity)
29+
public int AddItem(ItemSO item, int quantity, List<ItemParameter> itemState = null)
3030
{
3131
if(item.IsStackable == false)
3232
{
3333
for (int i = 0; i < inventoryItems.Count; i++)
3434
{
3535
while(quantity > 0 && IsInventoryFull() == false)
3636
{
37-
quantity -= AddItemToFirstFreeSlot(item, 1);
37+
quantity -= AddItemToFirstFreeSlot(item, 1, itemState);
3838
}
3939
InformAboutChange();
4040
return quantity;
@@ -45,12 +45,15 @@ public int AddItem(ItemSO item, int quantity)
4545
return quantity;
4646
}
4747

48-
private int AddItemToFirstFreeSlot(ItemSO item, int quantity)
48+
private int AddItemToFirstFreeSlot(ItemSO item, int quantity
49+
, List<ItemParameter> itemState = null)
4950
{
5051
InventoryItem newItem = new InventoryItem
5152
{
5253
item = item,
53-
quantity = quantity
54+
quantity = quantity,
55+
itemState =
56+
new List<ItemParameter>(itemState == null ? item.DefaultParametersList : itemState)
5457
};
5558

5659
for (int i = 0; i < inventoryItems.Count; i++)
@@ -162,6 +165,7 @@ public struct InventoryItem
162165
{
163166
public int quantity;
164167
public ItemSO item;
168+
public List<ItemParameter> itemState;
165169
public bool IsEmpty => item == null;
166170

167171
public InventoryItem ChangeQuantity(int newQuantity)
@@ -170,6 +174,7 @@ public InventoryItem ChangeQuantity(int newQuantity)
170174
{
171175
item = this.item,
172176
quantity = newQuantity,
177+
itemState = new List<ItemParameter>(this.itemState)
173178
};
174179
}
175180

@@ -178,6 +183,7 @@ public static InventoryItem GetEmptyItem()
178183
{
179184
item = null,
180185
quantity = 0,
186+
itemState = new List<ItemParameter>()
181187
};
182188
}
183189
}

Model/ItemParameters.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Inventory.Model
6+
{
7+
[CreateAssetMenu]
8+
public class ItemParameterSO : ScriptableObject
9+
{
10+
[field: SerializeField]
11+
public string ParameterName { get; private set; }
12+
}
13+
}

Model/ItemParameters/ItemParameterSO.cs.meta

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

Model/ItemSO.cs

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections;
23
using System.Collections.Generic;
34
using UnityEngine;
@@ -23,6 +24,22 @@ public abstract class ItemSO : ScriptableObject
2324

2425
[field: SerializeField]
2526
public Sprite ItemImage { get; set; }
27+
28+
[field: SerializeField]
29+
public List<ItemParameter> DefaultParametersList { get; set; }
30+
31+
}
32+
33+
[Serializable]
34+
public struct ItemParameter : IEquatable<ItemParameter>
35+
{
36+
public ItemParameterSO itemParameter;
37+
public float value;
38+
39+
public bool Equals(ItemParameter other)
40+
{
41+
return other.itemParameter == itemParameter;
42+
}
2643
}
2744
}
2845

Model/StatsModifiers.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
[CreateAssetMenu]
6+
public class CharacterStatHealthModifierSO : CharacterStatModifierSO
7+
{
8+
public override void AffectCharacter(GameObject character, float val)
9+
{
10+
Health health = character.GetComponent<Health>();
11+
if (health != null)
12+
health.AddHealth((int)val);
13+
}
14+
}

Model/StatsModifiers/CharacterStatHealthModifierSO.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public abstract class CharacterStatModifierSO : ScriptableObject
6+
{
7+
public abstract void AffectCharacter(GameObject character, float val);
8+
}

Model/StatsModifiers/CharacterStatModifierSO.cs.meta

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

0 commit comments

Comments
 (0)