GoldBagPickup.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using UnityEngine;
  2. using SoftKitty.InventoryEngine;
  3. using SoftKitty;
  4. public class GoldBagPickup : MonoBehaviour
  5. {
  6. [Header("Gold Settings")]
  7. public int goldAmount = 50;
  8. public int currencyType = 0;
  9. [Header("Pickup Settings")]
  10. public bool autoPickup = true;
  11. public float pickupRadius = 2.5f;
  12. public float pickupDelay = 1f;
  13. [Header("Effects")]
  14. public ParticleSystem spawnParticles;
  15. public ParticleSystem pickupParticles;
  16. public AudioClip pickupSound;
  17. [Header("Visuals")]
  18. public bool enableBobbing = true;
  19. public float bobHeight = 0.3f;
  20. public float bobSpeed = 2f;
  21. public bool enableRotation = true;
  22. public float rotationSpeed = 90f;
  23. private Vector3 startPos;
  24. private Transform player;
  25. private bool pickedUp = false;
  26. private bool landed = false;
  27. void Start()
  28. {
  29. if (spawnParticles) spawnParticles.Play();
  30. GameObject playerObj = GameObject.FindWithTag("Player");
  31. if (playerObj) player = playerObj.transform;
  32. Invoke("LandGold", 1f);
  33. }
  34. void LandGold()
  35. {
  36. startPos = transform.position;
  37. landed = true;
  38. Rigidbody rb = GetComponent<Rigidbody>();
  39. if (rb) Destroy(rb);
  40. Collider col = GetComponent<Collider>();
  41. if (col) col.enabled = false;
  42. }
  43. void Update()
  44. {
  45. if (pickedUp || !landed) return;
  46. if (enableBobbing)
  47. {
  48. float y = startPos.y + Mathf.Sin(Time.time * bobSpeed) * bobHeight;
  49. transform.position = new Vector3(startPos.x, y, startPos.z);
  50. }
  51. if (enableRotation)
  52. {
  53. transform.Rotate(Vector3.forward, rotationSpeed * Time.deltaTime);
  54. }
  55. if (!player) return;
  56. float dist = Vector3.Distance(transform.position, player.position);
  57. if (dist <= pickupRadius)
  58. {
  59. if (autoPickup)
  60. {
  61. Pickup();
  62. }
  63. else if (Input.GetKeyDown(KeyCode.E))
  64. {
  65. Pickup();
  66. }
  67. }
  68. }
  69. void Pickup()
  70. {
  71. if (pickedUp) return;
  72. pickedUp = true;
  73. StartCoroutine(PickupWithDelay());
  74. }
  75. System.Collections.IEnumerator PickupWithDelay()
  76. {
  77. if (ItemManager.PlayerInventoryHolder != null)
  78. {
  79. ItemManager.PlayerInventoryHolder.AddCurrency(currencyType, goldAmount);
  80. }
  81. if (pickupParticles)
  82. {
  83. ParticleSystem fx = Instantiate(pickupParticles, transform.position, Quaternion.identity);
  84. Destroy(fx.gameObject, 2f);
  85. }
  86. if (pickupSound)
  87. {
  88. AudioSource.PlayClipAtPoint(pickupSound, transform.position);
  89. }
  90. else
  91. {
  92. try { SoundManager.Play2D("Loot"); } catch { }
  93. }
  94. // Show UI message (uncomment after setting up GoldPickupMessage in Canvas)
  95. GoldPickupNotifier.Show(goldAmount);
  96. yield return new WaitForSeconds(pickupDelay);
  97. Destroy(gameObject);
  98. }
  99. void OnDrawGizmosSelected()
  100. {
  101. Gizmos.color = Color.yellow;
  102. Gizmos.DrawWireSphere(transform.position, pickupRadius);
  103. }
  104. }