113 lines
3.3 KiB
C#
113 lines
3.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class GroupedDelayedWaveLogic : AbstractWaveLogic
|
|
{
|
|
private List<GameObject> ennemiesSpawned = new List<GameObject>();
|
|
private int numberEnnemyGroupsLeft;
|
|
private IEnumerator coroutine;
|
|
|
|
public override void StartWave(int wn)
|
|
{
|
|
base.StartWave(wn);
|
|
numberEnnemyGroupsLeft = GetNumberOfGroupsForCurrentWave();
|
|
ennemiesSpawned.Clear();
|
|
|
|
coroutine = SpawnEnnemyGroups();
|
|
StartCoroutine(coroutine);
|
|
}
|
|
public override bool StartWaveValidation()
|
|
{
|
|
return true;
|
|
}
|
|
public override bool EndWaveValidation()
|
|
{
|
|
return (ennemiesSpawned.Count == 0 || ennemiesSpawned.FindIndex(x => x != null) == -1)
|
|
&& numberEnnemyGroupsLeft == 0;
|
|
}
|
|
|
|
private IEnumerator SpawnEnnemiesForGroup()
|
|
{
|
|
int numberEnnemies = GetNumberEnnemiesPerGroupForCurrentWave();
|
|
GameObject path = GetPathForGroup();
|
|
|
|
if (path == null)
|
|
yield return null;
|
|
|
|
GameObject ennemy;
|
|
Transform t = path.transform.GetChild(0).transform;
|
|
for(int i = 0; i < numberEnnemies; i++)
|
|
{
|
|
ennemy = Instantiate(GetEnnemyPrefab(), t.position, t.rotation);
|
|
ennemy.GetComponent<EnemyMovement>().SetupWaypoints(path);
|
|
ennemiesSpawned.Add(ennemy);
|
|
yield return new WaitForSecondsRealtime(0.75f);
|
|
}
|
|
}
|
|
|
|
private IEnumerator SpawnEnnemyGroups()
|
|
{
|
|
|
|
while(numberEnnemyGroupsLeft > 0)
|
|
{
|
|
StartCoroutine(SpawnEnnemiesForGroup());
|
|
numberEnnemyGroupsLeft -= 1;
|
|
yield return new WaitForSecondsRealtime(GetDelayBetweenGroupsForCurrentWave());
|
|
}
|
|
|
|
StopCoroutine(coroutine);
|
|
}
|
|
|
|
private GameObject GetEnnemyPrefab()
|
|
{
|
|
if (waveObject.specialEnnemies.Count > 0 && IsSpecialEnnemy())
|
|
return waveObject.specialEnnemies[Random.Range(0, waveObject.specialEnnemies.Count)];
|
|
|
|
if (waveObject.baseEnnemies.Count == 0)
|
|
{
|
|
Debug.LogError("WaveObject " + waveObject.name + " does not have any enemies set.");
|
|
return null;
|
|
}
|
|
|
|
return waveObject.baseEnnemies[Random.Range(0, waveObject.baseEnnemies.Count)];
|
|
}
|
|
|
|
private GameObject GetPathForGroup()
|
|
{
|
|
if (paths.Count == 0)
|
|
{
|
|
Debug.LogError("No Paths for AIs in Scene!");
|
|
return null;
|
|
}
|
|
|
|
return paths[Random.Range(0, paths.Count)];
|
|
}
|
|
|
|
#region Formulas
|
|
private int GetNumberEnnemiesPerGroupForCurrentWave()
|
|
{
|
|
return waveObject.baseEnnemyPerGroupCount + (Mathf.FloorToInt(currentWaveNumber) / 2);
|
|
}
|
|
|
|
private int GetNumberOfGroupsForCurrentWave()
|
|
{
|
|
return waveObject.baseEnnemyGroupCount + (Mathf.FloorToInt(currentWaveNumber - 0.5f) / 2);
|
|
}
|
|
|
|
private int GetDelayBetweenGroupsForCurrentWave()
|
|
{
|
|
return Mathf.FloorToInt(waveObject.baseSpawnDelaySeconds + GetNumberEnnemiesPerGroupForCurrentWave() - waveObject.baseEnnemyPerGroupCount);
|
|
}
|
|
|
|
private bool IsSpecialEnnemy()
|
|
{
|
|
if (waveObject.specialEnnemies.Count == 0)
|
|
return false;
|
|
|
|
float probability = Mathf.Max(0, -0.5f / (1 + currentWaveNumber * waveObject.baseSpecialEnnemyCoefficient));
|
|
return Random.value < probability;
|
|
}
|
|
#endregion
|
|
}
|