Untitled

mail@pastecode.io avatar
unknown
csharp
5 months ago
3.0 kB
2
Indexable
using UnityEngine;
using System.Collections.Generic;

public class FireballManager : MonoBehaviour
{
    public Transform target;  // O alvo em torno do qual as fireballs giram
    public float rotationSpeed = 50f;  // Velocidade de rotação
    public float cooldown = 2f;  // Tempo de cooldown para ativar/desativar fireballs
    public int amount = 3;  // Quantidade de fireballs a serem geradas

    public GameObject fireballPrefab;  // Prefab da fireball
    public float fireballRadius = 1f;  // Tamanho da fireball
    public float fireballDamage = 10f;  // Dano da fireball

    private List<GameObject> fireballs;
    private float cooldownTimer;

    private void Start()
    {
        fireballs = new List<GameObject>();

        for (int i = 0; i < amount; i++)
        {
            GameObject fireball = Instantiate(fireballPrefab, transform);
            fireball.SetActive(false);
            fireballs.Add(fireball);
        }

        cooldownTimer = cooldown;

    }

    private void Update()
    {
        cooldownTimer -= Time.deltaTime;

        if (cooldownTimer <= 0f)
        {
            SetAmount(amount);
            ToggleFireballs();
            cooldownTimer = cooldown;
        }

        RotateFireballs();
    }

    private void RotateFireballs()
    {
        for (int i = 0; i < fireballs.Count; i++)
        {
            if (fireballs[i].activeSelf)
            {
                // Calcula a posição das fireballs ao redor do alvo
                float angle = i * Mathf.PI * 2 / amount + Time.time * rotationSpeed * Mathf.Deg2Rad;
                Vector3 offset = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle)) * 3f; // Distância do alvo
                fireballs[i].transform.position = target.position + offset;
                fireballs[i].transform.rotation = Quaternion.LookRotation(Vector3.forward, target.position - fireballs[i].transform.position);  // Faz a fireball olhar para o centro
            }
        }
    }

    private void ToggleFireballs()
    {
        foreach (GameObject fireball in fireballs)
        {
            fireball.SetActive(!fireball.activeSelf);
            Fireball fb = fireball.GetComponent<Fireball>();
            if (fb != null)
            {
                fb.SetRadius(fireballRadius);
                fb.damage = fireballDamage;
            }
        }
    }

    public void SetAmount(int newAmount)
    {
        // foi aqui que o gpt cagou no meu, tive que mudar.
            
            amount = newAmount;
            while (fireballs.Count < amount)
            {
                GameObject fireball = Instantiate(fireballPrefab, transform);
                fireball.SetActive(false);
                fireballs.Add(fireball);
            }

            while (fireballs.Count > amount)
            {
                Destroy(fireballs[fireballs.Count - 1]);
                fireballs.RemoveAt(fireballs.Count - 1);
            }
    }
}
Leave a Comment