using System.Collections; using UnityEngine; public class ExperiencePickup : MonoBehaviour { public int experienceAmount = 50; public bool destroyOnCollect = true; public float respawnCooldown = 12f; public float rotationSpeed = 70f; public LevelSystem levelSystem; public NotificationUI notificationUI; private bool playerInRange; private bool collected; private Renderer[] renderers; private Collider[] colliders; private void Awake() { renderers = GetComponentsInChildren(); colliders = GetComponentsInChildren(); if (levelSystem == null) { levelSystem = FindObjectOfType(); } if (notificationUI == null) { notificationUI = FindObjectOfType(); } } private void Update() { transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime, Space.World); if (playerInRange && !collected && Input.GetKeyDown(KeyCode.E)) { Collect(); } } private void OnTriggerEnter(Collider other) { if (other.GetComponentInParent() != null) { playerInRange = true; if (notificationUI != null) { notificationUI.Show("Нажмите E, чтобы забрать кристалл опыта"); } } } private void OnTriggerExit(Collider other) { if (other.GetComponentInParent() != null) { playerInRange = false; } } private void Collect() { collected = true; if (levelSystem != null) { levelSystem.AddExperience(experienceAmount); } if (notificationUI != null) { notificationUI.Show($"Кристалл опыта: +{experienceAmount} XP"); } if (destroyOnCollect) { Destroy(gameObject); } else { StartCoroutine(RespawnRoutine()); } } private IEnumerator RespawnRoutine() { SetVisible(false); yield return new WaitForSeconds(Mathf.Max(1f, respawnCooldown)); collected = false; SetVisible(true); } private void SetVisible(bool visible) { for (int i = 0; i < renderers.Length; i++) { if (renderers[i] != null) { renderers[i].enabled = visible; } } for (int i = 0; i < colliders.Length; i++) { if (colliders[i] != null) { colliders[i].enabled = visible; } } } }