Files
Crash-Course/Assets/Scripts/Player/UseItem.cs

91 lines
3.2 KiB
C#
Raw Normal View History

2025-03-29 15:18:27 -04:00
using System.Collections;
2025-02-17 19:02:14 -05:00
using UnityEngine;
public class UseItem : MonoBehaviour
{
2025-03-17 18:20:03 -04:00
[SerializeField] private string itemTag;
private GameObject heldItem;
private bool isHoldingItem = false;
2025-03-20 14:45:29 -04:00
private float holdStartTime;
2025-03-28 13:00:37 -04:00
public float holdTime;
private Damageable damageable;
2025-03-29 15:18:27 -04:00
[SerializeField] private Transform head;
private void Start()
{
damageable = GetComponent<Damageable>();
}
void Update()
2025-02-17 19:02:14 -05:00
{
if (isHoldingItem)
{
// Keeps hat on the player's head
2025-03-29 15:18:27 -04:00
heldItem.transform.localPosition = Vector3.zero;
2025-03-20 14:45:29 -04:00
if (GameManager.gameMode == GameManager.GameMode.keepAway)
{
// Adds time to the player's leaderboard standing
2025-03-28 13:00:37 -04:00
holdTime += Time.deltaTime;
2025-03-25 22:21:21 -04:00
GameManager.Instance.UpdatePlayerHoldTime(gameObject, holdTime);
2025-03-20 14:45:29 -04:00
}
}
2025-02-17 19:02:14 -05:00
}
private void OnCollisionEnter2D(Collision2D collision) // Player automatically picks up hat when touching it
{
if (collision.gameObject.CompareTag("Hat") && !isHoldingItem && !damageable.dying)
{
PickUpItem(collision.gameObject);
}
}
private void PickUpItem(GameObject item) // Player picks up hat and starts hold counter
{
if (damageable.dying) return; // Prevent picking up items if the player is dying
2025-03-29 15:18:27 -04:00
if (HatRespawn.canBePickedUp == false) return; // Prevent picking up items if they are not interactable
heldItem = item;
isHoldingItem = true;
2025-03-20 14:45:29 -04:00
holdStartTime = Time.time;
item.GetComponent<Collider2D>().enabled = false;
2025-03-17 18:20:03 -04:00
item.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
item.GetComponent<HatRespawn>().Interact();
2025-03-29 15:18:27 -04:00
item.transform.parent = head;
item.transform.localRotation = Quaternion.identity;
item.transform.localPosition = Vector3.zero;
2025-03-20 14:45:29 -04:00
if (!GameManager.playerHoldTimes.ContainsKey(gameObject))
{
GameManager.playerHoldTimes[gameObject] = 0f;
}
}
public void DropItem() // Player drops hat when hit
{
if (isHoldingItem)
{
heldItem.GetComponent<Collider2D>().enabled = true;
2025-03-29 15:18:27 -04:00
HatRespawn.canBePickedUp = false;
StartCoroutine(WaitForInteractability());
2025-03-17 18:20:03 -04:00
heldItem.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
2025-03-29 15:18:27 -04:00
heldItem.GetComponent<Rigidbody2D>().AddForce(Vector2.up * Random.Range(10f, 30f) + Vector2.right * Random.Range(-10, 10), ForceMode2D.Impulse);
heldItem.GetComponent<Rigidbody2D>().AddTorque(Random.Range(-5, 5), ForceMode2D.Impulse);
//heldItem.transform.position += Vector3.up * 3f;
2025-03-28 23:07:33 -04:00
heldItem.GetComponent<HatRespawn>().OnHatDropped();
2025-03-29 15:18:27 -04:00
heldItem.transform.parent = GameManager.Instance.transform;
heldItem = null;
isHoldingItem = false;
2025-03-20 14:45:29 -04:00
if (GameManager.playerHoldTimes.ContainsKey(gameObject))
{
GameManager.playerHoldTimes.Remove(gameObject);
}
}
}
2025-03-28 23:07:33 -04:00
2025-03-29 15:18:27 -04:00
private IEnumerator WaitForInteractability()
{
yield return new WaitForSeconds(0.1f);
HatRespawn.canBePickedUp = true;
}
2025-02-17 19:02:14 -05:00
}