/*
RSK World – Tower Defense 3D (WaveManager Evolution v3)
For support, licensing, or custom development inquiries: help@rskworld.in • Phone/WhatsApp: +91 9330539277
Website: RSK World • Business Inquiries: help@rskworld.in
🤝 Contributing: Suggestions and improvements are welcome!
*/

export class WaveManager {
	constructor() {
		this.isSpawning = false;
		this.spawnTimer = 0;
		this.enemiesToSpawn = [];
	}

	startWave(waveNumber) {
		this.isSpawning = true;
		this.spawnTimer = 0;
		this.enemiesToSpawn = this._generateWaveComposition(waveNumber);
	}

	_generateWaveComposition(wave) {
		const list = [];
		// Dense enemy waves for higher difficulty
		const count = 15 + wave * 8; 
		
		for (let i = 0; i < count; i++) {
			let type = 'barbarian';
			
			if (wave >= 2 && Math.random() < 0.45) type = 'archer';
			if (wave >= 3 && Math.random() < 0.3) type = 'giant';
			if (wave >= 5 && Math.random() < 0.25) type = 'crobot';
			if (wave >= 4 && Math.random() < 0.2) type = 'dragon';
			if (wave >= 8 && Math.random() < 0.4) type = 'dragon';

			list.push(type);
		}
		
		return list.sort(() => Math.random() - 0.5);
	}

	update(delta, spawnEnemyCb) {
		if (!this.isSpawning) return;
		
		this.spawnTimer -= delta;
		if (this.spawnTimer <= 0 && this.enemiesToSpawn.length > 0) {
			const type = this.enemiesToSpawn.shift();
			spawnEnemyCb(type);
			// Intense spawn speed
			this.spawnTimer = 0.4 - Math.min(0.25, 0.015 * this.enemiesToSpawn.length);
		}
		
		if (this.enemiesToSpawn.length === 0) {
			this.isSpawning = false;
		}
	}
}
