Cosmic Strike
 
Cargando...
Buscando...
Nada coincide
SpawnManager.cs
Ir a la documentación de este archivo.
1
11
12using UnityEngine;
13using System.Collections;
14
19[System.Serializable]
20public class Wave
21{
25 public string name;
26
31 public GameObject[] enemyPrefabs;
32
36 public int count;
37
41 public float spawnRate;
42}
43
48public class SpawnManager : MonoBehaviour
49{
50 // ========== CONFIGURACIÓN DE OLEADAS ==========
51
55 [Header("Oleadas y niveles")]
56 public Wave[] waves;
57
61 public float timeBetweenWaves = 5f;
62
66 public float difficultyScalePerLevel = 1.2f;
67
68 // ========== CONFIGURACIÓN DE SPAWN ==========
69
73 [Header("Puntos de Spawn")]
74 public Transform[] spawnPoints;
75
76 // ========== CONFIGURACIÓN DE POWER-UPS ==========
77
81 [Header("Power-Ups")]
82 [Tooltip("Lista de prefabs de Power-Up a spawnear aleatoriamente")]
83 public GameObject[] powerUpPrefabs;
84
88 [Tooltip("Número base de power-ups por nivel")]
89 public int basePowerUpsPerLevel = 1;
90
91 // ========== ESTADO INTERNO ==========
92
96 private int currentWaveIndex = 0;
97
101 private int currentLevel = 1;
102
103 // ========== FLUJO PRINCIPAL ==========
104
108 void Start()
109 {
110 StartCoroutine(SpawnWavesRoutine());
111 }
112
117 IEnumerator SpawnWavesRoutine()
118 {
119 yield return new WaitForSeconds(timeBetweenWaves);
120
121 while (true)
122 {
124 Debug.Log($"Nivel {currentLevel} – Oleada: {wave.name}");
125
126 // === Calculamos generación de Power-Ups durante la oleada ===
127 int puCount = Mathf.CeilToInt(basePowerUpsPerLevel * currentLevel);
128 if (puCount > 0 && powerUpPrefabs.Length > 0)
129 {
130 float waveDuration = wave.count / wave.spawnRate;
131 StartCoroutine(SpawnPowerUpsDuringWave(puCount, waveDuration));
132 }
133
134 // === Generación de enemigos ===
135 for (int i = 0; i < wave.count; i++)
136 {
137 SpawnEnemyFromWave(wave);
138 yield return new WaitForSeconds(1f / wave.spawnRate);
139 }
140
141 // Espera a que no quede ningún enemigo en pantalla
142 while (GameObject.FindGameObjectsWithTag("Enemy").Length > 0)
143 yield return null;
144
145 // Preparar siguiente oleada / nivel
147 if (currentWaveIndex >= waves.Length)
148 {
149 currentLevel++;
152 }
153
154 yield return new WaitForSeconds(timeBetweenWaves);
155 }
156 }
157
163 private IEnumerator SpawnPowerUpsDuringWave(int count, float duration)
164 {
165 float interval = duration / count;
166 for (int i = 0; i < count; i++)
167 {
168 yield return new WaitForSeconds(interval);
169
170 var puPrefab = powerUpPrefabs[Random.Range(0, powerUpPrefabs.Length)];
171 var spawnPt = spawnPoints[Random.Range(0, spawnPoints.Length)];
172 Instantiate(puPrefab, spawnPt.position, spawnPt.rotation);
173 }
174 }
175
181 {
182 var prefab = wave.enemyPrefabs[Random.Range(0, wave.enemyPrefabs.Length)];
183 var point = spawnPoints[Random.Range(0, spawnPoints.Length)];
184 var enemy = Instantiate(prefab, point.position, Quaternion.Euler(0, 0, 180));
185 enemy.tag = "Enemy";
186 }
187
193 {
194 Debug.Log($"Subiendo a nivel {currentLevel}, escalando dificultad...");
195 for (int i = 0; i < waves.Length; i++)
196 {
197 waves[i].count = Mathf.CeilToInt(waves[i].count * difficultyScalePerLevel);
198 waves[i].spawnRate *= difficultyScalePerLevel;
199 }
200 }
201}
Componente encargado de generar oleadas de enemigos y Power-Ups. Escala automáticamente la dificultad...
float timeBetweenWaves
Tiempo de espera entre oleadas consecutivas, en segundos.
Wave[] waves
Lista de oleadas configuradas que se repetirán y escalarán en cada nivel.
IEnumerator SpawnWavesRoutine()
Corrutina que controla el flujo continuo de oleadas y niveles. Se encarga de generar enemigos,...
int currentLevel
Nivel actual de dificultad, comienza en 1 y se incrementa tras recorrer todas las oleadas.
void ScaleWavesForNextLevel()
Escala la dificultad de las oleadas incrementando su tamaño y velocidad de aparición....
void Start()
Inicia la corrutina principal que controla la secuencia de oleadas.
IEnumerator SpawnPowerUpsDuringWave(int count, float duration)
Corrutina que genera los Power-Ups de forma espaciada durante la duración de una oleada.
Transform[] spawnPoints
Puntos posibles donde pueden aparecer enemigos o Power-Ups.
void SpawnEnemyFromWave(Wave wave)
Instancia un enemigo aleatorio de la oleada actual en un punto de aparición aleatorio.
int basePowerUpsPerLevel
Número base de Power-Ups a generar por nivel (se multiplica por el nivel actual).
float difficultyScalePerLevel
Factor de escalado de dificultad entre niveles. Aumenta count y spawnRate.
int currentWaveIndex
Índice actual de oleada dentro del array waves.
GameObject[] powerUpPrefabs
Lista de prefabs de Power-Ups que pueden aparecer durante una oleada.
Estructura de datos que define una oleada de enemigos. Permite especificar cuántos enemigos,...
float spawnRate
Velocidad de aparición de los enemigos (enemigos por segundo).
GameObject[] enemyPrefabs
Lista de prefabs de enemigos que pueden aparecer en esta oleada. Se seleccionan aleatoriamente al ins...
string name
Nombre de la oleada, útil para depuración y feedback visual.
int count
Número total de enemigos que se generarán en la oleada.