using System.Collections; using UnityEngine; using Game; using Music; using Player; namespace Player { public class UseItem : MonoBehaviour { [SerializeField] private string itemTag; private GameObject heldItem; private bool isHoldingItem = false; private float holdStartTime; public float holdTime; private Damageable damageable; [SerializeField] public Transform head; private void Start() { damageable = GetComponent(); } void Update() { if (isHoldingItem) { // Keeps hat on the player's head heldItem.transform.localPosition = Vector3.zero; if (GameManager.gameMode == GameManager.GameMode.keepAway) { // Adds time to the player's leaderboard standing holdTime += Time.deltaTime; GameManager.Instance.UpdatePlayerHoldTime(gameObject, holdTime); } } } private void OnCollisionEnter2D(Collision2D collision) // Player automatically picks up hat when touching it { if (collision.gameObject.CompareTag("Hat") && !isHoldingItem && !damageable.dying) { PickUpItem(collision.gameObject); } } public 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 if (HatRespawn.canBePickedUp == false) return; // Prevent picking up items if they are not interactable heldItem = item; isHoldingItem = true; holdStartTime = Time.time; item.GetComponent().enabled = false; item.GetComponent().bodyType = RigidbodyType2D.Static; item.GetComponent().Interact(); item.transform.parent = head; item.transform.localRotation = Quaternion.identity; item.transform.localPosition = Vector3.zero; item.transform.GetChild(0).transform.localPosition = item.GetComponent().initialSubhatPosition; if (!GameManager.playerHoldTimes.ContainsKey(gameObject)) { GameManager.playerHoldTimes[gameObject] = 0f; } AudioManager.Instance.PlaySound("Pickup Hat"); GameManager.Instance.StopCoroutine("MoveHatToWinner"); } public void DropItem() // Player drops hat when hit { if (GameManager.Instance.gameOver) return; // Prevent dropping items if the game is over if (isHoldingItem) { heldItem.GetComponent().enabled = true; HatRespawn.canBePickedUp = false; StartCoroutine(WaitForInteractability()); heldItem.GetComponent().bodyType = RigidbodyType2D.Dynamic; heldItem.GetComponent().AddForce(Vector2.up * Random.Range(10f, 30f) + Vector2.right * Random.Range(-10, 10), ForceMode2D.Impulse); heldItem.GetComponent().AddTorque(Random.Range(-5, 5), ForceMode2D.Impulse); heldItem.GetComponent().OnHatDropped(); heldItem.transform.parent = GameManager.Instance.transform; heldItem = null; isHoldingItem = false; if (GameManager.playerHoldTimes.ContainsKey(gameObject)) { GameManager.playerHoldTimes.Remove(gameObject); } } } private IEnumerator WaitForInteractability() { yield return new WaitForSeconds(0.1f); HatRespawn.canBePickedUp = true; } public bool IsHoldingItem() { return isHoldingItem; } } }