fgm24/Assets/Scripts/Controller/EnemySpawner/EnemySpawner.cs

53 lines
1.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
// Shared
public int Wave = 0;
public float difficulty = 0;
// Inspector
[SerializeField] private float difficultyIncreasePerWave = 0.1f;
[SerializeField] private float WaveTime;
[SerializeField] private EnemyList enemyList;
// Private
private bool nextWaveRequested = false;
private float timer = 0f;
public void StartSpawning() => StartCoroutine(SpawnLoop());
public void StartNextWave() => nextWaveRequested = true;
private IEnumerator SpawnLoop()
{
while (true)
{
yield return new WaitUntil(() => timer > WaveTime || nextWaveRequested);
SpawnWave(difficulty);
Wave++;
difficulty *= difficultyIncreasePerWave + 1;
}
}
void SpawnWave(float difficulty)
{
var decendingList = enemyList.List.OrderByDescending(x => x.Difficulty).ToArray();
for (int i = 0; i < decendingList.Length; i++)
{
while (difficulty > decendingList[i].Difficulty)
{
Instantiate(decendingList[i].prefab);
difficulty -= decendingList[i].Difficulty;
}
}
}
}