-
Notifications
You must be signed in to change notification settings - Fork 0
/
InteractionHandler.cs
58 lines (46 loc) · 1.66 KB
/
InteractionHandler.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
using Inventory.Enums;
using UnityEngine;
public class InteractionHandler : MonoBehaviour
{
public float interactionRange = 2f;
public KeyCode interactionKey = KeyCode.E;
[SerializeField] private MenuUIHandler uiHandler;
[SerializeField] public InventoryManager inventory;
private void Update()
{
if (Input.GetKeyDown(interactionKey)) //take item
{
Interact();
}
if (!uiHandler.craftOpened && !uiHandler.inventoryOpened && Input.GetMouseButtonDown(0)) //use item
{
if (inventory.selectedItem != null)
{
inventory.selectedItem.Use(this);
if (inventory.selectedItem.slotType != SlotType.def) //if not a default object, add usury
{
inventory.selectedItem.AddUsury();
Debug.Log(inventory.selectedItem.itemUsury);
}
if (inventory.selectedItem.itemUsury <= 0) // if usury less then zero, then remove it from the inventory
{
inventory.Remove(inventory.selectedItem, 1);
}
inventory.RefreshUI();
}
}
}
private void Interact()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, interactionRange))
{
Pickup pickup = hit.transform.GetComponent<Pickup>();
if (pickup != null)
{
inventory.Add(pickup.data, pickup.stackSize);
GameObject.Destroy(pickup.gameObject);
}
}
}
}