-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemUseSystem.cs
More file actions
112 lines (96 loc) · 3.22 KB
/
ItemUseSystem.cs
File metadata and controls
112 lines (96 loc) · 3.22 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
using UnityEngine;
public class ItemUseSystem : MonoBehaviour
{
private PlayerInventory inventory;
public int selectedSlot = -1;
public GameObject arrowPrefab;
public float arrowSpeed = 20f;
public float bowCooldown = 0.5f;
private float lastShotTime;
private Vector2 lastMovementDirection = Vector2.right;
private ItemBar itemBar;
void Start()
{
inventory = GetComponent<PlayerInventory>();
itemBar = FindObjectOfType<ItemBar>();
}
void Update()
{
float horizontalInput = Input.GetAxisRaw("Horizontal");
float verticalInput = Input.GetAxisRaw("Vertical");
if (horizontalInput != 0 || verticalInput != 0)
{
lastMovementDirection = new Vector2(horizontalInput, verticalInput).normalized;
}
bool shiftHeld = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
// Handle slot selection (1-5 keys) only when shift is NOT held
if (!shiftHeld)
{
for (int i = 0; i < 5; i++)
{
if (Input.GetKeyDown(KeyCode.Alpha1 + i))
{
selectedSlot = i;
// Update the item bar visuals when slot changes
if (itemBar != null)
{
itemBar.UpdateItemBar();
}
}
}
}
// Handle item use
if (Input.GetKeyDown(KeyCode.Space) && selectedSlot != -1)
{
UseSelectedItem();
}
}
void UseSelectedItem()
{
var itemBarItems = inventory.GetItemBarItems();
if (selectedSlot >= itemBarItems.Count) return;
string itemName = itemBarItems[selectedSlot].Key;
if (itemName == "Bow" && Time.time >= lastShotTime + bowCooldown)
{
// Check if player has arrows in inventory
int totalArrows = inventory.GetTotalItemCount("Arrow");
if (totalArrows > 0)
{
ShootArrow();
// Remove one arrow from inventory
inventory.RemoveItems("Arrow", 1);
lastShotTime = Time.time;
}
else
{
// No arrows available
FindObjectOfType<UIManager>().ShowUpperMessage("You need arrows to shoot the bow!");
}
}
}
void ShootArrow()
{
// Get mouse position in world coordinates
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// Calculate direction from player to mouse
Vector2 direction = (mousePosition - (Vector2)transform.position).normalized;
// Create arrow
GameObject arrow = Instantiate(arrowPrefab, transform.position, Quaternion.identity);
// Calculate the angle from the direction and add 225 degrees to align the sprite
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 225f;
// Set the arrow's rotation to point in the direction of travel
arrow.transform.rotation = Quaternion.Euler(0, 0, angle);
// Initialize arrow direction using the Arrow component
Arrow arrowComponent = arrow.GetComponent<Arrow>();
if (arrowComponent != null)
{
arrowComponent.Initialize(direction);
}
else
{
Debug.LogError("Arrow component not found on arrow prefab!");
}
// Destroy arrow after 5 seconds as a fallback
Destroy(arrow, 5f);
}
}