2025-02-17 19:02:14 -05:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class UseItem : MonoBehaviour
|
|
|
|
|
{
|
2025-02-28 13:49:02 -05:00
|
|
|
private GameObject heldItem;
|
|
|
|
|
private bool isHoldingItem = false;
|
|
|
|
|
|
|
|
|
|
void Update()
|
2025-02-17 19:02:14 -05:00
|
|
|
{
|
2025-02-28 13:49:02 -05:00
|
|
|
if (isHoldingItem)
|
|
|
|
|
{
|
|
|
|
|
heldItem.transform.position = transform.position;
|
|
|
|
|
}
|
2025-02-17 19:02:14 -05:00
|
|
|
}
|
|
|
|
|
|
2025-02-28 13:49:02 -05:00
|
|
|
private void OnTriggerEnter2D(Collider2D collision)
|
|
|
|
|
{
|
|
|
|
|
if (collision.CompareTag("Item") && !isHoldingItem)
|
|
|
|
|
{
|
|
|
|
|
PickUpItem(collision.gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PickUpItem(GameObject item)
|
|
|
|
|
{
|
|
|
|
|
heldItem = item;
|
|
|
|
|
isHoldingItem = true;
|
|
|
|
|
item.GetComponent<Collider2D>().enabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DropItem()
|
|
|
|
|
{
|
|
|
|
|
if (isHoldingItem)
|
|
|
|
|
{
|
|
|
|
|
heldItem.GetComponent<Collider2D>().enabled = true;
|
|
|
|
|
heldItem = null;
|
|
|
|
|
isHoldingItem = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
Punch.OnPlayerPunched += HandlePlayerPunched;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
Punch.OnPlayerPunched -= HandlePlayerPunched;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandlePlayerPunched(GameObject punchedPlayer)
|
2025-02-17 19:02:14 -05:00
|
|
|
{
|
2025-02-28 13:49:02 -05:00
|
|
|
if (punchedPlayer == gameObject)
|
|
|
|
|
{
|
|
|
|
|
DropItem();
|
|
|
|
|
}
|
2025-02-17 19:02:14 -05:00
|
|
|
}
|
|
|
|
|
}
|