116 lines
2.7 KiB
C#
116 lines
2.7 KiB
C#
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<Renderer>();
|
|
colliders = GetComponentsInChildren<Collider>();
|
|
|
|
if (levelSystem == null)
|
|
{
|
|
levelSystem = FindObjectOfType<LevelSystem>();
|
|
}
|
|
|
|
if (notificationUI == null)
|
|
{
|
|
notificationUI = FindObjectOfType<NotificationUI>();
|
|
}
|
|
}
|
|
|
|
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<SimplePlayerController>() != null)
|
|
{
|
|
playerInRange = true;
|
|
if (notificationUI != null)
|
|
{
|
|
notificationUI.Show("Нажмите E, чтобы забрать кристалл опыта");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
if (other.GetComponentInParent<SimplePlayerController>() != 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;
|
|
}
|
|
}
|
|
}
|
|
}
|