-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.cs
More file actions
132 lines (109 loc) · 3.99 KB
/
Copy pathInventory.cs
File metadata and controls
132 lines (109 loc) · 3.99 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
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
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace DarkTreeFPS
{
public class Inventory : MonoBehaviour
{
[System.Serializable]
public class OnAddItem : UnityEvent { }
public List<Item> characterItems = new List<Item>();
public UIInventory inventoryUI;
public bool debug = true;
public OnAddItem onAddItem;
//Method to add item to inventory
public void GiveItem(Item item)
{
if (CheckFreeSpace() == false)
{
return;
}
characterItems.Add(item);
inventoryUI.AddNewItem(item);
item.gameObject.SetActive(false);
if (debug) Debug.Log("Added item: " + item.title);
//Events
item.onPickupEvent.Invoke();
onAddItem.Invoke();
}
//Method return true if inventory found free space
public bool CheckFreeSpace()
{
if (inventoryUI.UIItems.FindLast(i => i.item == null))
{
if (debug)
Debug.Log("Free space found");
return true;
}
if (debug)
Debug.Log("No free space found");
return false;
}
//Method to check if item reference exist in inventory. Used by remove method to check if we delete item that is really exist
public Item CheckForItem(Item item)
{
return characterItems.Find(x => item.GetHashCode() == x.GetHashCode());
}
//Method to check if we have an item or items that have needed name. Will return true if find first suitable item
public bool CheckIfItemExist(string name)
{
if (characterItems.Find(x => name == x.title))
return true;
else
return false;
}
//Remove item by name. If destroy true inventory will not drop item ahead of player after remove.
public void RemoveItem(string name, bool destroy)
{
var _item = characterItems.Find(x => name == x.title);
if (_item != null)
{
if (_item.gameObject != null && !destroy)
{
_item.gameObject.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 0.5f;
_item.gameObject.SetActive(true);
}
if (debug)
Debug.Log("Remove item: " + _item.title);
characterItems.Remove(_item);
inventoryUI.RemoveItem(_item);
}
else
{
if (debug)
Debug.Log("No item found");
}
}
//Remove item by item reference
public void RemoveItem(Item item, bool destroy)
{
var _item = CheckForItem(item);
if (_item != null)
{
if (_item.gameObject != null && !destroy)
{
_item.gameObject.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 0.5f;
_item.gameObject.SetActive(true);
}
if (debug)
Debug.Log("Remove item: " + _item.title);
characterItems.Remove(_item);
inventoryUI.RemoveItem(_item);
}
else
{
if (debug)
Debug.Log("No item found");
}
}
public void UseItem(Item item, bool closeInventory)
{
item.onUseEvent.Invoke();
// !!! Crutch for grenade object !!!
if (item.id != 105)
RemoveItem(item, true);
if (closeInventory)
InventoryManager.showInventory = false;
}
}
}